var randomBannerIndex = null; // this variables useful for the
var the_items = new Array();
var the_times = new Array();
var the_itmes_opacity = new Array();
var the_times_opacity = new Array();
/**

 */
function outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{
	var str = "";
	
	var bannerArrLength = bannerArr.length;
	
	if (bannerArrLength > 0)
	{
		str += "<table " + tableAttributes + ">";
	
		var i = 1;
		
		for (indexKey in bannerArr)
		{
			switch (bannerArr[indexKey][0])
			{
				case "1":
					// hinh gif
					str += "<tr>";
					str += "<td " + tdAttributes + ">";
					
					if (bannerArr[indexKey][4] != "")
					{
						str += "<a href='" + bannerArr[indexKey][4] + "' target='" + bannerArr[indexKey][5] + "'><img src='" + bannerArr[indexKey][3] + "' border='0' alt='" + bannerArr[indexKey][6] + "'></a>";
					}
					else
					{
						str += "<img src='" + bannerArr[indexKey][3] + "' border='0' alt='" + bannerArr[indexKey][6] + "'>";
					}
					
					str += "</td>";
					str += "</tr>";
					
					break;
				case "2":
					// hinh flash
					str += "<tr>";
					str += "<td " + tdAttributes + ">";
					str += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"  width="' + bannerArr[indexKey][1] + '" height="' + bannerArr[indexKey][2] + '"><param name="movie" value="' + bannerArr[indexKey][3] + '?linkf=' + bannerArr[indexKey][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[indexKey][3] + '?linkf=' + bannerArr[indexKey][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[indexKey][1] + '" height="' + bannerArr[indexKey][2] + '"></embed></object>';
					str += "</td>";
					str += "</tr>";
					
					break;
			}
			
			if ((tdSpaceAttributes != "") && (i < bannerArrLength))
			{
				str += "<tr>";
				str += "<td " + tdSpaceAttributes + ">";
				str += "</td>";
				str += "</tr>";
			}
			
			i++;
		}
		
		str += "</table>";
	}
	
	return str;
}

/**

 */
function makeADivObject(divName)
{
	document.write('<div id="' + divName + '"></div>');
}

/**

 */
function doSlideBanner(bannerArr, times)
{
	this.globalBanner = "";
	
	if(bannerArr.length > 0)
	{
		for(i=0; i < bannerArr.length-1; i++)
		{	
			this.globalBanner += bannerArr[i] + ",";
		}
		this.globalBanner += bannerArr[i];
	}
	
	this.theDivObjectName = "slide" + Math.round(Math.random() * Math.pow(10,Math.round(Math.random()*10)));
	
	the_times[theDivObjectName] = 0;
	
	if(!times)
		times = 10000;
	else
	{
		if(isNaN(times))
			times = 10000;
	}
	
	makeADivObject(this.theDivObjectName);
		
	doSlideBannerA1(this.globalBanner, this.theDivObjectName, times);
}

/**

*/



function doSlideBannerA1(bannerArr, theDivObjectName, times)
{
	if(the_items[theDivObjectName] != null)
	{
		window.clearInterval(the_items[theDivObjectName]);
		the_times[theDivObjectName] ++;
	}
		
	if(bannerArr.length != 0)
	{
		var arrTemp = new Array();
		arr 	= bannerArr.split(",");
		numArr 	= arr.length / 8;
		index	= 0;
		
		for(i = 0; i < numArr; i++)
		{
			arrTemp[i] = new Array();
			for(j = 0; j < 8; j++)
			{
				arrTemp[i][j] = arr[index];
				index++;
			}
		}
			
		var bannerArrLength = arrTemp.length;
		randomBannerIndex 	= the_times[theDivObjectName] % bannerArrLength;
		
		if(the_times[theDivObjectName] == bannerArr.length)
			the_times[theDivObjectName] = 0;
		
		slideBanner(arrTemp, theDivObjectName, randomBannerIndex); //duoc
	}
	
	the_items[theDivObjectName] = window.setInterval('doSlideBannerA1(\''+ bannerArr +'\',\''+ theDivObjectName  + '\',' + times + ')', times);
}

