﻿/***************************/
//@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){
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {	
	$("#iesixbackgroundPopup").css({
			"opacity": "0.7"
		});
		$("#iesixbackgroundPopup").fadeIn("slow");
	}
	    $("select").css("display","none");
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

function loadPopupUpsell(){

	//loads popup only if it is disabled
	if(popupStatus==0){
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {	
	$("#iesixbackgroundPopup").css({
			"opacity": "0.7"
		});
		$("#iesixbackgroundPopup").fadeIn("slow");
	}
	$("select").css("display","none");
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContactupsell").fadeIn("slow");
		popupStatus = 1;
	}
}


//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
	    
		$("#backgroundPopup").fadeOut("slow");
		$("#iesixbackgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		$("#popupContactupsell").fadeOut("slow");
		$("select").css("display","inline");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){

	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	
	if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

	
	var windowHeight = document.documentElement.clientHeight;

	}
	
	else if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
        var windowHeight = window.innerHeight;
    }
    
	
	if(/Safari/.test(navigator.userAgent))
{
 var windowHeightScroll = document.body.scrollTop;
}
	
	
	else {
	var windowHeightScroll = document.documentElement.scrollTop;
		}
	
	
	var popupHeight = '260';
	var popupWidth = $("#popupContact").width();
	//var popupHeightupsell = $("#popupContactupsell").height();
	var popupHeightupsell = '566';
	//centering
	//alert (windowHeightScroll);

	var top = (windowHeight / 2 - popupHeight / 2);
	if (top <= 10) {
		top = 10;
	}
	
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeightScroll + top,	
		"left": "200px"
	});

	var top = (windowHeight / 2 - popupHeightupsell / 2);
	if (top <= 10) {
		top = 10;
	}
		
	$("#popupContactupsell").css({
		"position": "absolute",
		"top": windowHeightScroll + top,
		//"top": "300px",
		"left": "300px"
	});
	//only need force for IE6
if ($.browser.msie && $.browser.version.substr(0,1)<7) {	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"top": windowHeightScroll
	});
	
	$("#iesixbackgroundPopup").css({
		"height": windowHeight,
		"top": windowHeightScroll
	});
	}

}



//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$(".listeraddtocart").click(function(){
	
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$(".popupclose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});