$(document).ready(function() {
	$("#nav a, #secondaryNav a, .sectionLinks a, a.graphicButton").hover(
				// over
				function(){
					// store the original image src
					$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
					
					// add -over to the image unless it is already -over
					if (!$(this).children(":first-child").attr("src").match(/-over/) ) {
						$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
					}
					
				},
				
				// out
				function(){
					// replace the original src
					$(this).children(":first-child").attr(
						"src", $(this).children(":first-child").attr("originalsrc")
					);
					
					$(this).children(":first-child").attr(
						"originalsrc", null
					);
				}
			);
	
	// move map to the row of the neighborhood and change marker color
	$(".neighborhood").hover(
			function(){
					var pos = $(this).position()
					
					$("#mapHolder").stop();
					$("#mapHolder").animate({ 
				        top : pos.top + "px"
				      }, 500, 'swing' );

					var id = $(this).attr('id');
					var i = id.substr(13);
			
					//var letter = String.fromCharCode("A".charCodeAt(0) + (i * 1));
					gmarkers[i - 1].setImage("/images/map_markers/blue/" + i + ".png");
				
					//GEvent.trigger(gmarkers[i], "click");
					
				},
				
				// out
				function(){
					var length = gmarkers.length;
					for (i=0; i < length; i++) {
						//var letter = String.fromCharCode("A".charCodeAt(0) + i);
						gmarkers[i].setImage("/images/map_markers/tan/" + (i + 1) + ".png");
					}
				}
	);

	// swap out main photo of listing
	$("#photoThumbs a").click(function(){
		$("#mediaHolder").html('<a href="' + $(this).attr('href') + '" target="_blank" id="detailPhotoZoom"><img src="' + $(this).attr('href') + '" id="detailPhoto"></a>');
		return false;
	});
	
	// lightbox for listing image
	if ($("#detailPhotoZoom")) {
		// use livequery to rebind after image is replaced
		$("#detailPhotoZoom").livequery(function(){ 
			$(this).lightBox({
				imageLoading: '/images/lightbox/ico-loading.gif',
				imageBtnClose: '/images/lightbox/btn-close.gif',
				imageBtnPrev: '/images/lightbox/btn-prev.gif',
				imageBtnNext: '/images/lightbox/btn-next.gif',
				imageBlank: '/images/lightbox/blank.gif'
	   		});
		});
	}
	
	// media tabs
	$("#mediaTabs a").click(function(){
		$(".mediaTabContent").hide();
		var div = $(this).attr('href').substring(1);
		$("#" + div).show();
		$("#mediaTabs a").css({
				'background-color' : '#FFFFFF',
				'border' : '1px solid #B4B1AD'
			});
		$(this).css({
				'background-color' : '#F6F3EE',
				'border-bottom' : '1px solid #F6F3EE'
			});
		return false;
	});
	
	// search tabs
	$("#searchTabs a").click(function(){
		$(".searchForm").hide();
		var div = $(this).attr('href').substring(1);
		$("#" + div + 'Search').show();
		$("#searchTabs a").css({
				'background-color' : '#FFFFFF',
				'border' : '1px solid #B4B1AD'
			});
		$(this).css({
				'background-color' : '#F6F3EE',
				'border-bottom' : '1px solid #F6F3EE'
			});
		return false;
	});

	// open and close portfolio
	if ($("#favoritesOpen")) {
		$("#favoritesOpen").livequery(function(){ 
			$(this).click(function(){
				$("#favoritesClosed").hide();
				$("#favoritesOpened").show();
				return false;
			});
		});
	}
	
	if ($("#favoritesClose")) {
		$("#favoritesClose").livequery(function(){
			$(this).click(function(){
				$("#favoritesOpened").hide();
				$("#favoritesClosed").show();
				return false;
			});
		});
	}

	
	

	// fix PNGs for IE6
	//$(document).pngFix();	
		
		
	// init carousel
	if ($(".carousel")) {
		 $(".carousel").livequery(function(){
			var visible = 3;
			var length = $(".carousel .carouselItem").length;
	 
			 if (length <= visible) {
				 $(".next").addClass("disabled");
			 }
		 
		 	$(this).jCarouselLite({
	    	    btnNext: ".next",
    	    	btnPrev: ".prev",
				visible: visible,
				circular: false
			});
			
	    });
	}
	

			
	
	// hide favoritesOpened after carousel init
	// must not be display : none for carousel to init properly
	if ($("#favoritesOpened")) {
		$("#favoritesOpened").livequery(function(){
			$(this).css('visibility', 'visible');
			$(this).hide();
		});		
	}
	
	
	$("#homeAreaGuide a").hover(
		function(){
			if ($(".areaTip").is(":visible") ) {
				$(this).next().show();
			} else {
				$(this).next().fadeIn('slow');
			}
		},
		function(){
			$(this).next().fadeOut();
		}
	);
	
	$("#refineSearchRes .refineSearchAreas input:checkbox").change(function() {
			var params = $("#refineSearchRes").serialize();
		
			params += '&rand=' + Math.random();
		
			$.get("/cgi-bin/listings/neighborhoods_for_search.cgi", params, function(data){
				$("#refineSearchRes .neighborhoodSelect").html(data);
			});
	});
	
	$("#refineSearchLnd .refineSearchAreas input:checkbox").change(function() {
			var params = $("#refineSearchLnd").serialize();
		
			params += '&rand=' + Math.random();
		
			$.get("/cgi-bin/listings/neighborhoods_for_search.cgi", params, function(data){
				$("#refineSearchLnd .neighborhoodSelect").html(data);
			});
	});
	
	
	$('.contactAgent').click(function (e) {
		e.preventDefault();
		$('#contactAgentForm').modal({opacity : '75'});
	});
	
	$('.sendFriend').click(function (e) {
		e.preventDefault();
		$('#sendFriendForm').modal({opacity : '75'});
	});
	
	$('.mlsDisclaimer').click(function (e) {
		e.preventDefault();
		$('#mlsDisclaimer').modal({opacity : '75'});
	});
	
	$('.greatschoolsDisclaimer').click(function (e) {
		e.preventDefault();
		$('#greatschoolsDisclaimer').modal({opacity : '75'});
	});
	
	$('#privacy').click(function (e) {
		e.preventDefault();
		$.get("/privacy.html", function(data){
			$.modal(data, {opacity : '75'});
		});
	});

	
	// section clicks
	$('.sectionLinks a').click(function(){
			// show the section
			var theDiv = $(this).attr("href").substring(1);
			theDiv = theDiv.replace(/\/$/, "");
			showSection(theDiv);

			// reset
			$('.sectionLinks a').each(function() {
				$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/-over\./, ".") );
			});
			// set selected
			$(this).children(":first-child").attr( "src", $(this).children(":first-child").attr("src").replace(/\./, "-over.") );
			$(this).children(":first-child").attr( "originalsrc" , $(this).children(":first-child").attr("src") );
			
			
	});
	
	// neighborhood refine search
	$("#refineToggle").click(function() {
		if ($("#refineForm").is(":visible")) {
			$("#refineForm").slideUp();
		} else {
			$("#refineForm").slideDown();
		}
		return false;
	});
	
	
	// agent swap
	$('#agentLinks a').click(function(){
		var theDiv = $(this).attr("href").substring(1);
		theDiv = theDiv.replace(/\/$/, "");
		showSection(theDiv);
		
		// re-init sifr so the titles that were hidden show
		initSifr();
		
		// reset
		$('#agentLinks a').each(function() {
			//$(this).css('background', '#DAD1C2');
			$(this).removeClass('selected');
		});
		// set selected
		//$(this).css('background', '#CBE5E2');
		$(this).addClass('selected');
		
		return false;
	});

	
	
});

