var oldgridSelectedColor
function setMouseOverColor(element)
{
    element.className       = 'cssTRViewSelect';
}

function setMouseOutColor(element)
{
    element.className       = 'cssTRView';
}

var iMaxTS=6,iMaxTBS=3;
var iReadStep=0;
var sLoDID=',';
var iDelay=3650000;
var iDisplayCateNo=0;
var iTSTimer=0;
var iTSCount,iTSCur,iTSFading,iTSPrev=0,iTSNext=0,iTSFastMove=0;
var iFadeDelay=3650000;
var iBoxCount=0;

function getNodeValue(o){
	try	{
		return o.item(0).firstChild.nodeValue;
	}
	catch(err) {
		return '';
	}
	// if (o.item(0)){
		// if (o.item(0).firstChild){
			// return o.item(0).firstChild.nodeValue;
		// }
		// else{
			// return '';
		// }
	// }
	// else{
		// return '';
	// }
}

function displayid(id,add){
	if (typeof(add)=='undefined') add=true;
	if (sLoDID.indexOf(id)<=0){
		if (add) sLoDID=sLoDID.concat(id).concat(',');
		//sLoDID=sLoDID.concat(id).concat(',');
		return true;
	}
	else{
		return false;
	}
}

function showtopstoryitem(sNameTS,sPathTS,arItemTS){
	iTSCount = arItemTS.length-1;
	var sHTML='';
	sHTML=sHTML.concat('<table border="0" cellspacing="0" align="center" cellpadding="0"><tr><td style="height:330px;width:489px;" valign="bottom"><div style="position:relative;width:488px;height:337px;">');// khung ngoai cung
	sHTML=sHTML.concat('	<div style="left:0px;top:0px;width:485px;height:3px;" class="HomeTopStoryBorder"></div>'); // Vien tren
	sHTML=sHTML.concat('	<div style="left:0px;top:334px;width:485px;height:3px;" class="HomeTopStoryBorder"></div>');// Vien duoi
	sHTML=sHTML.concat('	<div style="left:0px;top:3px;width:3px;height:331px;" class="HomeTopStoryBorder"></div>');// Vien trai
	sHTML=sHTML.concat('	<div style="left:485px;top:3px;width:3px;height:331px;" class="HomeTopStoryBorder"></div>');// Vien Phai
	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:-10000px')).concat(';top:0px;width:485px;height:337px;">');
		sHTML=sHTML.concat('		<a href="').concat(arItemTS[i][1]).concat('"><img border="0" src="').concat(arItemTS[i][4]).concat('" width="485" height="337"></a>');
		sHTML=sHTML.concat('	</div>');
		sHTML=sHTML.concat('	<div id="divTopStoryLayer').concat(i).concat('" onmousemove="cleartopstorycounter();" style="').concat(iif(i==0,'left:0px','left:-10000px')).concat(';top:270px;width:485px;height:65px;" class="HomeTopStoryColorLayer"></div>');
		sHTML=sHTML.concat('	<div id="divTopStoryTitle').concat(i).concat('" onmousemove="cleartopstorycounter();" style="').concat(iif(i==0,'left:0px','left:-10000px')).concat(';top:270px;width:485px;height:65px;" class="HomeTopStoryColorLayerContent">');
		var strTemp1 = arItemTS[i][2]
        if(strTemp1.length > 80)
        {
            strTemp1 = strTemp1.substring(0,80)
            var intCheck1 = strTemp1.lastIndexOf(' ')
            if(intCheck1 >=0 )
            {
                strTemp1 = strTemp1.substring(0,intCheck1)
            }
        }
		sHTML=sHTML.concat('		<div style="left:7px;top:4px;width:405px;height:18px;" class="HomeTopStoryTitle" onclick="window.location=\'').concat(arItemTS[i][1]).concat('\'">').concat(strTemp1).concat('</div>');
		sHTML=sHTML.concat('		<div style="left:8px;top:5px;width:405px;height:18px;" class="HomeTopStoryTitleShadow">').concat(strTemp1).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:460px;top:316px;width:30px;height:32px;" class="HomeTopStoryColorLayerSubImage">');
//	sHTML=sHTML.concat('<img id="imgTSPrev" src="/Vinafood1/Images/FlashImages/Prev2.gif" hspace="2" vspace="0" onmouseover="iTSPrev=1;this.src=\'/Vinafood1/Images/FlashImages/Prev.gif\'" onmouseout="iTSPrev=0;this.src=\'/Vinafood1/Images/FlashImages/Prev2.gif\'" style="cursor:pointer" onclick="changetopstory(-1,false);">');
//	sHTML=sHTML.concat('<img id="imgTSNext" src="/Vinafood1/Images/FlashImages/Next2.gif" hspace="2" vspace="0" onmouseover="iTSNext=1;this.src=\'/Vinafood1/Images/FlashImages/Next.gif\'" onmouseout="iTSNext=0;if (!iTSFading) this.src=\'/Vinafood1/Images/FlashImages/Next2.gif\'" style="cursor:pointer" onclick="changetopstory(1,false);">');
//	sHTML=sHTML.concat('</div>');
	sHTML=sHTML.concat('</div>');	
	sHTML=sHTML.concat('</td>');
	sHTML=sHTML.concat('<td valign="top" style="width:2px;" class="cssBGBreakIndexHome" >');
	sHTML=sHTML.concat('</td>');
    sHTML=sHTML.concat('<td valign="top" style="width:243px;background-color:White;">');
    sHTML=sHTML.concat('    <table border="0" cellpadding="1" cellspacing="4" style="width:243px;background-color:White;">');
    for (var k=0;k<=iTSCount;k++) {
        //sHTML = sHTML.concat('        <tr ><td class="cssTRView" onclick="changetopstoryView(' + k + ',this);" onmouseout="setMouseOutColor(this);" onmouseover="setMouseOverColor(this);">');
        sHTML = sHTML.concat('        <tr ><td class="cssTRView" onclick="window.location=\'').concat(arItemTS[k][1]).concat('\'" onmouseout="setMouseOutColor(this);" onmouseover="setMouseOverColor(this);">');
        sHTML=sHTML.concat('                <table border="0" cellpadding="0" cellspacing="0" ><tr ><td>');
        sHTML=sHTML.concat('                    <table border="0" class="cssNewsTableImage" cellpadding="2" cellspacing="0"><tr><td valign="top" align="justify"><img border="0" src="').concat(arItemTS[k][4]).concat('" width="55" height="40"></td></tr></table></td> ');
        var strTemp = arItemTS[k][2]
        if(strTemp.length > 46)
        {
            strTemp = strTemp.substring(0,46)
            var intCheck = strTemp.lastIndexOf(' ')
            if(intCheck >=0 )
            {
                strTemp = strTemp.substring(0,intCheck)
            }
        }
        sHTML=sHTML.concat('                        <td style="padding-left:2px;cursor:hand;text-align:justify;">').concat(strTemp).concat('</td>');
        sHTML=sHTML.concat('                    </tr></table> ');
        sHTML=sHTML.concat('            </td>');
        sHTML=sHTML.concat('        </tr>');
    }
    sHTML=sHTML.concat('    </table>');
    sHTML=sHTML.concat('</td></tr>');
    sHTML=sHTML.concat('</table> ');
	gmobj('tdTS').innerHTML=sHTML;
	iTSCur=0;
	setInterval(function(){topstorycounter();},100)
}

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){
		gmobj('divTopStory'+(iTop+1)).style.left='-1000px';
		gmobj('divTopStoryLayer'+(iTop+1)).style.left='-1000px';
		gmobj('divTopStoryTitle'+(iTop+1)).style.left='-1000px';
		outdirectionimage();
		iTSFading=0;
	}
	iTSTimer=0;
	if (!fade){
		iTSFastMove=1;
		changeOpac(100,oBot);
		changeOpac(40,oBotLayer);
		changeOpac(100,oBotTitle);
		gmobj(oTop).style.zIndex=1;
		gmobj(oBot).style.zIndex=2;
		gmobj(oBot).style.left='0px';
		gmobj(oTop).style.left='-1000px';
		gmobj(oBotLayer).style.left='0px';
		gmobj(oTopLayer).style.left='-1000px';
		gmobj(oBotTitle).style.left='0px';
		gmobj(oTopTitle).style.left='-1000px';
		iTSFading=0;
		iTSCur=iBot;
	}
	else{
		iTSFastMove=0;
		changeOpac(100,oBot);
		changeOpac(0,oBotLayer);
		changeOpac(0,oBotTitle);
		gmobj(oTop).style.zIndex=2;
		gmobj(oBot).style.zIndex=1;
		gmobj(oBot).style.left='0px';
		gmobj(oBotLayer).style.left='0px';
		gmobj(oBotTitle).style.left='0px';
		iTSFading=1;
		changingdirectionimage(direction);
		changingtopstory(iTop,iBot,0,100);
	}
}

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*40/100,oTopLayer);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory(iTop,iBot,0,iCurOpac)},iFadeDelay);
			}
			else{
				setTimeout(function(){changingtopstory(iTop,iBot,1,100)},2000);
			}
		}
		else if (iStep==1){
			if (iCurOpac>=0){
				changeOpac(iCurOpac,oTop);
				iCurOpac-=5;
				setTimeout(function(){changingtopstory(iTop,iBot,1,iCurOpac)},iFadeDelay);
			}
			else{
				iTSCur=iBot;
				gmobj(oTop).style.left='-1000px';
				gmobj(oTopLayer).style.left='-1000px';
				gmobj(oTopTitle).style.left='-1000px';
				setTimeout(function(){changingtopstory(iTop,iBot,2,0)},100);
			}
		}
		else if (iStep==2){
			if (iCurOpac<=100){
				changeOpac(iCurOpac,oBotTitle);
				changeOpac(iCurOpac*40/100,oBotLayer);
				iCurOpac+=5;
				setTimeout(function(){changingtopstory(iTop,iBot,2,iCurOpac)},iFadeDelay);
			}
			else{
				outdirectionimage();
				iTSFading=0;
				iTSTimer=0;
			}
		}
	}
	else if (iStep<2 && iTSFastMove==0) {
		gmobj(oBot).style.left='-1000px';
		gmobj(oBotLayer).style.left='-1000px';
		gmobj(oBotTitle).style.left='-1000px';
		outdirectionimage();
		iTSFading=0;
		iTSTimer=0;
	}
}

