
function geolocate(timezone, cityPrecision) {
	  var key = 'd9138368c3aa6c9f87e0fe18e009118258953e0c45b40eca6c9f22a2b2370be8';
	  var api = (cityPrecision) ? "ip-city" : "ip-country";
	  var domain = 'api.ipinfodb.com';
	  var version = 'v3';
	  var url = "http://" + domain + "/" + version + "/" + api + "/?key=" + key + "&format=json" + "&callback=?";
	  var geodata;
	  var JSON = JSON || {};
	  
	  // implement JSON.stringify serialization
	  JSON.stringify = JSON.stringify || function (obj) {
	    var t = typeof (obj);
	    if (t != "object" || obj === null) {
	      // simple data type
	      if (t == "string") obj = '"'+obj+'"';
	        return String(obj);
	    } else {
	    // recurse array or object
	      var n, v, json = [], arr = (obj && obj.constructor == Array);
	      for (n in obj) {
	        v = obj[n]; t = typeof(v);
	        if (t == "string") v = '"'+v+'"';
	        else if (t == "object" && v !== null) v = JSON.stringify(v);
	        json.push((arr ? "" : '"' + n + '":') + String(v));
	      }
	      return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	    }
	  };
	  
	  // implement JSON.parse de-serialization
	  JSON.parse = JSON.parse || function (str) {
	    if (str === "") str = '""';
	      eval("var p=" + str + ";");
	      return p;
	  };
	  
	  //Check if cookie already exist. If not, query IPInfoDB
	  this.checkcookie = function(callback) {
	    geolocationCookie = getCookie('geolocation');
	    if (!geolocationCookie) {
	      getGeolocation(callback);
	    } else {
	      geodata = JSON.parse(geolocationCookie);
	      callback();
	    }
	  }
	  
	  //Return a geolocation field
	  this.getField = function(field) {
	    try {
	      return geodata[field];
	    } catch(err) {}
	  }
	  
	  //Request to IPInfoDB
	  function getGeolocation(callback) {
	    try {
	      $.getJSON(url,
	      function(data){
	        if (data['statusCode'] == 'OK') {
	          geodata = data;
	          JSONString = JSON.stringify(geodata);
	          setCookie('geolocation', JSONString, 1);
	          callback();
	        }
	      });
	    } catch(err) {}
	  }
	  
	  //Set the cookie
	  function setCookie(c_name, value, expire) {
	    var exdate=new Date();
	    exdate.setDate(exdate.getDate()+expire);
	    document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
	  }
	  
	  //Get the cookie content
	  function getCookie(c_name) {
	    if (document.cookie.length > 0 ) {
	      c_start=document.cookie.indexOf(c_name + "=");
	      if (c_start != -1){
	        c_start=c_start + c_name.length+1;
	        c_end=document.cookie.indexOf(";",c_start);
	        if (c_end == -1) {
	          c_end=document.cookie.length;
	        }
	        return unescape(document.cookie.substring(c_start,c_end));
	      }
	    }
	    return '';
	  }
	}

jQuery(document).ready(function(){
	var setPlaceholder=function(field){
		if(typeof field.each != "function")field=jQuery(this);
		field.each(function(){
			if(this.value==""){
				this.value=jQuery(this).attr("placeholder");
			}
		});
	}
	jQuery("div.searchfield input").focus(function(){
		if(this.value==jQuery(this).attr("placeholder")){
			this.value="";
		}
	});
	jQuery("div.searchfield input").blur(setPlaceholder);
	setPlaceholder(jQuery("div.searchfield input"));
	var setGeoFields=function(){
		console.log("set geo fields")
		var visitorGeolocation = new geolocate(false, false);
		var callback = function(){
			            var cname=jQuery("option[value='"+visitorGeolocation.getField('countryCode')+"']").text()
						var ac=jQuery("option.autocountry")
						ac.text(cname);
						ac.attr("value",visitorGeolocation.getField('countryCode'))
						ac.attr("disabled",false)
		                jQuery("span.currentCountry").text(visitorGeolocation.getField('countryName'));
		                jQuery("input.currentCountryCode").attr("value",visitorGeolocation.getField('countryCode'));
		                jQuery("input[name=country]").attr("value",visitorGeolocation.getField('countryName'));
		                jQuery("input[name=ip]").attr("value",visitorGeolocation.getField('ipAddress'));
		                jQuery("input[name=pagetitle]").attr("value",document.title);
		               };
		visitorGeolocation.checkcookie(callback);
	}
	var geoTest=jQuery("span.currentCountry, input.currentCountryCode, input[name=country]")
	if(geoTest.size()>=0)setGeoFields()
});
/**/


