(function(){
	var $  = function(o) { return typeof(o) == 'string' ? document.getElementById(o) : o; };
	var $C = function(s) { return document.createElement(s); };
	var $E = function(e) { tempObj = e.target ? e.target : event.srcElement; return tempObj; };
	var addEvent = function (elm, evType, fn, useCapture){
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	};
	if(!checkaa && !window.checkaa) {
		var checkaa = function(){return true;};
		window.checkaa = checkaa;
	}
	var delEvent = function(elm, evType, fn, useCapture){
		if (elm.removeEventListener) {
			elm.removeEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.detachEvent) {
			var r = elm.detachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = null;
			return;
		}
	};
	Function.prototype.bind=function(object,args){
	  args=args==null?[]:args;
	  var __method=this;
	  return function(){__method.apply(object,args)}
	};
	var LoginSelect = {
		iForm : "showLogin",
		iUser : "username",
		iPass : "password",
		iFldselect : "loginFldselect",
		iFldselectdis : null,
		iValObj : null,
		OptList : null,
		option : [
			{text:"通行证",value:"1",action:"http://passport.eastmoney.com/SLogin.EmUser",un:"username",pw:"password"},
			{text:"",value:"",action:"",un:"",pw:""},
			{text:"博客",value:"2",action:"http://passport.eastmoney.com/SLogin.EmUser",un:"username",pw:"password"},
			{text:"股吧",value:"3",action:"http://passport.eastmoney.com/SLogin.EmUser",un:"username",pw:"password"},
			{text:"财迷",value:"4",action:"http://passport.eastmoney.com/SLogin.EmUser",un:"username",pw:"password"}
		],
		getCookie: function(key){
	    var cookieArray = document.cookie.split(";"); 
	    for(var i = 0; i < cookieArray.length; i++){ 
	        var arr = cookieArray[i].split("="); 
	        if(arr[0].replace(" ","") == key)
	            return unescape(decodeURI(arr[1]));
	    }
	    return ""; 
		},
		LogOut: function(){
			var date = new Date();
			document.cookie = "dcusername=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuserinfo=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcusermingchen=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuserpass=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuserpubinfo=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuserpubs=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuserkeys=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_name=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_info=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_mingchen=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_pass=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_pubinfo=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_pubs=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "dcuser_keys=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "puser_pname=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "puser_pinfo=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			document.cookie = "pi=;path=/;domain=eastmoney.com;expires=" + date.toGMTString();
			location.reload();
		},
		init:function(){
			$(this.iForm).action = "http://passport.eastmoney.com/SLogin.EmUser";
			$(this.iUser).name = "username";
			$(this.iPass).name = "password";
			var cVal = this.getCookie("pi");
			if(cVal && cVal!=null && cVal!=""){
				$(this.iForm).style.display = "none";
				var cVals = cVal.split(";");
				var uId   = cVals[0];
				var uName = cVals[1];
				var uNick = cVals[2];
				var showTxt = $C("div");
				showTxt.className = "userState";
				showTxt.innerHTML = "<span style=\"float:left; margin-left:8px;\">欢迎您，<b>"+uNick+"</b> <a href=\"http://passport.eastmoney.com/UserCenter.EmUser\" target=\"_blank\">用户中心</a> <a href=\"http://control.blog.eastmoney.com/control/blog_add.aspx\" target=\"_blank\">发博文</a> <a href=\"http://guba.eastmoney.com/\" target=\"_blank\">进股吧</a> <a href=\"http://t.eastmoney.com/\" target=\"_blank\">围观财迷</a></span><span style=\"float:right;\"><a href=\"javascript:void(0);\" onclick=\"LoginSelect.LogOut();\" target=\"_self\">安全退出</a></span>";
				$(this.iForm).parentNode.insertBefore(showTxt, $(this.iForm));
			}else{
				$(this.iForm).style.display = "block";
				var loginFldselectBar = $C("div");
				loginFldselectBar.className = "loginFldselectBar";
				
				this.iFldselectdis = $C("span");
				this.iFldselectdis.id = "loginFldselectdis";
				this.iFldselectdis.innerHTML = this.option[0].text;
				loginFldselectBar.appendChild(this.iFldselectdis);
				
				var icoSplit = $C("span");
				icoSplit.className = "ico split";
				loginFldselectBar.appendChild(icoSplit);
				var icoDown = $C("span");
				icoDown.className = "ico down";
				loginFldselectBar.appendChild(icoDown);
		
				$(this.iFldselect).innerHTML = "";
				$(this.iFldselect).appendChild(loginFldselectBar);
				addEvent(loginFldselectBar,"click",this.clickLoginSelect.bind(this));
				
				this.OptList = $C("ul");
				this.OptList.className = "loginFldselectop";
				this.OptList.style.display = "none";
				this.OptList.style.opacity = 0.9;
				this.OptList.style.filter = "alpha(opacity:90)";
				var _optList = "";
				for(var i in this.option){
					if(this.option[i].value == "" && this.option[i].text == ""){
						_optList+="<li class=\"loginFldselectop_line\"></li>";
					}else{
						_optList+="<li><a onclick=\"LoginSelect.Select('"+this.option[i].value+"')\" target=\"_self\" href=\"javascript:void(0);\">"+this.option[i].text+"</a></li>";
					}
				}
				this.OptList.innerHTML = _optList;
				$(this.iFldselect).appendChild(this.OptList);
						
				this.iValObj = $C("input");
				this.iValObj.type = "hidden";
				this.iValObj.name = "select1";
				this.iValObj.value = "1";
				$(this.iFldselect).appendChild(this.iValObj);
			}
		},
		clickLoginSelect:function(){
			if(this.OptList.style.display == "block"){
				this.LoginSelectClose();
			}else{
				this.OptList.style.display = "block";
				addEvent(document.body,"mousedown",this.LoginSelectClickOther);
			}
		},
		LoginSelectClickOther:function (e){
			thisObj = e.target?e.target:event.srcElement;
			do{
				if(thisObj.id == "loginFldselect" || thisObj == $("user_pass")) return;
				if(thisObj.tagName == "BODY"){LoginSelect.LoginSelectClose();return;};
				thisObj = thisObj.parentNode;
			}while(thisObj.parentNode);
		},
		LoginSelectClose:function (){
			this.OptList.style.display = "none";
			delEvent(document.body,"mousedown",this.LoginSelectClickOther);
		},
		Select:function (title){
			var titleName;
			for(var i in this.option){
				if(this.option[i].value == title){
					titleName = this.option[i].text;
					this.iValObj.value = this.option[i].value;
					$(this.iForm).action = this.option[i].action;
					$(this.iUser).name = this.option[i].un;
					$(this.iPass).name = this.option[i].pw;
					break;
				}
			}
			this.iFldselectdis.innerHTML = titleName;
			this.LoginSelectClose();
		}
	};
	window.LoginSelect = LoginSelect;
})();

