/////////////////////////////////////
//                                 //
//  STCenter javascript functions  //
//                                 //
/////////////////////////////////////

// A netscape bug makes layers lose their css positioning when the browser is
// resized. check if that's happened here, if it has, reload the document to
// fix the the page layout.
function fixNav4CSS() 
{
	current_width = document.layers[layertoCheck].document.width;
	current_height = document.layers[layertoCheck].document.height;

	if (current_width != start_width || current_height != start_height)
	{
		location.reload();
	}
}

// Now that the layers have been established, record the height and width of
// the content layer if the browser is netscape.
function getlayerDimensions(theLayer)
{
	if (is_nav4)
	{
		start_width = document.layers[theLayer].document.width;
		start_height = document.layers[theLayer].document.height;
	}
}

function clipValues(el,which) {
	if (is_nav4) {
		if (which=="t") return el.clip.top
		if (which=="r") return el.clip.right
		if (which=="b") return el.clip.bottom
		if (which=="l") return el.clip.left
	}
	else
	{
		var clipv = el.clip.split("rect(")[1].split(")")[0].split("px")
		if (which=="t") return Number(clipv[0])
		if (which=="r") return Number(clipv[1])
		if (which=="b") return Number(clipv[2])
		if (which=="l") return Number(clipv[3])
	}
}

function clipBy(el,t,r,b,l)
{
	if (is_nav4) {
		el.clip.top = clipValues(el,'t') + t
		el.clip.right = clipValues(el,'r') + r
		el.clip.bottom = clipValues(el,'b') + b
		el.clip.left = clipValues(el,'l') + l
	}
	else el.clip = "rect("+(this.clipValues(el,'t')+t)+"px " +
			(this.clipValues(el,'r')+r)+"px " +
			Number(this.clipValues(el,'b')+b)+"px " + 
			Number(this.clipValues(el,'l')+l)+"px)"
}

// Workaround for nav4.x crash caused by CSS border
function showNav4Border(el)
{
	var border = document.netscapeBorderID;

	border.clip.width = el.clip.width + 2;
	border.clip.height = el.clip.height + 2;

	border.top = el.top - 1;
	border.left = el.left - 1;

	showEl(border);
}

function hideNav4Border()
{
	var border = document.netscapeBorderID;
	hideEl(border);
}
									   														
function showBorder(el)
{
	if (is_nav4)
	{
		showNav4Border(el);
	}
	else
	{
		el.borderColor = "black";
		el.borderStyle = "solid";
		el.borderBottomWidth = 1;
		el.borderTopWidth = 1;
		el.borderLeftWidth = 1;
		el.borderRightWidth = 1;
	}
}

function hideBorder(el)
{
	if (is_nav4)
	{
		hideNav4Border();
	}
	else
	{
		el.borderColor = "";
		el.borderStyle = "none";
		el.borderBottomWidth = 0;
		el.borderTopWidth = 0;
		el.borderLeftWidth = 0;
		el.borderRightWidth = 0;
	}
}

// Callback to handle updating sametime logins
function handleData()
{
   if (goodToGo)
   {
      var temp = (is_nav4) ? document.sametimelogins : document.all.sametimelogins;
      var res = "handleData(...)";

      if (arguments.length > 1)
      {
         var tokens = arguments[1].split("=");
         if (tokens != null)
         {
            if (is_nav4)
            {
					// Due to a bug in Netscape, need to re-create some of the style sheet information on the fly
					temp.document.open();
               temp.document.writeln('<DIV STYLE="color:black; font-size:small">' + gSametimeLoginsLabel + "<b>" + tokens[1] + "</b>" + "</DIV>");
					temp.document.close();
					showEl(sametimelogins);
            }
            else
            {
               temp.innerHTML = gSametimeLoginsLabel + "<b>" + tokens[1] + "</b>";
               showEl(sametimelogins);
            }
         }
      }
   }
}

function clearAllFocusRects()
{	
	// Only works in IE
	if (! is_nav4)
	{
		for (i=0; i<document.anchors.length; i++)
		{
			document.anchors[i].blur();
		}
	}
}

function updateBackgroundImage(el, on)
{
	if (on)
	{
		if (is_nav4)
		{
			previousBGColor = el.bgColor;
			el.bgColor = "#FFFF99";
		}
		else
		{
			el.backgroundColor = "#FFFF99";
		}
		showBorder(el);
	}
	else
	{
		if (is_nav4)
		{
			el.bgColor =  previousBGColor;
		}
		else
		{
			el.backgroundColor = "";
		}
		hideBorder(el);
	}
}

