// Sitewide scripts using jquery
$(document).ready(function() { 
//superfish plugin for navigation
	$("ul.sf-menu").superfish({ 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       'fast',                          // faster animation speed 
		autoArrows:  false                            // disable generation of arrow mark-up 
	}).find('ul').bgIframe({opacity:false});
	
// initialise Header Rotating Images using InnerFade
	$('#headers').innerfade({
		speed: 10000,
		timeout: 20000,
		type: 'random',
		containerheight: '130px'
	});
	
//remove the outline of a link when clicked
	$("a").click(function() {
		this.blur();
	});
	
//activate png transparency fix
	$(document).pngFix();
	
//jcarousel for showing featured listings
	$('#mycarousel').jcarousel();
	
//ajax call to get cities from county selection
	$("#search_county").change(function () {
	  $("#search_county option:selected").each(function () {
		$("#search_city").load('search_ajax.php', {county: this.value});
	  });
	});
	
//display trends when a dropdown is selected
	$("#trends").change(function () {
	  $("#trends option:selected").each(function () {
	  	var photo = this.value;
		$("#trends_view").html("<img src='" + photo + "'/>");
	  });
	});

//refresh page using sort option
	$("#search_sort").change(function () {
	  var pageurl = $(this).attr("name");
	  $("#search_sort option:selected").each(function () {
		var sorturl = pageurl + "?sort=" + this.value;
		window.location = sorturl;
	  });
	});
	
//slideshow on search detail
	$("#gallery").jqGalScroll({width:408, height:320, speed:500});
	
//create google streetview
	$("#streetview").jmap("init", {"mapCenter":[37.982948, -121.317518], "mapShowjMapsIcon": false});
	$("#streetview").empty().text("streetview is not available for this address");
	
	$(".add_streetview").toggle(function() {
		var address = $(this).attr("href");
		//search address to get geocode
		$("#streetview").jmap("SearchAddress", {
            "query": address,
            "returnType": "getLocations"
        }, function(result, options) {
            var valid = Mapifies.SearchCode(result.Status.code);
            if (valid.success) {
				$.each(result.Placemark, function(i, point){
					//get latitude and longitude
					var latlong = [point.Point.coordinates[1], point.Point.coordinates[0]];
					
					//create streetview
					$('#streetview').jmap("CreateStreetviewPanorama", {
						"latlng":latlong
					});
				});
            } else {
                $("#streetview").text("Sorry! This address can not be shown in street view.");
            }
			$("#streetview").show();
        });
		$(this).addClass("hide_streetview");
		return false;
	}, function() {
		$("#streetview").hide();
		$(this).removeClass("hide_streetview");
		return false;
	});
	
//back in history button
	$('a.btn_back').click(function() {
		history.go(-1);
		return false;
	});
	
// validate signup form on keyup and submit
 	$("#request_info, #ask_agent, #schedule_showing, #home_value").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true,
				minlength: 7
			},
			property_address: "required"
		},
		messages: {
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
			email: "Please enter a valid email address",
			phone: "Please enter a valid phone number",
			property_address: "Please enter a valid address"
		}
	});
	
// validate email property form
 	$("#email_property").validate({
		rules: {
			f_name: "required",
			f_email: {
				required: true,
				email: true
			},
			name: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			f_name: "Please enter your friend's name",
			f_email: "Please enter your friend's email",
			name: "Please enter your name",
			email: "Please enter your email"
		}
	});
	
//make external links
	$("a[href^='http://']").attr("target", "_blank");
	
//disable navigation link
	$('#n_search, #n_buysell, #n_community').click(function() {return false;});
});