var geocoder;
var geohtml5=getCookie("Tipao-geohtml");
if (geohtml5!=null && geohtml5!="")
{
}
else
{
	if (navigator.geolocation) {
	  navigator.geolocation.getCurrentPosition(successFunction);
	  setCookie("Tipao-geohtml", 1, '10');
	} 
}

//Get latitude and longitude;
function successFunction(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
  codeLatLng(lat, lng)
}

function initialize() {
  try{geocoder = new google.maps.Geocoder({});}
  catch(error){}
}

function codeLatLng(lat, lng) {

  var latlng = new google.maps.LatLng(lat, lng);
  geocoder.geocode({'latLng': latlng}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      if (results[1]) {
      //find country name
           for (var i=0; i<results[0].address_components.length; i++) {
          for (var b=0;b<results[0].address_components[i].types.length;b++) {

          //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
              if (results[0].address_components[i].types[b] == "locality") {
                  //this is the object you are looking for
                  city_name = results[0].address_components[i].long_name;
                  break;
              }
              if (results[0].address_components[i].types[b] == "country") {
                  //this is the object you are looking for
                  country_code = results[0].address_components[i].short_name;
                  country_name = results[0].address_components[i].long_name;
                  break;
              }
          }
      }

      //city data
   	 jQuery.post(URL_BASE+"/location/google_locator",{latitude:lat,longitude:lng,city:city_name,country:country_name,country_code:country_code},
   			 function(data){
				 if(data){
						
					 //MODAL DIALOG
					 (function($){
						$('#html5_geo_id').val(data.change_location_id);
						$('#html5_country_code').val(data.change_country_code);
						$('#city_change_geo_html').html(data.change_location_name);
						 
					 	$( "#dialog-choosegeo:ui-dialog" ).dialog( "destroy" );

					 		$( "#dialog-choosegeo" ).dialog({
					 			width:600,
					 			modal: true
					 		});
					 	
					 })(jQuery);
					 
				 }
		   	 }, "json");

      } else {
        //alert("No results found");
      }
    } else {
      //alert("Geocoder failed due to: " + status);
    }
  });
}

function majGeoHtml(html5_geo_id,html5_country_code){
	html5_geo_id = document.getElementById('html5_geo_id').value;
	html5_country_code = document.getElementById('html5_country_code').value;
	jQuery.post(URL_BASE+"/location/ajax_init_geo/",{city_id:html5_geo_id,country_post:html5_country_code,col:false},
  			 function(data){
				 if(data){
						if (data.stat == 'ok'){
							window.location = URL_BASE+'/home/';
						}
				 }
		   	 }, "json");
}
jQuery(document).ready( function($){
	initialize();

	closeDialog = function() {
					$( "#dialog-choosegeo:ui-dialog" ).dialog( "destroy" );
			      };
});
