var closeButton = 'http://www.delta-biz.com/milestone/images/close.gif';
var loadingImage= 'http://www.delta-biz.com/milestone/images/loading.gif';

function getPageScroll(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	return new Array('',yScroll);
}

function getPageSize(){
	var xScroll, yScroll;
	var pageHeight,pageWidth;
	
	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;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
}

function getKey(e){
	var keycode = (e == null) ? event.keyCode : e.which;
	if (keycode==0) keycode=e.keyCode;
	var key = String.fromCharCode(keycode).toLowerCase();

	if(key == 'x' || keycode == 27){ a_cancel_edit(); }
}
function listenKey () {	document.onkeypress = getKey; }


function showLightbox()
{
	// prep objects
	var objOverlay = document.getElementById('overlay');

	var arrayPageSize = getPageSize();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}
function hideLightbox()
{
	// get objects
	var objOverlay = document.getElementById('overlay');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
}

function showObjectbox(obj)
{
	showLightbox(obj);

	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');

	var hiddenstuff = $('hiddenstuff');
	while (objLightbox.hasChildNodes()) {
		var child = objLightbox.firstChild;
		objLightbox.removeChild( child );
		if (null != hiddenstuff) hiddenstuff.appendChild(child);
	}

	if (obj.parentNode != null) {
		obj.parentNode.removeChild(obj);
	}

	objLightbox.appendChild(obj);

	//must set as block before accessing the offsetHeight/Width
	objLightbox.style.display = 'block';

	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();

	// center lightbox and make sure that the top and left values are not negative
	// and the image placed outside the viewport
	var lightboxTop  = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - obj.offsetHeight) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - obj.offsetWidth) / 2);

	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


	// After image is loaded, update the overlay height as the new image might have
	// increased the overall page height.
	arrayPageSize = getPageSize();
	objOverlay.style.height = (arrayPageSize[1] + 'px');

	// Check for 'x' keypress
	listenKey();
}
function hideObjectbox()
{
	var objLightbox = document.getElementById('lightbox');
	objLightbox.style.display = 'none';

	// disable keypress listener
	document.onkeypress = '';

	hideLightbox();
}


var loadingboxon = false;
function showLoadingbox(showTheLight)
{
	loadingboxon = true;
	
	if (showTheLight != false)
		showLightbox();

	var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');

	var arrayPageScroll = getPageScroll();
	var arrayPageSize = getPageSize();

	// center loadingImage if it exists
	if (objLoadingImage) {
		alert("here");
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';

		if (objLightbox) {
			objLightbox.style.zIndex = '5000';
		}
	}
}
function hideLoadingbox(hideTheLight)
{
	var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');

	if (objLoadingImage) {
		objLoadingImage.style.display = 'none';

		if (objLightbox) {
			objLightbox.style.zIndex = '5001';
		}
	}

	if (hideTheLight != false)
		hideLightbox();

	loadingboxon = false;
}



function initLightbox()
{
	if (!document.getElementsByTagName){ return; }

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	// </div>
	// <img id="loadingImage" />
	// <div id="lightbox">
	//		<a href="#" onclick="a_cancel_edit(); return false;" title="...">
	//			<img id="closeButton" />
	//		</a>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '4001';
 	objOverlay.style.width = '100%';

	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '6000';
		objLoadingImage.style.display='none';

		objBody.insertBefore(objLoadingImage, objOverlay.nextSibling);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = loadingImage;

	{
		objOverlay.onclick = function () { if(!loadingboxon)a_cancel_edit(); return false;}

		// create lightbox div, same note about styles as above
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.style.display = 'none';
		objLightbox.style.position = 'absolute';
		objLightbox.style.zIndex = '5000';	
		objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
		// create link
		var objLink = document.createElement("a");
		objLink.setAttribute('href','javascript:a_cancel_edit();');
		objLink.setAttribute('title','Close (X for hotkey)');
		objLightbox.appendChild(objLink);
	
		// preload and create close button image
		var imgPreloadCloseButton = new Image();
	
		// if close button image found, 
		imgPreloadCloseButton.onload=function(){
			var objCloseButton = document.createElement("img");
			objCloseButton.src = closeButton;
			objCloseButton.setAttribute('id','closeButton');
			objCloseButton.style.position = 'absolute';
			objCloseButton.style.zIndex = '5001';
	
			objLink.appendChild(objCloseButton);
	
			return false;
		}
	
		imgPreloadCloseButton.src = closeButton;
	}
}
