// File: Common.js
//
// ********** Begin of Fly Out Menu. **********
// fom = Fly Out Menu.

var fom_Item = new Array();	// This is the list of open menu items.
var fom_TimeoutId = null;	// The timer that will close the open menus.
var fom_Level = 0;			// Current level the menu is at.

// This is when the mouse enters the menu item.
function fom_MouseOver(menuCode, level, selectedBackgroundColor, selectedColor, doMenuDown)
{
	var menuItemObj = GetObjectPosition(GetObject(menuCode + "i"));
	var menuObj = GetObject(menuCode);

	// Hide all the menu items to this level.
	fom_Hide(level);

	// Do we have a sub menu?
	if (menuObj)
	{// Yes, show it.
		if (doMenuDown == true)
		{
			ChangePosition(menuObj, menuItemObj.MyLeft, menuItemObj.MyTop + menuItemObj.MyHeight);
		}
		else
		{
			ChangePosition(menuObj, menuItemObj.MyLeft + menuItemObj.MyWidth, menuItemObj.MyTop);
		}
		ChangeVisibility(menuObj, true);

		if (level == 1)
			ChangeDropdownVisibility(false);
	}

	fom_Item[level] = menuItemObj;
	menuItemObj.MyMenuObj = menuObj;
	menuItemObj.style.backgroundColor = selectedBackgroundColor;
	menuItemObj.style.color = selectedColor;
}


// This is when the mouse leaves the menu item.
function fom_MouseOut()
{
	// Cancel the last timeout if any.
	if (fom_TimeoutId) window.clearTimeout(fom_TimeoutId);

	// Don't want to hide things before the user has a chance to move the mouse.
	fom_TimeoutId = window.setTimeout(fom_MouseOutTimeout,250);
}

// The time out calls this.
function fom_MouseOutTimeout()
{
	fom_Hide(0);
}

// Now hide all the menus down to this level.
function fom_Hide(newLevel)
{
	if (fom_TimeoutId)
	{
		window.clearTimeout(fom_TimeoutId);
		fom_TimeoutId = null;
	}

	for (var idx = fom_Level; idx >= newLevel; idx--)
	{
		var menuItemObj = fom_Item[idx];
		if (menuItemObj)
		{
			menuItemObj.style.backgroundColor = "";
			menuItemObj.style.color = "";
			var menuObj = menuItemObj.MyMenuObj
			if (menuObj)
			{
				ChangeVisibility(menuObj, false);
			}
			fom_Item[idx] = null;
		}
	}
	fom_Level = newLevel;

	if (newLevel <= 0)
		ChangeDropdownVisibility(true);
}

// This will set the visibility of the dropdown elements.
function ChangeDropdownVisibility(isVisible)
{
	if (isAllObject)
	{
		var visibilityText = (isVisible ? 'inherit' : 'hidden');
		var selectObjects = document.all.tags('select');
		if (selectObjects != null)
		{
			for (i = 0; i < selectObjects.length; i++)
			{
				selectObjects[i].style.visibility = visibilityText;
			}
		}
	}
}

// ********** End of Fly Out Menu. **********


// This tests if the browser is opera.
window.isWin32 = (navigator.platform ? (navigator.platform == "Win32") : false);

function GetObjectPosition(obj)
{
	x = 0;
	y = 0;
	if (!document.layers)
	{
		var parent = obj;
		var lastLeftOffset = 0;
		var lastTopOffset = 0;
		while (parent)
		{
			if ( parent.leftMargin && !window.isWin32 ) { x += parseInt(parent.leftMargin); y += parseInt(parent.topMargin); }
			if ( (parent.offsetLeft != lastLeftOffset) && parent.offsetLeft ) x += parseInt(parent.offsetLeft);
			if ( (parent.offsetTop != lastTopOffset) && parent.offsetTop )  y += parseInt(parent.offsetTop);
			if ( parent.offsetLeft != 0 ) lastLeftOffset = parent.offsetLeft;
			if ( parent.offsetTop != 0 ) lastTopOffset = parent.offsetTop;
			parent = parent.offsetParent;
		}
	} else if (obj.x) { x = obj.x; y = obj.y; }
	obj.MyTop = y;
	obj.MyLeft = x;
	
	obj.MyWidth = obj.offsetWidth;
	obj.MyHeight = obj.offsetHeight;

	return obj;
}


// Hide an object.
function HideObject(tagName)
{
	var Obj = GetObject(tagName);
	if (Obj != null)
		if (Obj.style.visibility != 'hidden')
			Obj.style.visibility = 'hidden';
}


// Show an object.
function ShowObject(tagName)
{
	var Obj = GetObject(tagName);
	if (Obj != null)
		if (Obj.style.visibility != 'inherit')
			Obj.style.visibility = 'inherit';
}

// Display an object.
function DisplayObject(tagName, displayIt)
{
	var Obj = GetObject(tagName);
	if (Obj != null)
		Obj.style.display = (displayIt ? '' : 'none');
}

// Enable an object.
function EnableObject(tagName, enableIt)
{
	var Obj = GetObject(tagName);
	if (Obj != null)
		Obj.enabled = enableIt;
}

