/* Global Varaibles */

var reQuery;

var gmap;

var mysqldb;

var isPortal;

$(document).ready(function() {
		
	/* Load the Map */
	if(argItems('@district')) document.getElementById("set_comm").value = argItems('@district');
    document.getElementById("set_city").value = argItems('@citytownname');
	
	
    
	gParameters();
	gPortal();
	
	gmap = new GMap2(document.getElementById("gmap"));

	gmap.setCenter(new GLatLng(52.25470880113083,-113.807373046875), 6);

	gSetLocation();

	gmap.addControl(new GLargeMapControl());

	gmap.addControl(new GMapTypeControl());

	mgr = new MarkerManager(gmap,{maxZoom:19});

	

	

	

	$("#listingtype").change(function(){

	    var c_area = $("#Area").val();

		var c_district = $("#Community").val();

		var c_type = $(this).val();

		$("#Area").empty();

		$("#Community").empty();

		//Load Area

		if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/areas/"+mysqldb+"/"+$(this).val()+"/"+$(this).val()+"_c.html";
		else var url ="/gssi/scripts/searchforms/areas/"+mysqldb+"/"+$(this).val()+"/"+$(this).val()+".html";

		$("#Area").load(url,function(){

				if(c_area) $("#Area").val(c_area);

				if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/districts/"+mysqldb+"/"+c_type+"/"+c_area.replace("@","")+"_c.html";
				else var url ="/gssi/scripts/searchforms/districts/"+mysqldb+"/"+c_type+"/"+c_area.replace("@","")+".html";

				$("#Community").load(url,function(){

					if(c_district) $("#Community").val(c_district);

					count_listings_by_criteria();

					filter_listings_by_criteria(0,gmap.getZoom());

				});

		});

	});

	

	

	$("#Area").change(function(){

		var c_district = $("#Community").val();

		 var c_area = $("#Area").val();

		$("#Community").empty();

		if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/districts/"+mysqldb+"/"+$("#listingtype").val()+"/"+c_area.replace("@","")+"_c.html";
		else var url ="/gssi/scripts/searchforms/districts/"+mysqldb+"/"+$("#listingtype").val()+"/"+c_area.replace("@","")+".html";

		$("#Community").load(url,function(){

			if(c_district) $("#Community").val(c_district);

			count_listings_by_criteria();

			filter_listings_by_criteria(0,gmap.getZoom());

		});

	});

	

	$("#Community").change(function(){

		count_listings_by_criteria();

		filter_listings_by_criteria(0,15);

	});

	

	$("#gbeds").change(function(){

		count_listings_by_criteria();

		filter_listings_by_criteria(0,gmap.getZoom());

	});

	

	$("#gbaths").change(function(){

		count_listings_by_criteria();

		filter_listings_by_criteria(0,gmap.getZoom());

	});

	

	$("#maxlp").change(function(){

		count_listings_by_criteria();

		filter_listings_by_criteria(0,gmap.getZoom());

	});

	

	$("#minlp").change(function(){

		count_listings_by_criteria();

		filter_listings_by_criteria(0,gmap.getZoom());

	});

	



});
function gPortal(){
	$.ajax({
		type: "POST",
		url: "/php-bin/gmaps_new.php?overlord=gPortal",
		data: reQuery,
		success: function(data){
		  $("#isPortal").val(data);
		}
	 });
}
function gParameters(){

	

	if($("#realtor").attr("rebid")){

		$("#googlemapsearch #criteria").append("<input type='hidden' name='@agent1' value='"+$("#realtor").attr("rebid")+"'");

	}

	$(".param").each(function(i){

		$("#googlemapsearch #criteria").append("<input type='hidden' id='"+$(".param").attr("nm")+"' name='"+$(".param").attr("nm")+"' value='"+$(".param").attr("vl")+"'");

	});
	

	$(".crtHide").each(function(i){

		$("#"+$(this).attr("ele")+"p").hide();

	});

}

function gSetLocation(){

	$.getJSON("/php-bin/gmaps_new.php",{overlord: "gSetLocation"},function(j){

		 gmap.setCenter(new GLatLng(j[0].lat,j[0].lng),j[0].zoom);

     //load types

		 mysqldb = j[0].db;

		 if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/areas/"+j[0].db+"/types_c.html";
		 else var url ="/gssi/scripts/searchforms/areas/"+j[0].db+"/types.html";

     $("#listingtype").load(url,function(){

		 			if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/areas/"+j[0].db+"/"+j[0].type+"/"+j[0].type+"_c.html";
					else var url ="/gssi/scripts/searchforms/areas/"+j[0].db+"/"+j[0].type+"/"+j[0].type+".html";
					
					$("#listingtype [value="+j[0].type+"]").attr('selected', 'selected');
					
					 $("#Area").load(url,function(){

		 			 			if(!$("#realtor").attr("rebid")) $("#Area").val("@"+j[0].area);

								 if($("#isPortal").val() == "1") var url ="/gssi/scripts/searchforms/districts/"+j[0].db+"/"+j[0].type+"/"+j[0].area+"_c.html"; 
								 else var url ="/gssi/scripts/searchforms/districts/"+j[0].db+"/"+j[0].type+"/"+j[0].area+".html";
					
								 	
									
								 $("#Community").load(url,function(){
											if($("#set_city").val()){
												
												$("#Area").val("@"+$("#set_city").val());   
											 }		
											 	
												 if($("#set_comm").val()){
													$("#Community").val($("#set_comm").val());   
	  											 }
								 				count_listings_by_criteria();

												filter_listings_by_criteria(0,11);

								}); 

		 			 });														 

		 }); 

	});

}

