//
// This script is based on the dimmingdiv.js script by Massimo Beatini.
// Not much of the original is left, but for legal reasons I am including
// his copyright notice.
//
// You can use and hack this script to your hearts content. I don't mind. 
// Just mention Massimo as the author of the original script
//
// Martin Fairfax
// May 2007

//************************************************************************************
// Copyright (C) 2006, Massimo Beatini
//
// This software is provided "as-is", without any express or implied warranty. In 
// no event will the authors be held liable for any damages arising from the use 
// of this software.
//
// Permission is granted to anyone to use this software for any purpose, including 
// commercial applications, and to alter it and redistribute it freely, subject to 
// the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim 
//    that you wrote the original software. If you use this software in a product, 
//    an acknowledgment in the product documentation would be appreciated but is 
//    not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be 
//    misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
//************************************************************************************

//
// dynamically add a div to 
// dim all the page
//
function buildDimmerDiv()
{
	var objBody = document.getElementsByTagName("body").item(0);
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objDimmer = document.createElement("div");
	objDimmer.setAttribute('id','dimmer');
	objDimmer.style.display = 'none';
	objDimmer.style.position = 'absolute';
	objDimmer.style.top = '0';
	objDimmer.style.left = '0';
	objDimmer.style.zIndex = '90';
 	objDimmer.style.width = '100%';
	objBody.appendChild(objDimmer);
}


//
//
//
function displayFloatingDiv(divId, title, width, height, left, top) 
{
	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();
	
	var objDimmer = document.getElementById('dimmer');
	var objDiv = document.getElementById(divId);
	
	// set height of Dimmer to take up whole page and show
	objDimmer.style.height = (arrayPageSize[1] + 'px');
	objDimmer.style.display = 'block';

	if (width > 0) {
		objDiv.style.width = width + 'px';
	}
	if (height > 0) {
		objDiv.style.height = height + 'px';
	}
	objDiv.style.left = left + 'px';
	objDiv.style.top = (arrayPageScroll + top) + 'px';	
		
	objDiv.style.display = 'block';
	
	// After div is loaded, update the Dimmer height as the new image might have increased the overall page height.
	arrayPageSize = getPageSize();
	objDimmer.style.height = (arrayPageSize[1] + 'px');
		
	hideSelectBoxes();
	hideFlash();
}


//
//
//
function hideFloatingDiv(divId) 
{
	var objDimmer = document.getElementById('dimmer');
	var objDiv = document.getElementById(divId);
	
	objDiv.style.display = 'none';
	objDimmer.style.display = 'none';
		
	showSelectBoxes();
	showFlash();
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){
	if (self.pageYOffset) { return self.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop) { return document.documentElement.scrollTop; }
	else if (document.body) { return document.body.scrollTop; }

	return 0;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  } else {
    elm['on' + evType] = fn;
  }
}

//
//
//
function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

//
//
//
function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

//
//
//
function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

//
//
//
function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

//
//
//
function initDimmingDiv()
{
	// add the div used to dim the page
	buildDimmerDiv();

}

// When the page loads, set up the rollovers
addEvent (window, 'load', initDimmingDiv, false);

