//
function ExpandingWindow(url, arguments, divId, maxHeight, selfId)
{
	this.url = url;
	this.arguments = arguments;
	this.divId = divId;
	this.maxHeight = maxHeight;
	this.isExpanding = 0;
	this.isOpen = 0;
	this.selfId = selfId;
	this.overlayOpacity = 10;
	this.containerElem = null;
	this.frameElem = null;
	this.backgroundElem = null;
	this.overlayElem = null;
	this.currHeight = 0;
	this.expandTimeStep = 3;
	this.expandHeightStep = 40;
	this.numSteps = this.maxHeight/this.expandHeightStep;
	this.overlayStep = this.overlayOpacity/this.numSteps;
	this.lastTimeout = null;
	this.contents = null;
	this.oHTTPRequest = null;

	this.OpenWindow = OpenWindow;
	this.PostHTTPRequest = PostHTTPRequest;
	this.ExpandDiv = ExpandDiv;
	this.FadeDiv = FadeDiv;
	this.CloseWindow = CloseWindow;
}

//
function OpenWindow()
{
	if(this.isExpanding == 1)
		return 0;
	
	this.isExpanding = 1;
	this.isOpen=1
	this.containerElem = document.getElementById(this.divId);
	this.containerElem.style.visibility = "visible";
	this.frameElem= document.getElementById(this.divId + "_frame");
	this.backgroundElem = document.getElementById(this.divId + "_background");
	this.overlayElem = document.getElementById(this.divId + "_overlay");
	this.ExpandDiv();
	//If this window's content has never been requested before,
	if(this.contents == null)
	{
		//then show the loading icon
		this.frameElem.innerHTML = "";
		//and send the HTTP request.
		this.PostHTTPRequest();
	}
	else	//Otherwise load whatever was returned last time.
		this.frameElem.innerHTML = this.contents;
	this.frameElem.style.position = "relative";
}

//
function PostHTTPRequest()
{
	oHTTPRequest = null;
	divId = this.divId;
	frameElem = this.frameElem;
	//instantiate the http request object
	if (window.XMLHttpRequest) 			//If the browser is Mozilla, Safari, or ...
	{
		oHTTPRequest = new XMLHttpRequest(); 
	}
	else if(window.ActiveXObject)		//Otherwise, if the browser is IE
	{
		oHTTPRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//Define the callback function.
	oHTTPRequest.onreadystatechange = function()
	{		
		if(oHTTPRequest.readyState == 4)
		{ 
			if(oHTTPRequest.status == 200)
			{
				//Get the list of manufacturers		
				frameElem.innerHTML = oHTTPRequest.responseText;
			}
			else
			{
				frameElem.innerHTML = "Error making HTTP request from EXPANDINGDIV.JS. Please contact webmaster@wesource.com.";
			}
		}	
	}
	//Make the request
	oHTTPRequest.open('GET', this.url + this.arguments , true); 
	oHTTPRequest.send(null);
	
	this.oHTTPRequest = oHTTPRequest;
}

//Convert a decimal number to a string representation of a decimal.
function d2h(d)
{
	return d.toString(16);
}

//Do a time phased increase of the divs height.
function ExpandDiv()
{
	//alert(containerDiv.childNodes[1].style.pixelHeight <= this.maxHeight);
	if(this.frameElem.offsetHeight <= this.maxHeight)
	{
		//Grow all three elements (frame, background, and overlay) simultaneously.
		this.currHeight += this.expandHeightStep;
		this.frameElem.style.height = this.currHeight;
		this.backgroundElem.style.height = this.currHeight;
		this.overlayElem.style.height = this.currHeight;
		this.lastTimeout = setTimeout(this.selfId + ".ExpandDiv()", this.expandTimeStep);
	}
	else
	{
		this.isExpanding = 0; 
		this.FadeDiv();	
	}
}

//
function FadeDiv()
{
	if(this.overlayOpacity > 0)
	{
		//Fade the div
		this.overlayOpacity -= this.overlayStep;
		this.overlayElem.style.opacity = (this.overlayOpacity)/10;
		this.overlayElem.style.filter = 'alpha(opacity=' + (this.overlayOpacity)*10 + ')';
		this.lastTimeout = setTimeout(this.selfId + ".FadeDiv()", this.expandTimeStep);
	}
}

//Return the window to the state before OpenWindow was called.
function CloseWindow()
{
	//If the content has been loaded, remember this window's content for the next time it is open.
	this.contents = this.frameElem.innerHTML;
	if(this.contents == "")
	{
		this.contents = null;
		this.oHTTPRequest.abort();
	}
	//Now reset the div to the state it had before being opened.
	this.frameElem.innerHTML = "";
	//Reset this window.
	this.frameElem.style.position = "absolute";
	this.frameElem.style.height = 0;
	this.backgroundElem.style.height = 0
	this.containerElem.style.visibility = "hidden";
	this.isOpen = 0;
	this.currHeight = 0;
	//Reset the overlay's opacity
	this.overlayOpacity = 10;
	this.overlayElem.style.opacity = (this.overlayOpacity)/10;
	this.overlayElem.style.filter = 'alpha(opacity=' + (this.overlayOpacity)*10 + ')';
	//If the window is still being expanded, stop it!
	if(this.lastTimeout != null)
	{
		clearTimeout(this.lastTimeout);
		this.isExpanding = 0;
	}
}

//Helper for opening and closing windows
oLastWindow = null;
function ToggleWindows(oCurrentWindow)
{
	if(oLastWindow != null)
	{
		oLastWindow.CloseWindow();
	}
		
	if(oLastWindow == oCurrentWindow)
	{
		oLastWindow = null;
	}
	else
	{					
		oCurrentWindow.OpenWindow();
		oLastWindow = oCurrentWindow;
	}
}