
jQuery(document).ready(function($){
	$("#nav-one ul").fadeOut("fast").slideUp('fast').show();
    $("#nav-one li").hoverIntent(function(){ $("ul.lev1", this).fadeIn("fast").slideDown('fast');}, function(){$("ul.lev1", this).fadeOut("slow").slideUp('fast');} );
	$("#nav-one ul.lev1 li").hoverIntent(function(){ $("ul.lev2", this).fadeIn("fast").slideDown('fast');}, function(){$("ul.lev2", this).fadeOut("slow").slideUp('fast');} );
	$("#nav-one ul.lev2 li").hoverIntent(function(){ $("ul.lev3", this).fadeIn("fast").slideDown('fast');}, function(){$("ul.lev3", this).fadeOut("slow").slideUp('fast');} );
	$("#nav-one ul.lev3 li").hoverIntent(function(){ $("ul.lev4", this).fadeIn("fast").slideDown('fast');}, function(){$("ul.lev4", this).fadeOut("slow").slideUp('fast');} );
	$(".sub").append("<img src='"+basePath+"/images/arrow.gif'>");

	/* Tabs */
	jQuery("#tabs").tabs(); jQuery("#tabs2").tabs();
	// Scroll to tab
	$('#tabs ul li a').mouseup(function(){ $.scrollTo( "#tabs", {duration:1000} ); });
	// Request more info (Detail; Page)
	var $tabs = $('#tabs').tabs(); // first tab selected
	$('.requestInfoLink').click(function() { 
		$tabs.tabs('select', 4); // switch to third tab
		return false;
	});
	// Remove err after tab click (Detail Page)
	$('#tabs li a').click(function(){ $(".formError").remove(); });


	// Back To Top
	$('.totop').click(function(){ $().scrollTo( 0, {duration:1000} ); });

	// Pic preview
	$('.previewpic').tooltip({ track:true, opacity: 1, fixPNG: true, delay: 0, top: -110, left: 50, extraClass: "picPreview", showURL: false, opacity: 1, bodyHandler: function() { return $("<img/>").attr("src", this.src); } });

	// Tooltips
	$('*[title]').tooltip({track:true, delay:0, showURL:false, showBody: " - ", fade:350});

	// Quick Search
	$("#qsearch").autocomplete(basePath+"includes/auto/quicksearch.php", { width: 260, selectFirst: false });

	// Sort
	$('.resultsort').click( 
		function(){ greyScreen('<h2>Updating Results... Please Wait</h2>'); $.post(basePath+"includes/auto/session_update.php", {"sort": $(this).attr("value")}, 
			function(){ location.reload(true); }
		);  
	});


	// Target Price
	$('#tpricesrch').click( function(){ 
			greyScreen('<h2>Updating Results... Please Wait</h2>');
			var url = $('#currUrl').val();
			var curTp = $.url.param("tp");
			if(curTp && curTp != ''){ url = url.replace("&tp="+curTp, ''); }
			if($('#targetprice').val() != ''){
				var tpValue = $('#targetprice').val();
				tpValue = tpValue.replace("$", ""); 
				tpValue = tpValue.replace(",", "");
				if(IsNumeric(tpValue)){
					var tp = "&tp="+tpValue;
				}
			}else{ 
				var tp = '&tp='; 
			}
			window.location = url + tp;		
	});


	// Display Amount
	$('#displayAmount').change( 
		function(){ greyScreen('<h2>Updating Results... Please Wait</h2>');$.post(basePath+"includes/auto/session_update.php", {"displayAmount": $(this).val()},
			function(){ 
				newURL = window.location.protocol + "//" + window.location.host + window.location.pathname;
				//location.reload(true);
				window.location = newURL;
			}
		); 
	});

	
	// Favorites
	if($.cookie("favs") != null){
		$('#favbar').animate({"left": "-366px"}, { duration: 800});
		$('#favsCont').load(basePath+"includes/favs/favsblock.php");
	}else{
		$('#favbar').animate({"left": "-450px"}, { duration: 800});
		$('#favsCont').load(basePath+"includes/favs/favsblock.php");
	}
	
	// Email favs
	$('.efFormFields').hide();
	$('.efHead').click( function(){ 
		$('.efFormFields').slideToggle("slow");
	});


	jQuery(".addtofavs").click( function(){ 
		//$(this).updateFavorite("add", $(this).attr("id")); 
		$.ajax({
			type: "POST",
			url: basePath+"includes/favs/favsblock.php",
			data: "type=add&mlsid="+$(this).attr("id"),
			success: function(msg){
				//alert( msg );
				$.cookie("favs", msg, { path: '/', expires: 30 });
				favsCheck();
			}
		});
		//alert($(this).attr("id"));
		lnk = $(this).attr("id").replace(/favlink_/, "");
	   $("#"+lnk).find(".removefavs").show();
	   $("#"+lnk).find(".addtofavs").hide();
	});	

	jQuery(".removefavs").click( function(){ 
		//$(this).updateFavorite("remove", $(this).attr("id"));
		$.ajax({
			type: "POST",
			url: basePath+"includes/favs/favsblock.php",
			data: "type=remove&mlsid="+$(this).attr("id"),
			success: function(msg){
				//alert( msg );
				if(msg == ""){
					$.cookie("favs", msg, { path: '/', expires: -30 });
				}else{
					$.cookie("favs", msg, { path: '/', expires: 30 });
				}
				favsCheck();
			}
		});
		lnk = $(this).attr("id").replace(/favlink_/, "");
		$("#"+lnk).find(".removefavs").hide();
	    $("#"+lnk).find(".addtofavs").show();
	});	


	$("#favbar").hoverIntent({    
		 sensitivity: 8, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 75, // number = milliseconds for onMouseOver polling interval    
		 over: slideOut, // function = onMouseOver callback (REQUIRED)    
		 timeout: 1000, // number = milliseconds delay before onMouseOut    
		 out: slideIn // function = onMouseOut callback (REQUIRED)    
	});
	

	function slideIn(){  $(this).animate({"left": "-366px"}, 800, function(){ favsCheck(); }); }
	function slideOut(){  $(this).animate({"left": "0"}, { duration: 800}); }
	
	
});



		
function greyScreen(msg){
	$('#screen').css({opacity: 0.6, 'width':$(document).width(),'height':$(document).height()});
	$('#box').css({'display': 'block'}).html(msg);
	if(msg == "<h1>Your Favs Were Sent</h1>"){
		unBlock();
	}
}
function unBlock(){
	$('#screen').hide();
	$('#box').hide();
}

