/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;



var popupStatus = 0;
var currentPopUpID='';

//var popupName = document.getElementById(divID);
//loading popup with jQuery magic!
function loadPopup(id){
	//loads popup only if it is disabled
	centerPopup(id);
	if(popupStatus==0){
		$("#backgroundPopup").fadeIn();
		
		$("#"+id).fadeIn();
		popupStatus = 1;
	}
	currentPopUpID = id;
	
	if(id='login-window' && $("section.slide1").is(':visible') ){
		movableObject();
	}
		
}

//disabling popup with jQuery magic!
function disablePopup(id){
	//disables popup only if it is enabled
	if(popupStatus==1){
		movableClose();
		$("#backgroundPopup").fadeOut();
		
		$("#"+id).fadeOut();
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(id){
/*
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#"+id).height();
	var popupWidth = $("#"+id).width();
	//centering
	$("#"+id).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
*/
	//$('#'+id).centerInClient();
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	
	$(".popupClose").live('click',function(){
		disablePopup(currentPopUpID);
		return false;
	});
	$(".closeButton").live('click',function(){
		disablePopup(currentPopUpID);
		return false;
	});
	
	$(".closeBtn").live('click',function(){	
		disablePopup(currentPopUpID);
		return false;
	});

	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup(currentPopUpID);
			return false;
		}
	});

});


$.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;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        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 specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}
