// global variables
var adArray = null;
var imageArray = null;
var currentAd = 0;
var mainAd = null;
var counter = 0;
var currentTimeOutID = 0;

// tooltip variables
var ttObj = null;
var ttObjTable = null;
var ttObjText = null;
var ttOffsetXPoint=15;
var ttOffsetYPoint=-18;
var ttEnabled=false;

function parseLinkImage(adSectionArray, adLinkArray, adImagePathArray, adCaptionArray, adLink2Array, adImagePath2Array, adCaption2Array, adIndex)
{
	// get section name
	var sectionLabel = "";
	if (adSectionArray[adIndex] == "1")
		sectionLabel = "Consumer Solutions";
	else if (adSectionArray[adIndex] == "2")
		sectionLabel = "Business Solutions";
	else if (adSectionArray[adIndex] == "3")
		sectionLabel = "Government & Education";
	else if (adSectionArray[adIndex] == "4")
		sectionLabel = "Resellers & Service Providers";
	
	document.writeln("<table cellspacing=0 cellpadding=0 width=596><tr><td width=400 valign=top>");
	
	//main ad area
	document.writeln("<div><a href=\"" + adLinkArray[adIndex] + "\"><img src=\"" + adImagePathArray[adIndex] + "\" alt=\"" + adCaptionArray[adIndex] + "\" border=0 ID=\"image" + adIndex + "\" width=400 height=185></a></div>");
	
	document.write("</td><td valign=top width=197>");
	
	//secondary ad area
	document.writeln("<div><a href=\"" + adLink2Array[adIndex] + "\"><img src=\"" + adImagePath2Array[adIndex] + "\" alt=\"" + adCaption2Array[adIndex] + "\" border=0 ID=\"image2nd" + adIndex + "\" width=196 height=185></a></div>");
	document.writeln("</td></tr></table>");
	
	//small ad area
	document.write("<div><img src=\"" + smallAdImagePath[adSectionArray[adIndex]] + "\" border=0 ID=\"image3rd" + adIndex + "\" width=596 height=60 usemap=\"#smallAd" + adSectionArray[adIndex] + "\">" + smallAdImageMapping[adSectionArray[adIndex]] + "</div>");

	//bottom color bar and jumpTo links
	document.write("<div ID=jumpto" + adIndex + " style=\"padding:5px;padding-left:7px;background-color:" + allSection[adSectionArray[adIndex]] + "\"><span class=hpSmallAdHeader>" + sectionLabel + "</span><span class=hpJumpTo>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Jump to:&nbsp;&gt;&nbsp;" + jumpTo[adSectionArray[adIndex]] + "</span></div>");
	
	setOpacity(getObj("image" + adIndex), 0);
	setOpacity(getObj("image2nd" + adIndex), 0);
	setOpacity(getObj("image3rd" + adIndex), 0);

	getObj("ad" + adIndex).style.visibility = "hidden";
	getObj("ad" + adIndex).style.zIndex = 1;
}

function parseNav(adImagePathArray2, adCaptionArray, adIndex)
{
	document.write("<div align=\"right\" style=\"position:relative;\"><font size=1 face=arial style=\"color: #b1b4b5\">");
	var selectedIndex = 0;
	for (j = 0; j < adImagePathArray2.length; j++)
	{
		selectedIndex = j + 1;

		if (j == adIndex)
			document.write("<a style=\"color: #717475\"");
		else
			document.write("<a style=\"color: #b1b4b5\"");
		document.write("href=\"#\" onclick=\"runEffect('" + selectedIndex + "'); return false;\" onmouseover=\"createTT('" + adCaptionArray[j].replace(/\'/g, "\\\'") + "', '#FFFFE7', 200); return false;\" onmouseout=\"hideTT(); return false;\">" + selectedIndex + "</a> | ");
	}
	document.writeln("</font><a href=\"#\" onclick=\"runEffect('prev'); return false;\" onmouseover=\"createTT('" + adCaptionArray[getPrev(adIndex,adImagePathArray2.length)].replace(/\'/g, "\\\'") + "', '#FFFFE7', 200); return false;\" onmouseout=\"hideTT(); return false;\"><img src=\"\/images\/home\/main\/arrow_prev_gray.gif\" width=\"8\" height=\"9\" border=\"0\" align=\"absbottom\"></a><img src=\"\/images\/spacer.gif\" width=\"6\" height=\"1\"><a href=\"#\" onclick=\"runEffect('next'); return false;\" onmouseover=\"createTT('" + adCaptionArray[getNext(adIndex,adImagePathArray2.length)].replace(/\'/g, "\\\'") + "', '#FFFFE7', 200); return false;\" onmouseout=\"hideTT(); return false;\"><img src=\"\/images\/home\/main\/arrow_next_gray.gif\" width=\"8\" height=\"9\" border=\"0\" align=\"absbottom\"></a></div>");
}

