$(document).ready(function() {
/**
* Hide Invisible Elements
**/
	$("#contact").hide();
	$("#login").hide();
	$("#login_loading").hide();
	$("#load_search").hide();
	$("#black_drop").hide();
	$("#black_drop").fadeTo( "fast", 0.00 );
	$("#info_con").hide();
	$("#info_con").fadeTo( "fast", 0.00 );
	$("#img_con").hide();
	$("#img_con").fadeTo( "fast", 0.00 );
	
/**
* Create Recently Added Tick VIA innerFade jQuery Plugin
**/

/**
* Adding all the Click functions 
**/
	// Contact
    $("#contact_b").click(function () {
      $("#contact").slideToggle("slow");
    });
	// Contact
    $("#login_contact").click(function () {
      $("#contact").slideToggle("slow");
    });
	// Login
    $("#login_b").click(function () {
      $("#login").slideToggle("slow");
    });
	// Advance / Simple Search Buttons
	$("#search_btn").click(function () {
		if($("#search_btn").hasClass("advanced")) {
			$("#search").css('display', 'none');
			$("#a_search").css('display', 'block');
			$(this).removeClass = 'advanced';
			$(this).addClass = 'simple';
		} else {
			$("#search").css('display', 'block');
			$("#a_search").css('display', 'none');
			$(this).removeClass = 'simple';
			$(this).addClass = 'advanced';
		}
	});
	// Video Viewer
	$("#black_drop").click(function () {
		$("#black_drop").fadeTo( "slow", 0.00, function () {
			$("#black_drop").hide();
		});
		$("#info_con").fadeTo( "normal", 0.00, function () {
			$("#info_con").hide();
		});
		$("#vid_con").fadeTo( "normal", 0.00, function () {
			$("#vid_con").hide();
		});
	});
	
	var newsoption1 = {
		firstname: "mynews",
		secondname: "showhere",
		fourthname: "news_button",
		mouseover: false,
		newsspeed: '8000'
	}
	$.init_news(newsoption1);
	
	$("#sort_listings").tablesorter({widgets: ['zebra']});
	
    $("#sort_listings").tablesorterPager({container: $("#pager"), positionFixed: false, size: 5 });

});
var ajaxRequest = function() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
	// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}
var createQuery = function() {
    var elms, elm, Qstring = '', k = 0;
    elms = document.forms[0].elements;
    while( elm = elms[k++] ) {
        if( elm.name && elm.value ) {
            Qstring += elm.name + '=' + escape( elm.value ) + '&';
		}
	}
    return Qstring.replace( /&$/, '' );
}
var getData = function( con, source ) {
	var xmlHttp = ajaxRequest();
	if( con != null )
		var container = document.getElementById( con );
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			if( container )
				container.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open( "GET", source, true );
	xmlHttp.send( null );
}
var clientLogin = function() {
	$("#login_loading").show();
	var xmlHttp = ajaxRequest();
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			setTimeout( function() {
				document.getElementById( 'login_loading' ).innerHTML = xmlHttp.responseText;
				setTimeout( function() {
					window.location = "index.php";
				}, 4000 );
			}, 2000 );
		}
	}
	xmlHttp.open( "POST", 'incs/login.inc.php', true );
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	var querySTR = createQuery();
	xmlHttp.send( querySTR );
}
/**
* AJAX Function for Pagination
**/
var paginate = function( con, source, query ) {
	var xmlHttp = ajaxRequest();
	if( con != null )
		var container = document.getElementById( con );
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			if( container )
				container.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open( "GET", source + query, true );
	xmlHttp.send( null );
}
var sendPost = function( con, action ) {
	if( con != null )
		var container = document.getElementById( con );
	var xmlHttp = ajaxRequest();
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			if( container )
				container.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open( "POST", action, true );
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	var querySTR = createQuery();
	xmlHttp.send( querySTR );
}
/**
* AJAX Function for Searching
**/
var searchIt = function() {
	$("#load_search").show();
	var xmlHttp = ajaxRequest();
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			//setTimeout( function() {
				document.getElementById( 'content' ).innerHTML = xmlHttp.responseText;
			//}, 5000 );
		}
	}
	xmlHttp.open( "POST", 'incs/search.inc.php', true );
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    var elms, elm, Qstring = '', k = 0;
    elms = document.forms[1].elements;
    while( elm = elms[k++] ) {
        if( elm.name && elm.value ) {
            Qstring += elm.name + '=' + escape( elm.value ) + '&';
		}
	}
    var querySTR = Qstring.replace( /&$/, '' );
	xmlHttp.send( querySTR );
}

/**
* AJAX Function for display Videos
**/
var showImg = function( img ) {
	var xmlHttp = ajaxRequest();
	var container = document.getElementById( 'img_con' );
	container.innerHTML = '<img src="house_imgs/full/' + img + '" alt="" border="" />';
	$("#img_con").show();
	$("#img_con").fadeTo( "normal", 1.00 );
}

/**
* AJAX Function for display Videos
**/
var showVid = function( hid ) {
	displayVidBackdrop();
	var xmlHttp = ajaxRequest();
	var container = document.getElementById( 'vid_con' );
	container.innerHTML = 'Loading video, please wait...';
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			setTimeout( function() {
				container.innerHTML = xmlHttp.responseText;
			}, 2000 );
		}
	}
	xmlHttp.open( "GET", 'vids.php?hid=' + hid, true );
	xmlHttp.send( null );
}

/**
* AJAX Function for display info
**/
var showInfo = function( hid ) {
	displayBackdrop();
	var xmlHttp = ajaxRequest();
	var container = document.getElementById( 'info_con' );
	container.innerHTML = '<img src="images/search-loader.gif" border="0" alt="" width="18" align="absbottom" /> Updating content, please wait...';
	xmlHttp.onreadystatechange = function() {
		if( xmlHttp.readyState == 4 ) {
			setTimeout( function() {
				container.innerHTML = xmlHttp.responseText;
			}, 2000 );
		}
	}
	xmlHttp.open( "GET", 'info.php?hid=' + hid, true );
	xmlHttp.send( null );
}

var displayBackdrop = function() {
	
	$("#black_drop").show();
	$("#black_drop").fadeTo( "slow", 0.50 );
	
	$("#info_con").show();
	$("#info_con").fadeTo( "normal", 1.00 );
}

var displayVidBackdrop = function() {
	
	$("#black_drop").show();
	$("#black_drop").fadeTo( "slow", 0.50 );
	
	$("#vid_con").show();
	$("#vid_con").fadeTo( "normal", 1.00 );
}

AIM = {

    frame : function(c) {

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function') {
            i.onComplete = c.onComplete;
        }

        return n;
    },

    form : function(f, name) {
        f.setAttribute('target', name);
    },

    submit : function(f, c) {
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
            i.onComplete(d.body.innerHTML);
        }
    }

}

function startCallback() {
	// make something useful before submit (onStart)
	return true;
}

function completeCallback(response) {
	// make something useful after (onComplete)
	document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
	document.getElementById('r').innerHTML = response;
}
