// Write by Pizazz ( Liu Shuai ) on November 10, 2007
var flag=true;
var currentAdInex=0;
var currentTimeOutID=0;

var adImageArray=null;
var adLinkArray=null;
var adCaptionArray=null;

function playFilterEffect(defaultAdIndex)
{
	if(adImageArray.length>1)
	{
		currentAdIndex=defaultAdIndex;
		showAd(currentAdIndex);
		runEffect();
	}
}

function showAd(currentAdIndex)
{
	document.write("<table cellspacing=0 cellpadding=0 width=\"100%\"><tr><td width=\""+adWidth+"\"><div id=\"mainad\" style=\"width:"+adWidth+"px;height:"+adHeight+"px\">");
	if(isIE5plus())
	{
		document.writeln("<div id=\"adimg\" style=\"position:absolute;display:block;visibility:visible\"><a href=\""+adLinkArray[currentAdIndex]+"\" onmouseover=\"stopEffect();\" onmouseout=\"startEffect();\" target=\"_blank\"><img src=\""+adImageArray[currentAdIndex]+"\" title=\""+adCaptionArray[currentAdIndex]+"\" border=0 id=\"image"+currentAdIndex+"\"></a></div>");
	}
	else
	{
		for(i=0;i<adImageArray.length;i++)
		{
			document.write("<div id=\"ad"+i+"\" style=\"position:absolute;display:block;");
			if(i==currentAdIndex)
				document.write("visibility:visible\">");
			else
				document.write("visibility:hidden\">");
			document.writeln("<a href=\""+adLinkArray[i]+"\" onmouseover=\"stopEffect();\" onmouseout=\"startEffect();\" target=\"_blank\"><img src=\""+adImageArray[i]+"\" title=\""+adCaptionArray[i]+"\" border=0 id=\"image"+i+"\"></a></div>");
			setOpacity(getObj("image"+i),0);
		}
	}
	document.write("</div>");
	showNumberNav(currentAdIndex);
	document.write("</td></tr></table>");
}


function showNumberNav(currentAdIndex)
{
	document.writeln("<div id=\"numnav\" align=\"right\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr align=\"center\">");
	for(i=0;i<adImageArray.length;i++)
	{
		document.write("<td");
		if(i<adImageArray.length-1)
			document.write(" style=\"border-right:0\"");
		document.write(" onclick=\"runEffect('"+(i+1)+"');return false;\"><img id=\"navimg"+i+"\" src=\"images\/flash\/numnav\/");
		if(i!=currentAdIndex)
			document.write(i+1);
		else
			document.write((i+1)+""+(i+1));
		document.write(".gif\">");
	}
	document.writeln("</td></tr></table></div>");
}

function runEffect(AdIndex)
{
	if(flag)
	{
		flag=false;
		setOpacity(getObj("image"+currentAdIndex),100);
		currentTimeOutID=setTimeout("runEffect()",5000);
		return;
	}
	var nextAdIndex=getNext(currentAdIndex,adImageArray.length);
	var runAdIndex=currentAdIndex;
	if(!isNaN(parseInt(AdIndex)))
		currentAdIndex=parseInt(AdIndex)-1;
	else
		currentAdIndex=nextAdIndex;
	if(currentAdIndex!=runAdIndex)
	{
		if(isIE5plus())
		{
			//var vt=Math.floor(Math.random()*23);
			adObj = getObj("adimg");
			//adObj.style.filter="revealTrans(duration=1.0,transition=6) blendTrans(duration=1.5)";
			adObj.style.filter="blendTrans(duration=1.5)";
			adObj.filters(0).apply();
			//adObj.filters(1).apply();
			
			adObj.innerHTML="<a href=\""+adLinkArray[currentAdIndex]+"\" onmouseover=\"stopEffect();\" onmouseout=\"startEffect();\" target=\"_blank\"><img src=\""+adImageArray[currentAdIndex]+"\" title=\""+adCaptionArray[currentAdIndex]+"\" border=0 id=\"image"+currentAdIndex+"\"></a>";
			
			adObj.filters(0).play();
			//adObj.filters(1).play();
		}
		else
		{
			fadeOut(runAdIndex,99.999);
			fadeIn(currentAdIndex,0);
		}
		changeNav(currentAdIndex);
	}
	if(!isNaN(parseInt(currentAdIndex)))
		clearTimeout(currentTimeOutID);
	currentTimeOutID=setTimeout("runEffect()",adDuration);
}

function changeNav(currentAdIndex)
{
	for(i=0;i<adImageArray.length;i++)
	{
		if(i==currentAdIndex)
			document.images['navimg'+i].src="images\/flash\/numnav\/"+(i+1)+""+(i+1)+".gif";
		else
			//getObj("navbg"+i).innerHTML="<img src=\"img_ad\/"+(i+1)+".jpg\">";
			document.images['navimg'+i].src="images\/flash\/numnav\/"+(i+1)+".gif";
	}
}

function callEffect(AdIndex)
{
	callEffectTimeOutID = setTimeout("callRunEffect('" + AdIndex + "')", 300);
}

function callRunEffect(AdIndex)
{
	eval("runEffect('" + AdIndex + "')");
	stopEffect();
}

function cancelEffect(AdIndex)
{
	clearTimeout(callEffectTimeOutID);	
}

function stopEffect()
{
	clearTimeout(currentTimeOutID);
}

function startEffect()
{
	clearTimeout(currentTimeOutID);
	currentTimeOutID = setTimeout("runEffect()", 2000);
}

function getNext(currentAdIndex,adLength)
{
	var nextIndex = (currentAdIndex + 1) % adLength;
	return nextIndex;
}

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 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];
}
