function $(objName){
	if(document.getElementById){
		return document.getElementById(objName);
	}else{
		return document.all.objName;
	}
}
if( typeof $C == 'undefined' )$C = function(t){return document.createElement(t)};

Function.prototype.Bind = function() { 
	var __m = this, object = arguments[0], args = new Array(); 
	for(var i = 1; i < arguments.length; i++){
		args.push(arguments[i]);
	}
	
	return function() {
		return __m.apply(object, args);
	}
};

Function.prototype.BindForEvent = function() { 
	var __m = this, object = arguments[0], args = new Array();
	for(var i = 1; i < arguments.length; i++){
		args.push(arguments[i]);
	}
	
	return function(event) {
		return __m.apply(object, [( event || window.event)].concat(args));
	}
}

var SortFunctions = {
	compareArr: function (_index) {
		return function compareArr(a, b) {
			var value1 = a[_index] * 1, value2 = b[_index] * 1;

			if (value1 > value2) {
				return -1;
			}
			else if (value1 < value2) {
				return 1;
			}
			else {
				return 0;
			}
		};
	},
	compareFloat: function (_index) {
		return function compareFloat(a, b) {
			if (a.cells[_index].firstChild.nodeType == 1) {
				var value1 = a.cells[_index].firstChild.innerHTML * 1, value2 = b.cells[_index].firstChild.innerHTML * 1;
			}
			else {
				var value1 = a.cells[_index].innerHTML * 1, value2 = b.cells[_index].innerHTML * 1;
			}

			if (value1 > value2) {
				return -1;
			}
			else if (value1 < value2) {
				return 1;
			}
			else {
				return 0;
			}
		};
	},
	compareRate: function (_index) {
		return function compareRate(a, b) {
			var value1 = a.cells[_index].firstChild.innerHTML.replace("%", "") * 1, value2 = b.cells[_index].firstChild.innerHTML.replace("%", "") * 1;

			if (value1 > value2) {
				return -1;
			}
			else if (value1 < value2) {
				return 1;
			}
			else {
				return 0;
			}
		};
	}
}

