//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popupActiveId = null;

//loading popup with jQuery magic!
function loadPopup(popupID){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#" + popupID).fadeIn("fast");
		popupStatus = 1;
		popupActiveId = popupID;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#shadow1").hide();
		$("#shadow2").hide();
		$("#" + popupActiveId).fadeOut("fast");
		popupStatus = 0;
		popupActiveId = null;
	}
}


//centering popup
function centerPopup(popupID){
	//request data for centering
	var scroll_top = document.documentElement.scrollTop || document.body.scrollTop;
    var scroll_height = document.documentElement.scrollHeight;
    var client_height = window.innerHeight || document.documentElement.clientHeight;
	
	var windowWidth = document.documentElement.clientWidth;
	
	var popupHeight = $("#" + popupID).height();
	var popupWidth = $("#" + popupID).width();
	//centering
	$("#" + popupID).css({
		"position": "absolute",
		"top": scroll_top + client_height/2-popupHeight/2,				
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	$("#backgroundPopup").css({
		"height": scroll_height
	});

	$("#shadow1").css({
		"right": windowWidth/2-popupWidth/2 - 3,
		"top": scroll_top + client_height/2-popupHeight/2,
		"height": popupHeight + 3,
		"width":  3,
		"opacity": "0.2"
	}).fadeIn('fast');

	$("#shadow2").css({
		"left": windowWidth/2-popupWidth/2,
		"top": scroll_top + client_height/2 + popupHeight/2,
		"height": 3,
		"width": popupWidth,
		"opacity": "0.2"
	}).fadeIn('fast');
	
	
}


$(document).ready(function(){
	
	$(".popup1").click(function(){		
		centerPopup("popup1");
		loadPopup("popup1");	
		return false;
	});

	$(".popup2").click(function(){		
		centerPopup("popup2");
		loadPopup("popup2");	
		return false;
	});

	$(".popupHPlayer").click(function(){		
		centerPopup("popupHPlayer");
		loadPopup("popupHPlayer");	

		flowplayer("player", "http://assets.adigami.com/swf/flowplayer-3.1.2.swf");
		return false;
	});
	
	
				
	//CLOSING POPUP
	//Click the x event!
	$(".popupClose").click(function(){
		disablePopup();		
		return false;
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	

});