﻿// Set popup status
//0 disabled 1 enabled
var popupStatus = 0;

// Load popup
function loadPopup(){	
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$(".popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}


function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(".popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(".popupContact").height();
	var popupWidth = $(".popupContact").width();
    var vertScroll = 0; // set initial vertscroll value
    
    // Check browser type to determine offset
    if( typeof(window.pageYOffset) == 'number') {
        //Netscape compliant
        vertScroll = window.pageYOffset;    
    }
    else if(document.body && document.body.scrollTop) 
    {
        //DOM compliant
        vertScroll = document.body.scrollTop;    
    } else if(document.documentElement && document.documentElement.scrollTop) {
        //IE6 standards compliant mode
        vertScroll = document.documentElement.scrollTop;    
    }
                
    // Define constant distance from viewport "top"
    var seperator = 25; 
    var zindex = "10000";
    
    //centering TEST working version
	$(".popupContact").css({
		"z-index": zindex,
		"position": "absolute",		
		"top": seperator + vertScroll,	
		"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6 here	
	$("#backgroundPopup").css({
		"height": windowHeight
	});	    	
}

function setPopUpContents(productNumber){       
    $("#ctl00_MainContent_popUpImage").html("<center><p class='popUpInfo'>Product number: "+productNumber+"</p></center><img src='DisplayProductImage.aspx?Product_Number="+productNumber+"&ImageSize=large' alt='RDM image viewer' />");
}

//CONTROLLING EVENTS IN jQuery 
$(document).ready(function(){
	
	//LOADING POPUP	
	$(".popUpClickArea").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP "x"	
	$(".popupContactClose").click(function(){
		disablePopup();
	});
	//Click outside image area
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Escape key event
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});