function showEl(el)
{
	el.visibility = (is_nav4) ? 'show' : 'visible';
}

function hideEl(el)
{
	el.visibility = (is_nav4) ? 'hide' : 'hidden';
}

function displayWelcomeInfo()
{  
   welcomeTop = intro.top;
   showEl(intro);
   initEls();
}

/* Position elements */
function positionEls()
{
	var item; 
	var delta = 0; //Note:  this assumes item 1 fits on 1 line
	if (is_nav4)
	{
		delta = stEls[0].clip.height;
	}
	else
	{
		delta = stEls[0].offsetHeight;
	}

	// Special case first item
	item = (is_nav4) ? stEls[0] : stEls[0].style;
	showEl(item);
	item.top = ystart;
			 
	for (var i = 1; i < stEls.length; i++)
	{
		item = (is_nav4) ? stEls[i] : stEls[i].style;
		showEl(item);
		if (is_nav4)
		{
			item.top = stEls[i-1].top + stEls[i-1].clip.height + delta;
		}
		else
		{
			item.top = stEls[i-1].offsetTop + stEls[i-1].offsetHeight + delta; 
		} 
	}

	// Position site HREFS and build info
	if (is_nav4)
	{
		sitelinks.top = stEls[stEls.length-1].top + 100;
		showEl(sitelinks);

		/* buildinfo.top = headerLayer.clip.bottom + buildinfoYOffset; */
		/* buildinfo.left =  headerLayer.left + buildinfoXOffset; */
		/* showEl(buildinfo); */

		sametimelogins.top = headerLayer.clip.bottom + sametimeloginsYOffset;
		//sametimelogins.left =  headerLayer.left + sametimeloginsXOffset;
	}
	else
	{
		sitelinks.top = stEls[stEls.length-1].offsetTop + stEls[stEls.length-1].offsetHeight + (8 * delta);
		sitelinks.width = maxScreenWidth - 50;
		showEl(sitelinks);
		
      	sametimelogins.top = headerLayer.offsetHeight + sametimeloginsYOffset;
   }
}

function isRoomToDrawLine(itemLayout)
{
   var isRoom = false;

   if (is_nav4)
   {
      if (itemLayout.clip.right + minLineLength < introLayer.left)
      {
         isRoom = true;
      }
   }
   else
   {
      if (itemLayout.offsetLeft + itemLayout.offsetWidth + minLineLength < introLayer.offsetLeft)
      {
         isRoom = true;
      }
   }

   return isRoom;
}

/* Draw connection lines between element and pop-up help */
function drawConnectionLines(fromEl, realFromEl, toEl)
{
	var x;
	var y;
	var width;
	var width2 = 0;
	var height;

	if (is_nav4)
	{
		var nshlinestart = document.layers["ns_hlinestart"];
		var nshlineend = document.layers["ns_hlineend"];
		var nsvline = document.layers["ns_vline"];

		if (isRoomToDrawLine(fromEl))
		{
			// Calculate the first horizontal line coords
			hideEl(nshlinestart);
			width = (introLayer.left - (fromEl.left + fromEl.clip.right)) / 2;
			if (fromEl != realFromEl)
			{
				x = realFromEl.left + realFromEl.clip.right;
				y = (realFromEl.clip.bottom - realFromEl.top) / 2;
				width2 = fromEl.left + fromEl.clip.right - x;
			}
			else
			{
				x = fromEl.left + fromEl.clip.right;
				y = fromEl.top + (fromEl.clip.bottom / 2);
			}			

			nshlinestart.left = x;
			nshlinestart.top = y;
			nshlinestart.clip.width = width + width2;
			showEl(nshlinestart);

			// Calculate the second horizontal line coords
			hideEl(nshlineend);
			nshlineend.left = x + width + width2;
			nshlineend.top = toEl.top + connectionYOffset;
			nshlineend.clip.width =  toEl.left - nshlineend.left;
			showEl(nshlineend);

			// Calcualte the vertical line coords
			hideEl(nsvline);
			nsvline.left = nshlineend.left;
			if (nshlinestart.top < nshlineend.top)
			{
				nsvline.top = nshlinestart.top;
			}
			else
			{
				nsvline.top = nshlineend.top;
			}   
			nsvline.clip.height = Math.abs(nshlineend.top - nshlinestart.top) + 2;
			showEl(nsvline);      
		}
	}
	else
	{
		if (isRoomToDrawLine(fromEl))
		{
			// Calculate the first horizontal line coords
			hideEl(hlinestart);
			width = (introLayer.offsetLeft - (fromEl.offsetLeft + fromEl.offsetWidth)) / 2;
			if (fromEl != realFromEl)
			{
				x = realFromEl.offsetLeft + realFromEl.offsetWidth;
				y = (realFromEl.offsetHeight - realFromEl.offsetTop) / 2;
				width2 = fromEl.offsetLeft + fromEl.offsetWidth - x;
			}
			else
			{
				x = fromEl.offsetLeft + fromEl.offsetWidth;
				y = fromEl.offsetTop + (fromEl.offsetHeight / 2);
			}
			hlinestart.left = x;
			hlinestart.top = y;
			hlinestart.width = width + width2;
			showEl(hlinestart);

			// Calculate the second horizontal line coords
			hideEl(hlineend);
			hlineend.left = x + width + width2;
			hlineend.top = toEl.offsetTop + connectionYOffset;
			hlineend.width =  toEl.offsetLeft - x - width + 1;
			showEl(hlineend);

			// Calculate the vertical line coords
			hideEl(vline);
			vline.left = hlineend.left;
			if (parseInt(hlinestart.top) < parseInt(hlineend.top))
			{
				vline.top = hlinestart.top;
			}
			else
			{
				vline.top = hlineend.top;
			}   
			vline.height = Math.abs(parseInt(hlineend.top) - parseInt(hlinestart.top)) + 2;
			showEl(vline);
		}
	}
}

