
// 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 ie=document.all;
var ns6=document.getElementById && !document.all;
var ttEnabled=false;

function parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, adIndex)
{
	if (adLinkArray[adIndex] != null)
		document.writeln("<div><a href=\"" + adLinkArray[adIndex] + "\"><img src=\"" + adImagePathArray[adIndex] + "\" alt=\"" + adCaptionArray[adIndex] + "\" border=0 ID=\"image" + adIndex + "\"></a></div>");
	else
		document.writeln("<div><img src=\"" + adImagePathArray[adIndex] + "\" ID=\"image" + adIndex + "\"></div>");

	setOpacity(getObj("image" + adIndex), 0);
	getObj("ad" + adIndex).style.visibility = "hidden";
	getObj("ad" + adIndex).style.zIndex = 1;
}

function parseNav(adImagePathArray2, adCaptionArray, adIndex)
{
//	document.write("<div align=\"left\" style=\"position:relative;\"><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=\"\/pictures\/arrow_prev_gray.gif\" width=\"8\" height=\"9\" border=\"0\" align=\"absbottom\"></a><img src=\"\/pictures\/spacer.gif\" width=\"6\" height=\"1\"><font size=\"1\" face=arial style=\"color: #b1b4b5\"> ");
	document.write("<div align=\"left\" style=\"position:relative;\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><img src=\"HomePageSlideshow\/BeforeNumbers.jpg\" border=\"0\" height=\"17\">");
	var selectedIndex = 0;
	for (j = 0; j < adImagePathArray2.length; j++)
	{
		selectedIndex = j + 1;

		if (j == adIndex)
			//document.write("<a style=\"color: Yellow\"");
			document.write("<td><a ");
		else
			//document.write("<a style=\"color: Blue\"");
			document.write("<td><a ");
		//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.write("href=\"#\" onclick=\"runEffect('" + selectedIndex + "'); return false;\" onmouseover=\"createTT('" + adCaptionArray[j].replace(/\'/g, "\\\'") + "', '#FFFFE7', 200); return false;\" onmouseout=\"hideTT(); return false;\"><img src=\"HomePageSlideshow\/" + selectedIndex + ".jpg\" border=\"0\"></a></td>");
	}
	//document.writeln("</font><img src=\"\/pictures\/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=\"\/pictures\/arrow_next_gray.gif\" width=\"8\" height=\"9\" border=\"0\" align=\"absbottom\"></a></div>");
	document.writeln("<td><img src=\"HomePageSlideshow\/AfterNumbers.jpg\" border=\"0\" height=\"17\"></td></td></table></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);
	adObj = getObj("ad" + objId);
	if (imgObj != null && adObj != null )
	{
		if (opacity > -1)
		{
			setOpacity(imgObj, 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);
	adObj = getObj("ad" + objId);
	if (imgObj != null && adObj != null )
	{
		if (opacity <= 99.999)
		{
			adObj.style.visibility = "visible";
			adObj.style.zIndex = 2;
			setOpacity(imgObj, 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 runEffect(whichAd)
{
	if (counter++ == 0)
	{
		setOpacity(getObj("image" + 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)
	{
		fadeOut(currDispAd, 99.999);
		fadeIn(currentAd, 0);
	}
	// 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 filterEffect(adLinkArray, adImagePathArray, adCaptionArray, adWidth, adHeight, showNav)
{
	// generate random number
	var randomChoice = Math.floor(Math.random() * adLinkArray.length);
	//var randomChoice = 0;
	
	// show effect and list only for IE5(PC) & Firefox
	if ( (isIE5plus() || isFirefox() || isNetscape6or7()) && (adImagePathArray.length > 1) )
	{
		currentAd = randomChoice;
		document.writeln("<div id=\"tt_div\" style=\"position: absolute; width: 200px; visibility: hidden; z-index: 100;\"><table id=\"tt_table\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"border-collapse: collapse; border: 1px solid black;\"><tr><td><div id=\"tt_text\" style=\"font-size: 8pt; font-face: arial;\"></div></td></tr></table></div>");
		document.writeln("<div id=\"mainAd\" style=\"width:" + adWidth + "px;height:" + adHeight + "px\">");
	
		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 == randomChoice)
				document.write(" visibility: visible\">");
			else
				document.write(" visibility: hidden\">");
	
			parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, 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");
		ttObj = getObj("tt_div");
		ttObjTable = getObj("tt_table");
		ttObjText = getObj("tt_text");
		
		runEffect();
	}
	else
	{
		parseLinkImage(adLinkArray, adImagePathArray, adCaptionArray, randomChoice);
		return;
	}
	
}

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;
}

function createTT(sText, sColor, nWidth)
{
  if (ns6||ie)
  {
    if (typeof nWidth != "undefined")
      ttObj.style.width = nWidth+"px";
    else
      ttObj.style.width = "200px";

    if (typeof sColor != "undefined" && sColor != "")
      ttObjTable.style.backgroundColor = sColor;
    else
      ttObjTable.style.backgroundColor ="#FFFFFF";

    ttObjText.innerHTML=sText;
    ttEnabled=true;
    return false;
  }
}

function positionTT(e)
{
  if (ttEnabled)
  {
    var curX = (ns6)?e.pageX:event.clientX;
    var curY = (ns6)?e.pageY-window.pageYOffset:event.clientY;
    var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-ttOffsetXPoint : window.innerWidth-e.clientX-ttOffsetXPoint-20;
    var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-ttOffsetYPoint : window.innerHeight-e.clientY-ttOffsetYPoint-20;
    var leftedge=(ttOffsetXPoint<0)? ttOffsetXPoint*(-1) : -1000;

    if (rightedge<ttObjTable.offsetWidth) ttObj.style.left=ie? ietruebody().scrollLeft+event.clientX-ttObjTable.offsetWidth-10+"px" : window.pageXOffset+e.clientX-ttObjTable.offsetWidth-10+"px";
    else if (curX<leftedge) ttObj.style.left="5px";
    else ttObj.style.left=curX+ttOffsetXPoint+"px";

    if (bottomedge<ttObjTable.offsetHeight) ttObj.style.top=ie? ietruebody().scrollTop+event.clientY-ttObjTable.offsetHeight-ttOffsetYPoint+"px" : window.pageYOffset+e.clientY-ttObjTable.offsetHeight-ttOffsetYPoint+"px";
    else ttObj.style.top=curY+ttOffsetYPoint+"px";

    ttObj.style.visibility="visible";
  }
}

function hideTT()
{
  if (ns6||ie)
  {
    ttEnabled=false;
    ttObj.style.visibility="hidden";
    ttObj.style.left="-1000px";
    ttObj.style.backgroundColor='';
    ttObj.style.width='';
  }
}

document.onmousemove=positionTT;