function setOpacity(obj, opacity)
{
	if (obj != null)
	{
		opacity = (opacity == 100)?99.999:opacity;
 
		// IE/Win
		obj.style.filter = "alpha(opacity:"+opacity+")";
 
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;
 
		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;
 
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	}
}

function fadeOut(objId,opacity)
{
	imgObj = getObj("image" + objId);
	img2ndObj = getObj("image2nd" + objId);
	img3rdObj = getObj("image3rd" + objId);
	adObj = getObj("ad" + objId);
	if (imgObj != null && img2ndObj != null && img3rdObj != null && adObj != null )
	{
		if (opacity > -1)
		{
			setOpacity(imgObj, opacity);
			setOpacity(img2ndObj, opacity);
			setOpacity(img3rdObj, opacity);
			
			opacity -= 10;
				
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
		}
		else
		{
			adObj.style.visibility = "hidden";
			adObj.style.zIndex = 1;
		}
	}
}

function fadeIn(objId,opacity)
{
	imgObj = getObj("image" + objId);
	img2ndObj = getObj("image2nd" + objId);
	img3rdObj = getObj("image3rd" + objId);
	adObj = getObj("ad" + objId);
	if (imgObj != null && img2ndObj != null && img3rdObj != null && adObj != null )
	{
		if (opacity <= 99.999)
		{
			adObj.style.visibility = "visible";
			adObj.style.zIndex = 2;
			setOpacity(imgObj, opacity);
			setOpacity(img2ndObj, opacity);
			setOpacity(img3rdObj, opacity);

			opacity += 10;
				
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
	}
}

function getPrev(whichAd, adLength)
{
	var previousAd = whichAd - 1;
	if (previousAd <= -1)
		previousAd = adLength - 1;
	return previousAd;
}

function getNext(whichAd, adLength)
{
	var nextAd = (whichAd + 1) % adLength;
	return nextAd;
}

function changeBGcolor(section,sectionID,changeToColor)
{
	if(document.all){ // browser="IE";        
		eval('document.all.'+section+'.style.backgroundColor = changeToColor;');
		eval('document.all.'+section+'.style.backgroundImage = "url(/images/home/corner' + sectionID + '.gif)";');
	}       
	if(!document.all && document.getElementById){ // browser="NN6+ or IE5+";          
		document.getElementById(section).style.backgroundColor = changeToColor;
		document.getElementById(section).style.backgroundImage = 'url(/images/home/corner' + sectionID + '.gif)';
	}
}
//function changeCurrentSectionBGcolor(divID,changeSection)
//{  
	//for (i = 1; i < allSection.length; i++)
	//{
		//if (changeSection==i){//Reset all leftmenu background to default
			//eval("changeBGcolor('"+divID+i+"',i,allSection[changeSection]);");
		//}
		//else{//Change bgcolor of the corresponding left menu item to match the ad's section type
			//eval("changeBGcolor('"+divID+i+"',0,allSection[0]);");
		//}
	//}
//}

function runEffect(whichAd)
{
	if (counter++ == 0)
	{
		setOpacity(getObj("image" + currentAd), 100);
		setOpacity(getObj("image2nd" + currentAd), 100);
		setOpacity(getObj("image3rd" + currentAd), 100);
		getObj("ad" + currentAd).style.visibility = "visible";
		getObj("ad" + currentAd).style.zIndex = 2;
		// next loop after a preset period
		currentTimeOutID = setTimeout("runEffect()", adDuration);
		return;
	}

	var previousAd = getPrev(currentAd, imageArray.length);
	var nextAd = getNext(currentAd, imageArray.length);
	var currDispAd = currentAd;
	
	// find out the next one
	if (!isNaN(parseInt(whichAd)))
		currentAd = parseInt(whichAd) - 1;
	else if (whichAd == "prev")
		currentAd = previousAd;
	else
		currentAd = nextAd;

	if (currDispAd != currentAd)
	{
		// hide jumpto bar and top section bar upfront to circumvent the delay issue on the first item in the array
		adCurrDispAdObj = getObj("jumpto" + currDispAd);
		adCurrDispAdObj.style.visibility = "hidden";
		adCurrentAdObj = getObj("jumpto" + currentAd);
		adCurrentAdObj.style.visibility = "visible";

		fadeOut(currDispAd, 99.999);
		fadeIn(currentAd, 0);
		
		// change section color
		//changeCurrentSectionBGcolor(leftMenuName,adSection[currentAd]);
	}

	// clear previously set time out if previous and next buttons is clicked
	if ((!isNaN(parseInt(whichAd))) || whichAd == "prev" || whichAd == "next")
		clearTimeout(currentTimeOutID);
	
	// next loop after a preset period
	currentTimeOutID = setTimeout("runEffect()", adDuration);
}

function callEffect(whichAd)
{
	// next loop after a preset period
		callEffectTimeOutID = setTimeout("callRunEffect('" + whichAd + "')", 300);
}

function callRunEffect(whichAd)
{
	eval("runEffect('" + whichAd + "')");
	stopEffect();
}

function cancelEffect(whichAd)
{
	clearTimeout(callEffectTimeOutID);	
}

function stopEffect()
{
	clearTimeout(currentTimeOutID);
}

function startEffect()
{
	clearTimeout(currentTimeOutID);
	currentTimeOutID = setTimeout("runEffect()", adDuration);
}

function filterEffect(firstAd, adSectionArray, adLinkArray, adImagePathArray, adCaptionArray, adLink2Array, adImagePath2Array, adCaption2Array, adWidth, adHeight, showNav)
{
	// generate random number
	//var randomChoice = Math.floor(Math.random() * adLinkArray.length);
	//var randomChoice = 0;

	currentAd = firstAd;
	document.writeln("<div id=\"mainAd\" style=\"width:" + adWidth + "px;height:" + adHeight + "px\" onmouseover=\"stopEffect();\" onmouseout=\"startEffect(" + adDuration + ")\">");

	for (i = 0; i < adImagePathArray.length; i++)
	{

		document.write("<div id=\"ad" + i + "\" style=\"position:absolute; display:block; width:" + adWidth + "px;height:" + adHeight + "px");
		if (i == firstAd)
			document.write(" visibility: visible\">");
		else
			document.write(" visibility: hidden\">");

		parseLinkImage(adSectionArray, adLinkArray, adImagePathArray, adCaptionArray, adLink2Array, adImagePath2Array, adCaption2Array, i);
		
		if (showNav != null)
			parseNav(adImagePathArray, adCaptionArray, i);

		document.writeln("</div>");
	}

	document.writeln("</div>");
	
	adArray = new Array(adImagePathArray.length);
	imageArray = new Array(adImagePathArray.length);

	for (i = 0; i < adImagePathArray.length; i++)
	{
		adArray[i]	= getObj("ad" + i).style;
		imageArray[i]	= getObj("image" + i);
	}

	mainAd = getObj("mainAd");
	
	//set left menu bgcolor to match the type of first ad
	//changeCurrentSectionBGcolor(leftMenuName,adSection[currentAd]);
	
	runEffect();
}

function isIE5plus()
{
	var browser = navigator.userAgent;
	var startPos = browser.indexOf("MSIE");
	if (startPos < 0)
		return false;

	var IEversion = parseInt(browser.substring(startPos+5, browser.indexOf(".", startPos)));
	
	if (IEversion < 5)
		return false;
	else
		return true;
}

function isFirefox()
{
	var browser = navigator.userAgent;
	var startPos = browser.indexOf("Firefox");
	if (startPos < 0)
		return false;
	else
		return true;
}

function isNetscape6or7()
{
	var browser = navigator.userAgent;
	var startPosNS6 = browser.indexOf("Netscape6");
	var startPosNS7 = browser.indexOf("Netscape/7");
	
	if ((startPosNS6 >= 0) || (startPosNS7 >= 0))
		return true;
	else
		return false;
}

function getObj(name)
{
  if (document.getElementById)
    return document.getElementById(name);
  else if (document.all)
    return document.all[name];
  else if (document.layers)
    return document.layers[name];
}

function ietruebody()
{
  return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}
