// css-related 'constants' 

var WIDTH = 870;
var HEADERHEIGHT = 125;
var COLUMNOFFSET_Y = -10;
var CONTAINER_OFFSET_Y = 20;
var WRAPPER_BORDER_STYLE = "1px solid #dddddd";
var WRAPPER_BACKGROUND_COLOR = "#ffffff";
var BODY_BACKGROUND_COLOR = "#f0f0f0";

// global variables for tween effect

var startX, startY, startWidth, startHeight;
var endX, endY, endWidth, endHeight;
var tweenAt;
var idTween;
var isAnimating;
var dirIsUp;
var assetName;
var assetBgCol;
var isJavaApplet;
var javaClass;

// ============================================================

function makeFlash9String(f, col, $wmode)
{
	if (! $wmode) $wmode = "default";

	var s = "";
	
	s+='  	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
	s+='			id="flash_obj" width="100%" height="100%"'
	s+='			codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'
	s+='			<param name="movie" value="' + f + '" />'
	s+='			<param name="quality" value="high" />'
	s+='			<param name="wmode" value="' + $wmode + '" />'
	s+='			<param name="bgcolor" value="#' + col + 'ffffff" />'
	s+='			<param name="allowFullScreen" value="true" />'
	s+='			<param name="allowScriptAccess" value="sameDomain" />'
	s+='			<embed src="' + f + '" quality="high" bgcolor="#' + col + 'ffffff"'
	s+='				width="100%" height="100%" name="flashitem" align="middle"'
	s+='				play="true"'
	s+='				loop="false"'
	s+='				quality="high"'
	s+='				wmode="' + $wmode + '"'
	s+='				allowFullScreen="true"'
	s+='				allowScriptAccess="sameDomain"'
	s+='				type="application/x-shockwave-flash"'
	s+='				pluginspage="http://www.adobe.com/go/getflashplayer">'
	s+='			</embed>'
	s+='	</object>'

	return s;
}

function makeJavaAppletString($filename, $bgColor, $javaClass)
{
	// * Using <applet> tag not working for me, in any browser, in Win7 as of 8-2010

	var isIe = (BrowserDetect.browser == "Explorer")
	var s = "";

	if (! isIe)
	{
		if ($javaClass.indexOf(".class") == -1) $javaClass += ".class";
		if ($javaClass.indexOf("java:") != 0) $javaClass = "java:" + $javaClass;
		// .. results in, eg, "java:Tentacles.class"
		
		s += '<object classid="' + $javaClass + '" type="application/x-java-applet" archive="' + $filename + '" width="100%" height="100%">';
		s += '<param name="archive" value="' +  $filename + '" />';
		s += '<param name="bgcolor" value="' + $bgColor + '">'; // no effect it seems; always defaults to white bg while initializing
		s += '<param name="mayscript" value="true" />';
		s += '<param name="scriptable" value="true" />';
		s += '</object>';
	}
	else
	{
		if ($javaClass.indexOf(".class") == -1) $javaClass += ".class";
		// .. results in, eg, "Tentacles.class"
		
		s += '<object classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA" width="100%" height="100%">';
		s += '<param name="code" value="' + $javaClass + '">';
		s += '<param name="archive" value="' + $filename + '" />';
		s += '<param name="mayscript" value="true" />';
		s += '<param name="scriptable" value="true" />';
		s += '</object>';
	}

	return s;
}


var showNowFlag;

function showBox(pBaseFilename, pWidth, pHeight, pColor, pCaller, pIsJavaApplet, pJavaClass)
{
	isJavaApplet = pIsJavaApplet;
	assetBaseFilename = pBaseFilename;
	assetBgCol = pColor;
	javaClass = pJavaClass;

	cleanupAsset();

	document.getElementById("assetcontainer").style.backgroundColor = "#" + assetBgCol;
	document.getElementById("closebox").style.display = "none";
	document.getElementById("togglepage").style.display = "none";
	document.getElementById("dimmer").style.display = "block";
	
	dirIsUp = true;
	isAnimating = true;

	startWidth = pCaller.width - 2; // "-2" b/c of border;
	startHeight = pCaller.height - 2;
	startX = getHorizontalPos(pCaller) - (docWidth() - WIDTH)/2  +  1;
	startY = getVerticalPos(pCaller) + COLUMNOFFSET_Y;	
	endWidth = pWidth;
	endHeight = pHeight + CONTAINER_OFFSET_Y;
	endX = Math.floor( ((WIDTH) - pWidth )/2 );
	endY = getVerticalPos(pCaller) - 18;
	if (endY < HEADERHEIGHT) endY = HEADERHEIGHT; // clear flash piece in the header
	
	document.getElementById("dimmer").onclick = onDimmerClick;
	
	if (showNowFlag) 
	{
		startX = endX;
		startY = endY;
		startWidth = endWidth;
		startHeight = endHeight; // meh
	
		showNowFlag = false;
		tweenAt = 1;
		tweenLoop();
		return;
	}
	else
	{
		tweenAt = 0;
		tweenLoop();
		clearInterval(idTween);
		idTween = setInterval(tweenLoop,1000/30, true);
	}

}

