var iMaxTS=5,iMaxTBS=3;
var iReadStep=0;
var sLoDID=',';
var iDelay=200;
var iDisplayCateNo=0;
var iTSTimer=0;
var iTSCount,iTSCur,iTSFading,iTSPrev=0,iTSNext=0,iTSFastMove=0;
var iFadeDelay=50;
var iBoxCount=0;


function getNodeValue(o){
	try	{
		return o.item(0).firstChild.nodeValue;
	}
	catch(err) {
		return '';
	}
}

function displayid(id,add){
	if (typeof(add)=='undefined') add=true;
	if (sLoDID.indexOf(id)<=0){
		if (add) sLoDID=sLoDID.concat(id).concat(',');
		return true;
	}
	else{
		return false;
	}
}
function iif(iEx1,r1,r2){
	if(iEx1)return r1;
	else return r2;
}


function showtopstoryitem( arItemTS ) {
	iTSCount = arItemTS.length-1;
	var sHTML = '';
	
	sHTML = sHTML.concat('<div style="position:relative;width:395px;height:264px;">');
	sHTML = sHTML.concat('	<div style="left:0px;top:0px;width:395px;height:3px;" class="HomeTopStoryBorder"></div>');
	sHTML = sHTML.concat('	<div style="left:0px;top:261px;width:395px;height:3px;" class="HomeTopStoryBorder"></div>');
	sHTML = sHTML.concat('	<div style="left:0px;top:3px;width:3px;height:258px;" class="HomeTopStoryBorder"></div>');
	sHTML = sHTML.concat('	<div style="left:392;top:3px;width:3px;height:258px;" class="HomeTopStoryBorder"></div>');
	
	for( var i=0; i <= iTSCount; i++ ) {
		sHTML = sHTML.concat('	<div id="divTopStory').concat(i).concat('" onmousemove="cleartopstorycounter();" style="overflow:hidden;position:absolute;').concat(iif(i==0,'left:0px','left:-1000px')).concat(';top:0px;width:395px;height:264px;">');
		sHTML = sHTML.concat('		<a href="').concat(arItemTS[i][1]).concat('"><img border="0" src="').concat(arItemTS[i][4]).concat('" width="395" height="264"></a>');
		sHTML = sHTML.concat('	</div>');
		sHTML = sHTML.concat('	<div id="divTopStoryLayer').concat(i).concat('" onmousemove="cleartopstorycounter();" style="').concat(iif(i==0,'left:0px','left:-1000px')).concat(';top:200px;width:395px;height:65px;" class="HomeTopStoryColorLayer"></div>');
		sHTML = sHTML.concat('	<div id="divTopStoryTitle').concat(i).concat('" onmousemove="cleartopstorycounter();" style="').concat(iif(i==0,'left:0px','left:-1000px')).concat(';top:200px;width:395px;height:65px;" class="HomeTopStoryColorLayerContent">');
		sHTML = sHTML.concat('		<div style="left:7px;top:4px;width:390px;height:18px;" class="HomeTopStoryTitle" onclick="window.location=\'').concat(arItemTS[i][1]).concat('\'">').concat(arItemTS[i][2]).concat('</div>');
		sHTML = sHTML.concat('		<div style="left:8px;top:5px;width:390px;height:18px;" class="HomeTopStoryTitleShadow">').concat(arItemTS[i][2]).concat('</div>');
		sHTML = sHTML.concat('		<div style="left:8px;top:28px;width:350px;height:18px;" class="HomeTopStoryLead">').concat(arItemTS[i][3]);
		sHTML = sHTML.concat('		</div>');
		sHTML = sHTML.concat('	</div>');
	}
	
	sHTML = sHTML.concat('<div style="left:365px;top:244px;width:40px;height:32px;" class="HomeTopStoryColorLayerSubImage">');
	sHTML = sHTML.concat('<img id="imgTSPrev" src="'+ boxImageUrl +'Prev2.gif" hspace="2" vspace="0" onmouseover="iTSPrev=1;this.src=\''+ boxImageUrl +'Prev.gif\'" onmouseout="iTSPrev=0;this.src=\''+ boxImageUrl +'Prev2.gif\'" style="cursor:pointer" onclick="changetopstory(-1,false);">');
	
	sHTML = sHTML.concat('<img id="imgTSNext" src="'+ boxImageUrl +'Next2.gif" hspace="2" vspace="0" onmouseover="iTSNext=1;this.src=\''+ boxImageUrl +'Next.gif\'" onmouseout="iTSNext=0;if (!iTSFading) this.src=\''+ boxImageUrl +'Next2.gif\'" style="cursor:pointer" onclick="changetopstory(1,false);">');
	
	sHTML = sHTML.concat('</div>');
	sHTML = sHTML.concat('</div>');
	
	//document.getElementById('tdTS').innerHTML=sHTML;
	document.getElementById('tdTS').innerHTML=sHTML;
	iTSCur=0;
	
	setInterval(function(){topstorycounter();},50);

}

