﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/
var popupStatus = 0;
function loadPopup(){
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").css({
			"height": document.body.scrollHeight
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#frontPopup").fadeIn("slow");
		popupStatus = 1;
	}
}
function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#frontPopup").fadeOut("slow");
		popupStatus = 0;
	}
}
$(document).ready(function(){
	$("#frontPopup").centerInClient({ container: window, forceAbsolute: true });
	setTimeout("loadPopup()",1000);
	setTimeout("disablePopup()",8000);

	$("#frontPopupClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$("#frontPopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
$.fn.centerInClient = function(options) {
    var opt = { forceAbsolute: false,
		container: window,    // selector of element to center in
		completeHandler: null
	  };
    $.extend(opt, options);   
    return this.each(function(i) {
	var el = $(this);
	var jWin = $(opt.container);
	var isWin = opt.container == window;
	if (opt.forceAbsolute) {
		if (isWin)
			el.remove().appendTo("body");
		else
			el.remove().appendTo(jWin.get(0));
	}
	el.css("position", "absolute");
	var heightFudge = isWin ? 2.0 : 1.8;
	var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
	var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
	el.css("left", x + jWin.scrollLeft());
	el.css("top", y + jWin.scrollTop());
	if (opt.completeHandler)
		opt.completeHandler(this);
    });
}