// the name of the curtain element. 
//
var kFadeSteps 				= 15;
var kCurtainOpacity			= 60;
var gCurtainCurrentOpacity 	= 0; 
var gCurtainId 				= "modalLayer";
var mCurtain;

// globals and consts for keeping the modal 
// window in the correct location
//
var gMMBTimeout;
var gMBIsVisible	        = false;
var gOldTopVal		        = kModalBoxTop;
var kModalBoxTop	        = 30;
var gCurtainTimeout;


// -------------------------------------------------------------------
// AnimateExpand()
//   Animation effect for expanding the height of an element. 
// Arguments:
//	 id		- the id of the element
//	 start	- the starting height
//	 max	- the max height for it to reach
//	 speed	- amount the div is increased on each cycle 
//			  (use a negative number to contract)
// -------------------------------------------------------------------
//
function AnimateExpand(id, start, max, speed, callback) {
	var t;
	var e = new getObj(id);
	e.style.overflow = "hidden";	

	//_DEBUG("AnimateExpand(): " + e.id + " is currently " + e.obj.clientHeight);
	start += speed;

	// expand
	//
	if (speed > 0) {
		if(start >= max) {
			if (t!=null) {clearTimeout(t);}
			e.style.height = max + "px";
			if (arguments[4] != null && arguments[4] != "") {
				eval(callback);
			}
			
			return;
		}
	// contract
	//
	} else {
		if(start <= max) {
			if (t!=null) {clearTimeout(t);}
			e.style.height = max + "px";
			if (arguments[4] != null && arguments[4] != "") {
				eval(callback);
			}
			return;
		}
	}

	e.style.height= start + "px";
	//_DEBUG("AnimateExpand(): Calling: " + "AnimateExpand(\""+ id + "\", " + start + ", " + max + ", " + speed + ", '" + callback + "')");
	t = setTimeout("AnimateExpand(\""+ id + "\", " + start + ", " + max + ", " + speed + ", '" + callback + "')", 30);
}


// -------------------------------------------------------------------
// KeepModalBoxPosition()
//   Animation effect for expanding the height of an element. 
// Arguments:
//	 id - the id of the element to keep positioned
// -------------------------------------------------------------------
//
function KeepModalBoxPosition(id) {
	var pos;
	var e = new getObj(id);
	
	// _DEBUG("KeepModalBoxPosition(): Grabbing element " + e.id + ".");
	
	if (!gMBIsVisible) {
		_DEBUG("KeepModalBoxPosition(): modal box is hidden.");
		gMMBTimeout = null;
	} else {
		if (window.innerHeight) {
			// mozilla, safari
			//
			pos = window.pageYOffset;
		} else if (document.documentElement && !document.documentElement.scrollTop) {
			// IE6 +4.01 but no scrolling going on
			//
			pos = 0;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			// IE6 +4.01 and user has scrolled
			//
			pos = document.documentElement.scrollTop;
		} else {
			// IE 5
			//
			pos = document.body.scrollTop
		}
		if (pos < kModalBoxTop) {pos = kModalBoxTop;}
		else {pos += 30;}
		
		if (pos == gOldTopVal) {e.style.top = pos + "px";}

		gOldTopVal  = pos;
		gMMBTimeout = setTimeout('KeepModalBoxPosition("' + id + '")',350);
	}
}

// -------------------------------------------------------------------
// DropCurtain()
//   Method that drops the curtain for modal UI effects 
// -------------------------------------------------------------------
//
function DropCurtain() {
	var curtain;
	
	// look for the curtain div.
	//
	if (!document.getElementById(gCurtainId)) {
		_DEBUG("DropCurtain(): Creating curtain...")
		
		// first, let's see if things have changed in the page. 
		//
		PrestoCommon();
		curtain = document.createElement("div");
		curtain.setAttribute("id", gCurtainId);
		curtain.style.height 	= gScreenY + "px";
		curtain.style.width  	= gScreenX + "px";
		curtain.style.top		= 0;
		curtain.style.left		= 0;
		curtain.style.position	= "absolute";
		SetOpacityOfElement(curtain, 1);

		// now attach it to the body
		//
		document.getElementsByTagName("body")[0].appendChild(curtain);
		
	} else {
		curtain = document.getElementById(gCurtainId);
		curtain.style.height 	= gScreenY + "px";
		curtain.style.width  	= gScreenX + "px";
		curtain.style.top		= 0;
		curtain.style.left		= 0;
		curtain.style.position	= "absolute";	
		SetOpacityOfElement(curtain, 1);	
	}
	
	mCurtain = new getObj(gCurtainId);
	
	FadeCurtain();
}

// -------------------------------------------------------------------
// FadeCurtain()
//   Method that handles the fade effect of the curtain. 
// Arguments 
//   [bool] - by passing in a bool, we will attempt to fade the 
//   curtain out, vs. the normal fading it in. We're referencing 
//   the kCurtainOpacity const for how dark the curtain should get
// -------------------------------------------------------------------
//
function FadeCurtain() {
	//_DEBUG("FadeCurtain(): Current opacity is: " + gCurtainCurrentOpacity);
	
	// see if we want to fade in or out
	//
	if (arguments.length > 0) {
		if (gCurtainCurrentOpacity > 0) {
			gCurtainCurrentOpacity = gCurtainCurrentOpacity - kFadeSteps
			mCurtain.SetOpacity(gCurtainCurrentOpacity);
			
			setTimeout("FadeCurtain('down')", 100);
			return;
		}
		return;
	}

	// otherwise fade in
	//
	if (gCurtainCurrentOpacity < kCurtainOpacity) {
		gCurtainCurrentOpacity = gCurtainCurrentOpacity + kFadeSteps
		mCurtain.SetOpacity(gCurtainCurrentOpacity);
		setTimeout("FadeCurtain()", 100);
		return;
	}
	return;
}

// -------------------------------------------------------------------
// RemoveCurtain()
//   Method that rolls up the curtain 
// Arguments 
//   callback - a string representation of a method to callback 
//   once the curtain is fully up. 
// -------------------------------------------------------------------
//
function RemoveCurtain(callback) {
	// fade if we need to
	//
	if (gCurtainCurrentOpacity >= kCurtainOpacity) {FadeCurtain(true);}
	
	// wait for the fade
	//
	if (gCurtainCurrentOpacity > 0) {
		gCurtainTimeout = setTimeout("RemoveCurtain('" + callback + "')", 300);
		return;
	} else {
		gCurtainTimeout = null;
	}
	mCurtain.style.width	= 1 + "px";
	mCurtain.style.height	= 1 + "px";
	mCurtain.style.top		= -50;
	mCurtain.style.left		= -999;
	eval(callback);
}

_DEBUG("Completed loading UiUtils.js...");