function changetopstory(direction,fade){
	var iTop, iBot;
	var oTop, oBot, oTopLayer, oBotLayer, oTopTitle, oBotTitle;
	iTop=iTSCur;
	iBot=iTop+direction;
	if (iBot<0) iBot=iTSCount;
	if (iBot>iTSCount) iBot=0;
	oTop = 'divTopStory'+iTop;
	oBot = 'divTopStory'+iBot;
	oTopLayer = 'divTopStoryLayer'+iTop;
	oBotLayer = 'divTopStoryLayer'+iBot;
	oTopTitle = 'divTopStoryTitle'+iTop;
	oBotTitle = 'divTopStoryTitle'+iBot;
	if (iTSFading==1){
		document.getElementById('divTopStory'+(iTop+1)).style.left='-1000px';
		document.getElementById('divTopStoryLayer'+(iTop+1)).style.left='-1000px';
		document.getElementById('divTopStoryTitle'+(iTop+1)).style.left='-1000px';
		outdirectionimage();
		iTSFading=0;
	}
	iTSTimer=0;
	if (!fade){
		iTSFastMove=1;
		changeOpac(100,oBot);
		changeOpac(100,oBotLayer);
		changeOpac(100,oBotTitle);
		document.getElementById(oTop).style.zIndex=1;
		document.getElementById(oBot).style.zIndex=2;
		document.getElementById(oBot).style.left='0px';
		document.getElementById(oTop).style.left='-1000px';
		document.getElementById(oBotLayer).style.left='0px';
		document.getElementById(oTopLayer).style.left='-1000px';
		document.getElementById(oBotTitle).style.left='0px';
		document.getElementById(oTopTitle).style.left='-1000px';
		iTSFading=0;
		iTSCur=iBot;
	}
	else{
		iTSFastMove=0;
		changeOpac(100,oBot);
		changeOpac(0,oBotLayer);
		changeOpac(0,oBotTitle);
		document.getElementById(oTop).style.zIndex=2;
		document.getElementById(oBot).style.zIndex=1;
		document.getElementById(oBot).style.left='0px';
		document.getElementById(oBotLayer).style.left='0px';
		document.getElementById(oBotTitle).style.left='0px';
		iTSFading=1;
		changingdirectionimage(direction);
		changingtopstory(iTop,iBot,0,50);
	}
}

function changingtopstory(iTop,iBot,iStep,iCurOpac){
	var oTop, oBot, oTopLayer, oBotLayer, oTopTitle, oBotTitle;
	oTop = 'divTopStory'+iTop;
	oBot = 'divTopStory'+iBot;
	oTopLayer = 'divTopStoryLayer'+iTop;
	oBotLayer = 'divTopStoryLayer'+iBot;
	oTopTitle = 'divTopStoryTitle'+iTop;
	oBotTitle = 'divTopStoryTitle'+iBot;
	if (iTSFading==1){
		if (iStep==0){
			if (iCurOpac>=0)
			{
				changeOpac(iCurOpac,oTopTitle);
				changeOpac(iCurOpac*80/100,oTopLayer);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory(iTop,iBot,0,iCurOpac)},iFadeDelay);
			}
			else{
				setTimeout(function(){changingtopstory(iTop,iBot,1,50)},500);
			}
		}
		else if (iStep==1){
			if (iCurOpac>=0){
				changeOpac(iCurOpac,oTop);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory(iTop,iBot,1,iCurOpac)},iFadeDelay);
			}
			else{
				iTSCur=iBot;
				document.getElementById(oTop).style.left='-1000px';
				document.getElementById(oTopLayer).style.left='-1000px';
				document.getElementById(oTopTitle).style.left='-1000px';
				setTimeout(function(){changingtopstory(iTop,iBot,2,0)},50);
			}
		}
		else if (iStep==2){
			if (iCurOpac<=50){
				changeOpac(iCurOpac,oBotTitle);
				changeOpac(iCurOpac*80/100,oBotLayer);
				iCurOpac+=5;
				setTimeout(function(){changingtopstory(iTop,iBot,2,iCurOpac)},iFadeDelay);
			}
			else{
				outdirectionimage();
				iTSFading=0;
				iTSTimer=0;
			}
		}
	}
	else if (iStep<2 && iTSFastMove==0) {
		document.getElementById(oBot).style.left='-1000px';
		document.getElementById(oBotLayer).style.left='-1000px';
		document.getElementById(oBotTitle).style.left='-1000px';
		outdirectionimage();
		iTSFading=0;
		iTSTimer=0;
	}
}

function changingdirectionimage(direction){
	if (direction>0) document.getElementById('imgTSNext').src = boxImageUrl +"Next.gif";
	else document.getElementById('imgTSPrev').src = boxImageUrl +"Prev.gif";
}

function outdirectionimage(){
	if (iTSNext==0) document.getElementById('imgTSNext').src = boxImageUrl +"Next2.gif"
	if (iTSPrev==0) document.getElementById('imgTSPrev').src = boxImageUrl +"Prev2.gif"
}

function topstorycounter(){
	iTSTimer++;
	if (iTSTimer>50) {
		iTSTimer=0;
		changetopstory(1,true);
	}
}

function cleartopstorycounter(){
	changeOpac(100,'divTopStory'+iTSCur);
	changeOpac(100,'divTopStoryLayer'+iTSCur);
	changeOpac(100,'divTopStoryTitle'+iTSCur);
	iTSFading=0;
	iTSTimer=0;
}

function changeOpac(opacity, id) {
	document.getElementById(id).style.opacity = (opacity / 100);
	document.getElementById(id).style.MozOpacity = (opacity / 100);
	document.getElementById(id).style.KhtmlOpacity = (opacity / 100);
	document.getElementById(id).style.filter = "alpha(opacity=" + opacity + ")";
}

