/***************************/
//@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;

function openPopup(title, url, width, height, enableClose, refreshParent, params) {
    if (popupStatus == 0) {
        if ($.browser.msie && $.browser.version < 7) {
            //IE6 hack - dropdowns are always above popup
            $("select").css({ 'visibility': 'hidden' });
        }
        //load popup
        $("#popupTitle").text(title);
		
        $("#popupContainer").css({ 'width': width, 'height': height });
        $("#popupContent").load(url, params, loadPopup);

        if (enableClose) {
            registerCloseEvents(refreshParent);
        }
    }
}

//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        centerPopup();
        $("#popupBackground").css({
            "opacity": "0.7"
        });
        $("#popupBackground").fadeIn("slow");
        $("#popupContainer").fadeIn("slow");
        popupStatus = 1;
    }
    $('#popupContent input[type="submit"]').click(function() {
        disablePopup();
    });

}

//disabling popup with jQuery magic!
//param:
//  null or false: close popup
//  true: close popup and reload parent window
//  string: close popup and load parent url (string must contain a valid URL)
function disablePopup(param) {
    var reload = false;
    var reloadUrl = '';
    if (typeof (param) != 'undefined') {
        reload = true;
        if (typeof (param) == 'string') {
            reloadUrl = param;
        }
    }
    
	//disables popup only if it is enabled
    if (popupStatus == 1) {
        if (!reload) {
            $("#popupBackground").fadeOut("slow");
            $("#ctl00_radMenu").fadeIn("slow");
        }
	    $("#popupContainer").fadeOut("slow");
	    popupStatus = 0;

	    if (reload) {
	        if (reloadUrl != '') {
	            document.location.replace(reloadUrl);
	        }
	        else {
	            document.location.reload();
	        }
	    }
	}
	if ($.browser.msie && $.browser.version < 7) {
	    $("select").css({ 'visibility': 'visible' });
	}
}


//centering popup
function centerPopup()
{
	//request data for centering
    var windowWidth = 0;
    var windowHeight = 0;

    if ($.browser.msie) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else 
    {
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    }
    
	
	var scrollTop = $(window).scrollTop();
	var popupHeight = $("#popupContainer").height();
	var popupWidth = $("#popupContainer").width();

	var posTop;
	if (popupHeight > scrollTop) {
	    posTop = 250;
	} else {
	    posTop = (windowHeight / 2 - popupHeight / 2) + scrollTop;
	}

	var posLeft;
	
	posLeft = windowWidth / 2 - popupWidth / 2;
	

	//centering
	$("#popupContainer").css({
		"position": "absolute",
		"top": posTop,
		"left": posLeft
	});
	$(".popupContent").css({
	    "width": popupWidth - 26,
	    "height": popupHeight - 54 
	});
	//only need force for IE6

	$(window).bind('resize', function() {
	    $("#popupBackground").css({
	    "width": windowWidth,
	    "height": windowHeight
	    });
	});

	$("#popupBackground").css({
	    "width": windowWidth,
		"height": windowHeight
	});
	
}

function registerCloseEvents(refreshParent) {

    if (!refreshParent)
        refreshParent = undefined;
        
    //close on "x" icon click
    $("#popupContainerClose").click(function() {
        disablePopup(refreshParent);
    });
    //close on background click
    $("#popupBackground").click(function() {
        disablePopup(refreshParent);
    });

}


