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));
	}
}

function GetOffsetPos(element) {
	var posTop = 0, posLeft = 0;
	do {
		posTop += element.offsetTop || 0;
		posLeft += element.offsetLeft || 0;
		element = element.offsetParent;
		} while (element);
	return [posLeft, posTop];
}

var isIE6 = false;
var userAgent = navigator.userAgent.toLowerCase();
if ((userAgent.indexOf('msie') != -1) && (userAgent.indexOf('opera') == -1) && (userAgent.indexOf('msie 7.0') == -1)) {
	isIE6 = true;
}
//------------------------------------------------------------------------------
//table over effect
//------------------------------------------------------------------------------
var OverEffect = {
	Init: function () {
		this.rows = $("data_tbl").tBodies[0].rows;
		for (var i = 0; i < this.rows.length; i++) {
			this.rows[i].onmouseover = this._show.BindForEvent(this, i);
			this.rows[i].onmouseout = this._cancel.BindForEvent(this);
		};
	},
	
	_show: function (evt, _index) {
		if (this.tr_now && this.tr_now.className == "over") {
			this.tr_now.className = "";
		}
		this.rows[_index].className = "over";
		this.tr_now = this.rows[_index];
	},
	_cancel: function () {
		if (this.tr_now) {
			this.tr_now.className = "";
		}
	}
}

//------------------------------------------------------------------------------
//title follow
//------------------------------------------------------------------------------
var TitlesFollow = {
	Init: function () {
		this.titleTbl = $("titles_tbl");
		this.titleTbl_XY = GetOffsetPos(this.titleTbl);
		window.onscroll = this.scrollControl.Bind(this);
	},

	scrollControl: function () {
		if (document.documentElement.scrollTop > this.titleTbl_XY[1]) {
			if (isIE6) {
				this.titleTbl.style.top = document.documentElement.scrollTop - this.titleTbl_XY[1] + "px";
			}
			else if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1){
				this.titleTbl.style.cssText = "position: fixed; top:0px;";
			}
			else {
				this.titleTbl.style.cssText = "position: fixed; top:0px; left:" + this.titleTbl_XY[0] + "px;";
			}
		}
		else if (document.documentElement.scrollTop <= this.titleTbl_XY[1]) {
			this.titleTbl.style.cssText = "";
		}
	}
}


//------------------------------------------------------------------------------
// Cookie Related
//------------------------------------------------------------------------------

var Cookie = function(){};

Cookie.prototype = {
	_items: {},
	_nItems: 0,

	_init: function() {
		if (document.cookie.length == 0) {
			return;
		}
		
		var cookiesArr = document.cookie.split(';');
		var temp;
		this._nItems = cookiesArr.length;

		if (this._nItems == 0) {
			return;
		}
		
		for (var i = 0; i < this._nItems; i++) {
			temp = cookiesArr[i].split('=');
			if (temp.length != 2) {
				continue;
			}
			this._items[temp[0].trim()] = temp[1].trim();
		}
	},
	
	get: function(name) {
		this._init();
		
		if (typeof this._items[name] != 'undefined') {
			return unescape(this._items[name]);
		}
		
		return '';
	},
	
	set: function(name, value, expire, path, domain, secure) {
		if (typeof name == 'undefined') {
			return false;
		}
		
		if (typeof value == 'undefined') {
			return false;
		}
		
		var cookieStr = name + '=' + escape(value) + '; ';
		
		if (typeof expire != 'undefined') {
			cookieStr += 'expires=' + expire + '; '
		}
		
		if (typeof path != 'undefined') {
			cookieStr += 'path=' + path + '; '
		}
		
		if (typeof domain != 'undefined') {
			cookieStr += 'domain=' + domain + '; ';
		}
		
		if (secure == true) {
			cookieStr += 'secure;';
		}
		
		document.cookie = cookieStr;
	},
	
	remove: function(name) {
		document.cookie = name + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
	},
	
	getCount: function() {
		this._init();
		return this._nItems;
	}
};

