

/*
	Common Functions
*/



/*
	Browser identification.
*/
var browser_windows = (navigator.userAgent.indexOf('Windows') >= 0);
var browser_mac = (navigator.userAgent.indexOf('Macintosh') >= 0);
var browser_ie = (navigator.userAgent.indexOf('MSIE') >= 0);
var browser_ie6 = (navigator.userAgent.indexOf('MSIE 6.0') >= 0);
var browser_firefox = (navigator.userAgent.indexOf('Firefox') >= 0);
var browser_opera = (navigator.userAgent.indexOf('Opera') >= 0);
var browser_safari = (navigator.userAgent.indexOf('Safari') >= 0);





/*
	Get page parameters from query string.
*/
var rayPageParameters = null;
function getPageParameters()
{	
	rayOutParameters = new Array();
	strQueryString = location.search.replace(/\?/,'&');
	rayQueryString = strQueryString.split('&');
	for (i=1; i < rayQueryString.length; i++)
	{	strParameter = rayQueryString[i];
		rayParameter = strParameter.split('=');
		rayOutParameters[rayParameter[0]] = rayParameter[1];
	}
	return rayOutParameters;
}
function getPageParameter(strInParam)
{	
	if (!rayPageParameters) rayPageParameters = getPageParameters();
	return rayPageParameters[strInParam];
}





/*
	Object reference functions.
*/
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function bt_findObj(inObjName)
{
	return document.getElementById ?
		document.getElementById(inObjName): document.all? document.all[inObjName]: null;
}

/*
	Screen size and object position functions.
*/
function getWindowSize()
{	intWidth = 0;  intHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{	/*	Non-IE */
		intWidth = window.innerWidth;
		intHeight = window.innerHeight;
	}
	else
	if( document.documentElement &&
	  ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{	/*	IE 6+ in 'standards compliant mode' */
		intWidth = document.documentElement.clientWidth;
		intHeight = document.documentElement.clientHeight;
	}
	else
	if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{	/*	IE 4 compatible */
		intWidth = document.body.clientWidth;
		intHeight = document.body.clientHeight;
	}
	return new Array(intWidth,intHeight);
}

function getScreenSize()
{
	var intScreenW = 640, intScreenH = 480;
	if (parseInt(navigator.appVersion)>3) {
		intScreenW = screen.width;
		intScreenH = screen.height;
	}
	else if (navigator.appName == "Netscape" 
		&& parseInt(navigator.appVersion)==3
		&& navigator.javaEnabled() ) 
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		intScreenW = jScreenSize.width;
		intScreenH = jScreenSize.height;
	}
	
	return new Array(intScreenW,intScreenH);
}