// Show help popup.
function Help(file,height,width)
{
	if (height == null)
		height = 300;
	
	if (width == null)
		width = 500;

	window.open('/Help/' + file,'SmartCenterHelp','toolbar=0,scrollbars=yes,status=0,resizable=1,menubar=0,top=0,right=10,width=' + width + ',height=' + height);
}

// Old, use GetObject().
function FindObject(objId) { return GetObject(objId); }

//////////////////////////////////////////////////////
//   Start of Cross-Browser independent functions.  //

var isLayerObject = (document.layers) ? true : false;
var isAllObject = (document.all) ? true: false;

// Get the object by its ID.
function GetObject(id)
{
	if (isLayerObject) return document.layers[''+id+''];
	else if (isAllObject) return document.all[''+id+''];
	else return document.getElementById(''+id+'');
}

// Set the visibility of an object.
function ChangeVisibility(obj, isVisible)
{
	if (isVisible)
	{
		if (isLayerObject)
			obj.visibility = "show";
		else
			obj.style.visibility = "visible";
	}
	else
	{
		if (isLayerObject)
			obj.visibility = "hide";
		else
			obj.style.visibility = "hidden";
	}
	return;
}

// Set the position of an object.
function ChangePosition(obj, x, y)
{
	if (isLayerObject)
	{
		obj.left = x;
		obj.top = y;
	}
	else if (isAllObject)
	{
		obj.style.left = x;
		obj.style.top = y;
	}
	else
	{
		obj.style.left = x+"px";
		obj.style.top = y+"px";
	}
	return;
}

 // Returns the position of an element relative to the client window
 // Not tested.
function GetElementPosition(element)
{
	var position = {'x':0, 'y':0};
	while (element != null)
	{
		position.x += element.offsetLeft;
		position.y += element.offsetTop;
		element = element.offsetParent;
	}
	return position;
}

//   End of Cross-Browser independent functions.  //
////////////////////////////////////////////////////

var RotatorList = new Array();

// This setsup the box that is changed every "delay" seconds.
// The "prefix" is the first part of the string ID.  The ID is in this form:
// "prefix#" where # is a sequential number started with 1.
// Html looks like this: <div id="box1" style="display:none">somthing</div>
function BoxRotatorStartup(prefix, delay)
{
	var boxes = new Array();
	var idx = 0;

	var newBox = GetObject(prefix + (idx+1))
	while (newBox != null)
	{
		boxes[idx] = newBox;
		idx++;
		newBox = GetObject(prefix + (idx+1));
	}

	var rotatorIdx = RotatorList.length;
	RotatorList[rotatorIdx] = new BoxRotatorData(boxes);

	BoxRotatorNext(rotatorIdx);
	window.setInterval('BoxRotatorNext(' + rotatorIdx + ');', delay * 1000);
}

// This has the data.
function BoxRotatorData(boxes)
{
	var dt = new Date();
	this.Boxes = boxes;
	// Start at a random spot.
	this.Idx = Math.floor(dt.getTime()/100) % boxes.length;
}

// This function is called by the timer and the BoxRotatorData has the data.
function BoxRotatorNext(rotatorIdx)
{
	var item = RotatorList[rotatorIdx];
	var lastBox = item.Boxes[item.Idx];

	item.Idx++;
	if (item.Idx >= item.Boxes.length)
		item.Idx = 0;

	var currentBox = item.Boxes[item.Idx];
	
	lastBox.style.display = 'none';
	currentBox.style.display = '';
}

// This setsup the image that is changed every "delay" seconds.
function ImageRotatorStartup(imageId, delay, basePath, imageList)
{
	var obj = GetObject(imageId);
	if (obj != null)
	{
		var idx = RotatorList.length;
		RotatorList[idx] = new ImageRotatorData(obj, basePath, imageList);
		
		if (obj.style.filter != null)
		{
			obj.style.filter = 'progid:DXImageTransform.Microsoft.Fade(Overlap=2.00)';
		}
		ImageRotatorNext(idx);
		window.setInterval('ImageRotatorNext(' + idx + ');', delay * 1000);
	}
}

// This has the data.
function ImageRotatorData(imageObj, basePath, imageList)
{
	this.ImageObj = imageObj;
	this.PasePath = basePath;
	this.ImageList = imageList;

	var dt = new Date();
	// Start at a random spot.
	this.Idx = Math.floor(dt.getTime()/100) % imageList.length;
}

// This function is called by the timer and the ImageRotatorData has the data.
function ImageRotatorNext(rotatorIdx)
{
	var item = RotatorList[rotatorIdx];

	var imagePath = item.PasePath + item.ImageList[item.Idx];
	
	if (item.ImageObj.style.filter != null)
	{
		item.ImageObj.filters[0].apply();
		item.ImageObj.src = imagePath;
		item.ImageObj.filters[0].play();
	}
	else
	{
		item.ImageObj.src = imagePath;
	}

	item.Idx++;
	if (item.Idx >= item.ImageList.length)
		item.Idx = 0;
}
