﻿//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusEmail = 0;
var popupStatusFlag = 0;
var popupStatusHome = 0;
var popupStatusBuyAlbum = 0;

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.outerHeight() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.outerWidth() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

//loading popup with jQuery magic!
function loadPopup(div){
	//loads popup only if it is disabled
	if (div=="#popupEmail")
	{
	    if(popupStatusEmail==0){
		    $("#backgroundPopup").css({
			    "opacity": "0.7"
		    });
		    $("#backgroundPopup").fadeIn("fast");
		    $(div).fadeIn("fast");    		
		    popupStatusEmail = 1;		   
	    }
	}
	if (div=="#popupBuyAlbum")
	{
	    if(popupStatusBuyAlbum==0){
		    $("#backgroundPopup").css({
			    "opacity": "0.7"
		    });
		    $("#backgroundPopup").fadeIn("fast");
		    $(div).fadeIn("fast");    		
		    popupStatusBuyAlbum= 1;		   
	    }
	}
	if (div=="#popupHome")
	{
	    if(popupStatusHome==0){
		    $("#backgroundPopup").css({
			    "opacity": "0.7"
		    });
		    $("#backgroundPopup").fadeIn("fast");
		    $(div).fadeIn("fast");    		
		    popupStatusHome = 1;		   
	    }
	}
	if (div=="#popupFlag")
	{
	    if(popupStatusFlag==0){
		    $("#backgroundPopup").css({
			    "opacity": "0.7"
		    });
		    $("#backgroundPopup").fadeIn("fast");
		    $(div).fadeIn("fast");    		
		    popupStatusFlag = 1;		   
	    }
	}
	if (div=="#popupDiv")
	{
	    if(popupStatusFlag==0){
		    $("#backgroundPopup").css({
			    "opacity": "0.7"
		    });
		    $("#backgroundPopup").fadeIn("fast");
		    $(div).fadeIn("fast");    		
		    popupStatusFlag = 1;		   
	    }
	}
	
}

//disabling popup with jQuery magic!
function disablePopup(div){
	//disables popup only if it is enabled
	if (div=="#popupEmail")
	{		    
	    if(popupStatusEmail==1){
		    $("#backgroundPopup").fadeOut("fast");
		    $(div).fadeOut("fast");
		    popupStatusEmail = 0;
	    }
	}
	if (div=="#popupBuyAlbum")
	{		    
	    if(popupStatusBuyAlbum==1){
		    $("#backgroundPopup").fadeOut("fast");
		    $(div).fadeOut("fast");
		    popupStatusBuyAlbum = 0;
	    }
	}
	if (div=="#popupHome")
	{		    
	    if(popupStatusHome==1){
		    $("#backgroundPopup").fadeOut("fast");
		    $(div).fadeOut("fast");
		    popupStatusHome= 0;
	    }
	}
	if (div=="#popupFlag")
	{		    
	    if(popupStatusFlag==1){
		    $("#backgroundPopup").fadeOut("fast");
		    $(div).fadeOut("fast");
		    popupStatusFlag = 0;
	    }
	}
	if (div=="#popupDiv")
	{		    
	    if(popupStatusFlag==1){
		    $("#backgroundPopup").fadeOut("fast");
		    $(div).fadeOut("fast");
		    popupStatusFlag = 0;
	    }
	}
}

//centering popup
function centerPopup(div){
	//request data for centering
	//var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	//var popupHeight = $(div).height();
	//var popupWidth = $(div).width();
	
//	//centering	
//	$("#popupContact").css({
//		"position": "absolute",
//		"top": windowHeight/2-popupHeight/2,
//		"left": windowWidth/2-popupWidth/2
//	});
	//centering	
//	alert(event);
//	if (document.all) { // grab the x-y pos.s if browser is IE
//	    
//        tempY = event.clientY;
//    } else {  // grab the x-y pos.s if browser is NS
//        
//        tempY = e.pageY;
//    }  	
//    
//    if(windowHeight-tempY < popupHeight + 100)
//        tempY = windowHeight - popupHeight - 100;        
   // alert($(div)); 
   $(div).center();
//	$(div).css({
//		"position": "absolute",
//		"top": windowHeight/2-popupHeight/2,
//		"left": windowWidth/2-popupWidth/2
//	});
	//alert(event.clientY);
	//alert(windowWidth);
	//only need force for IE6	
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}
function myPopUpClassSelector(triggerClass,divContent,lnkClose)
{
$(triggerClass).click(function(event){
		//centering with css
		centerPopup(divContent);
		loadPopup(divContent);
		
		if(divContent=="#popupDiv")
		{
		    document.getElementById('hdTemplateId').value =this.href.substring(this.href.lastIndexOf('/')+1,this.href.length);
		    event.preventDefault();
		}
	});
				
	//CLOSING POPUP
	//Click the x event!
	$(lnkClose).click(function(){
		disablePopup(divContent);
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup(divContent);
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
        if (divContent=="#popupDiv")
	    {
		    if(e.keyCode==27 && popupStatusFlag==1){
			    disablePopup(divContent);
		    }
        }
        
	});
}
function myPopUp(triggerButton,divContent, lnkClose){
	//LOADING POPUP
	//Click the button event!
	//alert(triggerButton);
	$(triggerButton).click(function(event){
		//centering with css
		//alert('cp1');
		centerPopup(divContent);
		//alert('cp2');
		//load popup
		loadPopup(divContent);
		//alert('cp3');
		if (divContent=="#popupFlag")
		{
		    loadCategories();
		}
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$(lnkClose).click(function(){
		disablePopup(divContent);
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup(divContent);
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
	    if (divContent=="#popupEmail")
	    {
		    if(e.keyCode==27 && popupStatusEmail==1){
			    disablePopup(divContent);
		    }
        }
        if (divContent=="#popupBuyAlbum")
	    {
		    if(e.keyCode==27 && popupStatusBuyAlbum==1){
			    disablePopup(divContent);
		    }
        }
        if (divContent=="#popupHome")
	    {
		    if(e.keyCode==27 && popupStatusHome==1){
			    disablePopup(divContent);
		    }
        }
        if (divContent=="#popupFlag")
	    {
		    if(e.keyCode==27 && popupStatusFlag==1){
			    disablePopup(divContent);
		    }
        }
        if (divContent=="#popupDiv")
	    {
		    if(e.keyCode==27 && popupStatusFlag==1){
			    disablePopup(divContent);
		    }
        }
        
	});
}


//note: instead of this, I called the function << myPopUp(".JQDiv"); >> on page load in every page I need 
//  (solving a bug this way: after a page postback the popup isn't working anymore)
//CONTROLLING EVENTS IN jQuery
//$(document).ready(function(){	
//	myPopUp(".JQDiv");
//});