function changingdirectionimage(direction){
	//if (direction>0) gmobj('imgTSNext').src="/Vinafood1/Images/FlashImages/Next.gif"
	//else gmobj('imgTSPrev').src="/Vinafood1/Images/FlashImages/Prev.gif"
}

function outdirectionimage(){
	//if (iTSNext==0) gmobj('imgTSNext').src="/Vinafood1/Images/FlashImages/Next2.gif"
	//if (iTSPrev==0) gmobj('imgTSPrev').src="/Vinafood1/Images/FlashImages/Prev2.gif"
}

function topstorycounter(){
	iTSTimer++;
	if (iTSTimer>70) {
		iTSTimer=0;
		changetopstory(1,true);
	}
}

function cleartopstorycounter(){
	changeOpac(100,'divTopStory'+iTSCur);
	changeOpac(40,'divTopStoryLayer'+iTSCur);
	changeOpac(100,'divTopStoryTitle'+iTSCur);
	iTSFading=0;
	iTSTimer=0;
}

function changeOpac(opacity, id) {
	gmobj(id).style.opacity = (opacity / 100);
	gmobj(id).style.MozOpacity = (opacity / 100);
	gmobj(id).style.KhtmlOpacity = (opacity / 100);
	gmobj(id).style.filter = "alpha(opacity=" + opacity + ")";
}

