var HQLoader = function () {
	this._elementTarget = null;
	this._stringHtmlTemplate = "";
	this._arrayList = [];
	this._functionProcess = null;
	this._intThread = -1;
	this._fill = function () {
		var __stringHtmlTemplate = this._stringHtmlTemplate;
		var __arrayHtml = __stringHtmlTemplate.split("<" + "!-- @LOOP@ -->");
		var __stringHtmlHead = __arrayHtml[0];
		var __stringHtmlBody = __arrayHtml[1];
		var __stringHtmlFoot = __arrayHtml[2];
		for (var i = 0; i < this._arrayList.length; i++) {
			if (this._arrayList[i].indexOf("h_") == 0) {
				continue;
			}
			var __stringHtmlLoop = __stringHtmlBody;
			var __arrayData = this._functionProcess(this._arrayList[i]);
			var __arrayKeys = __stringHtmlLoop.match(/(<!--\s)?@[^@]*@(\s-->)?/gm);
			if (__arrayKeys != null) {
				for (var j = 0; j < __arrayKeys.length; j++) {
					var __stringValue = __arrayData[__arrayKeys[j].replace(/(<!--\s)|(\s-->)/g, "").replace(/@/g, "")];
					__stringValue = typeof __stringValue == "undefined" ? "--" : __stringValue;
					__stringHtmlLoop = __stringHtmlLoop.replace(__arrayKeys[j], __stringValue);
				}
			}
			__stringHtmlHead += __stringHtmlLoop.replace(/_style=/g, "style=");
		}
		this._elementTarget.innerHTML = __stringHtmlHead + __stringHtmlFoot;
	};
	this.boot = function (__stringElementId, __arrayList, __functionProcess, __intInterval, __arrayUrl) {
		this._elementTarget = document.getElementById(__stringElementId);
		this._stringHtmlTemplate = this._elementTarget.innerHTML;
		this._arrayList = __arrayList;
		this._functionProcess = __functionProcess;
		this._intThread = ScriptCtrl.load(this._arrayList, this._fill.Bind(this), __intInterval);
	};
};
function fillZS() {
	var __hqLoader = new HQLoader();
	var __arrayName = {"RMBUSD": "美元兑人民币", "RMBEUR": "欧元兑人民币", "RMBJPY": "日元兑人民币", "RMBHKD": "港币兑人民币","RMBGBP":"英镑兑人民币"};
	var __arrayUrl = {
			"test": "http://finance.sina.com.cn/money/forex/hq/DINI.shtml"
	};
	__hqLoader.boot(
		"divZS",
		["RMBUSD", "RMBEUR", "RMBJPY", "RMBHKD","RMBGBP","h_RMBUSD", "h_RMBEUR", "h_RMBJPY", "h_RMBHKD","h_RMBGBP"],
		function (__stringKey) {
			var __arrayData = window["hq_str_" + __stringKey].split(",");
			__arrayData[51] = __arrayName[__stringKey];
			__arrayData[52] = (__arrayData[4] *  1 / 100).toFixed(4);
			var __arrayData_h = window["hq_str_h_" + __stringKey].split(",");
			__arrayData[53] = (__arrayData_h[4] *  1 / 100).toFixed(4);
			return __arrayData;
		},
		10 * 60 * 1000
	);
}
function fillSP() {
	var __hqLoader = new HQLoader();
	var __arrayName = {"RMBUSD": "美元兑人民币", "RMBEUR": "欧元兑人民币", "RMBJPY": "日元兑人民币", "RMBHKD": "港币兑人民币","RMBGBP":"英镑兑人民币"};
	var __arrayUrl = {
			"test": "http://finance.sina.com.cn/money/forex/hq/DINI.shtml"
	};
	__hqLoader.boot(
		"divSP",
		["RMBUSD", "RMBEUR", "RMBJPY", "RMBHKD","RMBGBP"],
		function (__stringKey) {
			var __arrayData = window["hq_str_" + __stringKey].split(",");
			__arrayData[51] = __arrayName[__stringKey];
			__arrayData[52] = (__arrayData[2] *  1 / 100).toFixed(4);
			__arrayData[53] = (__arrayData[3] *  1 / 100).toFixed(4);
			//~ if (typeof __arrayUrl != "undefined" && __stringKey in __arrayUrl && __arrayUrl[__stringKey] != "") {
				//~ __arrayData[53] = '<a href="' + __arrayUrl[__stringKey] + '" target="_blank">' + __arrayData[53] + '</a>';
			//~ }
			return __arrayData;
		},
		10 * 60 * 1000
	);
}