function favsCheck(){
	if($.cookie("favs") != null){
		$('#favbar').animate({"left": "-366px"}, { duration: 800});
		$('#favsCont').load(basePath + "includes/favs/favsblock.php");
	}else{
		$('#favbar').animate({"left": "-450px"}, { duration: 800});
		$('#favsCont').load(basePath+"includes/favs/favsblock.php");
	//alert($.cookie("favs"));
	}
}


jQuery.fn.remFavorite = function(mlsid) {
	//alert(mlsid);
	$.ajax({
			type: "POST",
			url: basePath+"includes/favs/favsblock.php",
			data: "type=remove&mlsid="+$(this).attr("id"),
			success: function(msg){
				//alert( msg );
				if(msg == ""){
					$.cookie("favs", msg, { path: '/', expires: -30 });
				}else{
					$.cookie("favs", msg, { path: '/', expires: 30 });
				}
			}
		});
		lnk = $(this).attr("id").replace(/favlink_/, "");
		$("#"+lnk).find(".removefavs").hide();
		$("#"+lnk).find(".addtofavs").show();

		lnk = mlsid.replace(/favlink_/, "fav_");
		$('#'+lnk).animate({"left": "-400px"}, { duration: 800 });
		$('#'+lnk).animate({"height": "0"}, { duration: 400 }).fadeOut();
		//$('#'+lnk).fadeOut();

};



function remove(p){
	$('#'+p).hide();	
}


function resetSSearch(s){if(s.value=='Search MLS...')s.value='';}
function clearSSearch(s){if(s.value=='')s.value='Search MLS...';}


function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}
