$(function() {
	$(window).resize(function() {
		updateOverlayDimensions();
	});
	
	$('#overlay').bind("click", hideOverlay);
	$("#overlayCloseLink").bind("click", hideOverlay);
});

function getViewHeight() {
	if ($.browser.msie) {
		return document.body.offsetHeight;
	} else {
		return window.innerHeight;
	}
}

function getViewWidth() {
	if ($.browser.msie) {
		return document.body.offsetWidth;
	} else {
		return window.innerWidth;
	}
}

function loadOverlay(content) {
	updateOverlayDimensions();
	
	$('#overlay').show();
	$('#overlay-loader').show();
	$('#overlay-box').hide();
	$('#overlay-container').hide();
	
	window.scrollTo(0, 100);
	
	loadOverlayContent(content);
}

function loadOverlayContent(content) {
	$('#overlay-container').load("template/overlay/"+content+".tmp.php", null, handleOverlayLoad);
}

function handleOverlayLoad(responseText, textStatus, XMLHTTP) {
	$('#overlay-loader').hide();
	$('#overlay-box').show();
	$('#overlay-container').show();
	$("#overlayCloseLink").show();
	
	if ($.browser.version == "6.0" && $.browser.msie) {
		$("select").hide();
	}
}

function hideOverlay() {
	$('#overlay, #overlay-box, #overlay-loader, #overlay-container, #overlayCloseLink').hide();	
	showScrollbars();
	
	if ($.browser.version == "6.0" && $.browser.msie) {
		$("select").show();
	}
}

function hideScrollbars() {
	document.body.style.overflow = "hidden";
}

function showScrollbars() {
	document.body.style.overflow = "";
}

function updateOverlayDimensions() {
	var ps = getPageSize();
	
	$('#overlay').css({
		width: ps[0] + "px",
		height: ps[1] + "px"
	})
}

/** 
 * From Lightbox: http://www.huddletogether.com/projects/lightbox2/
 * See their page for License
 */
function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}