function loadScript(__objectLoader, __stringUrl, __functionCallback) {
	var __elementScript = document.createElement("script");
	__elementScript.type = "text/javascript";
	__elementScript.src = __stringUrl;
	var __stringCallback = "callback_" + (new Date()).getTime();
	window[__stringCallback] = __functionCallback;
	__elementScript._stringCallback = __stringCallback;
	__elementScript[document.all ? "onreadystatechange" : "onload"] = function () {
		if (document.all && this.readyState != "loaded" && this.readyState != "complete") {return;}
		if (window[this._stringCallback] != null) {
			window[this._stringCallback]();
		}
		window[this._stringCallback] = null;
		this._stringCallback = null;
		this[document.all ? "onreadystatechange" : "onload"] = null;
		this.parentNode.removeChild(this);
	};
	(typeof __objectLoader == "string" ? document.getElementById(__objectLoader) : __objectLoader).appendChild(__elementScript);
}

function callbackGGYHCJLL() {
	if ("ak_company" in window) {
		var __arrayData = window["ak_company"];
		for (var i = 0; i < __arrayData.length; i++) {
			var __stringKey = __arrayData[i][0]["unit"];
			if (",USD,EUR,JPY,GBP,CHF,AUD,CAD,".indexOf("," + __stringKey + ",") != -1 && __arrayData[i][0]["bank"] == "LIBOR") {
				document.getElementById("gy_" + __stringKey).innerHTML = __arrayData[i][0]["rate"] * 1 == 0 ? "--" : __arrayData[i][0]["rate"];
				document.getElementById("yz_" + __stringKey).innerHTML = __arrayData[i][1]["rate"] * 1 == 0 ? "--" : __arrayData[i][1]["rate"];
			}
		}
	}
	
}
function fillGGYHCJLL() {
	loadScript("for_loader", "http://money.finance.sina.com.cn/work/bank_cjll.php", callbackGGYHCJLL);
}


/* 支撑阻力 begin */
function submitZCZL(__form) {
	var __stringMoneyPair = __form["selectZCZL_money_pair"].value;
	var __stringPeriod = __form["selectZCZL_period"].value;
	if (__stringMoneyPair != "" && __stringPeriod != "") {
		window["forex_resistance"] = "";
		loadScript("for_loader", "http://biz.finance.sina.com.cn/forexcount/forex_interface.php?money_pair=" + __stringMoneyPair + "&period=" + __stringPeriod, callbackZCZL);
	}
	return false;
}
function callbackZCZL() {
	var __arrayResult = ["--", "--┊--┊--", "--┊--┊--"];
	if (window["forex_resistance"] != "") {
		__arrayForexResistance = window["forex_resistance"].split(",");
		__arrayResult = [__arrayForexResistance[0], __arrayForexResistance[2] + "┊" + __arrayForexResistance[4] + "┊" + __arrayForexResistance[6], __arrayForexResistance[1] + "┊" + __arrayForexResistance[3] + "┊" + __arrayForexResistance[5]];
	}
	var __arrayTarget = ["jj", "zc", "zl"];
	for (var i = 0; i < __arrayTarget.length; i++) {
		document.getElementById("spanZCZL_" + __arrayTarget[i]).innerHTML = __arrayResult[i];
	}
}
function autosubmitZCZL() {
	submitZCZL(document.getElementById("formZCZL"));
}

