﻿var iReadStep=0, iDelay=20;
var sLoDID=',', sDate='';

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 ShowGoldPrice(){
	var sHTML = '';	
	sHTML = sHTML.concat('<div style="text-align:right;color:#8A0000;font:bold 10px arial;">ĐVT: tr.&#273;/l&#432;&#7907;ng</div>');
	sHTML = sHTML.concat('<table border="0px" cellpadding="2px" cellspacing="1px" class="tbl-goldprice">');
	sHTML = sHTML.concat('	<tr>');
	sHTML = sHTML.concat('		<td class="td-weather-title" style="font-size:10px;width:30%;">Lo&#7841;i</td>');
	sHTML = sHTML.concat('		<td class="td-weather-title" style="text-align:center;font-size:10px;width:35%;">Mua</td>');
	sHTML = sHTML.concat('		<td class="td-weather-title" style="text-align:center;font-size:10px;width:35%;">B&#225;n</td>');
	sHTML = sHTML.concat('	</tr>');
	sHTML = sHTML.concat('	<tr>');
	sHTML = sHTML.concat('		<td class="td-weather-title">SBJ</td>');
	sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSbjBuy).concat('</td>');
	sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSbjSell).concat('</td>');
	sHTML = sHTML.concat('	</tr>');
	sHTML = sHTML.concat('	<tr>');
	sHTML = sHTML.concat('		<td class="td-weather-title">SJC</td>');
	sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSjcBuy).concat('</td>');
	sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vGoldSjcSell).concat('</td>');
	sHTML = sHTML.concat('	</tr>');
	sHTML = sHTML.concat('</table>');
	gmobj('eGold').innerHTML = sHTML;
}

function ShowForexRate(){
	var sHTML = '';
	sHTML = sHTML.concat('<table border="0px" cellpadding="2px" cellspacing="1px" class="tbl-weather">');
	for(var i=0;i<vForexs.length;i++){
		sHTML = sHTML.concat('	<tr>');
		sHTML = sHTML.concat('		<td class="td-weather-title">').concat(vForexs[i]).concat('</td>');
		sHTML = sHTML.concat('		<td class="td-weather-data txtr">').concat(vCosts[i]).concat('</td>');
		sHTML = sHTML.concat('	</tr>');
	}
	sHTML = sHTML.concat('</table>');
	gmobj('eForex').innerHTML = sHTML;
}

function FormatCurrency(num, currencyCode, isReplace, justFormat) {
    if (num == null)
        return "";
    var num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    var sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        switch (Trim(currencyCode.toLowerCase())) {
            case 'en-us':
                {
                    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
                    break;
                }
            case 'vi-vn':
                {
                    num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
                    break;
                }
        }
    }
    var res = '0';
    switch (Trim(currencyCode.toLowerCase())) {
        case 'en-us':
            {
                if (justFormat != null && justFormat == true) {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + '.' + cents);
                    else
                        res = (((sign) ? '' : '-') + num);
                }
                else {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + '$' + num + '.' + cents);
                    else
                        res = (((sign) ? '' : '-') + '$' + num);
                }
                break;
            }
        case 'vi-vn':
            {
                if (justFormat != null && justFormat == true) {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + ',' + cents);
                    else
                        res = (((sign) ? '' : '-') + num);
                }
                else {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + ',' + cents + '<u>d</u>');
                    else
                        res = (((sign) ? '' : '-') + num + '<u>d</u>');
                }
                break;
            }
    }
    return res;
}

//Top story
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;
var Next ='images/Next1.gif';
var Prev='images/Prev1.gif';
var Next2 = 'images/Next2.gif';
var Prev2='images/Prev2.gif';

function showtopstoryitem(arItemTS) {
    iTSCount = arItemTS.length - 1;
    
	var sHTML='';
	sHTML=sHTML.concat('<div style="position:relative;width:360px;height:265px;">');
//	sHTML=sHTML.concat('	<div style="left:0px;top:0px;width:362px;height:3px;" class="HomeTopStoryBorder"></div>');
//	sHTML=sHTML.concat('	<div style="left:0px;top:264px;width:362px;height:3px;" class="HomeTopStoryBorder"></div>');
//	sHTML=sHTML.concat('	<div style="left:0px;top:3px;width:3px;height:261px;" class="HomeTopStoryBorder"></div>');
//	sHTML=sHTML.concat('	<div style="left:359px;top:3px;width:4px;height:261px;" 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:362px;height:267px;">');
		
		sHTML = sHTML.concat('		<a title="').concat(arItemTS[i][2]).concat('" href="').concat(arItemTS[i][1]).concat('"><img style="border:1px #8d8c8d solid;" src="').concat(arItemTS[i][3]).concat('" alt="" width="358" height="263"></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:360px;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:360px;height:65px;" class="HomeTopStoryColorLayerContent">');
		
		sHTML=sHTML.concat('		<div style="left:3px;top:0px;width:355px;height:18px;"').concat('" class="HomeTopStoryTitle" onclick="window.location=\'').concat(arItemTS[i][1]).concat('\'">').concat(arItemTS[i][2]).concat('</div>');		
		sHTML=sHTML.concat('		<div style="left:4px;top:1px;width:355px;height:18px;" class="HomeTopStoryTitleShadow">').concat(arItemTS[i][2]).concat('</div>');
		sHTML=sHTML.concat('		<div style="left:4px;top:20px;width:340px;height:20px;" class="HomeTopStoryLead">').concat(arItemTS[i][4]).concat('</div>');

		sHTML=sHTML.concat('	</div>');
//		sHTML=sHTML.concat('	</div>');
	}
	
	if (arItemTS.length > 1)
	{
	    sHTML=sHTML.concat('<div style="left:334px;top:246px;width:40px;height:32px;" class="HomeTopStoryColorLayerSubImage">');
	    sHTML = sHTML.concat('<img id="imgTSPrev" src="'+ Prev2 +'" hspace="2" vspace="0" onmouseover="iTSPrev=1;this.src=\''+ Prev +'\'" onmouseout="iTSPrev=0;this.src=\''+ Prev2 +'\'" style="cursor:pointer" onclick="changetopstory(-1,false);">');
	    sHTML = sHTML.concat('<img id="imgTSNext" src="'+ Next2 +'" hspace="2" vspace="0" onmouseover="iTSNext=1;this.src=\''+ Next +'\'" onmouseout="iTSNext=0;if (!iTSFading) this.src=\''+ Next2 +'\'" style="cursor:pointer" onclick="changetopstory(1,false);">');
	    sHTML=sHTML.concat('</div>');
	}
	
	sHTML=sHTML.concat('</div>');	
	
	gmobj("divTopNews").innerHTML=sHTML;
	iTSCur=0;
	
	if (arItemTS.length > 1)
	{
	    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)},100); //change
			}
		}
		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)},1000);
			}
		}
		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 = Next;
	else gmobj('imgTSPrev').src =Prev;
}

function outdirectionimage(){
    if (iTSNext == 0) gmobj('imgTSNext').src = Next2;
	if (iTSPrev==0) gmobj('imgTSPrev').src=Prev2;
}

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 iif(iEx1,r1,r2){
	if(iEx1)return r1;
	else return r2;
}
