var popupStatus = 0;
var popupbox= "popupLayer";
var canDisable = true;


function init(appdomain,isPopupLayout){
	if (appdomain != "GERMANY"){
		popupbox = "popupBox";
	}
	
	if (isPopupLayout){
		popupbox= "popupLayer";
	}
}

function loadPopup(box) {
	
	if(popupStatus==0){
		
		$("#backgroundPopup").css({
			"opacity": "0.7",
			"z-index":"9019"
		});
		
		
		switch(box){
			case 1:	//no deal
				
				//popupbox = "popupBox";
				$("#popup_normal").show();
				$("#popuplayer_raf").hide();
				
				$("#popup_description").show();
				$("#popup_title1").show();

				break;

			case 2: // register

				popupbox = "popupLayer";
				$("#popup_normal").show();
				$("#popuplayer_raf").hide();

				$('#popup_description').hide();
				$("#popup_title1").hide();
				$("#popup_title2").show();
			
				break;

			case 3:	//raf
				
				
				popupbox = "popupLayer";
				$("#popup_normal").hide();
				$("#popuplayer_raf").show();
				break;

			case 4:	//user reg popup after newsletter reg
				
				popupbox = "popupLayer";
				$("#popup_normal").hide();
				$("#popuplayer_raf").hide();
				$("#popuplayer_add_reg").show();
				
				break;
				
			case 5: // optin popup
				
				popupbox = "popupLayer";
				$("#popup_normal").hide();
				$("#popuplayer_raf").hide();
				$("#popuplayer_add_reg").hide();
				$("#popuplayer_opt_in").show();
				canDisable = false;
				
				break;
		}

		$("#backgroundPopup").fadeIn("slow");
		$("#"+popupbox).fadeIn("slow");
		popupStatus = 1;
	}
}


function disablePopup() {

	if(popupStatus==1 && canDisable){

		$("#popuplayer_opt_in").fadeOut("slow");
		$("#popuplayer_add_reg").fadeOut("slow");
		$("#backgroundPopup").fadeOut("slow");
		$("#popupBox").fadeOut("slow");
		$("#popupLayer").fadeOut("slow");
		
		popupStatus = 0;

	}
}

function centerPopup() {
	
	var windowWidth = document.documentElement.clientWidth;
	//var windowHeight = document.documentElement.clientHeight;
	//var windowHeight = $(window).scrollTop();
	var popupHeight = $("#"+popupbox).height();
	var popupWidth = $("#"+popupbox).width();
	
	//find actual position
	var wPos = $(window).scrollTop();

	//centering
	$("#"+popupbox).css({
		"position": "absolute",
		"top": wPos,
		"left": windowWidth/2-popupWidth/2,
		"z-index": "9020"
	});

	//only need force for IE6
//	$("#backgroundPopup").css({
//		"height": windowHeight,
//		"background-color": "black",
//		"position": "absolute",
//		"top": "0",
//		"left": "0",
//		"z-index": "0",
//		"width": "100%",
//		"height": "200%"
//	});
	overlay_size();
}


function getPageScrollTop(){
    var yScrolltop;
    var xScrollleft;
    if (self.pageYOffset || self.pageXOffset) {
      yScrolltop = self.pageYOffset;
      xScrollleft = self.pageXOffset;
    } else if(document.documentElement&& document.documentElement.scrollTop
      || document.documentElement.scrollLeft ){   // Explorer 6 Strict
      yScrolltop = document.documentElement.scrollTop;
      xScrollleft = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScrolltop = document.body.scrollTop;
      xScrollleft = document.body.scrollLeft;
    }
    arrayPageScroll = new Array(xScrollleft,yScrolltop);
    return arrayPageScroll;
  }


function overlay_size() {
    try {
      if(window.innerHeight && window.scrollMaxY
        || window.innerWidth && window.scrollMaxX) {
        h = window.innerHeight + window.scrollMaxY;
        w = window.innerWidth + window.scrollMaxX;
        var deff = document.documentElement;
        var wff = (deff&&deff.clientWidth)
          || document.body.clientWidth || window.innerWidth || self.innerWidth;
        var hff = (deff&&deff.clientHeight) || document.body.clientHeight
          || window.innerHeight || self.innerHeight;
        w -= (window.innerWidth - wff);
        h -= (window.innerHeight - hff);
        
      } else if(document.body.scrollHeight > document.body.offsetHeight
        || document.body.scrollWidth > document.body.offsetWidth) {
          // all but Explorer Mac
        h = document.body.scrollHeight;
        w = document.body.scrollWidth;
      } else {
        // Explorer Mac.would also work in Explorer 6 Strict, Mozilla and Safari
        var left_top = getPageScrollTop();
        h = left_top[1] + settings.height;
        w = left_top[0] + settings.width;
        if( h < document.body.offsetHeight) {
          h = document.body.offsetHeight;
        }
        if( w < document.body.offsetWidth) {
          w = document.body.offsetWidth;
        }
      }
    } catch(err) {
      w = $(document.body).width();
      h = $(document.body).height();
    }
    $("#backgroundPopup").css({"height":h+"px", "width":w +"px", "background-color": "black", "position": "absolute", "top": "0", "left": "0", "z-index": "9099"});
  }


$(function() {
	$(".popup").click(function(e){
		$("#page").after($("#"+popupbox));
		//$("select").hide("fast");
		centerPopup();
		loadPopup(1);
		e.preventDefault();
	});
	$(".popupContactClose").click(function(){
		$("select").css({"display" : "inline"});
		disablePopup();
	});
	
	$("#backgroundPopup").click(function(){
		$("select").css({"display" : "inline"});
		disablePopup();
	});
	
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			$("select").css({"display" : "inline"});
			disablePopup();
		}
	});
});