/**

*/
function slideBanner(bannerArr, theDivObjectName, bannerIndex)
{	

	if(!bannerIndex)
		randomIndex = 0;
	else
		var randomIndex = bannerIndex;
	
	var theOutputString = "";
	//theOutputString += "<table " + tableAttributes + ">";	
	//theOutputString += "<tr>";
	//theOutputString += "<td " + tdAttributes + ">";
	
	switch (bannerArr[randomIndex][0])
	{
		case "99":
			// hinh gif
			var IdImg = getName(bannerArr[randomIndex][3]);
			
			if (bannerArr[randomIndex][4] != "")
			{
				theOutputString += "<a href='" + bannerArr[randomIndex][4] + "' target='" + bannerArr[randomIndex][5] + "'><img id='"+IdImg+"' src='" + bannerArr[randomIndex][3] + "' border='0' alt='" + bannerArr[randomIndex][6] + "' style='filter:alpha(opacity=100)'></a>";
			}//end if
			else
			{
				theOutputString += "<img id='"+IdImg+"' src='" + bannerArr[randomIndex][3] + "' border='0' alt='" + bannerArr[randomIndex][6] + "' style='filter:alpha(opacity=100)'>";
			}//end else
			
			break;
		case "103":
			// hinh flash
			theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[randomIndex][1] + '" height="' + bannerArr[randomIndex][2] + '"><param name="movie" value="' + bannerArr[randomIndex][3] + '?clicktag=' + bannerArr[randomIndex][4] + '"><param name="quality" value="high"><param name="wmode" value="opaque"><embed src="' + bannerArr[randomIndex][3] + '?clicktag=' + bannerArr[randomIndex][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[randomIndex][1] + '" height="' + bannerArr[randomIndex][2] + '"></embed></object>';
		
			break;
	}//end switch
	
	//theOutputString += "</td>";
	//theOutputString += "</tr>";
	//theOutputString += "</table>";
	//alert(theOutputString);
	var theDivObject = document.getElementById(theDivObjectName);
	
	theDivObject.innerHTML = theOutputString;
	
	/*if(bannerArr[randomIndex][0] == "99")
	{
		//objOpacity = document.getElementById(IdImg);
		//the_times_opacity[IdImg] = 0;
		//the_itmes_opacity[IdImg] = window.setInterval("setOpacity('"+IdImg+"')", 100);
	}*/
}

function setOpacity(objId)
{
	if(objId)
	{
		if(the_times_opacity[objId] < 100)
		{
			objOpacity = document.getElementById(objId);
			the_times_opacity[objId] += 10;
			objOpacity.style.filter = 'alpha(opacity='+the_times_opacity[objId]+')';	
			objOpacity.style.opacity = the_times_opacity[objId]/100;
		}
		else
		{
			the_times_opacity[objId] = 50;
			window.clearInterval(the_itmes_opacity[objId]);
		}
	}
}

function getName(str)
{
	ten = str.split("/");
	return ten[ten.length-1];
}


function Random(N) 
{
	return Math.floor(N*(Math.random()%1)) 
}

function Deal(N) 
{
	var J, K, Q = new Array(N);
	for (J=0; J<N; J++) 
	{
		K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J;
	}
	
	return Q;
}

//=================Banner QC khac====================
function BannerAD(bannerArr,tableAttributes, tdAttributes)
{
	//alert("so banner="+bannerArr.length);
	var bannerArrLength = bannerArr.length-1;
	
	var ranIn = new Array();
	ranIn = Deal(bannerArr.length);
	//alert("ran=" + ranIn);


	var theOutputString = "";
	
	if(bannerArr[0][7]==100)//TH random
	{		
//			theOutputString += "<table cellspacing='0' cellpadding='0'>";
			for(var i=0; i<=bannerArrLength; i++)
			{					
//				theOutputString += "<tr>";
//				theOutputString += "<td>";
//				theOutputString += "<table " + tableAttributes + ">";	
//				theOutputString += "<tr>";
//				theOutputString += "<td class='" + tdAttributes + "'>";

				switch (bannerArr[ranIn[i]][0])
				{
					case "99":
						//hinh gif
						if (bannerArr[ranIn[i]][4] != "")
						{
							theOutputString += "<a href='" + bannerArr[ranIn[i]][4] + "' target='" + bannerArr[ranIn[i]][5] + "'><img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'></a>";
						}
						else
						{
							theOutputString += "<img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'>";	
						}
						
						break;
					case "103":
						// hinh flash
						theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"><param name="movie" value="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"></embed></object>';
			
						break;
				}//end switch
//				theOutputString += "</td>";
//				theOutputString += "</tr>";
//				theOutputString += "</table>";
//				theOutputString += "</td>";
//				theOutputString += "</tr>";
			}//end for
			theOutputString += "</table>";
			//alert(theOutputString);
	}//end if
	else//norandom
	{		
//		theOutputString += "<table cellspacing='0' cellpadding='0'>";
		for(var i=0; i<=bannerArrLength; i++)
		{					
//			theOutputString += "<tr>";
//			theOutputString += "<td>";
//			theOutputString += "<table " + tableAttributes + ">";	
//			theOutputString += "<tr>";
	//		theOutputString += "<td class='" + tdAttributes + "'>";
			switch (bannerArr[i][0])
			{
				case "99":
					//hinh gif
					if (bannerArr[i][4] != "")
					{
						theOutputString += "<a href='" + bannerArr[i][4] + "' target='" + bannerArr[i][5] + "'><img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'></a>";
					}
					else
					{
						theOutputString += "<img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'>";	
					}
					
					break;
				case "103":
					// hinh flash
					theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '"><param name="movie" value="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '"></embed></object>';
		
					break;
			}//end switch
//			theOutputString += "		</td>";
//			theOutputString += "	</tr>";
//			theOutputString += "</table>";
//			theOutputString += "		</td>";
//			theOutputString += "	</tr>";
		}//end for
//		theOutputString += "</table>";
		//alert(theOutputString);	
			
	}//else//norandom

	document.write(theOutputString);
}

//=================Banner QC khac the DIV====================
function BannerADDIV(bannerArr)
{
	var bannerArrLength = bannerArr.length-1;
	
	var ranIn = new Array();
	ranIn = Deal(bannerArr.length);


	var theOutputString = '';
	
	if(bannerArr[0][7]==100)//TH random
	{		
			for(var i=0; i<=bannerArrLength; i++)
			{					

				switch (bannerArr[ranIn[i]][0])
				{
					case "99":
						//hinh gif
						if (bannerArr[ranIn[i]][4] != "")
						{
							theOutputString += "<a href='" + bannerArr[ranIn[i]][4] + "' target='" + bannerArr[ranIn[i]][5] + "'><img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'></a>";
						}
						else
						{
							theOutputString += "<img src='" + bannerArr[ranIn[i]][3] + "' border='0' alt='" + bannerArr[ranIn[i]][6] + "'>";	
						}
						
						break;
					case "103":
						// hinh flash
						theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"><param name="movie" value="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '"><param name="quality" value="high"><embed src="' + bannerArr[ranIn[i]][3] + '?clicktag=' + bannerArr[ranIn[i]][4] + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[ranIn[i]][1] + '" height="' + bannerArr[ranIn[i]][2] + '"></embed></object>';
			
						break;
				}//end switch
			}//end for
			//alert(theOutputString);
				
	}//end if(bannerArr[0][7]==1)//random
	
	if(bannerArr[0][7]!=100)//norandom
	{		
		for(var i=0; i<=bannerArrLength; i++)
		{					
			switch (bannerArr[i][0])
			{
				case "99":
					//hinh gif
					if (bannerArr[i][4] != "")
					{
						theOutputString += "<a href='" + bannerArr[i][4] + "' target='" + bannerArr[i][5] + "'><img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'></a>";
					}
					else
					{
						theOutputString += "<img src='" + bannerArr[i][3] + "' border='0' alt='" + bannerArr[i][6] + "'>";	
					}
					
					break;
				case "103":
					// hinh flash
					theOutputString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '" align="bottom"><param name="movie" value="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '"><param name="quality" value="high"><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="' + bannerArr[i][3] + '?clicktag=' + bannerArr[i][4] + '" align="bottom" quality="high" wmode="transparent" bgcolor="#ffffff" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + bannerArr[i][1] + '" height="' + bannerArr[i][2] + '"></embed></object>';
		
					break;
			}//end switch
		}//end for
		//alert(theOutputString);	
			
	}//else//norandom
	

	document.write(theOutputString);
}

/**

 */
function displayRandomBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{	
	bannerArr = randomArray(bannerArr);
	
	var outputedBanner = outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes);
	
	document.write(outputedBanner);
}