function changetopstoryView(direction,element){
	var iTop, iBot;
	var oTop, oBot, oTopLayer, oBotLayer, oTopTitle, oBotTitle;
	if(direction !=iTSCur)
	{
	    iTop=iTSCur;
	    iBot=direction;
	    oTop = 'divTopStory'+iTop;
	    oBot = 'divTopStory'+iBot;
	    oTopLayer = 'divTopStoryLayer'+iTop;
	    oBotLayer = 'divTopStoryLayer'+iBot;
	    oTopTitle = 'divTopStoryTitle'+iTop;
	    oBotTitle = 'divTopStoryTitle'+iBot;
    	
	    changeOpac(100,oBot);
	    changeOpac(40,oBotLayer);
	    changeOpac(100,oBotTitle);
	    gmobj(oTop).style.zIndex=1;
	    gmobj(oBot).style.zIndex=2;
	    gmobj(oBot).style.left='0px';
	    gmobj(oTop).style.left='-1000px';
	    gmobj(oBotLayer).style.left='0px';
	    gmobj(oTopLayer).style.left='-1000px';
	    gmobj(oBotTitle).style.left='0px';
	    gmobj(oTopTitle).style.left='-1000px';
	    iTSFading=0;
	    iTSCur=iBot;
	}
	//element.className       = 'cssTRViewSelect';
}