var _timeout = -1;
var SinaIndexes = {
	classUrl: "http://vip.stock.finance.sina.com.cn/q/view/newFLJK.php?param=class",
	areaUrl: "http://vip.stock.finance.sina.com.cn/q/view/newFLJK.php?param=area",
	companyUrl: "http://vip.stock.finance.sina.com.cn/q/view/newFLJK.php?param=company",
	industryUrl: "http://vip.stock.finance.sina.com.cn/q/view/newFLJK.php?param=industry",
	sinaIndustryUrl: "http://vip.stock.finance.sina.com.cn/q/view/newSinaHy.php",
	qmxIndustryUrl: "http://biz.finance.sina.com.cn/hq/qmxIndustryHq.php",
	setUrl: false,
	boardLink: '',
	stockLink: '<a href="http://finance.sina.com.cn/realstock/company/@CODE@/nc.shtml" target="_blank">@TEXT@</a>',
	__up_color: '#F00',
	__down_color: '#008000',
	__stable_color: '#000',
	__refreshType : 'auto',
	Init: function (type , refreshType) {
		this.__refreshType = refreshType;
		this.boardLink = '<a href="http://vip.stock.finance.sina.com.cn/mkt/#gainian_@CODE@" target="_blank">@TEXT@</a>';
		window.clearTimeout(_timeout);
		this.reset(type);
		this.type = type;
		if (!this.setUrl) {
			switch (this.type) {
			case "area":
				this.boardLink = '<a href="http://vip.stock.finance.sina.com.cn/mkt/#diyu_@CODE@" target="_blank">@TEXT@</a>';
				this._url = this.areaUrl;
				this.paramName = "S_Finance_bankuai_area";
				document.title ="地域板块_板块行情_新浪财经_新浪网";
				break;
			case "company":
				this.boardLink = '<a href="http://biz.finance.sina.com.cn/hq/index.php?page=stock_list.php?company=@CODE@" target="_blank">@TEXT@</a>';
				this._url = this.companyUrl;
				this.paramName = "S_Finance_bankuai_company";
				document.title ="同系板块_板块行情_新浪财经_新浪网";
				break;
			case "industry":
				this.boardLink = '<a href="http://vip.stock.finance.sina.com.cn/mkt/#hangye_@CODE@" target="_blank">@TEXT@</a>';
				this._url = this.industryUrl;
				this.paramName = "S_Finance_bankuai_industry";
				document.title ="行业板块_板块行情_新浪财经_新浪网";
				break;
			case "sinaindustry":
				this.boardLink = '<a href="http://vip.stock.finance.sina.com.cn/mkt/#@CODE@" target="_blank">@TEXT@</a>';
				this._url = this.sinaIndustryUrl;
				this.paramName = "S_Finance_bankuai_sinaindustry";
				document.title ="新浪行业_板块行情_新浪财经_新浪网";
				break;
			case "qmxindustry":
				this.boardLink = "<a href='http://biz.finance.sina.com.cn/qmx/industry.php?industry=@CODE@'>@TEXT@</a>";
				this._url = this.qmxIndustryUrl;
				this.paramName = "S_Finance_bankuai_qmxindustry";
				document.title ="启明星行业_板块行情_新浪财经_新浪网";
				break;
			default:
				this._url = this.classUrl;
				this.paramName = "S_Finance_bankuai_class";
				document.title ="概念板块_板块行情_新浪财经_新浪网";
			}
			//this.setUrl = true;
		}
		
		this._table = $("datatbl");
		/*
		this._table.removeChild(this._table.tBodies[0]);
		var _tbody = $C("tbody");
		var _tr = $C("tr");
		_tbody.appendChild(_tr);
		var _th = $C("th");
		_th.colSpan = 11;
		_th.style.verticalAlign = "middle";
		_th.style.height = "200px";
		_th.innerHTML = '数据读取中，请稍候...<br /><img src="http://i0.sinaimg.cn/cj/money/images/progress0.gif" />';
		_tr.appendChild(_th);
		this._table.appendChild(_tbody);
		*/
		var sLoader = new IO.Script();
		sLoader.load(this._url, this.update.Bind(this));
	},
	reset: function(industryId){
		var ulNodes = $("industrynav").childNodes;
		for(i=0; i < ulNodes.length; i ++)
		{
			if(ulNodes[i].nodeType ==1)
			{
				ulNodes[i].className = "";
				//alert(ulNodes[i].id);
				//ulNodes[i].onclick = this.Init.BindForEvent(this , ulNodes[i].id);
			}
		}
		$(industryId).className ="active";
    },
	update: function () {
		var _data = window[this.paramName];
		this._dataArr = [];
		for (var i in _data) {
			this._dataArr.push(_data[i].split(","));
		};

		if (SortTable.activeCol) {
			if (!SortTable.readyToOri) {
				this._dataArr.sort(SortFunctions.compareArr(SortTable.activeCol + 1));
			}
			else {
				this._dataArr.sort(SortFunctions.compareArr(SortTable.activeCol + 1)).reverse();
			}
		}
		else {
			this._dataArr.sort(SortFunctions.compareArr(5));
		}

		this.showTable();
		if(this.__refreshType =='auto'){
			_timeout = window.setTimeout(this.Init.Bind(this , this.type , this.__refreshType), 10000);
		}
	},
	showTable: function () {
		var _tbody = $C("TBODY");
		for (var i = 0; i < this._dataArr.length; i++) {
			var _tr = $C("TR");
			for (var j = 1; j < this._dataArr[i].length - 1; j++) {
				var _td = $C("TD");
				switch (j) {
				case 1:
					_td.innerHTML = this.boardLink.replace("@CODE@", this._dataArr[i][0]).replace("@TEXT@", this._dataArr[i][1]);
					break;
				case 2:
					_td.innerHTML = this._dataArr[i][j];
					break;
				case 6:
					_td.innerHTML = (this._dataArr[i][j] * 1 / 100).toFixed(2);
					break;
				case 7:
					_td.innerHTML = (this._dataArr[i][j] * 1 / 10000).toFixed(2);
					break;
				case 8:
					_td.className = "border";
					_td.innerHTML = this.stockLink.replace("@CODE@", this._dataArr[i][8]).replace("@TEXT@", this._dataArr[i][12]) + " (" + this._dataArr[i][8] + ")";
					break;
				default:
					if (j >= 2 && j <= 5) {
						if (j != 5) {
							_td.innerHTML = this.colorRender((this._dataArr[i][j] * 1).toFixed(2), this._dataArr[i][5]);
						}
						else {
							_td.innerHTML = this.colorRender((this._dataArr[i][j] * 1).toFixed(2) + "%", this._dataArr[i][5]);
						}
					}
					else {
						if (j != 9) {
							_td.innerHTML = this.colorRender((this._dataArr[i][j] * 1).toFixed(2), this._dataArr[i][9]);
						}
						else {
							_td.innerHTML = this.colorRender((this._dataArr[i][j] * 1).toFixed(2) + "%", this._dataArr[i][9]);
						}
					}
				}
				_tr.appendChild(_td);
			};
			_tbody.appendChild(_tr);
		};

		this._table.removeChild(this._table.tBodies[0]);
		this._table.appendChild(_tbody);

		SortTable.Init();
	},
	colorRender: function (targetText,value) {
		value = parseFloat(value);
		if (value > 0) {
			var color = this.__up_color;
		} else if (value < 0) {
			var color = this.__down_color;
		} else {
			var color = this.__stable_color;
		}
		return '<span style="color:' + color + ';">' + targetText + '</span>';
	}
}


