var popupStatus = 0;


function loadPopup(popup){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.5"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(popup).fadeIn("slow");
		popupStatus = 1;
		
	}
}


function disablePopup(popup){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(popup).fadeOut("slow");
		popupStatus = 0;
	}
}




function centerPopup(popup){

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(popup).height();
	var popupWidth = $(popup).width();

	$("#popupContact").css({"position": "absolute"});
        $("#popupContact").css({"top": (windowHeight/2) - (popupHeight/2)});
        $("#popupContact").css({"left": (windowWidth/2) - (popupWidth/2)});
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
	var background = document.getElementById('backgroundPopup');
	background.style.cssText = "display: none;";
	
}

// This should be removed - another way of doing this a unique id at the end of load function url
$.ajaxSetup ({
    cache: false
});

function loadBannerPopup(banner) {
    $("#corky").load("/banners #banner"+banner, function() {
		//centerPopup("#popupBanner");
		var windowWidth = document.documentElement.clientWidth;
		var width = windowWidth/2 - 363 + "px";
		
		loadPopup("#popupBanner");
        $("#popupBanner").fadeIn("slow");
		$("#popupBanner").css("background", "url(banner"+banner+".jpg) no-repeat");
		$("#popupBanner").css("position", "absolute");
        $("#popupBanner").css("top","290px");
        $("#popupBanner").css("left",width);
        $("#popupBannerClose").click(function() {
        	disablePopup("#popupBanner");
		});
		$("#popupBannerClose").css("left","498px");
		$("#popupBannerClose").css("top","10px");
		$("#popupBannerOppslag").css("left","370px");
		$("#popupBannerOppslag").css("top","85px");		
		$("#popupBannerOppslag").click(function() {
			disablePopup("#popupBanner");
			$("#corky").load("/corky_view #madness", function () {
                		centerPopup("#popupContact");
                		loadPopup("#popupContact");
                		$("#popupContactClose").click(function(){
                        		disablePopup("#popupContact");
                		});
                		$("#backgroundPopup").click(function(){
                        		disablePopup("#popupContact");
                		});
                	});

		});
		$("#popupBannerLink").css("left","140px");
		$("#popupBannerLink").css("top","355px");	
    });

    $(document).keypress(function(e){
                if(e.keyCode==27 && popupStatus==1){
                        disablePopup("#popupBanner");
			popupStatus = 1;
			disablePopup("#popupContact");
                }
    });

}

$(document).ready(function(){

	
	$("#corkyButton").click(function(){
		$("#corky").load("/corky_view #madness", function () {
		centerPopup("#popupContact");
		loadPopup("#popupContact");
		$("#popupContactClose").click(function(){
			disablePopup("#popupContact");
		});
		$("#backgroundPopup").click(function(){
			disablePopup("#popupContact");
		});
		
		});
		
	});
		
	/*
	$(".mosaic04").click(function(){
		$("#corky").load("/corky_view #madness", function () {
		centerPopup("#popupContact");
		loadPopup("#popupContact");
		$("#popupContactClose").click(function(){
			disablePopup("#popupContact");
		});
		$("#backgroundPopup").click(function(){
			disablePopup("#popupContact");
		});
		
		});
		
	});
	
	$(".mosaic04").find("a").each(function(i) {
	    $(this).attr("href","#");
	});
	*/

	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup("#popupContact");
		}
	});

});

