$(document).ready(function(){
	$('.main_menu li a[rel=business]').addClass('active');
 	$("#perioddiv input:radio").change(function(){
 		$("#locStatus").val(this.value);
 	});
	 $('#perioddiv input').customInput('');

    var countryID   =    $('#locCountryID').val();
	var state  =   $('#stateID').val(); 
	listStates(countryID,state);
			 

	var setLat	=	$("#locLatitude").val();
	var setLon	=	$("#locLongitude").val();
	if (setLat>0 || setLon>0)
	{
	} 
	else 
	{         
		var setLat = 25.7897333;
		var setLon = -80.2259474;
	
	}
	latlng = new google.maps.LatLng (setLat, setLon);
	myOptions 	=  {
					  zoom:10,
					  mapTypeId: google.maps.MapTypeId.ROADMAP,
					  center: latlng,
					  disableDefaultUI: true,
					  
					  zoomControl:true,
									zoomControlOptions : 
									{
									  style: google.maps.ZoomControlStyle.SMALL
									}
				   }
				   
	map = new google.maps.Map(document.getElementById("city_add_form_map"), myOptions);  
	
	
	marker = new google.maps.Marker(
	{
		position: latlng,
		map: map,
		zoom: 8,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		Draggable : true,
		streetViewControl: false
	});
	google.maps.event.addListener (marker, 'dragend', function (event)  
	{
	  var point = marker.getPosition();
	  $("#locLatitude").val(point.lat());
	  $("#locLongitude").val(point.lng());
	});
	
	google.maps.event.addDomListener(document.getElementById('locLatitude'), 'change', function(event) 		
	{
		newpos	=	new google.maps.LatLng ($("#locLatitude").val(), $("#locLongitude").val());
		marker.setPosition(newpos);
		map.panTo(newpos)
	});	
	google.maps.event.addDomListener(document.getElementById('locLongitude'), 'change', function(event) 		
	{
		newpos	=	new google.maps.LatLng ($("#locLatitude").val(), $("#locLongitude").val());
		marker.setPosition(newpos);
		map.panTo(newpos)
	});	

	google.maps.event.addDomListener(document.getElementById('locCity'), 'change', function(event) 		
	{
		city		=	$("#locCity").val();
		country		=	$("#locCountryID option:selected").text();
		province	=	$("#locName").val();
		state		=	'';
		if(province == "")
			state		=	$("#locStateID option:selected").text();
		else
			state		=	province;
		address		=	'';
		if(city != "")
			address	= city;
		if(state != "Select")
			address		=	address+","+state;	
		if(country != "Select")
			address		=	address+","+country;
			//alert(address);
		geocoder = new google.maps.Geocoder();
		geocoder.geocode( { 'address': address}, function(results, status) 
		{
		  if (status == google.maps.GeocoderStatus.OK) 
		  {
			geopos	=	results[0].geometry.location;
			marker.setPosition(geopos);
			map.panTo(geopos);
	 		$("#locLatitude").val(geopos.lat());
	  		$("#locLongitude").val(geopos.lng());			
		  } 
		 });		
	});
});

function listStates(countryID,stateID)
{
  $.ajax({
   type: "POST",
   url: GLOBAL_BASE_PATH+"/location/ajaxState/",
   data: "countryID="+countryID+"&stateID="+stateID,
   success: function(msg){
	$("#div_err_frmBusinessLocation_locProvince").addClass("hide");   
    if((msg).trim()!=''){
		$("#stateSelectedType").val('state');
		$("#locStateID").show();
		$("#locProvince").hide();
         $("#locStateID").html(msg);
		 $("#div_err_frmBusinessLocation_locProvince").addClass("hide");
    }
	else{
		$("#locProvince").show();
		$("#locStateID").hide();
		$("#locStateID").val('');
		$("#stateSelectedType").val('province');
	}
   
    }
  });
}