//通行证----跨域
/*
Author:Andyfoo
Email:andyfoo@163.com
Company:huacai.cn
*/
/*
passport.huacai.com/sso/crossdomain.jsp
passport.huacai.cn/sso/crossdomain.jsp
###########################

HC_LOGIN_TYPE=登录类型：email/nickname/mobile
HC_ACCOUNT=登录帐号，base64编码(email/nickname/mobile)
HC_BBS_SIGN=BBS认证签名信息
HC_BLOG_SIGN=BLOG认证加密信息
HC_CMS_SIGN=CMS认证加密信息
*/
function XPassport(name){
	var self = this;
	this.name = name;  //实例名称
	this.allowDomain = new Array("huacai.com", "huacai.cn", "", "");
	this.domain = this.getBaseDomain();
	this.urls = new Array();
	this.urls_status = new Array();
	this.urls_frames = new Array();

	this.body = document.getElementsByTagName("BODY").item(0);
	//this.body = document.getElementsByTagName("HEAD")[0];
	//this.body = document.documentElement;
	this.auth = "";
	this.plang = "jsp";

	this.LoginTimeout = 15 * 1000;
	this.LoginStatus = false;
}
XPassport.prototype.init = function(urls, auth, plang){
	
	if(this.domain == "huacai.cn" || this.domain == "huacai.com"){
		
	}else{
		urls.push("http://"+this.getDomain()+"/uc/sso/setcookie.jsp?host=empty")
	}
	
	this.urls = urls;
	this.auth = auth;
	this.plang = plang;
	
}
XPassport.prototype.get_auth = function(){
	auth = "HC_LOGIN_TYPE=" + this.readCookie("HC_LOGIN_TYPE") 
			+ "&HC_ACCOUNT=" + this.readCookie("HC_ACCOUNT") 
			+ "&HC_BBS_SIGN=" + this.readCookie("HC_BBS_SIGN") 
			+ "&HC_BLOG_SIGN=" + this.readCookie("HC_BLOG_SIGN") 
			+ "&HC_WEB_SIGN=" + this.readCookie("HC_WEB_SIGN") 
		    + "&LOGIN_SIGN=" + this.readCookie("LOGIN_SIGN") 
		    + "&HC_ARENA_SIGN=" + this.readCookie("HC_ARENA_SIGN") 
		    + "&WEB_CHECK=" + this.readCookie("WEB_CHECK") 
			+ "&HC_CMS_SIGN=" + this.readCookie("HC_CMS_SIGN");
	return auth;
}
XPassport.prototype.login = function(action){
	if(action && action == 'login' && !this.auth)this.auth = this.get_auth();
	for(var i = 0; i < this.urls.length; i++){
		this.urls_status[i] = 0;
		var url = this.urls[i];
		if(url.indexOf("?") != -1){
			url = url + "&ppid=" + i;
		}else{
			url = url + "?ppid=" + i;
		}
		url = url + "&action=" + action + "&plang=" + this.plang + "&domain=" + this.getDomain() + "&" + this.auth;
		this.createIframe(i, url);
	}
	var _this = this;
	window.setTimeout(function(){
			try{
				if(!_this.LoginStatus){
					_this.cleanIframe();
					_this.OnLoginTimeout();
				}
					
			}catch(e){}
	}, this.LoginTimeout);
}

XPassport.prototype.OnLoginSuccess = function(num, maxNum){
	var isLogin = (num = maxNum);
}
XPassport.prototype.OnLoginTimeout = function(){

}
XPassport.prototype.cleanIframe = function(id){
	for(var i in this.urls_frames){
		this.body.removeChild(this.urls_frames[i]);
	}
	this.urls_frames = new Array();
}
XPassport.prototype.IframeOnLoad = function(id){
	id = parseInt(id, 10);
	this.urls_status[id] = 1;
	var num = 0;
	for(var i in this.urls_status){ 
		if(typeof(this.urls_status[i]) == "number")num += this.urls_status[i];
	}
	try{
		this.LoginStatus = (num == this.urls_status.length);
		
		this.OnLoginSuccess(num, this.urls_status.length);
	}catch(e){

	}
}
XPassport.prototype.createIframe = function(id, url){
	try{
		var _this = this;
		var _iframe = null;
		var _iframe_id = "x_passport_frame_"+id;
		//style="display:none;"
		
		_iframe = document.createElement("IFRAME");
		_iframe.id = _iframe_id;
		_iframe.name = _iframe_id;
		_iframe.src=url;
		_iframe.frameborder = 0;
		_iframe.scrolling="no";
		_iframe.style.border = 0;
		_iframe.width = 0;
		_iframe.height = 0;

		//_iframe.style.border="1px";_iframe.style.borderStyle ="solid"; _iframe.style.borderColor="#000";_iframe.width = 510;_iframe.height = 310;
		//_iframe.style.display="none";
		//_iframe.style.position = "absolute";
		this.body.appendChild(_iframe);
		this.urls_frames[id] = _iframe;
		
	}catch(e){
		//alert(e)
	}
}
XPassport.prototype.getDomain = function(){
	var domain = document.domain;
	var port = document.location.port;
	return port && port != 80 ? domain + ":" + port : domain;
}
XPassport.prototype.getBaseDomain = function(){
	var domain=document.domain.split(".");
	var len=domain.length;
	//var port = document.location.port;
	if(len<=2){
		return document.domain ;
	}
	if(document.domain.indexOf(".com.cn")>-1 || document.domain.indexOf(".net.cn")>-1 || document.domain.indexOf(".org.cn")>-1 || document.domain.indexOf(".gov.cn")>-1){
		return domain[len-3]+"."+domain[len-2]+"."+domain[len-1];
	}
	return domain[len-2]+"."+domain[len-1];

	// + (port && port == 8080 ? ":" + port : "")
}

// alert( readCookie("myCookie") );
XPassport.prototype.readCookie = function(name)
{
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0)
	{ 
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
				cookieValue = ((document.cookie.substring(offset, end)))
		}
	}
	return cookieValue;
}
// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
XPassport.prototype.writeCookie = function(name, value, hours, domain)
{
	var expire = "";
	if(hours != null)
	{
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(escape(value)) + expire + "; domain=."+domain+"; path=/; ";
	
}
XPassport.prototype.writeLocCookie = function(name, value, hours)
{
	this.writeCookie(name, value, hours, this.domain);
}


//#################################
var passport = new XPassport("passport");
var LoginGoUrl = "";
passport.init(
	[
		"http://passport.huacai.com/uc/sso/setcookie.jsp?host=.huacai.com",
		"http://passport.huacai.cn/uc/sso/setcookie.jsp?host=.huacai.cn"
	],
	"",
	"jsp"
);

passport.OnLoginSuccess = function (num, maxNum){ 
	var isLogin = (num == maxNum);
	//window.status = window.status+"  *  "+(num+">ok");
	if(isLogin){
		//window.status = window.status +"  *  "+("登录成功");
		window.location = LoginGoUrl;
	}
}
passport.OnLoginTimeout = function (){
	//window.status=("登录超时，请稍后重试")
	window.location = LoginGoUrl;
}
function goLoginUrl(url, action){
	LoginGoUrl = url;
	passport.login(action);
}