function changeRLCX(__form) {
	var __arraySelect = ["mg", "oy", "dg", "rb", "yg", "rs", "az", "jnd"];
	var __stringParent = __form["selectRLCX"].value;
	for (var i = 0; i < __arraySelect.length; i++) {
		__form["selectRLCX_" + __arraySelect[i]].style.display = __stringParent == __arraySelect[i] ? "" : "none";
	}
}
/* 支撑阻力 end */

/* 外汇计算器 begin */
function submitWHJUQ(__form) {
	var __arrayMoney = ["JPY", "CHF", "GBP", "EUR", "HKD", "AUD", "CAD", "USDCNY"];
	var __stringSelectFrom = __form["selectWHJUQ_from"].value;
	var __floatFrom = "USD" == __stringSelectFrom ? 1 : (window["hq_str_" + __stringSelectFrom].split(","))[8] * 1;
	var __floatFromName = "USD" == __stringSelectFrom ? "美元" : (window["hq_str_" + __stringSelectFrom].split(","))[9];
	if (("_EUR_GBP_AUD_").indexOf(__stringSelectFrom) != -1) {
		__floatFrom = 1 / __floatFrom;
	}
	var __stringSelectTo = __form["selectWHJUQ_to"].value;
	var __floatTo = "USD" == __stringSelectTo ? 1 : (window["hq_str_" + __stringSelectTo].split(","))[8] * 1;
	var __floatToName = "USD" == __stringSelectTo ? "美元" : (window["hq_str_" + __stringSelectTo].split(","))[9];
	if ("_EUR_GBP_AUD_".indexOf(__stringSelectTo) != -1) {
		__floatTo = 1 / __floatTo;
	}
	__form["inputWHJUQ_to"].value = (__form["inputWHJUQ_from"].value * 1 / __floatFrom * __floatTo).toFixed(4);
	document.getElementById("spanWHJUQ").innerHTML = __floatFromName + "兑" + __floatToName + " " + (__floatTo / __floatFrom).toFixed(4);
	return false;
}
/* 外汇计算器 end */

/* 人民币走势 begin */
var ImageHqHSZS = function () {
	this._elementImage = null;
	this._elementHq = null;
	this._stringHtmlTemplate = "";
	this._stringCode = "";
	this._intThreadImage = -1;
	this._intThreadHq = -1;
	this._random = function () {
		return (new Date()).getTime();
	};
	this._fill = function () {
		var __stringHtml = this._stringHtmlTemplate;
		var __arrayData = window["hq_str_" + this._stringCode].split(",");
		var __intChange = __arrayData[8] - __arrayData[3];
		var __booleanTwo = "_DINI_JPY_SFYN_ERYN_UKYN_CAYN_HKYN_AUYN_".indexOf("_" + this._stringCode + "_") != -1;
		__arrayData[8] = (__arrayData[8] * 1).toFixed(__booleanTwo ? 2 : 4);
		__arrayData[3] = (__arrayData[3] * 1).toFixed(__booleanTwo ? 2 : 4);
		__arrayData[5] = (__arrayData[5] * 1).toFixed(__booleanTwo ? 2 : 4);
		__arrayData[10] = (__intChange / __arrayData[3]).toFixed(4);
		__arrayData[11] = __intChange > 0 ? "#F00" : (__intChange < 0 ? "#008000" :  "#000");
		var __arrayKeys = __stringHtml.match(/(<!--\s)?@[^@]*@(\s-->)?/gm);
		if (__arrayKeys != null) {
			for (var j = 0; j < __arrayKeys.length; j++) {
				var __stringValue = __arrayData[__arrayKeys[j].replace(/(<!--\s)|(\s-->)/g, "").replace(/@/g, "")];
				__stringValue = typeof __stringValue == "undefined" ? "--" : __stringValue;
				__stringHtml = __stringHtml.replace(__arrayKeys[j], __stringValue);
			}
		}
		__stringHtml = __stringHtml.replace(/_style=/g, "style=");
		this._elementHq.innerHTML = __stringHtml;
	};
	this._loadImage = function () {
		this._elementImage.src = "http://image.sinajs.cn/newchart/v5/forex/min" + (this._stringCode == "USDCNY" ? "30" : "") + "_m/" + this._stringCode + ".gif?" + this._random();
	};
	this._loadHq = function () {
		loadScript("for_loader", "http://hq.sinajs.cn/rn=" + this._random() + "&list=" + this._stringCode, this._fill.Bind(this));
	};
	this._change = function (__stringCode) {
		if (typeof __stringCode == "undefined") {
			__stringCode = document.getElementById("selectHSZS").value;
		}
		this._stringCode = __stringCode;
		document.getElementById("linkHSZS").href = "http://finance.sina.com.cn/money/forex/hq/" + this._stringCode + ".shtml";
		if (this._intThreadImage != -1) {
			clearInterval(this._intThreadImage);
		}
		this._loadImage();
		this._intThreadImage = setInterval(this._loadImage.Bind(this), 60000);
		if (this._intThreadHq != -1) {
			clearInterval(this._intThreadHq);
		}
		this._loadHq();
		this._intThreadHq = setInterval(this._loadHq.Bind(this), 10000);
		return false;
	};
	this.boot = function () {
		this._elementImage = document.getElementById("imageHSZS");
		this._elementHq = document.getElementById("divHSZS");
		this._stringHtmlTemplate = this._elementHq.innerHTML;
		this._change("USDCNY");
		window['submitHSZS'] = this._change.Bind(this);
	};
};

