// Class constructor
function IASPSite() { }

IASPSite.openFlyout = function(objLink) {
	// Display wrapper
	var objFly = document.getElementById(objLink.id + '_fly');
	
	if (objFly._open != 1) {	
		var x = findPosX(objLink);
		var y = findPosY(objLink);

		var linkSize = objLink.offsetWidth;

		x += linkSize;

		objFly.style.left = x + 'px';
		objFly.style.top = y + 'px';
		objFly.style.display = 'block';
		objFly._link = objLink;

		// Roll out menu
		var objSlide = document.getElementById(objLink.id + '_slide');

		for (var i=0; i < 10; i++) {
			setTimeout("document.getElementById('" + objSlide.id + "').style.left='-" + (linkSize - ((linkSize / 11) * i)) + "px';", i * 10);
		}
		
		objFly._open = 1;

		setTimeout(function() { objSlide.style.left='0px'; }, 110);
	} else {
		objFly._maintain = 1;
	}
}

IASPSite.maintainFlyout = function(objFly) {
	objFly._maintain = 1;
}

IASPSite.closeFlyoutFromLink = function(objLink) {
	var objFly = document.getElementById(objLink.id + '_fly');
	objFly._maintain = 0;
	setTimeout(function() { if (objFly._maintain != 1) { IASPSite.actualCloseFlyout(objLink); } },250);
}

IASPSite.closeFlyout = function(objFly) {
	objFly._maintain = 0;
	setTimeout(function() { if (objFly._maintain != 1) { IASPSite.actualCloseFlyout(objFly._link); } },250);
}

IASPSite.actualCloseFlyout = function(objLink) {
	// Roll in menu
	var objFly = document.getElementById(objLink.id + '_fly');
	
	var linkSize = parseInt(objLink.offsetWidth);
	
	var objSlide = document.getElementById(objLink.id + '_slide');
	
	for (var i=0; i < 10; i++) {
		setTimeout("document.getElementById('" + objSlide.id + "').style.left='-" + ((linkSize / 11) * i) + "px';", i * 10);
	}
	
	setTimeout("document.getElementById('" + objSlide.id + "').style.left='-" + linkSize + "px';", 110);
	setTimeout(function() { objFly.style.display = 'none'; objFly._open = 0; }, 115);
	
}

IASPSite.cycleSeeAlso = function(objLink,dataId) {
	if (objLink._open == 1) {
		// Close
		objDiv = document.getElementById('seealso_' + dataId);
		objDiv.style.display='none';
		objLink._open = 0;
		objLink.className = '';
	} else {
		// Open
		objDiv = document.getElementById('seealso_' + dataId);
		objDiv.style.display='block';
		objLink._open = 1;
		objLink.className = 'open';
	}
	return false;
}

IASPSite.raiseopac = function(objOpac) {
	if (objOpac._raised != '1') {
		objOpac._raised = '1';

		for (var i=0; i < 30; i++) {
			var thisVal = 0.71 + (i * 0.01);
			setTimeout("if (document.getElementById('" + objOpac.id + "')._raised == '1') cms_setOpacity(" + Math.ceil(thisVal * 100) + ",'" + objOpac.id + "');",i * 10);
		}
	}
}

IASPSite.loweropac = function(objOpac) {
	if (objOpac._raised == '1') {
		objOpac._raised = '0';
		for (var i=0; i < 30; i++) {
			var thisVal = 1 - (i * 0.01);
			setTimeout("if (document.getElementById('" + objOpac.id + "')._raised == '0') cms_setOpacity(" + Math.ceil(thisVal * 100) + ",'" + objOpac.id + "');",i * 10);
		}
	}
}

IASPSite.swapTheme = function() {
	var th1 = document.getElementById('theme_1');
	var th2 = document.getElementById('theme_2');
	var th3 = document.getElementById('theme_3');
	var th4 = document.getElementById('theme_4');
	var th5 = document.getElementById('theme_5');
	var thM = document.getElementById('theme_mst');

	if (th1 != null) {
		th1.id = 'theme_2';
	} else if (th2 != null) {
		th2.id = 'theme_3';
	} else if (th3 != null) {
		th3.id = 'theme_4';
	} else if (th4 != null) {
		th4.id = 'theme_5';
	} else if (th5 != null) {
		th5.id = 'theme_mst';
	} else if (thM != null) {
		thM.id = 'theme_1';
	}

	return false;
}

IASPSite._conveyorDefaultVelocity = 12;

