// Sitewide scripts using jquery
$(document).ready(function() { 
//make background white on all but home page
	$("body").not("body#home").css("background", "#FFFFFF");
	
//remove the outline of a link when clicked
	$("a").click(function() {
		this.blur();
	});
	
//activate png transparency fix
	$(document).pngFix();

//Print Page
	$(".btn_print").click(function() {
		var page = $("iframe_module");
		javascript:print(document);
		return false;
	});
	
 //jmap plugin for google maps api
/*	$('#map').jmap('init', {'mapCenter':[38.115384, -121.291385], 'mapShowjMapsIcon': false}, function(map, options) {
		$('#map').jmap('AddFeed', {feedUrl:'http://sherman.lodihomesforsale.net/featured_map.kml'});
	}).show();*/
	
//open larger map
	$('a#btn_map').click(function() {
		window.open($(this).attr("href"), "largemap", "resizable=1, status=0, toolbar=0");
		return false;
	});
	
//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});
	  });
	});

//refresh page using sort option
	$(".search_sort").change(function () {
	  var pageurl = $(this).attr("name");
		var sortval = $(this).val();
		var page;
		
		if (pageurl.indexOf("?") == -1) { // "?" not found in url string
			page = pageurl + "?sort=" + sortval;
		} else {
			page = pageurl + "&sort=" + sortval;
		}
		
		window.location = page;

	});
	
//ajax call to get cities from county selection
	$(".search_county").change(function () {
		var arr_county = new Array();
		var i = 0;
		$(".search_county option:selected").each(function () {
			arr_county[i++] = this.value;
	  });
		var counties = arr_county.join("|");

		//replace city list with a loading message
		$(".search_city").html('<option value="">Loading...</option>');
		//ajax in the new city option list
		$(".search_city").addClass("red_text").load('/search-process.php', {ajax: true, county: counties}, function() {
			$(this).removeClass("red_text");
		});
	});

//google street view
	$(".add_streetview").toggle(function() {
		var property = $(this).attr("href");
		var map = new GMap2(document.getElementById("streetview"));
		var geocoder = new GClientGeocoder();
		var lat, lng;
		geocoder.getLatLng(property, function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				panoramaOptions = {latlng:point};
        //check for nearest availiable streetview
        var client = new GStreetviewClient();
        client.getNearestPanoramaLatLng(point, function(pLatLng) {
          if(pLatLng) {
            //console.log("Street View Available.");
            var myPano = new GStreetviewPanorama(document.getElementById("streetview"), panoramaOptions);
          } else {
            //console.log("Streetview Unvailable");
            $("#streetview").text("Sorry! Street view not available for this property.");
          }
        });
			}
		});
		$("#streetview").show();
		$(this).addClass("hide_streetview").text("Hide Street View");
		return false;
	}, function() {
		$("#streetview").hide();
		$(this).removeClass("hide_streetview").text("Show Street View");
		return false;
	});

	
//back in history button
	$('a.btn_back').click(function() {
		history.go(-1);
		return false;
	});
	
//make external links
	$("a[href^='http://']").not("[href^='http://lodihomesforsale.net/']").attr("target", "_blank");

//create photo gallery
  $gallery = $("#gallery");
  if ($gallery.length && gallery_data != null) {
    Galleria.loadTheme('/scripts/gallery/galleria.classic.min.js');
    $gallery.galleria({
        width: 420,
        height: 370,
        data_source: gallery_data
    });
  }

});
