// Yourpartygirls.com - IP based Geo Location
//This script was created by CosmicMedia (http://www.freelancer.co.uk/users/1589047.html) 
//This script also includes an ip geo location API by ipinfodb.com


//function geolocate(timezone, cityPrecision, objectVar).
//If you rename your object name, you must rename 'visitorGeolocation' in the function
var visitorGeolocation = new geolocate(false, true, 'visitorGeolocation');
 
//Check for cookie and run a callback function to execute after geolocation is read either from cookie or IPInfoDB API
var callback = function(){
	
                var data = visitorGeolocation.getField('regionName');
				var state = data.toLowerCase();
				
				//Choose landing page based on location
				//All cases must be lower case!!!!!!!!!
				switch(state){
					
					case 'washington':
					RedirectTo('http://www.yourpartygirls.com/newsite/seattle-strippers-photo-gallery.html');
					break;
					
					case 'oregon':
					RedirectTo('http://www.yourpartygirls.com/newsite/portland-stripper-photo-gallery.html');
					break;
					
					case 'california':
					RedirectTo('http://www.yourpartygirls.com/newsite/san-francisco-strippers-photo-gallery.html');
					break;
					
				
				
				}//switch			
				
               };
			   
			   
			   
			   
//Launch			   
window.onload = function(){ 

visitorGeolocation.checkcookie(callback);


}










//This function redirects users
function RedirectTo(location){

		var cook = getCookie("ip_location");
		
		if (cook!=null && cook!=""){
			
		//Add alternative code 
		  
		  
		}else{
		
		//set cookie
		setCookie("ip_location", 'set', 1);
		window.location = location;
		
		}
		

}//func



function setCookie(c_name,value,exdays){
	
	

	
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
		
		if(getCookie(c_name)){return true;}else{return false;}
}



function getCookie(c_name){
	
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
		  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		  x=x.replace(/^\s+|\s+$/g,"");
		  if (x==c_name)
			{
			return unescape(y);
			}
		  }
}









//Below is the ip geolocation api script
function geolocate(timezone, cityPrecision, objectVar) {
 
  var api = (cityPrecision) ? "ip-city" : "ip-country";
  var domain = 'api.ipinfodb.com';
  var url = "http://api.ipinfodb.com/v3/"+api+"/?key=7b3d5e6cdb28be4f217bac6276261469b4741ed2c96c2fa9988aa6a62af566a5&format=json" + "&callback=" + objectVar + ".setGeoCookie";
  var geodata;
  var callbackFunc;
  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');
    callbackFunc = callback;
    if (!geolocationCookie) {
      getGeolocation();
    } else {
      geodata = JSON.parse(geolocationCookie);
      callbackFunc();
    }
  }
 
  //API callback function that sets the cookie with the serialized JSON answer
  this.setGeoCookie = function(answer) {
    if (answer['statusCode'] == 'OK') {
      JSONString = JSON.stringify(answer);
      setCookie('geolocation', JSONString, 365);
      geodata = answer;
      callbackFunc();
    }
  }
 
  //Return a geolocation field
  this.getField = function(field) {
    try {
      return geodata[field];
    } catch(err) {}
  }
 
  //Request to IPInfoDB
  function getGeolocation() {
    try {
      script = document.createElement('script');
      script.src = url;
      document.body.appendChild(script);
    } 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 '';
  }
}
