var adArray = null;
var imageArray = null;
var currentAd = 0;
var mainAd = null;
var counter = 0;
var currentTimeOutID = 0;

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)
{
	var sectionLabel = "";
	if (adSectionArray[adIndex] == "1")
		sectionLabel = "";
	else if (adSectionArray[adIndex] == "2")
		sectionLabel = "";
	else if (adSectionArray[adIndex] == "3")
		sectionLabel = "";
	else if (adSectionArray[adIndex] == "4")
		sectionLabel = "";
	
	document.writeln("<table cellspacing=0 cellpadding=0 width=596><tr><td width=400 valign=top>");
	
	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.writeln("<div><img src=\"" + adImagePathArray[adIndex] + "\" alt=\"" + adCaptionArray[adIndex] + "\" border=0 ID=\"image" + adIndex + "\" width=400 height=185></div>");
	
	document.write("</td><td valign=top width=197>");
	
	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("<div><img src=\"" + adImagePath2Array[adIndex] + "\" alt=\"" + adCaption2Array[adIndex] + "\" border=0 ID=\"image2nd" + adIndex + "\" width=196 height=185></div>");
	document.writeln("</td></tr></table>");
	
	//small ad area
	document.write("<div><table width=596 ><tr width=100%>");
	for (var i = 1; i <= 4; i++){
		//document.write("<script>alert(smallAdImageMapping[" + i+ "])</script>");
		document.write("<td width='25%'><a href='"+smallAdImageMapping[i]+"' border='0'><img src=\"" + smallAdImagePath[i] + "\" border=0 ID=\"image3rd" + adIndex + "\" width=\"149\" height=\"34\" usemap=\"#smallAd" + i + "\"></a></td>");
	}
	document.write("</tr></table></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=hpJumpTo>&nbsp;&nbsp;" + jumpTo[adSectionArray[adIndex]] + "</span></div>");
	
	setOpacity(getObj("image" + adIndex), 0);
	setOpacity(getObj("image2nd" + 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: #8d9497\"");
		else
			document.write("<a style=\"color: #8d9497\"");
		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;
		obj.style.filter = "alpha(opacity:"+opacity+")";
		obj.style.KHTMLOpacity = opacity/100;
		obj.style.MozOpacity = opacity/100;
		obj.style.opacity = opacity/100;
	}
}

function fadeOut(objId,opacity)
{
	imgObj = getObj("image" + objId);
	img2ndObj = getObj("image2nd" + objId);
	adObj = getObj("ad" + objId);
	if (imgObj != null && img2ndObj != null && adObj != null )
	{
		if (opacity > -1)
		{
			setOpacity(imgObj, opacity);
			setOpacity(img2ndObj, 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);
	adObj = getObj("ad" + objId);
	if (imgObj != null && img2ndObj != null && adObj != null )
	{
		if (opacity <= 99.999)
		{
			adObj.style.visibility = "visible";
			adObj.style.zIndex = 2;
			setOpacity(imgObj, opacity);
			setOpacity(img2ndObj, 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){    
		eval('document.all.'+section+'.style.backgroundColor = changeToColor;');
		eval('document.all.'+section+'.style.backgroundImage = "url(../images/cornerb' + sectionID + '.gif)";');
	}       
	if(!document.all && document.getElementById){
		document.getElementById(section).style.backgroundColor = changeToColor;
		document.getElementById(section).style.backgroundImage = 'url(../images/cornerb' + sectionID + '.gif)';
	}
}
function changeCurrentSectionBGcolor(divID,changeSection)
{  
	for (i = 1; i < allSection.length; i++)
	{
		if (changeSection==i){
			eval("changeBGcolor('"+divID+i+"',i,allSection[changeSection]);");
		}
		else{
			eval("changeBGcolor('"+divID+i+"',0,allSection[0]);");
		}
	}
}

function runEffect(whichAd)
{
	if (counter++ == 0)
	{
		setOpacity(getObj("image" + currentAd), 100);
		setOpacity(getObj("image2nd" + currentAd), 100);
		getObj("ad" + currentAd).style.visibility = "visible";
		getObj("ad" + currentAd).style.zIndex = 2;
		currentTimeOutID = setTimeout("runEffect()", adDuration);
		return;
	}

	var previousAd = getPrev(currentAd, imageArray.length);
	var nextAd = getNext(currentAd, imageArray.length);
	var currDispAd = currentAd;
	
	if (!isNaN(parseInt(whichAd)))
		currentAd = parseInt(whichAd) - 1;
	else if (whichAd == "prev")
		currentAd = previousAd;
	else
		currentAd = nextAd;

	if (currDispAd != currentAd)
	{
		adCurrDispAdObj = getObj("jumpto" + currDispAd);
		adCurrDispAdObj.style.visibility = "hidden";
		adCurrentAdObj = getObj("jumpto" + currentAd);
		adCurrentAdObj.style.visibility = "visible";
		
		fadeOut(currDispAd, 99.999);
		fadeIn(currentAd, 0);
		changeCurrentSectionBGcolor(leftMenuName,adSection[currentAd]);
	}
	if ((!isNaN(parseInt(whichAd))) || whichAd == "prev" || whichAd == "next")
		clearTimeout(currentTimeOutID);
	currentTimeOutID = setTimeout("runEffect()", adDuration);
}

function callEffect(whichAd)
{
		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 setArrayValue(adSectionArray){
	for (var i = 1; i <= 4; i++){
		smallAdImagePath[i] = allsmallAdImagePath[(adSectionArray -1) * 4 + i];
		smallAdImageMapping[i] = allsmallAdImageMapping[(adSectionArray -1) * 4 + i];
		//jumpTo[i] = alljumpTo[(adSectionArray -1) * 4 + i];
	}
}
function filterEffect(firstAd, adSectionArray, adLinkArray, adImagePathArray, adCaptionArray, adLink2Array, adImagePath2Array, adCaption2Array, adWidth, adHeight, showNav)
{
	//
	currentAd = firstAd;
	document.writeln("<div id=\"mainAd\" style=\"width:" + adWidth + "px;height:" + adHeight + "px\" onmouseover=\"stopEffect();\" onmouseout=\"startEffect(" + adDuration + ")\" align='left'>");

	for (i = 0; i < adImagePathArray.length; i++)
	{
		
		setArrayValue(adSectionArray[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");
	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;
}