/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").show();
		$("#popupContact").show();
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").hide();
		$("#popupContact").hide();
		$("#contactArea").html('');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(vcenter){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").outerHeight();
	var popupWidth = $("#popupContact").outerWidth();
	//centering
	var arrPageSizes = ___getPageSize();
	var arrPageScroll = ___getPageScroll();
    
    if (vcenter == true) {
        lower_down = 0;
    } else {
        lower_down = 150;
    }
    
	$("#popupContact").css({
		"position": "absolute",
		"top": arrPageScroll[1] + (arrPageSizes[3] / 10) + lower_down,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": arrPageSizes[3]
	});
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function ___getPageSize(){
	    var xScroll, yScroll;
	    if (window.innerHeight && window.scrollMaxY) {
	        xScroll = window.innerWidth + window.scrollMaxX;
	        yScroll = window.innerHeight + window.scrollMaxY;
	    }
	    else 
	        if (document.body.scrollHeight > document.body.offsetHeight) {
	            xScroll = document.body.scrollWidth;
	            yScroll = document.body.scrollHeight;
	        }
	        else {
	            xScroll = document.body.offsetWidth;
	            yScroll = document.body.offsetHeight;
	        }
	    var windowWidth, windowHeight;
	    if (self.innerHeight) {
	        if (document.documentElement.clientWidth) {
	            windowWidth = document.documentElement.clientWidth;
	        }
	        else {
	            windowWidth = self.innerWidth;
	        }
	        windowHeight = self.innerHeight;
	    }
	    else 
	        if (document.documentElement && document.documentElement.clientHeight) {
	            windowWidth = document.documentElement.clientWidth;
	            windowHeight = document.documentElement.clientHeight;
	        }
	        else 
	            if (document.body) {
	                windowWidth = document.body.clientWidth;
	                windowHeight = document.body.clientHeight;
	            }
	    if (yScroll < windowHeight) {
	        pageHeight = windowHeight;
	    }
	    else {
	        pageHeight = yScroll;
	    }
	    if (xScroll < windowWidth) {
	        pageWidth = xScroll;
	    }
	    else {
	        pageWidth = windowWidth;
	    }
	    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	    return arrayPageSize;
	};
	function ___getPageScroll(){
	    var xScroll, yScroll;
	    if (self.pageYOffset) {
	        yScroll = self.pageYOffset;
	        xScroll = self.pageXOffset;
	    }
	    else 
	        if (document.documentElement && document.documentElement.scrollTop) {
	            yScroll = document.documentElement.scrollTop;
	            xScroll = document.documentElement.scrollLeft;
	        }
	        else 
	            if (document.body) {
	                yScroll = document.body.scrollTop;
	                xScroll = document.body.scrollLeft;
	            }
	    arrayPageScroll = new Array(xScroll, yScroll);
	    return arrayPageScroll;
	};
