// JavaScript Document


var geocoder;
var foundLocation = false;
var userLocation = "";
var locationForms;
var addressGeo;
var addressGeoDesc;

$(document).ready(function() {

    locationForms = $('*[id="locationdistance"]').get();

    addressGeo = $('*[id="address_geo"], *[id="address_geo2"]').get();

    addressGeoDesc = $('*[id="address_geo_desc"]').get();

});
function checkForLocation(){
	
	if(foundLocation){
			setDistance(userLocation);
	}
}

function initializeGeolocation() 
{	
	$("#address_geo").html("Loading address from geo location");
		 var mybrowser = navigator.userAgent;
		 
		 
		 if (mybrowser.indexOf("MSIE") >= 0) {
			 $("#lc_share").addClass("lc_share_ie");
			 $("#browser_type").html("We've detected you are using Internet Explorer, and to get your current location, please click on <b>Allow once</b> or you can click on <b>Options for this site</b> and select <b>Always allow</b>");
		 }else if(mybrowser.indexOf("Chrome") >= 0){
			 $("#lc_share").addClass("lc_share_cm");
			 $("#browser_type").html("We've detected you are using Google Chrome, and to get your current location, please click on <b>Allow</b> and to remember for this site, click on the check box");	 
			 
		 }else if(mybrowser.indexOf("Mozilla") >= 0 ){
			 $("#lc_share").addClass("lc_share_mz");	
			 $("#browser_type").html("We've detected you are using Mozilla, andto get your current location, please click on <b>Share location</b> and to remember for this site, click on the check box");	 
		 }
			 
			 
	
	
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition( function (position) {  
			
			// Did we get the position correctly?
			// alert (position.coords.latitude);
			
			// To see everything available in the position.coords array:
			// for (key in position.coords) {alert(key)}
			
		$("#address_geo").html("got geo location");
		
			mapThisGoogle(position.coords.latitude,position.coords.longitude);
 			
 		}, // next function is the error callback
 			function (error){
			getLocationFromUser();
				
 			/*	switch(error.code) 
 				{
       				case error.TIMEOUT:
 						alert ('Timeout');
 						break;
 					case error.POSITION_UNAVAILABLE:
 						alert ('Position unavailable');
 						break;
 					case error.PERMISSION_DENIED:
 						alert ('Permission denied');
 						break;
 					case error.UNKNOWN_ERROR:
 						alert ('Unknown error');
 						break;
 				}*/
 			}
 		);
 	} 
 	else 
 	{
	 	$("div:address_geo").html("Enter your City, State, or Zip")
	 	$("div:address_geo2").html("Enter your City, State, or Zip")
		getLocationFromUser();
	}  
}

 
function getLocationFromUser(){
	if(!foundLocation)
		tb_open_new('/app/site/location.do?height=400&width=700&modal=true');	
	else{
		setDistance(userLocation);
	}
}
 
function mapThisGoogle(latitude,longitude)
{
	//var mapCenter = new GLatLng(latitude,longitude);
	//map = new GMap2(document.getElementById("map"));
//	map.setCenter(mapCenter, 15);
//	map.addOverlay(new GMarker(mapCenter));
	
	// Start up a new reverse geocoder for addresses?
	geocoder = new GClientGeocoder();
	geocoder.getLocations(latitude+','+longitude, addAddressToMap);
}
 
function addAddressToMap(response)
{
	if (!response || response.Status.code != 200) {
	 	$("#address_geo").html("Enter your City, State, or Zip");
	 	$("#address_geo2").html("Enter your City, State, or Zip");
		foundLocation = false;
		getLocationFromUser();
	} else {
		place = response.Placemark[0];
		var address = place.address.split(",");
		$('#address_geo').html(address[1] + "," + address[2] + "," + address[3]);
		$('#address_geo2').html(address[1] + "," + address[2] + "," + address[3]);
		var datasplit = $.trim( address[2]).split(" ");
		userLocation = address[1] + "," + datasplit[0] + ", " + datasplit[1] ;
		setDistance(userLocation);
		foundLocation = true;
		
		$.ajax({
			  url: "/app/site/catalogsetLocation.do?address=" + $.trim( address[0]) + "&city=" + $.trim( address[1]) + "&state=" + datasplit[0] + "&zip=" + datasplit[1] + "&country=" + address[3],
			  success: function(){
				  
			  }
		});
		
		
	}
}