function popSelect(tb,val,name,fltf,filtv,opt,ord){

  $.getJSON("/php-bin/gmaps_new.php",{overlord: "gfilters", tb: tb, val: val, name: name, fltf: fltf, filtv : filtv,ord:ord },function(j){

     var optn = '';

		 optn += '<option value="">Select '+opt+' </option>';

     for (var i = 0; i < j.length; i++) {

       optn += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

     }	

		$('#'+opt).html(optn);

    $('#'+opt+' option:first').attr('selected', 'selected');

   })

}

function updateCriteria(){

	var form = $('#criteria');

  reQuery = form.serialize();



}

function count_listings_by_criteria(){



	updateCriteria();



	$('#results').empty();



	$('#results').html('<p class="blocknotice align-center"><strong>Searching</strong> <img src="/gssi/googlemaps/i/loader.gif"/></p>');







	$.ajax({



		type: "POST",



		url: "/php-bin/gmaps_new.php?overlord=gSearchCount",



		data: reQuery,



		success: function(data){



				$('#results').empty();



				$('#results').html(data);



			}



	 });

}

function filter_listings_by_criteria(fl,z){



    mgr.clearMarkers();



	$('#details').empty();



	//$('#status').empty();







	//document.write("/php-bin/gmaps_ab.php?overlord=gSearch&"+reQuery);



	$.getJSON("/php-bin/gmaps_new.php?overlord=gSearch&"+reQuery,function(j){







		for (var i = 0; i < j.length; i++) {



			var posn = new GLatLng(j[i].location.lat,j[i].location.lng);



			var marker = new GMarker(posn,{title: "MLS:" + j[i].mlnum + " - "+ j[i].price, icon:GetIcon(j[i].icon)});



			marker.listingtype = j[i].icon;



			marker.id =  j[i].mlnum;



			marker.lat =  j[i].location.lat;



			marker.lng =  j[i].location.lng;



			GEvent.addListener(marker, "click", function() {



					this.openInfoWindowHtml('<div id="details" style="width:350px; height:200px;">&nbsp;</div>');



					 //this.openInfoWindowTabsHtml([new GInfoWindowTab('Details','<div id="details" style="width:350px; height:200px;">&nbsp;</div>'), new GInfoWindowTab('Remarks','<div id="remarks" style="width:350px; height:200px; overflow:auto;">&nbsp;</div>'), new GInfoWindowTab('More Photos','<div id="photos" style="width:350px; height:200px; overflow:auto;">&nbsp;</div>')]);



					show_listingsIW(this.id);



					//show_listingsTB(this.id, this.listingtype);



					//gmap.setCenter(new GLatLng(this.lat,this.lng),11);



			});



			mgr.addMarker(marker,0,19);



		}



		 if(!fl) gmap.setCenter(new GLatLng(j[j.length-1].location.lat,j[j.length-1].location.lng),z);



	 });



	



	 //$('#status').empty();



	// $('#status').html("Lisitngs Loaded");



}

function GetIcon(ic){



	var tbIcon =new GIcon();



	tbIcon.image = "http://www.internetbrokers.ab.ca/gssi/googlemaps/i/" +ic +".png";

	



	//tbIcon.iconSize=new GSize(12,11);
	tbIcon.iconSize=new GSize(32,32);



	tbIcon.iconAnchor=new GPoint(2,10);



	tbIcon.infoWindowAnchor = new GPoint(2,10);



	return tbIcon;







}

function show_listingsIW(ml){



	$('#details').empty();



	$('#details').html('<p class="blocknotice align-center"><strong>Searching</strong> <img src="/gssi/googlemaps/i/loader.gif"/></p>');


	$.ajax({



		type: "POST",



		url: "/php-bin/gmaps_new.php?overlord=gDetails&listingtype="+$("select#listingtype").val(),



		data: "ml="+ml+"&Nav="+ (window.path === undefined?"":path),



		success: function(data){



				var chunk = data.split('<!--split-->');



				$('#details').empty();



				$('#details').html(chunk[0]);



				



				$('#remarks').empty();



				$('#remarks').html(chunk[1]);



				



				$('#photos').empty();



				$('#photos').html(chunk[2]);



			}



	 });	



}
function argItems (theArgName) {
	var url = document.getElementById("qs").value
	sArgs = url.split('&');
	r = '';
	for (var i = 0; i < sArgs.length; i++) {
	if (sArgs[i].slice(0,sArgs[i].indexOf('=')) == theArgName) {
	r = sArgs[i].slice(sArgs[i].indexOf('=')+1);
	break;
	}
	}
	return (r.length > 0 ? unescape(r).split(',') : '')
}