function doGetObjPos(objInput)
{
	intOutLeft = 0;
	intOutTop = 0;
	intOutRight = objInput.offsetRight ? objInput.offsetRight : null;
	intOutBottom = objInput.offsetBottom ? objInput.offsetBottom : null;
	intOutWidth = objInput.offsetWidth ? objInput.offsetWidth : null;
	intOutHeight = objInput.offsetHeight ? objInput.offsetHeight : null;
	
	if (objInput.offsetParent)
	{	while (objInput.offsetParent)
		{	intOutLeft += objInput.offsetLeft;
			intOutTop += objInput.offsetTop;
			objInput = objInput.offsetParent;
		}
	}
	else
	if (objInput.x || objInput.y)
	{	intOutLeft += objInput.x;
		intOutTop += objInput.y;
		intOutWidth = objInput.width ? objInput.width : null;
		intOutHeight = objInput.height ? objInput.height : null;
	}
	
	
	if ((intOutRight == null) && intOutWidth) intOutRight = intOutLeft+intOutWidth;
	if ((intOutBottom == null) && intOutHeight) intOutBottom = intOutTop+intOutHeight;
	
	rayOutput = new Array();
	rayOutput['left'] = intOutLeft;
	rayOutput['right'] = intOutRight;
	rayOutput['top'] = intOutTop;
	rayOutput['bottom'] = intOutBottom;
	rayOutput['width'] = intOutWidth;
	rayOutput['height'] = intOutHeight;
	return rayOutput;
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {	while (obj.offsetParent)
        {	curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else
    if (obj.x){ curleft += obj.x; }
    
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
	{	while (obj.offsetParent)
        {	curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else
    if (obj.y){ curtop += obj.y; }
    
    return curtop;
}



/*
	String and number manipulation functions.
*/
function url_encode(sStr)
{
	return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function bt_isNumeric(strInput)
{	strValues = "0123456789";
    cL = strInput.length;
    for (c=0; c< cL; c++)
    {	strChar = strInput.substr(c,1);
        if (strValues.indexOf(strChar) < 0)
        {	return false; }
    }
    return true;
}

function isEven(intInput)
{	return (intInput%2 == 0); }

function padNumber(intNumber, intDigits)
{	strOutput = new String(intNumber);
	intL = intDigits - strOutput.length;
	for (i=0; i< intL; i++)
	{	strOutput = "0"+strOutput; }
	return strOutput;
}

function bt_replace(strSource, strFind, strReplace)
{	strOutput = '';
	if (strSource && strSource.length)
	{	raySource = strSource.split(strFind);
		strOutput = raySource.join(strReplace);
	}
	return strOutput;
}

function isDefined(object, variable)
{	return (typeof(eval(object)[variable]) != 'undefined'); }



/*
	Cookie functions
*/
function setCookie(strName,strValue,intExpireDays)
{
	var expdate = new Date();
	expdate.setDate(expdate.getDate()+intExpireDays);
	document.cookie = strName+ "=" +escape(strValue)+
		((intExpireDays==null)? "": ";expires="+expdate);
}
function getCookie(strName)
{
	var dc = document.cookie;
	var prefix = strName + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(strName)
{
    if (getCookie(strName))
    {
        document.cookie = strName + "=;";
    }
}





/*
	Parse standard data string. "key1:value1;key2:value2"
	return as keyed array [key1]="value1", [key2]="value2"
*/
function DataStrToRay(strInput)
{
	rayOutput = new Array();
	if (strInput)
	{	rayInput = strInput.split(';');
		for (dstri=0; dstri< rayInput.length; dstri++)
		{
			rayThisPair = rayInput[dstri].split(':');
			rayOutput[rayThisPair[0]] = rayThisPair[1];
		}
	}
	return rayOutput;
}
function DataRayToStr(rayInput)
{
	strOutput = '';
	for (drtsi in rayInput)
	{	if (drtsi != 'clone')
		{	if (strOutput) strOutput += ';';
			strOutput += drtsi+':'+rayInput[drtsi];
		}
	}
	return strOutput;
}



/*
	Swap out special characters for storage and display.
*/
function propertyToDisplay(strProperty)
{	strDisplay = strProperty.replace(/%2520/g,' ');
	strDisplay = strDisplay.replace(/%2527/g,"'");
	strDisplay = strDisplay.replace(/%2522/g,'"');
	strDisplay = strDisplay.replace(/%20/g,  ' ');
	strDisplay = strDisplay.replace(/%27/g,  "'");
	strDisplay = strDisplay.replace(/%22/g,  '"');
	return strDisplay;
}
function displayToProperty(strDisplay)
{	strProperty = strDisplay.replace(/ /g,'%2520');
	strProperty = strProperty.replace(/\'/g,'%2527');
	strProperty = strProperty.replace(/\"/g,'%2522');
	return strProperty;
}





/*
	Sequential operation handlers
*/
var raySequenceTimers = new Array();
var raySequences = new Array();
function addToSequence(intSequenceID, strNewCommand)
{	if (typeof(raySequences[intSequenceID])=='undefined') 
	{	raySequences[intSequenceID] = new Array(); }
	raySequences[intSequenceID].push(strNewCommand);
}
function doNextInSequence(intSequenceID)
{	if ((typeof(raySequences[intSequenceID])!='undefined') &&
		raySequences[intSequenceID].length )
	{	strThisCommand = raySequences[intSequenceID].shift();
		if (strThisCommand)
		{	eval(strThisCommand); }
	}
}
function startSequence(intSequenceID)
{	doNextInSequence(intSequenceID); }









/*
	Standard panel scaling and movement
*/
var intScaleWait = 50;
var rayScaleTimers = new Array();

function scaleLayer(strLayerName, strDimension, intScaleFrom, intScaleTo, 
	intScaleFrames, strFinishCode)
{	clearTimeout(rayScaleTimers[strLayerName]);
	
	/* debug debug("scaleLayer( strLayerName:"+strLayerName+", strDimension:"+strDimension+
		", intScaleFrom:"+intScaleFrom+", intScaleTo:"+intScaleTo+", intScaleFrames:"+
		intScaleFrames+", strFinishCode:"+strFinishCode+" )"); */
	
	objThisLayer = MM_findObj(strLayerName);
	rayThisLayerPos = doGetObjPos(objThisLayer);
	intCurrentSize = rayThisLayerPos[strDimension];
	intGrowAmount = Math.ceil((intScaleTo-intScaleFrom)/intScaleFrames);
	intNewSize = intCurrentSize + intGrowAmount;
	
	/*	check if scaling is complete. */
	booContinueScaling = true;
	if ( ((intNewSize <=  intScaleTo) && (intGrowAmount <= 0))
		|| ((intNewSize >= intScaleTo) && (intGrowAmount >= 0)) )
	{	/*	target reached. */
		intNewSize = intScaleTo;
		booContinueScaling = false;
	}
	
	/*	update layer size. */
	if (strDimension=='width')
	{	objThisLayer.style.width = intNewSize+'px'; }
	else
	if (strDimension=='height')
	{	objThisLayer.style.height = intNewSize+'px'; }
	
	
	/*	check if size update worked.  if not, we need to cancel this loop. */
	rayThisLayerPos = doGetObjPos(objThisLayer);
	intActualSize = rayThisLayerPos[strDimension];
	if (intCurrentSize==intActualSize)
	{	booContinueScaling = false;
		if (strDimension=='width')
		{	objThisLayer.style.width = intScaleTo+'px'; }
		else
		if (strDimension=='height')
		{	objThisLayer.style.height = intScaleTo+'px'; }
	}
	
	
	if (booContinueScaling)
	{	
		rayScaleTimers[strLayerName] = setTimeout("scaleLayer('"+strLayerName+"', '"+
			strDimension+"', new Number("+intScaleFrom+"), new Number("+intScaleTo+
			"), new Number("+intScaleFrames+"), '"+strFinishCode+"')", intScaleWait);
	}
	else
	{	
		/* debug debug("scale complete.  strFinishCode: "+strFinishCode); */
		if (strFinishCode) eval(strFinishCode);
	}
}



var intMoveWait = 50;
var rayMoveTimers = new Array();
var rayMoveStatus = new Array();

function moveLayer(strLayerName, strDimension, intMoveFrom, intMoveTo, 
	intMoveFrames, strFinishCode)
{	clearTimeout(rayMoveTimers[strLayerName]);
	
	/* debug */
	strDebug = "<br>move:"+strLayerName+"."+strDimension+
		" from:"+intMoveFrom+" to:"+intMoveTo+" frames:"+intMoveFrames+
		(strFinishCode ? " strFinishCode:"+strFinishCode : "");
	
	objThisLayer = MM_findObj(strLayerName);
	/*rayThisLayerPos = doGetObjPos(objThisLayer);
	intCurrentPosition = rayThisLayerPos[strDimension];*/
	intCurrentPosition = rayMoveStatus[strLayerName];
	if (typeof(intCurrentPosition)=='undefined') intCurrentPosition = intMoveFrom;
	
	//intMoveAmount = Math.ceil((intMoveFrom-intMoveTo)/intMoveFrames);
	intMoveAmount = Math.ceil((intMoveTo-intMoveFrom)/intMoveFrames);
	intNewPosition = intCurrentPosition + intMoveAmount;
	rayMoveStatus[strLayerName] = intNewPosition;
	
	/* debug */
	strDebug += " "+intCurrentPosition+" + "+intMoveAmount+" = "+intNewPosition;
	
	
	/*	check if movement is complete. */
	booContinueMoving = true;
	if ( ((intNewPosition <= intMoveTo) && (intMoveAmount <= 0)) ||
		 ((intNewPosition >= intMoveTo) && (intMoveAmount >= 0)) )
	{	/*
			Target reached.
		*/
		intNewPosition = intMoveTo;
		booContinueMoving = false;
		rayMoveStatus[strLayerName] = undefined;
	}
	
	/*	update layer position. */
	if (strDimension=='left')
	{	objThisLayer.style.left = intNewPosition+'px'; }
	else
	if (strDimension=='right')
	{	objThisLayer.style.right = intNewPosition+'px'; }
	else
	if (strDimension=='top')
	{	objThisLayer.style.top = intNewPosition+'px'; }
	else
	if (strDimension=='bottom')
	{	objThisLayer.style.bottom = intNewPosition+'px'; }
	
	
	/*	check if position update worked.  if not, we need to cancel this loop. */
	rayThisLayerPos = doGetObjPos(objThisLayer);
	intActualPosition = rayThisLayerPos[strDimension];
	if (intCurrentPosition==intActualPosition)
	{	
		/* debug  debug("<p>ERROR: move aborted. "+strDebug);*/
		
		booContinueMoving = false;
		rayMoveStatus[strLayerName] = undefined;
		if (strDimension=='left')
		{	objThisLayer.style.left = intMoveTo+'px'; }
		else
		if (strDimension=='right')
		{	objThisLayer.style.right = intMoveTo+'px'; }
		else
		if (strDimension=='top')
		{	objThisLayer.style.top = intMoveTo+'px'; }
		else
		if (strDimension=='bottom')
		{	objThisLayer.style.bottom = intMoveTo+'px'; }
	}
	
	
	if (booContinueMoving)
	{	
		rayMoveTimers[strLayerName] = setTimeout("moveLayer('"+strLayerName+"', '"+
			strDimension+"', new Number("+intMoveFrom+"), new Number("+intMoveTo+"), new Number("+
			intMoveFrames+"), '"+strFinishCode+"')", intMoveWait);
	}
	else
	{	
		/* debug debug("move complete.  strFinishCode: "+strFinishCode); */
		if (strFinishCode) eval(strFinishCode);
	}
	
}










/*
	Dynamic image loader.
*/

var rayLoadedImages = new Array();

function loadImage(strImageUrl)
{	
	objNewImage = new Image();
	objNewImage.src = strImageUrl;
	
	rayLoadedImages[strImageUrl] = objNewImage;
	return rayLoadedImages[strImageUrl];
}


var rayImageTimers = new Array();

function waitForImageComplete(strImageUrl, strFollowupFunction)
{	clearTimeout(rayImageTimers[strImageUrl]);
	
	/* debug doDebug(strImageUrl+".complete:"+
		rayLoadedImages[strImageUrl].complete+
		"? followup:"+strFollowupFunction+
		" at:"+new Date()+"<br>"); */
	
	if (rayLoadedImages[strImageUrl].complete)
	{	rayImageTimers[strImageUrl] = setTimeout(strFollowupFunction,0); }
	else
	{	rayImageTimers[strImageUrl] = setTimeout("waitForImageComplete('"+strImageUrl+
			"',\""+strFollowupFunction+"\")",100); }
}








