// JavaScript Document

/*	Show/Hide Drop-Down Menu
/*----------------------------------------------------------*/
function toggleMenu(menuID){
	if($("#"+menuID+" ul").css("display") == "none"){
		$("#dealerForm ul ul").hide();
		$("#"+menuID+" ul").show();
	}else{
		$("#"+menuID+" ul").hide();
	}
}


/*	Update Menu Selection
/*----------------------------------------------------------*/
function updateMenu(menuID, display, value){
	$("#"+menuID+" li a.choice").html(display);
	$("#"+menuID+" input.actual").val(value);
	$("#"+menuID+" ul").toggle();
}


/*	AJAX: Reload/Unload State List
/*----------------------------------------------------------*/
function loadStates(menuID, display, value){
	$("#states li a.choice").html("");
	$("#states li a.choice").attr("onclick", "");
	$("#states input.actual").val("");
	var country = value;
	if(country == "US" || country == "CA"){
		$("#ajax_states").load("/includes/ajax/ajax_states.php?country="+country);
		$("#states li a.choice").css("background-color", "#FFFFFF");
		$("#states li a.choice").click(function(){
			toggleMenu("states");
		});
	}else{
		$("#ajax_states").html("");
		$("#states li a.choice").css("background-color", "#999999");
		$("#states li a.choice").unbind("click");
	}
	updateMenu(menuID, display, value);
}


/*	Validate User's Postal Code and submit Search
/*----------------------------------------------------------*/
function doSearch(){
	var postal = $("#postal").val();
	var radius = $("#radius input.actual").val();
	if(postal != null && postal != "" && radius != null && radius != ""){
		if(GBrowserIsCompatible()){
			var geocoder = new GClientGeocoder();
			geocoder.getLatLng(postal, function(point){
				if(!point){
					alert("We were unable to locate your Postal Code in Google Maps. Please verify that you've typed your postal code correctly");
				}else{
					var lat = point.lat();
					var long = point.lng();
					$("#latitude").val(lat);
					$("#longitude").val(long);
					document.dealerForm.submit();
				}
			});
		}else{
			alert("We're sorry, but your browser is not supported by Google Maps. Please consider upgrading your browser so that you may utilize this feature of our website.");	
		}
	}else{
		document.dealerForm.submit();
	}
}


/*	Load Google Maps for Dealer Listing
/*----------------------------------------------------------*/
function loadMaps(){
	if(GBrowserIsCompatible()){
		$(".dealer").each(function(){
			var id = $(this).attr("id").replace("dealer", "");
			if(document.getElementById("gmap"+id)){
				var lat = $("#latitude"+id).html();
				var long = $("#longitude"+id).html();
				var gmap = new GMap2(document.getElementById("gmap"+id));
				var point = new GLatLng(lat, long);
				gmap.setCenter(point, 13);
				var marker = new GMarker(point);
				gmap.addOverlay(marker);
			}else{
				$("#map"+id).html('<img src="/images/no-map.jpg" />');	
			}
	   });
	}else{
		alert("We're sorry, but your browser is not supported by Google Maps. Please consider upgrading your browser so that you may utilize this feature of our website.");	
	}
}


/*	Dealer Listing Paging
/*----------------------------------------------------------*/
function viewPage(num){
	//$("#dealerlist").fadeOut("fast");
	$("#loader").fadeIn("fast");
	$("#dealerlist").load("/includes/ajax/ajax_dealers.php", {page:num}, function(){
		loadMaps();
		//$("#dealerlist").fadeIn("slow");
		$("#loader").fadeOut("slow");
	});
}