//------------------------------------------------------------------------------
// Last visited stocks begin
// Dependence: Cookie
//------------------------------------------------------------------------------
var __VISITED_FUTURES_NUM = 10;
var __VISITED_FUTURES_COOKIE_NAME = 'lipper_focused';

var VisitedStocks = function()
{
	this.Init.apply(this, arguments);
};

VisitedStocks.prototype = {
	_stocks: [],
	_cookie: null,
	
	Init: function() {
		this._cookie = new Cookie();
		
		var stocksStr = this._cookie.get(__VISITED_FUTURES_COOKIE_NAME);
		if (stocksStr.length != 0) {
			this._stocks = stocksStr.split('|');
		}
	},
	
	_findStock: function(code) {
		var nStocks = this._stocks.length;
		for (var i = 0; i < nStocks; i++) {
			if (this._stocks[i] == code) {
				return i;
			}
		}
		
		return -1;
	},
	
	set: function(code) {
		var pos = this._findStock(code);
		if (pos != -1) {
			this._stocks.splice(pos, 1);
		}
		
		this._stocks.unshift(code);
		
		if (this._stocks.length > __VISITED_FUTURES_NUM) {
			this._stocks.pop();
		}
		
		this._cookie.set(__VISITED_FUTURES_COOKIE_NAME, this._stocks.join('|'), 'Fri, 31 Dec 2099 23:59:59 GMT', '/', 'finance.sina.com.cn');
	},
	
	getAll: function() {
		return this._stocks;
	},
	
	clear: function() {
		this._cookie.remove(__VISITED_FUTURES_COOKIE_NAME);
	}
};

//------------------------------------------------------------------------------
//focus related
//------------------------------------------------------------------------------
var FocusdFunda = {
	Init: function () {
		this.rows = $("data_tbl").tBodies[0].rows;
		for (var i = 0; i < this.rows.length; i++) {
			this.rows[i].cells[0].firstChild.onclick = this.statusChange.Bind(this, this.rows[i].cells[0].firstChild);
		};
	},
	
	statusChange: function (_obj) {
		if (_obj.className == "unfocused") {
			_obj.className = "focused";
			_obj.innerHTML = "已关注";
			_obj.title = "点击取消关注";
		}
		else {
			_obj.className = "unfocused";
			_obj.innerHTML = "未关注";
			_obj.title = "点击关注该基金";
		}
	}
}



//------------------------------------------------------------------------------
//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);
};


//------------------------------------------------------------------------------
//execution
//------------------------------------------------------------------------------
function main() {
	//window.nav = new Nav(
	//	[
	//		["http://finance.sina.com.cn/", "财经首页"],
	//		["http://finance.sina.com.cn/futuremarket/", "期货首页"],
	//		["http://finance.sina.com.cn/stock/indexfuture/", "股指期货"],
	//		["http://finance.sina.com.cn/futuremarket/yspzx.html", "期指资讯"],
	//		["http://finance.sina.com.cn/futuremarket/gold.html", "黄金"],
	//		["http://finance.sina.com.cn/column/futuresnyzx.html", "能源"],
	//		["http://finance.sina.com.cn/futuremarket/indu.html", "工业品"],
	//		["http://finance.sina.com.cn/futuremarket/agri.html", "农产品"],
	//		["http://q.blog.sina.com.cn/qihuoblog/", "期货圈"],
	//		["http://bar.sina.com.cn/bar.php?name=%C6%DA%BB%F5", "期货吧"],
	//		["http://biz.finance.sina.com.cn/hq/", "行情中心"],
	//		["http://vip.stock.finance.sina.com.cn/portfolio/main.php", "自选股"]
	//	]
	//);

	OverEffect.Init();
	TitlesFollow.Init();
	//FocusdFunda.Init();	//注释掉关注功能
}