/*********************************************************************
********************************************************************* 
*
* JAVASCRIPT WINDOW FUNCTIONS
*
* copyright (c) 2002 - Aaron Howell -- Alpha Industries, LLC
*
********************************************************************* 
*********************************************************************/


/*********************************************************************
**WAITOPEN(location)
*
**Required Functions:  
*windowPosition()
*
** Arguments:
*none
*
** Returns:
*doesn't return;  spawns new wait window or closes wait window.
*
** Description:
*opens the wait window - closes the wait window.
*
*********************************************************************/
	function waitOpen() {
		var parameters, location;
		location = windowPosition(200, 200, .5, .5, true);
		parameters = "width=200,height=200,scrollbars=no,resizable=no,status=yes"+location;
		waitPop = window.open('wait.html', 'waitWindow', parameters);
		return true;
	}
	function waitClose() {
		waitPop.close();
		return true;
	}


/*********************************************************************
**POPWINDOW(url, name, windowWidth, windowHeight, xPerc, yPerc, center, tight, closeParent)
*
**Required Functions:  
*windowPosition()
*
** Arguments:
*url - the url for the image.
*name - the name of the new js window object.
*windowWidth - the width of the new window in pixels.
*windowHeight - the height of the new window in pixels.
*xPerc - the position of the left edge of the window as a percentage of the user's screen width.
*yPerc - the position of the top edge of the window as a percentage of the user's screen height.
*center - boolean: true positions the center of the window, false positions the upper left of the window.
*tight - boolean:  if true the window is not resizable and does not have scrollbars, if false the window does.
*closeParent - boolean: if true, will close the parent window.  if false, the window remains.
*
** Returns:
*doesn't return;  spawns new, positioned window.
*
** Description:
*opens the specified url in a new, positioned window.  New window becomes browser focus.
*
*********************************************************************/

function popWindow(url, name, windowWidth, windowHeight, xPerc, yPerc, center, tight, closeParent) {
	var location, parameters;
	if (tight === true) {
		tight = ",scrollbars=no,resizable=yes,status=yes";
	}
	else {
		tight = ",scrollbars=yes,resizable=yes,status=yes";
	}
	location = windowPosition(windowWidth, windowHeight, xPerc, yPerc, center);
	parameters = "width="+windowWidth+",height="+windowHeight+tight+location;
	newWindow = window.open(url, name, parameters);
	newWindow.focus();
	if (closeParent === true) {
		this.close();
	}
}



/*********************************************************************
**WINDOWPOSITION(windowWidth, windowHeight, xPerc, yPerc, center) 
*
**Required Functions:
*none.
*
**Arguments:
*windowHeight - the height of the new window in pixels.
*windowWidth - the width of the new window in pixels.
*xPerc - the position of the left edge of the window as a percentage of the user's screen width.
*yPerc - the position of the top edge of the window as a percentage of the user's screen height.
*center - boolean: true positions the center of the window, false positions the upper left of the window.
*
**Returns:
*js code for 'window.open' indicating pixel location for top left edge of new window.
*
**Description:
*provides the correct html code for the top left pixel location of new window based upon specifed location
*as percentage of user's screen size.  If window size is too large for the remaining screen area, then position
*is modified accordingly to fit user's screen.  Code is modified for IE vs Netscape window argument syntax.
*
*********************************************************************/

function windowPosition (windowWidth, windowHeight, xPerc, yPerc, center) {
	var termX, termY, position, xPos, yPos, xAdjust, yAdjust, screenWidth, screenHeight;
	xAdjust = 0;
	yAdjust = 0;
	if (navigator.appName.indexOf("Explorer") != -1) {
		termX = "left";
		termY = "top";
	}
	else {
		termX = "screenX";
		termY = "screenY";
	}
	if (screen) {
		screenWidth = Math.floor(screen.availWidth);
		screenHeight = Math.floor(screen.availHeight);
		if (center === true) {
			xAdjust = Math.floor(windowWidth/2);
			yAdjust = Math.floor(windowHeight/2);
		}
		xPos = Math.floor(((screen.availWidth)*xPerc) - xAdjust);
		yPos = Math.floor(((screen.availHeight)*yPerc) - yAdjust);
		if (Math.floor(windowWidth) > (screenWidth - xPos)) {
			xPos = screenWidth - windowWidth;
		}
		if (Math.floor(windowHeight) > (screenHeight - yPos)) {
			yPos = screenHeight - windowHeight;
		}
		position = "," + termX + "=" + xPos +"," + termY + "=" + yPos;
	}
	return position;
}