/**

 */
function displayBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes)
{	
	var outputedBanner = outputBanner(bannerArr, tableAttributes, tdAttributes, tdSpaceAttributes);
	
	document.write(outputedBanner);
}

/**

 */
function displayFloatBanner(floatBannerArr, handSide)
{
	str = "";
	
	if (floatBannerArr.length > 0)
	{
		switch (handSide)
		{
			case "left":
				str += '<div id="divAdLeft" style="left: -110px; width: 108px; position: absolute; top: 103px" align="right">';
				
				for (keyIndex in floatBannerArr)
				{
					if (floatBannerArr[keyIndex][4] != "")
					{
						str += "<a href='" + floatBannerArr[keyIndex][4] + "' target='" + floatBannerArr[keyIndex][5] + "'><img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'></a><br>";
					}
					else
					{
						str += "<img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'><br>";
					}
				}
				
				str += "</div>";
				break;
				
			case "right":
				str += '<div id=divAdRight style="right: -110px; width: 96px; position: absolute; top: 103px; padding-left: 3px;">';
				
				for (keyIndex in floatBannerArr)
				{
					if (floatBannerArr[keyIndex][4] != "")
					{
						str += "<a href='" + floatBannerArr[keyIndex][4] + "' target='" + floatBannerArr[keyIndex][5] + "'><img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'></a><br>";
					}
					else
					{
						str += "<img src='" + floatBannerArr[keyIndex][3] + "' border='0' alt='" + floatBannerArr[keyIndex][6] + "'><br>";
					}
				}
				
				str += "</div>";
				break;
		}
	}
	
	document.write(str);
	
	switch (handSide)
	{
		case "left":
			ShowAdDiv("divAdLeft", handSide);
			break;
		case "right":
			ShowAdDiv("divAdRight", handSide);
			break;
	}
}