IASPSite.beginConveyor = function() {

	if (document.getElementById('conveyor_0')) {
		var conveyorObj = document.getElementById('conveyor_0');

		// Set up conveyor
		if (conveyorObj._fixed != '1') {
			// Fix height of conveyor
			conveyorObj.style.height = conveyorObj.offsetHeight + 'px';
			conveyorObj._fixed = '1';
			conveyorObj._top = findPosY(conveyorObj);
			conveyorObj._childCount = 0;
		} else {
			conveyorObj._spanHeight = conveyorObj.offsetHeight / conveyorObj._childCount;
		}
			
		// Create array of objects to move
		IASPSite._conveyorObjects = new Array();
		var q = 0;
	
		for (var i=0; i < conveyorObj.childNodes.length; i++) {
			var tObj = conveyorObj.childNodes[i];
			if (tObj.tagName == 'DIV') {
				// Prepare for absolute positioning
				var x = findPosX(tObj);
				var y = findPosY(tObj);
				tObj._top = y;
				tObj._left = x;
				tObj._velocity = 12;
				conveyorObj._childCount++;
				tObj.style.zIndex = 10;

				// Insert into array
				IASPSite._conveyorObjects[q] = tObj;
				q++;
			}
		}
	
		// Position objects
		for (var i=0; i < IASPSite._conveyorObjects.length; i++) {
			var tObj = IASPSite._conveyorObjects[i];
		
			var x = tObj._left;
			var y = tObj._top;
			tObj.style.left = x + 'px';
			tObj.style.top = y + 'px';
			tObj.style.position = 'absolute';
		}
	
		// Schedule conveyor cycle
		IASPSite.scheduleConveyorCycle();
	}
}

IASPSite.scheduleConveyorCycle = function() {
	if (IASPSite._conveyorObjects[0].parentNode._hasMouse == 1) {
		// Mouse is currently over the conveyor; retry in one second.
		setTimeout("IASPSite.scheduleConveyorCycle();", 1000);
	} else {
		// Cycle through each conveyor object
		for (var i=0; i < IASPSite._conveyorObjects.length; i++) {
			// Destination object
			var objA = IASPSite._conveyorObjects[i];
			// Target object
			var objB = (i == IASPSite._conveyorObjects.length - 1) ? IASPSite._conveyorObjects[0] : IASPSite._conveyorObjects[i + 1];

			// Set target position and speed
			objB._nextStop = objA._top;
			objB._velocity = (objA._top - objB._top) / 12;

			// Handle zIndex/opacity drop for rear-cycling
			if (objB._velocity > 0) {
				// Set Opacity
				objB.style.opacity = 0.5;
				objB.style.MozOpacity = 0.5;
				objB.style.KhtmlOpacity = 0.5;
				objB.style.filter = "alpha(opacity=" + 50 + ")";
				objB.style.zIndex = 1;
			}

			// Animate
			for (var q=0; q < 12; q++) {
				setTimeout("IASPSite.nextObjectFrame('" + objB.id + "');", q * 30);
			}

			setTimeout("IASPSite.lastObjectFrame('" + objB.id + "'," + i + ");", 400);
		}
	}
}

IASPSite.nextObjectFrame = function(sprId) {
	var obj = document.getElementById(sprId);
	
	// Increment position
	obj._top += obj._velocity;
	obj.style.top = obj._top + 'px';
}

IASPSite.lastObjectFrame = function(sprId, iIdx) {
	var obj = document.getElementById(sprId);
	
	// Set to final position
	obj._top = obj._nextStop;
	obj.style.top = obj._top + 'px';
	
	// Reset opacity and zIndex if required
	if (obj._velocity > 0) {
		// Set Opacity
		obj.style.opacity = 0.7;
		obj.style.MozOpacity = 0.7;
		obj.style.KhtmlOpacity = 0.7;
		obj.style.filter = "alpha(opacity=" + 70 + ")";
		obj.style.zIndex = 10;
	}

	if (iIdx == 0) {
		setTimeout("IASPSite.scheduleConveyorCycle();", 3000);
	}
}

