/***************************/
//@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 Pop(nams) {
	this.popupStatus = 0;
	var name = nams;

	//loading popup with jQuery magic!
	this.loadPopup = function(){
		//loads popup only if it is disabled
		if(this.popupStatus==0){
			$("#backgroundPopup_" + name).css({
				"opacity": "0.7"
			});
			$("#backgroundPopup_" + name).fadeIn("slow");
			$("#popupContact_" + name).fadeIn("slow");
			this.popupStatus = 1;
		}
	}
	
	//disabling popup with jQuery magic!
	this.disablePopup = function(){
		//disables popup only if it is enabled
		if(this.popupStatus==1){
			$("#backgroundPopup_" + name).fadeOut("slow");
			$("#popupContact_" + name).fadeOut("slow");
			this.popupStatus = 0;
		}
	}
	
	//centering popup
	this.centerPopup = function(){			
		//request data for centering
		var windowWidth = $(window).width();
		var windowHeight = $(window).scrollTop() + $(window).height();
		var popupHeight = $("#popupContact_" + name).height();
		var popupWidth = $("#popupContact_" + name).width();
		
//			//centering
//			$("#popupContact_" + name).css({
//				"position": "absolute",
//				"top": windowHeight/2-popupHeight/2,
//				"left": windowWidth/2-popupWidth/2
//			});
//			//only need force for IE6
//			
//			$("#backgroundPopup_" + name).css({
//				"height": windowHeight
//			});
		
		var opts = { forceAbsolute: true,
                container: window,    // selector of element to center in
                completeHandler: null
              };
		
		$("#popupContact_" + name).centerInClient(opts);
	}
}

function Popup(name) {

	//SETTING UP OUR POPUP
	//0 means disabled; 1 means enabled;
	
	var pop = new Pop(name);
	
	
	//CONTROLLING EVENTS IN jQuery
	this.init = function() {
		//centering with css
		//this.pop = new Pop(name);
		
		pop.centerPopup();
		//load popup
		pop.loadPopup();
	}
	
	
	//LOADING POPUP
	//Click the button event!
	$("#button_" + name).click(this.init);
	
				
	//CLOSING POPUP
	//Click the x event!
	$("#closeButton_" + name).click(function(){
		pop.disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup_" + name).click(function(){
		pop.disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && pop.popupStatus==1){
			pop.disablePopup();
		}
	});
}


$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: true,
                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.appendTo("body");
            else
                el.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);
    });
}