/**

 */
function ShowAdDiv(theBannerDivID, handSide)
{
	var theBannerDivObject = document.getElementById(theBannerDivID);
	
	if (document.body.clientWidth < 980)
	{
		theBannerDivObject.style.left = -110;
	}
	else
	{
		switch (handSide)
		{
			case "left":
				theBannerDivObject.style.left = 0;
				
				FloatTopDiv2(theBannerDivID);
				break;
			case "right":
				theBannerDivObject.style.left = document.body.clientWidth - 110;
				
				FloatTopDiv(theBannerDivID);
				break;
		}
	}
}

/**

 */
function FloatTopDiv2(leftFloatBanner)
{
	startX2 = document.body.clientWidth - 1004, startY2 = 103;
	var ns2 = (navigator.appName.indexOf("Netscape") != -1);
	var d2 = document;
		
	if (document.body.clientWidth < 980) startX2 = -110;
	
	function ml2(id)
	{
		var el2=d2.getElementById?d2.getElementById(id):d2.all?d2.all[id]:d2.layers[id];
		
		if(d2.layers)el2.style=el2;
		
		el2.sP = function(x,y)
		{
			this.style.left=x;
			this.style.top=y;
		};
		
		el2.x = startX2;
		el2.y = startY2;
		
		return el2;
	}
	
	window.stayTopLeft2=function()
	{
		if (document.body.clientWidth < 980)
		{
			ftlObj2.x = - 115;ftlObj2.y = 0;	ftlObj2.sP(ftlObj2.x, ftlObj2.y);
		}
		else
		{
			if (document.documentElement && document.documentElement.scrollTop)
				var pY2 = ns2 ? pageYOffset : document.documentElement.scrollTop;
			else if (document.body)
				var pY2 = ns2 ? pageYOffset : document.body.scrollTop;

			if (document.body.scrollTop > 103){startY2 = 17} else {startY2 = 103};

			if (document.body.clientWidth >= 1024)
			{
				ftlObj2.x =  0;ftlObj2.y += (pY2 + startY2 - ftlObj2.y)/32;	ftlObj2.sP(ftlObj2.x, ftlObj2.y);
			}
			else
			{		
				ftlObj2.x  = startX2;
				ftlObj2.y += (pY2 + startY2 - ftlObj2.y)/32;
				ftlObj2.sP(ftlObj2.x, ftlObj2.y);
			}
		}
		
		setTimeout("stayTopLeft2()", 1);
	}
	
	ftlObj2 = ml2(leftFloatBanner);
	stayTopLeft2();		
}

/**

 */
function FloatTopDiv(rightFloatBanner)
{
	startX = document.body.clientWidth - 110, startY = 103;
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
		
	if (document.body.clientWidth < 980)
	{
		startX = -110;
	}
	
	function ml(id)
	{
		var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
		if (d.layers)
		{
			el.style=el;
		}
		
		el.sP = function(x,y)
		{
			this.style.left=x;
			this.style.top=y;
		};
		
		el.x = startX;
		el.y = startY;
		
		return el;
	}
	
	window.stayTopLeft=function()
	{		
		if (document.body.clientWidth < 980)
		{
			ftlObj.x = - 115;ftlObj.y = 0;	ftlObj.sP(ftlObj.x, ftlObj.y);
		}			
		else
		{
			if (document.documentElement && document.documentElement.scrollTop)
				var pY = ns ? pageYOffset : document.documentElement.scrollTop;
			else if (document.body)
				var pY = ns ? pageYOffset : document.body.scrollTop;

			if (document.body.scrollTop > 103){startY = 17} else {startY = 103};

			if (document.body.clientWidth >= 1024)
			{
				ftlObj.x = document.body.clientWidth - 110;ftlObj.y += (pY + startY - ftlObj.y)/32;ftlObj.sP(ftlObj.x, ftlObj.y);
			}
			else
			{			
				ftlObj.x  = startX;
				ftlObj.y += (pY + startY - ftlObj.y)/32;
				ftlObj.sP(ftlObj.x, ftlObj.y);
			}
		}
		
		setTimeout("stayTopLeft()", 1);
	}
	
	ftlObj = ml(rightFloatBanner);
	
	stayTopLeft();
}

function getRandom(theNumber)
{
	return Math.round(Math.random() * theNumber);
}

function createNumRand(num) {
	rate = 1;
	while(num > 1) {
		rate *= 10;
		num /= 10;
	}
	num *= rate;
	return (Math.floor(Math.random()*rate)%num);
}