﻿
$Id = function(id){return document.getElementById(id)}
var Infoonload =true;
/* =============== forex ==================== */
function GetForexRow(Currency, Rate)
{
//	return '<tr bgcolor=white><td class=BoxItem>&nbsp;' + Currency + '</td><td class=BoxItem align=right>' + Rate + '&nbsp;</td></tr>';
	return '<tr class=\"tr-normal\"><td class=\"td-normal\"> &nbsp;'+Currency+ ' </td><td class=\"td-normal\"> '+Rate+ ' </td> </tr>'
}
function ShowForexRate()
{
	var html="<table id='tableForex'>";
	html += '<tr class="table-title">  <td> Tiền tệ </td>  <td> Tỷ giá  </td>  </tr>';
	for(var i=0;i<vForexs.length;i++) 
	{
	    html += GetForexRow(vForexs[i], vCosts[i]);
	}	
	html+="</table>";
	$Id("divForex").innerHTML = html;	
}
/* =============== gold ==================== */
function ShowGoldPrice()
{
	if (typeof(vGoldSbjBuy) !='undefined') $Id("GoldSbjBuy").innerHTML = vGoldSbjBuy;
	if (typeof(vGoldSbjSell)!='undefined') $Id("GoldSbjSell").innerHTML = vGoldSbjSell;
	
	if (typeof(vGoldSjcBuy) !='undefined') $Id("GoldSjcBuy").innerHTML = vGoldSjcBuy;
	if (typeof(vGoldSjcSell)!='undefined') $Id("GoldSjcSell").innerHTML = vGoldSjcSell;

}
/* =============== weather ==================== */
requestRemoteJS = function(src)
{
	var s = document.createElement("script");
	s.type = "text/javascript";
	s.language = "javascript";
	s.src = src;
	document.getElementsByTagName("head")[0].appendChild(s);
}
Register = function(key,callback,limit)
{
	Registry[key] =
	{
		"count":0,
		"callback":callback,
		"limit":limit||10
	}
}
Registry = {};
waitGoldBuy = function(key)
{
	if(vGoldSbjBuy == null)
	{
		Registry[key].count++;
		if(Registry[key].count<Registry[key].limit) window.setTimeout("waitGoldBuy('vGoldSbjBuy')",1000);
	}
	else Registry[key]["callback"]();
}
waitForexs = function(key)
{
	if(vForexs == null)
	{
		Registry[key].count++;
		if(Registry[key].count<Registry[key].limit) window.setTimeout("waitForexs('vForexs')",1000);
	}
	else Registry[key]["callback"]();
}
showWeather = function(json)
{
	var r = json.FORM.ROWSET.ROW;
	$Id("imgWeather").src = r.StatusImg.replace("'","");
	var t = parseInt(r.Now);
	$Id("imgF").src =window.location.protocol + "//" + window.location.host + "/" + window.location.pathname.split("/")[1]+ "/images/weather/"+Math.floor(t/10)+".gif";
	$Id("imgL").src = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname.split("/")[1] +"/images/weather/"+(t%10)+".gif";

	$Id("weather_lblTitle").innerHTML = r.Status!=null?r.Status:"";
	var s = "";
	s += "Nhiệt độ: " + parseInt(r.Low) + " - " + parseInt(r.High) + "<br/>";
	s += "Độ ẩm: " + r.Humidity + "<br/>";
	s += "Tốc độ gió: " + r.Wind + "<br/>";
	s += "Tầm nhìn xa: " + (r.Visibility=="nlimited"?">10km":r.Visibility) + "<br/>";
	s += "Mặt trời mọc: " + r.Sunrise + "<br/>";
	s += "Mặt trời lặn: " + r.Sunset + "<br/>";;
	$Id("weather_lblStatus").innerHTML = s;
}
WeatherChangeLocation = function(sl)
{
	requestRemoteJS(WT.replace("$",sl.value))
}
var WT = "http://srv.vietnamnet.vn/vsinfo/xml2json.aspx?urlback=tyid=3|aid=$&objname=showWeather";

if(Infoonload)
{
    InfoInit();
    Infoonload = false;
}

function InfoInit()
{
	// gia vang
//	requestRemoteJS("http://vnexpress.net/Service/Gold_Content.js");
//	Register("vGoldSbjBuy",ShowGoldPrice,20);
//	vGoldSbjBuy = null;
//	waitGoldBuy("vGoldSbjBuy");

	// ty gia
	requestRemoteJS("http://vnexpress.net/Service/Forex_Content.js");
	Register("vForexs",ShowForexRate,20);
	vForexs = null;
	waitForexs("vForexs");

	// weather
//	requestRemoteJS(WT.replace("$",$Id("slWeather").value));
}