var iMaxTS1=5,iMaxTBS1=3;
var iReadStep1=0;
var sLoDID1=',';
var iDelay1=200;
var iDisplayCateNo1=0;
var iTSTimer1=0;
var iTSCount1,iTSCur1,iTSFading1,iTSPrev1=0,iTSNext1=0,iTSFastMove1=0;
var iFadeDelay1=50;
var iBoxCount1 = 0;
var Story = 'images/story.gif';
var video = 'images/video.gif';
var Photo = 'images/Photo.gif';
var Next ='images/Next.gif';
var Prev='images/Prev.gif';
var Next2 = 'images/Next2.gif';
var Prev2='images/Prev2.gif';

function showtopstoryitem1(arItemTS) {
    iTSCount1 = arItemTS.length - 1;
    
	var sHTML='';
	sHTML=sHTML.concat('<div style="position:relative;width:330px;height:205px;">');
	sHTML=sHTML.concat('	<div style="left:0px;top:0px;width:330px;height:2px;" class="HomeTopStoryBorder"></div>');
	sHTML=sHTML.concat('	<div style="left:0px;top:198px;width:330px;height:2px;" class="HomeTopStoryBorder"></div>');
	sHTML=sHTML.concat('	<div style="left:0px;top:2px;width:2px;height:198px;" class="HomeTopStoryBorder"></div>');
	sHTML=sHTML.concat('	<div style="left:328px;top:2px;width:2px;height:198px; " class="HomeTopStoryBorder"></div>');
	for (var i=0;i<=iTSCount1;i++){
		sHTML=sHTML.concat('	<div id="divTopStory2').concat(i).concat('" onmousemove="cleartopstorycounter1();" style="overflow:hidden;position:absolute;').concat((i==0?'left:0px':'left:-4000px')).concat(';top:0px;width:330px;height:200px;">');
		sHTML = sHTML.concat('		<a title="').concat(arItemTS[i][2]).concat('" href="').concat(arItemTS[i][5]).concat('"><img border="0" src="').concat(arItemTS[i][1]).concat(arItemTS[i][4]).concat('" alt="').concat(arItemTS[i][2]).concat('" width="328" height="200"></a>');	
		
		sHTML=sHTML.concat('	</div>');
		sHTML=sHTML.concat('	<div id="divTopStoryLayer2').concat(i).concat('" onmousemove="cleartopstorycounter1();" style="').concat((i==0?'left:0px':'left:-4000px')).concat(';top:146px;width:330px;height:55px;" class="HomeTopStoryColorLayer"></div>');
		sHTML=sHTML.concat('	<div id="divTopStoryTitle2').concat(i).concat('" onmousemove="cleartopstorycounter1();" style="').concat((i==0?'left:0px':'left:-4000px')).concat(';top:146px;width:330px;height:55px;" class="HomeTopStoryColorLayerContent">');
		sHTML=sHTML.concat('		<div style="left:3px;top:4px;width:328px;height:15px;" title="').concat(arItemTS[i][2]).concat('" class="HomeTopStoryTitle" onclick="window.location=\'').concat(arItemTS[i][5]).concat('\';">').concat(arItemTS[i][2]).concat('</div>');	
		
		sHTML=sHTML.concat('		<div style="left:4px;top:5px;width:328px;height:18px;" class="HomeTopStoryTitleShadow">').concat(arItemTS[i][2]).concat('</div>');
		sHTML=sHTML.concat('		<div style="left:4px;top:'+(arItemTS[i][2].length>31?'43':'28')+'px;width:328px;height:18px;" class="HomeTopStoryLead">').concat(arItemTS[i][3]);
		
		sHTML = sHTML.concat('		</div>');
		sHTML=sHTML.concat('	</div>');
	}
	sHTML=sHTML.concat('<div style="left:306px;top:185px;width:25px;height:17px;" class="HomeTopStoryColorLayerSubImage">');
	sHTML = sHTML.concat('<img id="imgTSPrev" src="'+ Prev2 +'" hspace="2" vspace="0" onmouseover="iTSPrev1=1;this.src=\''+ Prev +'\'" onmouseout="iTSPrev1=0;this.src=\''+ Prev2 +'\'" style="cursor:pointer" onclick="changetopstory1(-1,false);">');
	sHTML = sHTML.concat('<img id="imgTSNext" src="'+ Next2 +'" hspace="2" vspace="0" onmouseover="iTSNext1=1;this.src=\''+ Next +'\'" onmouseout="iTSNext1=0;if (!iTSFading1) this.src=\''+ Next2 +'\'" style="cursor:pointer" onclick="changetopstory1(1,false);">');
	sHTML=sHTML.concat('</div>');
	sHTML=sHTML.concat('</div>');
	$("tdTS1").innerHTML=sHTML;
	iTSCur1=0;
	//sua
	setInterval(function(){topstorycounter1();},100)
}


