$(document).ready(function(){

//Open Searchbar (Search our Portfolio)
	$("#searchbar-closed a").click(function(){
		$("#searchbar-closed").toggle();
		$("#searchbar").toggle();
		return false;
	});

//Zoom picture in and out
	$("#photos-open .large img").click(function(){
		photoBrowser(1,true);
		return false;
	});

//Close Send email for javascript enabled & Hide labels for javascript enabled
	var propertyDetails = $("#content-col").hasClass("property-details");
	if(propertyDetails) {
		$('#sendusemail').show();
		$('#sendusemail-open').hide();
		$("form label").hide();
		$(".right-column table tr").mouseover(function() { $(this).addClass('row-over'); }).mouseout(function() { $(this).removeClass('row-over'); });
	} else {
		$('#name').val('Your name');
		$('#emailFrom').val('Your email address');
		$('#message').val('Your message / enquiry.\n\n(Please include phone number if you prefer to be contacted by phone.)');
		$("form label").hide();
	}

//Show one row and set index for homepage thumbrow rotation
	var homepage = $("body").hasClass("homepage");
	if(homepage) {

		$("#menu-col li:first").addClass('selected');

		thumbrowLatest = $("body.homepage .table .latest .thumb-row");
		var thumbrowLatestFirst = thumbrowLatest[0];
		$(thumbrowLatestFirst).toggle();

		thumbrowRandom = $("body.homepage .table .random .thumb-row");
		var thumbrowRandomFirst = thumbrowRandom[0];
		$(thumbrowRandomFirst).toggle();

	};

//On searchbar refine, refine automatically
	var searchpage = $("body").hasClass("searchresults");
	if(searchpage) {
		$("#type").change(function(){submitSearch();});
		$("#location").change(function(){submitSearch();});
		$("#price").change(function(){submitSearch();});
	};

}); // end document.ready


function changeThumbrow(whichRow) {
	if(whichRow=='latest') {
		$("body.homepage .table .latest .thumb-row").toggle();
		if( $(".more-link.latest").html() == "3 PREVIOUS") {
			$(".more-link.latest").html("3 MORE"); }
		else if( $(".more-link.latest").html() == "3 MORE") {
			$(".more-link.latest").html("3 PREVIOUS"); 
		};
	}
	if(whichRow=='random') {
		$("body.homepage .table .random .thumb-row").toggle();
		if( $(".more-link.random").html() == "3 PREVIOUS") {
			$(".more-link.random").html("3 MORE");
		} else if ( $(".more-link.random").html() == "3 MORE") {
			$(".more-link.random").html("3 PREVIOUS");
		};
	}
};


//Change large photo in photobrowser from clicked thumbnail
	function photoBrowser(id,zoom) {
		if(zoom){
			$("#photos-open .thumbnail").toggle();			
			var photoWidth = $("#photo-large").attr("width");
			if (photoWidth == 378) {
				$("#photo-large").attr("width", "578");
			}else{
				$("#photo-large").attr("width", "378");
			};
		} else {
			$("#photo-large").attr({src : id});
			$("#photo-main").attr({src : id});
		};
		return false;
		
	};

//Open Send us an email
	function openSendusemail() {
		$('#sendusemail').toggle();
		$('#sendusemail-open').toggle();
		$('#name').val('Your name');
		$('#emailFrom').val('Your email address');
		$('#message').val('Your message / enquiry.\n\n(Please include phone number if you prefer to be contacted by phone.)');
		$('#name').focus();
	};

//Submit search bar form
	function submitSearch() {
		var searchString = "index.php?action=searchresults";

	//Build string
		if ($('#type').val() != ""){
			var arrayType = $('#type').val().split(";");
			var stringType = "";
			jQuery.each(arrayType, function(i){
				stringType = stringType + "&pclass%5B%5D=" + arrayType[+i];
			});
			//alert("stringType=" + stringType);
			searchString = searchString + stringType;
		};

		if ($('#location').val() != ""){
			var arrayLocation = $('#location').val().split(";");
			var stringLocation = "";
			jQuery.each(arrayLocation, function(i){
				stringLocation = stringLocation + "&location_or%5B%5D=" + arrayLocation[+i];
			});
			//alert("stringLocation=" + stringLocation);
			searchString = searchString + stringLocation;
		};		

		if ($('#price').val() != ""){
			var arrayPrice = $('#price').val().split(";");
			var stringPrice = "";
			stringPrice = stringPrice + "&price-min=" + arrayPrice[0] + "&price-max=" + arrayPrice[1];
			//alert("stringPrice=" + stringPrice);
			searchString = searchString + stringPrice;
		};


	//Merge string and redirect
		//alert(searchString);
		window.location.href = searchString; //$('#search-form').submit();
	};