function refreshWelcomeInfo()
{
	var currentStyle = (is_nav4) ? currentDesc : currentDesc.style;
	hideEl(currentStyle);
	currentDesc = introLayer;	
	showEl(intro);
	drawConnectionLines(stEls[0], headerLayer, introLayer)
}

function addItem(item, pos)
{
	if (item != null)
	{
		stEls[pos] = item;
		pos = pos + 1;
	}

	return pos;
}

/* Initialize selected elements for rotation */
function initEls()
{
	var j = 0;

	stEls = new Array();
   
	j = addItem(item1, j);
	j = addItem(item2, j);
	j = addItem(item3, j);
	j = addItem(item4, j);
	j = addItem(item5, j);
	j = addItem(item6, j);
	j = addItem(item7, j);
	j = addItem(item8, j);	

	//Hide all the borders initially
	for (var i = 0; i < stEls.length; i++)
	{
		var itemStyle = (is_nav4) ? stEls[i] : stEls[i].style;
		hideBorder(itemStyle);
	}

	positionEls();
	drawConnectionLines(stEls[0], headerLayer, introLayer)
}

function hideConnectionLines()
{
// Ones for IE
	hideEl(vline);
	hideEl(hlineend);
	hideEl(hlinestart);

// Ones for Netscape
	if (is_nav4)
	{
		hideEl(document.layers["ns_hlinestart"]);
		hideEl(document.layers["ns_hlineend"]);
		hideEl(document.layers["ns_vline"]);
	}
}

function handleHeaderMouseOverEvent(intro)
{
   // refreshWelcomeInfo();
}

function handleHeaderMouseOutEvent(intro)
{
   /* hideConnectionLines(); */
   /* hideEl(intro); */
}

function handleMouseOverEvent(itemLayer, descLayer)
{
	var itemStyle = (is_nav4) ? itemLayer : itemLayer.style;
	var descStyle = (is_nav4) ? descLayer : descLayer.style;
	var currentStyle = (is_nav4) ? currentDesc : currentDesc.style;   

	descStyle.top = welcomeTop;
	if (currentDesc != descLayer)
	{
		showEl(descStyle)
		hideEl(currentStyle);
		currentDesc = descLayer;
	}
	drawConnectionLines(itemLayer, itemLayer, descLayer);
	updateBackgroundImage(itemStyle, true);	

	/* stopTimer(); */
}

function handleMouseOutEvent(itemLayer, descLayer)
{
	var itemStyle = (is_nav4) ? itemLayer : itemLayer.style;
	// var descStyle = (is_nav4) ? descLayer : descLayer.style;

	updateBackgroundImage(itemStyle, false);
	/* startTimer(); */
}

function startTimer()
{
	timeoutID = setTimeout("refreshWelcomeInfo()", waitTime);
}

function stopTimer()
{
	if (timeoutID)
	{
		clearTimeout(timeoutID);
		timeoutID=null;
	}
}

