// css-related 'constants' 

var WIDTH = 780;
var HEADERHEIGHT = 125;
var COLUMNOFFSET_X = 21; // (not sure why)
var COLUMNOFFSET_Y = -10;
var WRAPPER_BORDER_STYLE = "1px solid black";
var CONTAINER_OFFSET_Y = 16;

// global variables for tween effect (ugly implementation)

var startX, startY, startWidth, startHeight;
var endX, endY, endWidth, endHeight;
var tweenAt;
var idTween;
var isAnimating;
var dirIsUp;
var assetName;
var assetBgCol;
var isJavaApplet; // used for handling Java applet instead of Flash

var isMyNemesis;
if (BrowserDetect.browser=="Explorer" && BrowserDetect.version >=5) isMyNemesis = true;


// ============================================================

function makeFlash9String(f)
{
	var s = "";
	
	s+='  	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
	s+='			id="llibre" width="100%" height="100%"'
	s+='			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'
	s+='			<param name="movie" value="' + f + '" />'
	s+='			<param name="wmode" value="opaque" />'
	s+='			<embed src="' + f + '"'
	s+='				width="100%" height="100%" name="flashitem" align="middle"'
	s+='				play="true"'
	s+='				loop="false"'
	s+='				wmode="opaque"'
	s+='				type="application/x-shockwave-flash"'
	s+='				pluginspage="http://www.adobe.com/go/getflashplayer">'
	s+='			</embed>'
	s+='	</object>'

	return s;
}

function makeJavaAppletString(f)
{
	var s = "";

	s+= '	<!--[if !IE]> --> ';
	s+= '		<object classid="java:spiral.class"  ';
	s+= '				type="application/x-java-applet" ';
	s+= '				archive="' + f + '" ';
	s+= '				width="100%" height="100%" ';
	s+= '				standby="Loading Processing software..." > ';

	s+= '			<param name="archive" value="' + f + '" /> ';

	s+= '			<param name="mayscript" value="true" /> ';
	s+= '			<param name="scriptable" value="true" /> ';

	s+= '			<param name="image" value="loading.gif" /> ';
	s+= '			<param name="boxmessage" value="Loading Processing software..." /> ';
	s+= '			<param name="boxbgcolor" value="#' + col + '" /> ';

	s+= '			<param name="test_string" value="outer" /> ';
	s+= '	<!--<![endif]--> ';

	s+= '		<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"  ';
	s+= '				codebase="http://java.sun.com/update/1.4.2/jinstall-1_4_2_12-windows-i586.cab" ';
	s+= '				width="100%" height="100%" ';
	s+= '				standby="Loading Processing software..."  > ';

	s+= '			<param name="code" value="spiral" /> ';
	s+= '			<param name="archive" value="assets/spiral.jar" /> ';

	s+= '			<param name="mayscript" value="true" /> ';
	s+= '			<param name="scriptable" value="true" /> ';

	s+= '			<param name="image" value="loading.gif" /> ';
	s+= '			<param name="boxmessage" value="Loading Processing software..." /> ';
	s+= '			<param name="boxbgcolor" value="#' + col + '" /> ';

	s+= '			<param name="test_string" value="inner" /> ';

	s+= '		</object> ';
			
	s+= '	<!--[if !IE]> --> ';
	s+= '		</object> ';
	s+= '	<!--<![endif]--> ';

	return s;
}



function showBox(pFilename, pWidth, pHeight, pColor, pCaller, pIsJavaApplet)
{
	isJavaApplet = pIsJavaApplet;

	cleanupAsset();
	
	isAnimating = true;

	assetFilename = pFilename;
	assetBgCol = pColor;

	startWidth = 458 - 2; // "-2" b/c of border;
	if (isMyNemesis) startWidth -= 20; // ??
	
	startHeight = 193;
	startX = 115;
	startY = 250;	
	endWidth = pWidth;
	endHeight = pHeight + CONTAINER_OFFSET_Y;
	endX = Math.floor( ((WIDTH) - pWidth )/2 );
	endY = 5;
	if (endY < HEADERHEIGHT) endY = HEADERHEIGHT; // clear flash piece in the header
	
	document.getElementById("closebox").style.display = "block";

	tweenAt = 0;
	dirIsUp = true;
	idTween = setInterval(tweenLoop,1000/30, true);
}

function closeBox()
{
	// globals already set by showAsset at this point
	isAnimating = true;

	document.getElementById("closebox").style.display = "none";
	document.getElementById("assetinside").innerHTML = "";

	tweenAt = 1;
	dirIsUp = false;
	idTween = setInterval(tweenLoop,1000/45);
}

function tweenLoop()
{
	doTween(tweenAt);

	if (document.getElementById("assetcontainer").style.display != "block") {
		document.getElementById("assetcontainer").style.display = "block";	
	}

	if (dirIsUp) 
	{
		if (tweenAt>=1) { finishTween(); return; }	
		tweenAt+= 0.1;
		if (tweenAt>1) tweenAt = 1;
	}
	else 
	{
		if (tweenAt<=0) { finishTween(); return; }	
		tweenAt-= 0.1;
		if (tweenAt<0) tweenAt = 0;
	}
}

function doTween(i)
{
	var o = document.getElementById("assetcontainer");
	var p = document.getElementById("assetinside");

	o.style.left =		Math.floor( startX + ((endX - startX) * i) ) + "px";
	o.style.top = 		Math.floor( startY + ((endY - startY) * i) ) + "px";
	o.style.width =		Math.floor( startWidth + ((endWidth - startWidth) * i) ) + "px";
	o.style.height =	Math.floor( startHeight + ((endHeight - startHeight) * i) ) + "px";

	p.style.width =		Math.floor( startWidth + ((endWidth - startWidth) * i) ) + "px";
	p.style.height =	Math.floor( startHeight + ((endHeight - startHeight) * i) - CONTAINER_OFFSET_Y) + "px";
	
}

function finishTween()
{
	clearInterval(idTween);
	isAnimating = false;
	
	if (tweenAt >=1)
	{
		var s;
		if (! isJavaApplet) 
			s = makeFlash9String("../flash/" + assetFilename + ".swf", assetBgCol);
		else
			s = makeJavaAppletString("assets/" + assetFilename + ".jar", assetBgCol);
			
		document.getElementById("assetinside").innerHTML = s;
		document.getElementById("assetcontainer").style.backgroundColor = "#" + assetBgCol; 
	}
	else 
	{
		cleanupAsset();
	}
}

function cleanupAsset()
{
	var o = document.getElementById("assetcontainer");
	var p = document.getElementById("assetinside");
	o.style.display = "none";
	p.innerHTML = "";

}


 
function docWidth()
{
	if (document.body.clientWidth) return document.body.clientWidth; 
	if (window.innerWidth) return window.innerWidth; 
}