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

(function ($) {
//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").fadeIn("slow");
		$("#youtubepopup").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow", function () {$(this).remove();});
		$("#youtubepopup").fadeOut("slow", function () {$(this).remove();});
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	if (null == document.getElementById('youtubepopup')) {
		$(document.body).append('<div id="youtubepopup" style="z-index: 6; width: 746px; height: 413px;"><object width="746" height="413"><param name="movie" value="http://www.youtube.com/cp/vjVQa1PpcFMZWuRHzEZJiSPaksGipgz7Ymvvy3UJxiw="></param><embed src="http://www.youtube.com/cp/vjVQa1PpcFMZWuRHzEZJiSPaksGipgz7Ymvvy3UJxiw=" type="application/x-shockwave-flash" width="746" height="413"></embed></object></div>');
	}
	if (null == document.getElementById('backgroundPopup')) {
		$(document.body).append('<div id="backgroundPopup" style="position: fixed; top: 0; left: 0; z-index: 5; width: 100%; height: 100%; background-color: black"></div>');
	}
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#youtubepopup").height();
	var popupWidth = $("#youtubepopup").width();
	//centering
	$("#youtubepopup").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

	$("#backgroundPopup").click(function(ev){
		disablePopup();
		ev.preventDefault();
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("a").filter(function () {return -1 < this.href.indexOf('youtube');})
	.click(function(ev){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		ev.preventDefault();
	});
				
	//CLOSING POPUP
	//Click the x event!
	//$("#youtubepopup a.close").click(function(ev){
	//	disablePopup();
	//	ev.preventDefault();
	//});
	//Click out event!
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.which==27 && popupStatus==1){
			disablePopup();
		}
	});

});
})(jQuery);

