﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//Heavily Modifed by Moshe Gottlieb//

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;

//current popup id
var currpopid = -1;

//check status of popup
function getpopupStatus(objid)
{
	return($("#popupContact_"+objid).is(':visible'));
}

//loading popup with jQuery magic!
function loadPopup(objid){
	//loads popup only if it is disabled
	if(!getpopupStatus(objid)){
		//$("#backgroundPopup").css({
		//	"opacity": "0.3"
		//});
		$("#backgroundPopup_"+objid).fadeIn("fast");
		$("#popupContact_"+objid).fadeIn("fast");
	}
}

//disabling popup with jQuery magic!
function disablePopup(objid){
	//disables popup only if it is enabled
	$("div[id^='popupContact']").fadeOut("fast");
	$("div[id^='backgroundPopup']").fadeOut("fast");
	if(getpopupStatus(objid)){
		//$("#backgroundPopup_"+objid).fadeOut("fast");
		//$("#popupContact_"+objid).fadeOut("fast");

	}
}

//centering popup
function cursorposPopup(objid,e){
		
	$("#popupContact_"+objid).css({
		"position": "absolute",
		"top": e.pageY,
		"left": e.pageX
	});
	
	
}

function centerPopup(objid){ 
	var pop = $("#popupContact_"+objid);
	
	var toppop = ($(window).height() - pop.height())/2 + $(window).scrollTop() + "px";
	var leftpop = ($(window).width() - pop.width())/2 +$(window).scrollLeft() + "px";
	//alert(toppop + ' ' + leftpop);
	pop.css({
	    "position":"absolute",
	    "top": toppop,
	    "left": leftpop
    }); 
}

function dopopup(objid)
{
	//centering with css
	centerPopup(objid);
	//load popup
	loadPopup(objid);
	
	currpopid = objid;
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("tr[id^='button']").click(function(e){
		//get unique counter id and pass to other functions
		objid = this.id.replace("button_","");
	
		dopopup(objid);
		
	});
	
				
	//CLOSING POPUP
	//Click the x event!
	$("a[id^='popupContactClose']").click(function(){
		disablePopup(currpopid);
	});
	//Click out event!
	$("div[id^='backgroundPopup']").click(function(){
		disablePopup(currpopid);
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup(currpopid);
		}
	});

});
