/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */
var GB_DONE = false;
var GB_HEIGHT = 0;
var GB_WIDTH = 0;
var GB_ANIMATION = false;
var GB_SPECIAL = null;

function GB_show(url, special, contenthtml, postdata){
	orange_showhide_selects(false);
	orange_loading_show();
    if (!GB_DONE) {
		GB_SPECIAL = special;
		$(document.body).append("<div id='GB_overlay'></div><div id='GB_window'></div>");
        $("#GB_overlay").click(GB_hide);
        $(window).resize(GB_position);
        GB_DONE = true;
    }
    
    if (!contenthtml) {
		$("#GB_window").load(url, postdata, function(responseText, textStatus, XMLHttpRequest){
			GB_HEIGHT = $("#GB_window").height();
			GB_WIDTH = $("#GB_window").width();
			GB_position();
			if (GB_ANIMATION) 
				$("#GB_window").slideDown("fast");
			else 
				$("#GB_window").show();
			
			orange_loading_hide();
			
			orange_make_helplinks();
			
			if (GB_SPECIAL == "LOGIN_LEVEL1") {
				$("#GB_window form").unbind("submit");
				$("#GB_window form").submit(function(){
					orange_loading_show();
					$.post("/Content.Node/ajax/messagebox.php?ajaxlogin=1", $(this).serializeArray(), function(data){
						orange_loading_hide();
						if (data && data != "<!--DIRECT_LEVEL2-->") {
							orange_messagebox(data);
						}
						else 
							if (data) {
								location.href = SUCCESS_URL;
							}
							else {
								if (auth_level == 1) 
									location.href = SUCCESS_URL;
								else 
									orange_specialbox("LOGIN_LEVEL2");
							}
					});
					return false;
				});
			}
			else {
				$("#GB_window form").each(function(){
					$(this).attr("submitto", "#GB_window");
					//$(this).attr("action", url);
				});
			}
		});
	}
	else
	{
		orange_loading_hide();
		$("#GB_window").html(url);
		GB_HEIGHT = $("#GB_window").height();
		GB_WIDTH = $("#GB_window").width();
		GB_position();
		if (GB_ANIMATION) 
			$("#GB_window").slideDown("fast");
		else 
			$("#GB_window").show();
		
		$("#GB_window form").each(function(){
			$(this).attr("submitto", "#GB_window");
			$(this).attr("action", url);
		});
	}
    //$("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");
    
    $("#GB_overlay").show();
}

function GB_hide(success){
	orange_showhide_selects(true);
	if (success == true && SUCCESS_URL) {
		location.href = SUCCESS_URL;
		return;
	}
	$("#GB_window,#GB_overlay").hide();
	switch(GB_SPECIAL)
	{
		case "LOGIN_LEVEL2":
			if(AUTH_LEVEL < 2)
				location.reload();
			break;
		default:
			break;
	}
}

function GB_position(){
    var de = document.documentElement;
    var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    var h = self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    $("#GB_window").css({
        left: ((w - GB_WIDTH) / 2) + "px",
        top: ((h - GB_HEIGHT) / 2) + "px"
    });
    GB_resizeOverlay();
    window.scrollTo(0,0);
}

function getWindowHeight(){
    if ($.browser.msie && $.browser.version < 7) {
        var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
        var offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
        
        if (scrollHeight < offsetHeight) {
            return $(window).height() + 'px';
        }
        else {
            return scrollHeight + 'px';
        }
    }
    else {
        return $(document).height() + 'px';
    }
}

function getWindowWidth(){
    if ($.browser.msie && $.browser.version < 7) {
        var scrollWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
        var offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);
        
        if (scrollWidth < offsetWidth) {
            return $(window).width() + 'px';
        }
        else {
            return scrollWidth + 'px';
        }
    }
    else {
        return $(document).width() + 'px';
    }
}

function GB_resizeOverlay(){
    $("#GB_overlay").css({
        width: 0,
        height: 0
    }).css({
        width: getWindowWidth(),
        height: getWindowHeight()
    })
}