function changetopstory1(direction,fade){
    var iTop, iBot;
	var oTop, oBot, oTopLayer, oBotLayer, oTopTitle, oBotTitle;
	iTop=iTSCur1;
	iBot=iTop+direction;
	if (iBot<0) iBot=iTSCount1;
	if (iBot>iTSCount1) iBot=0;
	//sua
	oTop = 'divTopStory2'+iTop;
	oBot = 'divTopStory2'+iBot;
	oTopLayer = 'divTopStoryLayer2'+iTop;
	oBotLayer = 'divTopStoryLayer2'+iBot;
	oTopTitle = 'divTopStoryTitle2'+iTop;
	oBotTitle = 'divTopStoryTitle2'+iBot;
	//alert(oBotLayer);
	
	if (iTSFading1==1){
	    //sua thuoc tinh left cua top thanh right
		$('divTopStory2'+(iTop+1)).style.right='-4000px';
		$('divTopStoryLayer2'+(iTop+1)).style.right='-4000px';
		$('divTopStoryTitle2'+(iTop+1)).style.right='-4000px';
		outdirectionimage1();
		iTSFading1=0;
	}
	iTSTimer1=0;
	if (!fade){
		iTSFastMove1=1;
		changeOpac1(100,oBot);
		changeOpac1(40,oBotLayer);
		changeOpac1(100,oBotTitle);
		//sua thanh right
		$(oTop).style.zIndex=1;
		$(oBot).style.zIndex=2;
		$(oBot).style.left='0px';
		$(oTop).style.left='-4000px';
		$(oBotLayer).style.left='0px';
		$(oTopLayer).style.left='-4000px';
		$(oBotTitle).style.left='0px';
		//sua
		//$(oTopTitle).style.left='-4000px';
		$(oTopTitle).style.left='-4000px';
		iTSFading1=0;
		iTSCur1=iBot;
	}
	else{
		iTSFastMove1=0;
		changeOpac1(100,oBot);
		changeOpac1(0,oBotLayer);
		changeOpac1(0,oBotTitle);
		//sua
		$(oTop).style.zIndex=2;
		//$(oTop).style.zIndex=2;
		$(oBot).style.zIndex=1;
		//bot la thuc the hien tai
		//bang 0 sua lai 2000
		$(oBot).style.left='0px';
		$(oBotLayer).style.left='0px';
		$(oBotTitle).style.left='0px';
		iTSFading1=1;
		changingdirectionimage1(direction);
		changingtopstory1(iTop,iBot,0,100);
	}
}

function changingtopstory1(iTop,iBot,iStep,iCurOpac){
	var oTop, oBot, oTopLayer, oBotLayer, oTopTitle, oBotTitle;
//sua
	oTop = 'divTopStory2'+iTop;
	oBot = 'divTopStory2'+iBot;
	oTopLayer = 'divTopStoryLayer2'+iTop;
	oBotLayer = 'divTopStoryLayer2'+iBot;
	oTopTitle = 'divTopStoryTitle2'+iTop;
	oBotTitle = 'divTopStoryTitle2'+iBot;
	
	if (iTSFading1==1){
		if (iStep==0){
			if (iCurOpac>=0){
				changeOpac1(iCurOpac,oTopTitle);
				changeOpac1(iCurOpac*40/100,oTopLayer);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory1(iTop,iBot,0,iCurOpac)},iFadeDelay1);
			}
			else{
				setTimeout(function(){changingtopstory1(iTop,iBot,1,100)},2000);
			}
		}
		else if (iStep==1){
			if (iCurOpac>=0){
				changeOpac1(iCurOpac,oTop);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory1(iTop,iBot,1,iCurOpac)},iFadeDelay1);
			}
			else{
				iTSCur1=iBot;
				$(oTop).style.left='-4000px';
				$(oTopLayer).style.left='-4000px';
				$(oTopTitle).style.left='-4000px';
				setTimeout(function(){changingtopstory1(iTop,iBot,2,0)},100);
			}
		}
		else if (iStep==2){
			if (iCurOpac<=100){
				changeOpac1(iCurOpac,oBotTitle);
				changeOpac1(iCurOpac*40/100,oBotLayer);
				iCurOpac+=5;
				setTimeout(function(){changingtopstory1(iTop,iBot,2,iCurOpac)},iFadeDelay1);
			}
			else{
				outdirectionimage1();
				iTSFading1=0;
				iTSTimer1=0;
			}
		}
	}
	else if (iStep<2 && iTSFastMove1==0) {
	//sua lai thanh right
		$(oBot).style.left='-4000px';
		$(oBotLayer).style.left='-4000px';
		$(oBotTitle).style.left='-4000px';
		outdirectionimage1();
		iTSFading1=0;
		iTSTimer1=0;
	}
}

function changingdirectionimage1(direction){
    //tu nhay thi su kien nay moi xay ra
    if (direction > 0) $('imgTSNext').src = Next;
	else $('imgTSPrev').src =Prev;
}

function outdirectionimage1(){
    if (iTSNext1 == 0) $('imgTSNext').src = Next2;
	if (iTSPrev1==0) $('imgTSPrev').src=Prev2;
}

function topstorycounter1(){
	iTSTimer1++;
	if (iTSTimer1>70) {
		iTSTimer1=0;
		changetopstory1(1,true);
	}
}

function cleartopstorycounter1(){
	changeOpac1(100,'divTopStory2'+iTSCur1);
	changeOpac1(40,'divTopStoryLayer2'+iTSCur1);
	changeOpac1(100,'divTopStoryTitle2'+iTSCur1);
	iTSFading1=0;
	iTSTimer1=0;
}

function changeOpac1(opacity, id) {
	$(id).style.opacity = (opacity / 100);
	$(id).style.MozOpacity = (opacity / 100);
	$(id).style.KhtmlOpacity = (opacity / 100);
	$(id).style.filter = "alpha(opacity=" + opacity + ")";
}