window.onload = function() {
		// preload
		$("#nav a img,  #secondaryNav a img, .sectionLinks a img, a.graphicButton img").each(
			function() {
				if (!$(this).attr("src").match(/-over/) ) {
					$("<img>").attr( "src", $(this).attr("src").replace(/\./, "-over.") );
				}
			}
		);
}



function removeFav(id) {
	var params = 'action=delete&favorite_id=' + id + '&rand=' + Math.random();

	$.get("/cgi-bin/listings/favorites.cgi", params, function(data){
		$("#favorites").html(data);
	});
	
}

function addFav(id) {

	var params = 'action=add&mls_id=' + id + '&rand=' + Math.random();

	$.get("/cgi-bin/listings/favorites.cgi", params, function(data){
		$("#favorites").html(data);
	});

	//var myUpdateDiv = "addFavContainer" + id;
	//$(myUpdateDiv).innerHTML = '<div id="addFav"><img src="/images/btn_listing_added.gif" alt="Listing Added"></div>';
	
}


function clearFav() {
	if (confirm("Are you sure you want to clear all listings from your portfolio?")) {
		var params = 'action=clear&rand=' + Math.random();

		$.get("/cgi-bin/listings/favorites.cgi", params, function(data){
			$("#favorites").html(data);
		});		

	} else {
		return;
	}
}


// for main search forms
function albemarleToggle(isChecked) {
	if (isChecked) {
		$("#albemarleAreas input:checkbox").each(function() {
			this.checked = true;
		});
	} else {
		$("#albemarleAreas input:checkbox").each(function() {
			this.checked = false;
		});
	}
}

// for main search forms
function typeChange(type) {
	if (type == 'res') {
		$("#lndFields").fadeOut('fast', function() {
			$("#resFields").fadeIn('fast');
		});
	} else {
		$("#resFields").fadeOut('fast', function() {
			$("#lndFields").fadeIn('fast');
		});
	}

}


function submitContactAgent() {
	 var params = $("#contactAgent").serialize();
			$.post("/cgi-bin/listings/contact_agent.cgi", params,
			   	function(data){
					$("#contactAgent").hide();
					$("#contactAgent").after(data);
			});
			
			return false;
}

function submitSendFriend() {
	 var params = $("#sendFriend").serialize();
			$.post("/cgi-bin/listings/send_friend.cgi", params,
			   	function(data){
					$("#sendFriend").hide();
					$("#sendFriend").after(data);
			});
			
			return false;
}

	function showSection(theDiv) {
		$("#sections .section:visible").hide();
		$("#" + theDiv).show();
	}