/*********************************************************************
**OPEN MEDIA(mediaUrl, titleUrl, mediaType, mediaWidth, mediaHeight, windowWidth, windowHeight, xPerc, yPerc, center, tight)
*
**Required Functions:
*windowPosition(), mediaEmbeder()
*
**Arguments:
*url - the url of the media to be placed.
*titleUrl - either 'none', or a url to a title image for the new window.
*mediaType - either 'image', or 'movie'.
*mediaWidth - the width of the media.
*mediaHeight - the height of the media.
*windowWidth - the width of the new window in pixels.
*windowHeight - the height of the new window in pixels.
*xPerc - the position of the left edge of the window as a percentage of the user's screen width.
*yPerc - the position of the top edge of the window as a percentage of the user's screen height.
*center - boolean: true positions the center of the window, false positions the upper left of the window.
*tight - boolean:  if true the window is not resizable and does not have scrollbars, if false the window does.
*focus - boolean: if true the new window becomes the browser focus, if false, the opening window regains focus.
*
**Returns:
*doesn't return;  spawns new, positioned window and writes html to insert specified media.
*
**Description:
*opens a new, positioned window with the specified media positioned using dhtml.  The new window will not have scrollbars
*and will not be resizable.  The opening window regains browser focus.
*
*********************************************************************/

function openMedia(mediaUrl, title, mediaType, mediaWidth, mediaHeight, windowWidth, windowHeight, xPerc, yPerc, center, tight, focus) {
	var pageContent, location, parameters, titleHeight, titleWidth, imageTop;
	mediaHeight += 15;
	imageTop = -1;
	pageContent = "<html><head>\n";
	pageContent += "<title>"+title+"</title>\n";
	pageContent += "<style type='text/css'>\n";
	pageContent += "<\!--\n";
	pageContent += "#mediaWrapper {\n";
	pageContent += "\tposition: absolute;\n";
	pageContent += "\tleft: 0;\n";
	pageContent += "\ttop: "+1+";\n";
	pageContent += "\twidth: "+windowWidth+";\n";
	pageContent += "\theight: "+windowHeight+";\n";
	pageContent += "}\n";
	pageContent += "#media {\n";
	pageContent += "\tposition: absolute;\n";
	pageContent += "\twidth: "+mediaWidth+";\n";
	pageContent += "\theight: "+mediaHeight+";\n";
	pageContent += "\tmargin: "+((windowHeight-mediaHeight)/2)+"px "+((windowWidth-mediaWidth)/2)+"px\n";
	pageContent += "}\n";
	pageContent += "-->\n";
	pageContent += "</style>\n";
	pageContent += "</head>\n";
	pageContent += "<body bgcolor='#000000' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>\n"
	pageContent += "<span id='mediaWrapper'>\n";
	pageContent += "<span id='media'>"+mediaEmbeder(mediaType, mediaUrl, mediaWidth, mediaHeight)+"</span>\n";
	pageContent += "</span>\n";
	pageContent += "</body></html>";
	location = windowPosition(windowWidth, windowHeight, xPerc, yPerc, center);
	parameters = "width="+windowWidth+",height="+windowHeight+",scrollbars=no,resizable=no"+location;
	imageWindow = window.open("", "image", parameters);
	imageWindow.document.write(pageContent);
	imageWindow.document.close();
	if (focus === true) {
		imageWindow.focus();
	}
	else if (focus === false) {
		this.focus();
	}
}



/*********************************************************************
**MEDIA EMBEDER(mediaType, mediaUrl, mediaWidth, mediaHeight)
*
**Required Functions:
*none.
*
**Arguments:
*mediaType - either 'image' or 'movie'.
*mediaUrl - url for the specified media.
*mediaWidth - width of media in pixels.
*mediaHeight - height of media in pixels.
*
* Returns:
*
*returns html to embed the specified media.
*
* Description:
*
*provides formatted html to embed the specified media at the specified size.
*
*********************************************************************/

function mediaEmbeder(mediaType, mediaUrl, mediaWidth, mediaHeight) {
	var result;
	if (mediaType == "image") {
		result = "<img name='image' src='"+mediaUrl+"' width='"+mediaWidth+"' height='"+mediaHeight+"'>\n";
	}
	else if (mediaType == "movie") {
		result = "<embed name='movie' src='"+mediaUrl+"' width='"+mediaWidth+"' height='"+mediaHeight+"'></embed>\n";
	}
	return result;
}
