/*
 * jquery region selector
 * By Anian Thrainer
 * Copyright (c) 2009 Anian Thrainer
*/

/****************************************************************************************
****                                                                                 ****
**** please make all adjustments here                                                ****
****                                                                                 ****
****************************************************************************************/

myRegionCookie = "amplid12_region"; //cookie name

region = [
  ["North America ", "en-us"],
  ["Deutschland ", "de-de"],
  ["Österreich  ", "de-at"],
  ["France  ", "fr-fr"],
  ["Europe (english) ", "eu"],
  ["Europa (deutsch) ", "de"],
  ["Schweiz (deutsch) ", "de-ch"],
  ["Suisse (français) ", "fr-ch"],
  ["日本語 ", "jp"]
];

/***  !!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!   ****/

//create regulaer expression to detect cookie
var expressionHelper = ""; 
for (var i = 0, j = region.length; i < j; i++) { //adds country codes out of array
  if (i+1 == j) { // no | is added, as this is the last option
    expressionHelper = expressionHelper + region[i][1]; 
  } else { //adds an addition | as more options are following
    expressionHelper = expressionHelper + region[i][1] + "|";
  }
}
var myExpression = new RegExp(".*?" + myRegionCookie + "=(" + expressionHelper + ").*?")

function regionModalWidth(id) { //determines the width of the modal window
  var w = $("div[id='"+id+"']").width();
  return w;
}

function regionModalHeight(id) { //determines the height of the modal window
  var h = $("div[id='"+id+"']").height();
  return h;
}

function at_detectMacXFF() { //determine if it's MacXFF
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function createCookie(region) {//creates cookie
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*365));
		document.cookie = myRegionCookie+"=" + region + ";expires=" + date.toGMTString() + ";path=/";
}

function changeRegion(region) {
  createCookie(region);
  if (document.referrer !== "") {
    if (document.referrer.substring(0, 22) == "http://www.amplid.com/") {
      document.location.href = document.referrer;
    } else {
      document.location.href = "../main.php";
    }    
  } else {
   document.location.href = "../main.php";
  }
}

function at_CreateModal() {//create modal window  
  try { //create mask depending on browser
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("AT_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='AT_HideSelect'></iframe><div id='AT_overlay'></div>");
			}
		}else{//all others
			if(document.getElementById("AT_overlay") === null){
				$("body").append("<div id='AT_overlay'></div>");
			}
		}
		
		if(at_detectMacXFF()){ //is it a MacXFF
			$("#AT_overlay").addClass("AT_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#AT_overlay").addClass("AT_overlayBG");//use background and opacity
		}
	} catch(e) {
		//nothing here
	}
   
  var modalWindow = $("<div id='AT_window' />");
  //modalWindow.append("<h4>Region Selector</h4>");
    
  var modalForm = $("<form id='selectRegion'></form>"); //create form
	
  var modalSelect = document.createElement("select"); //create select tag
	modalSelect.id = "regionSelect";
	
  for (var i = 0, j = region.length; i < j; i++) { //fill select with options
		modalSelect.options[i] = new Option(region[i][0], region[i][1]);
	}
	modalForm.append(modalSelect);
  
  var modalSubmit = document.createElement("input");
	modalSubmit.id = "regionSubmit";
  modalSubmit.type = "submit";
	modalSubmit.value = "Submit";
	
	modalForm.append(modalSubmit);
  
	modalForm.submit(function(e) { //helper function for submit
    e.preventDefault();
		var sValue = this.regionSelect.options[this.regionSelect.options.selectedIndex].value;
		createCookie(sValue);
		location.reload();
  });
  
  modalWindow.append(modalForm);
  
  $("body").append(modalWindow); //show modal
	modalWindow.fadeIn(150);
		
  var marginLeft = regionModalWidth('AT_window')/2; //adding margins to center modal window
  var marginTop = regionModalHeight('AT_window')/2;
  modalWindow.css({
		"margin-left": -marginLeft,
		"margin-top": -marginTop
	});
}

function regionInit() {
  if (document.cookie !== null) {
    if (document.cookie.match(myExpression)) {
      return;
    }
  }
	at_CreateModal();
}

//start init
$(document).ready(function(){
	regionInit();
});