function fillHSZS() {
	(new ImageHqHSZS()).boot();
}
/* 人民币走势 end */


/* 人民币牌价查询 begin */
function submitRMBPJCX(__form) {
	__form.action = "http://biz.finance.sina.com.cn/forex/forex.php?flag=0&money_code=" + __form["selectRMBPJCX_money"].value + "&type=" + __form["selectRMBPJCX_type"].value + "&startdate=" + __form["inputRMBPJCX_from_year"].value + "-" + __form["inputRMBPJCX_from_month"].value + "-" + __form["inputRMBPJCX_from_date"].value + "&enddate=" + __form["inputRMBPJCX_to_year"].value + "-" + __form["inputRMBPJCX_to_month"].value + "-" + __form["inputRMBPJCX_to_date"].value;
}
/* 人民币牌价查询 end */

/* 全球时间 begin */
var GlobalTime = function () {
	this._arrayStringPlace = ["BeiJing", "London", "NewYork", "Sydney"];
	this._arrayIntPlace = [0, getGlobalTimezone("UK"), getGlobalTimezone("US"), getGlobalTimezone("AU") + 1];
	this._date = null;
	this._arrayElementPlace = [];
	this._intThreadHq = -1;
	this._random = function () {
		return (new Date()).getTime();
	};
	this._fill = function () {
		this._date = new Date(this._date.getTime() + 1000);
		for (var i = 0; i < this._arrayIntPlace.length; i++) {
			var __date = new Date(this._date.getTime() + this._arrayIntPlace[i] * 3600000);
			this._arrayElementPlace[i].innerHTML = __date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
		}
	};
	this._start = function () {
		this._date = new Date(window["StandardBJTime"] * 1000);
		if (this._intThreadHq != -1) {
			clearInterval(this._intThreadHq);
		}
		this._fill();
		this._intThreadHq = setInterval(this._fill.Bind(this), 1000);
	};
	this.boot = function () {
		for (var i = 0; i < this._arrayStringPlace.length; i++) {
			this._arrayElementPlace.push(document.getElementById(this._arrayStringPlace[i]));
		}
		loadScript("for_loader", "http://counter.sina.com.cn/time?fm=JS&rn=" + this._random(), this._start.Bind(this));
	};
};

function fillTime() {
	var globalTime = new GlobalTime();
	globalTime.boot();
}
/* 全球时间 end */