var SortTable = {
	initialed: false,
	_TRs: [],
	tdThead: [],
	activeCol: null,
	readyToOri: null,
	Init: function () {
		this._table = $("datatbl");
		var _TRs = this._table.tBodies[0].rows;
		var _thead = this._table.firstChild.tagName == "THEAD" ? this._table.firstChild : this._table.firstChild.nextSibling;
		this._TRs = [];
		for (var i = 0; i < _TRs.length; i++) {
			this._TRs.push(_TRs[i]);
		};

		if (!this.initialed) {
			for (var i = 0; i < _thead.rows[0].cells.length; i++) {
				this.tdThead.push(_thead.rows[0].cells[i]);
				if (i != 0 & i != 7) {
					this.tdThead[i].firstChild.onclick = this.start.BindForEvent(this, i);
				}
			};
			this.initialed = true;
		}
		else if (this.activeCol){
			for (var i = 0; i < this._TRs.length; i++) {
				this._TRs[i].cells[this.activeCol].className = "sorting";
			};
		}
		
	},
	start: function (obj, _index) {
		if (_index != this.activeCol) {
			if (this.activeCol) {
				this.tdThead[this.activeCol].lastChild.className = "";
				for (var i = 0; i < this._TRs.length; i++) {
					this._TRs[i].cells[this.activeCol].className = "";
				};
			}
			
			this.activeCol = _index;
			this.readyToOri = null;
			this.tdThead[_index].lastChild.className = "down";
			if (_index != 4 && _index != 8) {
				this._TRs.sort(SortFunctions.compareFloat(_index));
			}
			else {
				this._TRs.sort(SortFunctions.compareRate(_index));
			}
			this.insertTbody(_index, true);
		}
		else if (!this.readyToOri){
			this.readyToOri = true;
			this._TRs.reverse();
			this.insertTbody(_index, true);
			this.tdThead[_index].lastChild.className = "up";
		}
		else {
			this.readyToOri = null;
			this.tdThead[this.activeCol].lastChild.className = "";
			for (var i = 0; i < this._TRs.length; i++) {
				this._TRs[i].cells[this.activeCol].className = "";
			};
			this.activeCol = null;
			this._TRs.sort(SortFunctions.compareRate(4));
			this.insertTbody(_index, false);
		}
	},
	insertTbody: function (_index, colorize) {
		var _fragment = document.createDocumentFragment();
		for (var i = 0; i < this._TRs.length; i++) {
			if (colorize) {
				this._TRs[i].cells[_index].className = "sorting";
			}
			_fragment.appendChild(this._TRs[i]);
		};
		this._table.tBodies[0].appendChild(_fragment);
	}
}


//------------------------------------------------------------------------------
//navigation top
//------------------------------------------------------------------------------
var Nav = function (arrayData) {
	this.element = $("nav");
	var table = $C("table");
	table.className = "table";
	table.cellPadding = 0;
	table.cellSpacing = 0;
	table.border = 0;
	var line = table.insertRow(0);
	for (var i = 0; i < arrayData.length; i++) {
		var cell = line.insertCell(i);
		cell.className = i == 0 ? "index" : "off";
		cell.onmouseover = function () {
			this.className = this.className == "index" ? "index" : "on";
		};
		cell.onmouseout = function () {
			this.className = this.className == "index" ? "index" : "off";
		};
		var link = $C("a");
		link.target = "_blank";
		link.href = arrayData[i][0];
		link.innerHTML = arrayData[i][1];
		cell.appendChild(link);
	}
	this.element.appendChild(table);
};

function generateNav() {
	window.nav = new Nav(
		[
			["http://finance.sina.com.cn/", "财经首页"],
			["http://finance.sina.com.cn/stock/index.shtml", "股票首页"],
			["http://stock.finance.sina.com.cn/", "行情"],
			["http://finance.sina.com.cn/column/jsy.html", "大盘"],
			["http://finance.sina.com.cn/column/ggdp.html", "个股"],
			["http://finance.sina.com.cn/column/ywgg.html", "要闻"],
			["http://finance.sina.com.cn/column/ssgs.html", "公司"],
			["http://finance.sina.com.cn/stock/newstock/index.shtml", "新股"],
			["http://finance.sina.com.cn/column/data.html", "数据"],
			["http://finance.sina.com.cn/stock/reaserchlist.shtml", "报告"],
			["http://club.finance.sina.com.cn/", "论坛"],
			["http://finance.sina.com.cn/stock/usstock/index.shtml", "美股"],
			["http://finance.sina.com.cn/stock/hkstock/index.shtml", "港股"],
			["http://biz.finance.sina.com.cn/hq/", "行情中心"],
			["http://vip.stock.finance.sina.com.cn/portfolio/main.php", "自选股"]
		]
	);
}