IASPSite.nextConveyorFrame = function() {
	var conveyorObj = document.getElementById('conveyor_0');

	var nextFrame = 30;
	var iTimeout = setTimeout("IASPSite.nextConveyorFrame()",nextFrame);

	if (conveyorObj != null) {
		if (conveyorObj._hasMouse != 1) {
			if (conveyorObj._fixed != '1') {
				// Fix height of conveyor
				conveyorObj.style.height = conveyorObj.offsetHeight + 'px';
				conveyorObj._fixed = '1';
				conveyorObj._top = findPosY(conveyorObj);
				conveyorObj._childCount = 0;
			} else {
				conveyorObj._spanHeight = conveyorObj.offsetHeight / conveyorObj._childCount;
			}

			var blockFurtherChanges = 0;

			//
			// Run bounds checking cycle
			//
			for (var i=0; i < conveyorObj.childNodes.length; i++) {
				if (blockFurtherChanges == 0) {
					var tObj = conveyorObj.childNodes[i];
					if (tObj.tagName == 'DIV') {
						if (tObj.style.position == 'absolute') {
							// Move up 2px
							var curTop = tObj._top;

							curTop -= tObj._velocity;
							if (curTop <= conveyorObj._top) {
								curTop = conveyorObj._top;
								nextFrame = 2000;
								clearTimeout(iTimeout);
								setTimeout("IASPSite.nextConveyorFrame()",nextFrame);
								blockFurtherChanges = 1;
								// Drop to bottom
								tObj._resetTimeout = Math.floor(conveyorObj._spanHeight / IASPSite._conveyorDefaultVelocity);
								//alert(tObj._resetTimeout);
								tObj._velocity = -1 * ((conveyorObj.offsetHeight - conveyorObj._spanHeight) / tObj._resetTimeout);
								tObj._resetTimeout += 1;
								tObj._top = curTop;
								tObj.style.top = curTop + 'px';
							}
						}
					}
				}
			}

			//
			// Run animation cycle
			//
			for (var i=0; i < conveyorObj.childNodes.length; i++) {
				if (blockFurtherChanges == 0) {
					var tObj = conveyorObj.childNodes[i];
					if (tObj.tagName == 'DIV') {
						if (tObj.style.position != 'absolute') {
							if (tObj._top == null) {
								// Get into proper positioning schema
								var x = findPosX(tObj);
								var y = findPosY(tObj);
								tObj._top = y;
								tObj._left = x;
								tObj._velocity = 12;
								conveyorObj._childCount++;
								tObj.style.zIndex = 10;
							} else {
								var x = tObj._left;
								var y = tObj._top;
								tObj.style.left = x + 'px';
								tObj.style.top = y + 'px';
								tObj.style.position = 'absolute';
							}
						} else {
							// Move up 2px
							var curTop = tObj._top;

							// Check velocity reset timeout
							if (tObj._resetTimeout > 0) {
								// Running reset cycle
								tObj._resetTimeout--;
								if (tObj._resetTimeout == 0) {
									tObj._velocity = IASPSite._conveyorDefaultVelocity;
								} else if (tObj._resetTimeout == 1) {
									// Set Opacity
									tObj.style.opacity = 0.7;
									tObj.style.MozOpacity = 0.7;
									tObj.style.KhtmlOpacity = 0.7;
									tObj.style.filter = "alpha(opacity=" + 70 + ")";
									tObj.style.zIndex = 10;
								} else {
									// Set Opacity
									tObj.style.opacity = 0.5;
									tObj.style.MozOpacity = 0.5;
									tObj.style.KhtmlOpacity = 0.5;
									tObj.style.filter = "alpha(opacity=" + 50 + ")";
									tObj.style.zIndex = 1;
								}
							}

							curTop -= tObj._velocity;
							tObj._top = curTop;
							tObj.style.top = curTop + 'px';
							//tObj.innerHtml = curTop;
						}
					}
				}
			}
		}
	}
}

IASPSite.stopConveyor = function() {
	var conveyorObj = document.getElementById('conveyor_0');
	conveyorObj._hasMouse = 1;
	conveyorObj._hasMouseInner = 1;
}
IASPSite.startConveyor = function() {
	var conveyorObj = document.getElementById('conveyor_0');
	conveyorObj._hasMouseInner = 0;
	setTimeout(function() {	if (conveyorObj._hasMouseInner == 0) conveyorObj._hasMouse = 0; },500);
}

IASPSite.substitutePage = function(strUrl) {
	// Try to create XMLHTTP object
	var _xh = GetXMLHTTP();
	var wrapObj = document.getElementById('dyn_wrap');
	var targetObj = document.getElementById('dyn_inner');
	var loaderObj = document.getElementById('dyn_loader');
	
	if (_xh) {
		// Open loader object
		loaderObj.style.display = 'block';
		loaderObj.style.top = findPosY(targetObj) + 'px';
		loaderObj.style.left = findPosX(targetObj) + 'px';
		loaderObj.style.width = targetObj.offsetWidth + 'px';
		loaderObj.style.height = targetObj.offsetHeight + 'px';
		
		// Lock wrapper object height
		wrapObj.style.height = wrapObj.offsetHeight + 'px';
		wrapObj.style.overflow = 'hidden';
		
		// Fade down
		CMS.Presentation.FadeByID('dyn_inner',100,0,200);
		
		// Set up ORSC code
		_xh.onreadystatechange = function() {
			if (_xh.readyState == 4) {
				if (_xh.status == 200) {
					// Reload to work with IE7 ClearType hack
					targetObj = document.getElementById('dyn_inner');
					
					// Store content
					targetObj.innerHTML = _xh.responseText;
					
					// Hide loader
					loaderObj.style.display = 'none';
					
					// Resize wrapper
					CMS.Presentation.RollByID(200, 'dyn_wrap', parseInt(wrapObj.offsetHeight), targetObj.offsetHeight, function() { wrapObj.style.height = 'auto'; });
					
					// Fade up
					CMS.Presentation.FadeByID('dyn_inner',0,100,200);
					
				} else {
					window.location.href = strUrl;
				}
			}
		}
		
		// Start load in 0.2s
		setTimeout(function() {
			_xh.open('GET',strUrl,true);
			_xh.send(null);
		},200);
		return false;
	} else {
		// No XMLHTTP, just load normally
		return true;
	}
}