function onDimmerClick()
{
	if (tweenAt == 1) closeBox();
}

function closeBox()
{
	// globals already set by showAsset at this point
	isAnimating = true;

	document.getElementById("closebox").style.display = "none";
	document.getElementById("togglepage").style.display = "none";
	document.getElementById("assetinside").innerHTML = "";

	tweenAt = 1;
	dirIsUp = false;
	
	tweenLoop();
	clearInterval(idTween);
	idTween = setInterval(tweenLoop,1000/30);
}

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.075;
		if (tweenAt>1) tweenAt = 1;
	}
	else 
	{
		if (tweenAt<=0) { finishTween(); return; }	
		tweenAt-= 0.075;
		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";
	
	document.getElementById("dimmer").style.opacity = (i * 0.66);
}

function finishTween()
{
	clearInterval(idTween);
	isAnimating = false;
	
	if (tweenAt >=1)
	{
		document.getElementById("closebox").style.display = "block";
		/// document.getElementById("togglepage").style.display = "block";

		var s;
		if (! isJavaApplet) 
			s = makeFlash9String("/blog/assets/" + assetBaseFilename + ".swf", assetBgCol);
		else
			s = makeJavaAppletString("/blog/assets/" + assetBaseFilename + ".jar", assetBgCol, javaClass);
			
		document.getElementById("assetinside").innerHTML = s;
		
		chromeHack();
	}
	else 
	{
		cleanupAsset();
		showPage();		
	}
}

function chromeHack()
{
	// chrome oftentimes won't actually render Flash after dynamic creation of flash object,
	// so force a window update (!) (11-2010)

	if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
	{
		setTimeout(window.scroll, 66, window.pageXOffset,window.pageYOffset+1);
		setTimeout(window.scroll, 133, window.pageXOffset,window.pageYOffset-1);
	}
}

function cleanupAsset()
{
	document.getElementById("assetcontainer").style.display = "none";
	document.getElementById("assetinside").innerHTML = "";
	document.getElementById("dimmer").style.display = "none";
}

function getVerticalPos(o)
{
	if ( o.offsetParent ) 
	{
		for( var posX = 0, posY = 0; o.offsetParent; o = o.offsetParent ) 
		{
			posX += o.offsetLeft;
			posY += o.offsetTop;
		}
		return posY;
	} 
	else 
	{
		return o.y;
	}
}

function getHorizontalPos(o)
{
	if ( o.offsetParent ) 
	{
		for( var posX = 0, posY = 0; o.offsetParent; o = o.offsetParent ) 
		{
			posX += o.offsetLeft;
			posY += o.offsetTop;
		}
		return posX;
	} 
	else 
	{
		return o.x;
	}
}

function togglePage() 
{
	if (document.getElementById("page").style.visibility != "hidden")
		hidePage()
	else
		showPage();
}

function showPage()
{
	document.getElementById("page").style.visibility = "visible";
	document.getElementById("content").style.visibility = "visible";
	document.getElementById("sidebar").style.visibility = "visible";
	document.getElementById("footer").style.visibility = "visible";
	document.body.style.backgroundColor = BODY_BACKGROUND_COLOR;
	document.getElementById("wrapper").style.border = WRAPPER_BORDER_STYLE;
	document.getElementById("wrapper").style.backgroundColor = WRAPPER_BACKGROUND_COLOR;
}

function hidePage()
{
	document.getElementById("page").style.visibility = "hidden";
	document.getElementById("content").style.visibility = "hidden";
	document.getElementById("sidebar").style.visibility = "hidden";
	document.getElementById("footer").style.visibility = "hidden";
	document.body.style.backgroundColor = BODY_BACKGROUND_COLOR;
	document.getElementById("wrapper").style.border = "1px solid " + BODY_BACKGROUND_COLOR;
	document.getElementById("wrapper").style.backgroundColor = BODY_BACKGROUND_COLOR;
}
 
function docWidth()
{
	if (document.body.clientWidth) return document.body.clientWidth; 
	if (window.innerWidth) return window.innerWidth; 
}

// ***

// accordion-like stuffs

function toggleItem($itemName)
{
	var item = document.getElementById($itemName);
	var val = item.style.display;
	if (!item.style.display || item.style.display == "block")
		item.style.setProperty("display", "none", "");
	else
		item.style.setProperty("display", "block", "");
}

function expandAll($baseName)
{
	for (var i = 0; i < 99; i++)
	{
		var item = document.getElementById($baseName + i);
		if (item)
		{
			item.style.setProperty("display", "block", "");
		}
	}
}

// * be sure to call collapseAll at the bottom of the page to make display:none the 'default'
function collapseAll($baseName)
{
	for (var i = 0; i < 99; i++)
	{
		var item = document.getElementById($baseName + i);
		if (item)
		{
			item.style.setProperty("display", "none", "");
		}
	}		
}

