// recaptcha options
var RecaptchaOptions = {
    theme : 'custom',
    custom_theme_widget: 'recaptcha_widget'
};

// for clearing the search input if the user focuses on it
function clearMe(formfield, defaultText)
{
	if (formfield.value == defaultText) {
		formfield.value = "";
	}
}

// for restoring the search input if the user blurs without entering anything
function restoreMe(formfield)
{
	if (formfield.value == "") {
		formfield.value = formfield.defaultValue;
	}
}

function addFavorite(fav_id, fav_dir, fav_title, fav_img)
{
	// default title
	if (fav_title == "") fav_title = "Listing #" + fav_id;
	if (fav_img == null) fav_img = "/shared/images/li_square_dot.png";
	
	// post to left_favorites_mid.php and get html back
	$.post('/shared/php/left-box-favorites-mid.php', { cmd: 'add', id: fav_id, lid: '0', buildingname: fav_title, dir: fav_dir, img: fav_img },
			function(data){ document.getElementById('filters-favorites').innerHTML = data; }); 

	// open the box if its inactive
	if($('#listings-left-favorites').hasClass('listings-left-favorites-inactive'))
		toggleBox(document.getElementById('listings-left-favorites'));
}

function removeFavorite(fav_id, fav_dir, fav_title)
{
	$.post('/shared/php/left-box-favorites-mid.php', { cmd: 'remove', id: fav_id},
			function(data){ document.getElementById('filters-favorites').innerHTML = data; }); 
}

function toggleBox(this_obj)
{
	id = this_obj.id;
	filters = $('#'+id).next('.filters');
	
	/* set active or inactive status to div id# */
	if($(this_obj).hasClass(id+'-active')) 
	{
		$(this_obj).removeClass(id+'-active');
		$(this_obj).removeClass('ui-bar-color');
		$(this_obj).addClass(id+'-inactive');
		$(this_obj).addClass('ui-bar-color-inactive');
		set_cookie(id, 'inactive');
	}
	else  if ($(this_obj).hasClass(id+'-inactive'))
	{
		$(this_obj).removeClass(id+'-inactive');
		$(this_obj).removeClass('ui-bar-color-inactive');
		$(this_obj).addClass(id+'-active');
		$(this_obj).addClass('ui-bar-color');
		set_cookie(id, 'active');
	}

	/* set show or hide status to filters div and toggle it */
	if(filters.hasClass('filters')) {
		filters.slideToggle(300);
	}
}

function toggleAdvertiseBox(this_obj)
{
	filters = $(this_obj).next('.filters');
	
	if($(this_obj).hasClass('left-box-advertise-here-active')) 
	{
		$(this_obj).removeClass('left-box-advertise-here-active');
		$(this_obj).removeClass('ui-bar-color');
		$(this_obj).addClass('left-box-advertise-here-inactive');
		$(this_obj).addClass('ui-bar-color-inactive');
	}
	else  if ($(this_obj).hasClass('left-box-advertise-here-inactive'))
	{
		$(this_obj).removeClass('left-box-advertise-here-inactive');
		$(this_obj).removeClass('ui-bar-color-inactive');
		$(this_obj).addClass('left-box-advertise-here-active');
		$(this_obj).addClass('ui-bar-color');
	}

	/* set show or hide status to filters div and toggle it */
	if(filters.hasClass('filters')) {
		filters.slideToggle(300);
	}
}

function set_cookie ( cookie_name, cookie_value, lifespan_in_days, valid_domain) {
    var domain_string = valid_domain ? ("; domain=" + valid_domain) : '' ;
    
    document.cookie = cookie_name +
            "=" + encodeURIComponent( cookie_value ) +
            "; max-age=" + 60 * 60 *
            24 * 3 +
            "; path=/" + domain_string ;
}
 
function bookmarkPage(url, title) {
	
	// firefox
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} 
	// opera
	else if (window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if (document.all) {
		external.AddFavorite(location.href, document.title);
	}
	else {
		alert("Sorry! Your browser doesn't support this feature.");
	} 
}


// make heights of left and right listing boxes equal
function equalizeListingHeights() {
	if ($("#listing-left").length && $("#listing-right").length) {
		var left_h = $("#listing-left").height();
		
		// get bottom of right
		var right_h = $("#listing-right").height();
		var right_t = $("#listing-right").offset().top;
		var right_b = right_t + right_h;
		
		// top of left ad
		var leftad_t = $("#listing-left-ad").offset().top;
		
		// right bigger than left
		if (right_h > left_h) {
			// difference between bottom right and top left ad
			var leftad_h = right_b - leftad_t;
			
			$("#listing-left-ad").css("height", leftad_h+"px");
			$("#listing-left-ad").css("position", "relative");
			$("#listing-left-ad .ad-300x250").css("position", "absolute");
			$("#listing-left-ad .ad-300x250").css("top", "50%");
			$("#listing-left-ad .ad-300x250").css("margin-top", "-120px");
			$("#listing-left-ad .ad-300x250").css("left", "50%");
			$("#listing-left-ad .ad-300x250").css("margin-left", "-150px");
		}
		// left bigger than right
		else {
			$("#listing-right").css("height", left_h-10+"px");
		}
	}
}


// for header slideshow
$(document).ready(function() {
	
	if ($(".slideshow").length) {
	    $('.slideshow').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed: 4000
		});
	}
	
	// slower scroll to animation for hashchange clicks
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
				&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body')
				.animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	
	// SEARCH AUTOCOMPLETE
	if ($("#location").length) {		
		$("#location").autocomplete(city_listings, {
			autoFill: false,
			formatItem: function(row) {
				return row.city_state;
			}
		});
	}
	
	// SEARCH AUTOCOMPLETE
	if ($("#primary-location").length) {		
		$("#primary-location").autocomplete(city_listings, {
			autoFill: false,
			formatItem: function(row) {
				return row.city_state;
			}
		});
	}
	
	// ADVANCED SEARCH
	if ($("#search-listings-advanced").length) {

		$("#search-box-advanced").click(function() {
			$("#search-listings-advanced").slideToggle(300);
		});
		$("#search-box-hide").click(function() {
			$("#search-listings-advanced").slideToggle(300);
		});
		$("#search-box-reset").click(function() {
			$("#search-listings")[0].reset();
		});
		$("#search-box-search2").click(function() {
			$("#search-listings").submit();
		});
		
		
	}
	
	$('#photo-clickformore a').mouseover(function() {
		$('#photo-selected a').attr('href', $(this).attr('href'));
		$('#photo-selected a img').attr('src', jQuery('img', this).attr('src'));
	});
    
	$('input[type="text"]').addClass("idleField");
	$('input[type="text"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if ($(this).hasClass("inactiveField")) {
			$(this).removeClass("inactiveField");
		}
	});  
	$('input[type="text"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");		
	});
	    
    $("ul.gallery img").hover(
   		function() {
   			var hovered = $(this);
   	 	$("#photo-large img").attr("src", hovered.attr("src"));
   		},
   		function() {}
   	);

    // RECAPTCHA
	$('#refrimg').click(function() { Recaptcha.reload(); }); 

	// DATEPICKER
	$( "#datepicker" ).datepicker({
		showButtonPanel: true
	});
	
	// if no m-left, make the m-content 100% wide
	if (!$("#m-left").length && $("#m-content").length) {
		$("#m-content").css({ width: "100%" });
	}
	


	equalizeListingHeights();
	
});





	
