var sso = (function() {
	// TODO:检查是否已载入lhgcore.min.js，未载入则加载
	// TODO:检查是否已载入lhgdialog.js，未载入则加载

	var ie6 = window.ActiveXObject && !window.XMLHttpRequest;
	
	var getMap = function(file) {
		file = file || 'sso.js';
		var len, bp, fp, src = J('script'), i = 0, l = src.length;

		for (; i < l; i++) {
			bp = src[i].src.substr(0, src[i].src.toLowerCase().indexOf(file));
			len = bp.lastIndexOf('/');

			if (len > 0)
				bp = bp.substr(0, len + 1);

			if (bp)
				break;
		}

		if (!J.browser.ie || J.browser.i8 || bp.indexOf('http:') !== -1){
			return bp;
		}else {
			fp = window.location.href;
			fp = fp.substr(0, fp.lastIndexOf('/'));

			if (bp == '')
				return fp + '/';

			if (bp.indexOf('../') !== -1) {
				while (bp.indexOf('../') >= 0) {
					bp = bp.substr(3);
					fp = fp.substr(0, fp.lastIndexOf('/'));
				}
				return fp + '/' + bp;
			} else if (bp.indexOf('/') == 0) {
				bp = document.location.protocol + '//' + document.location.host
						+ bp;
				return bp;
			} else
				return fp + '/' + bp;
		}
	};
	
	var base = getMap();
	if(base.indexOf(document.location.protocol)!=0)
		base = document.location.protocol + '//' + document.location.host + base;
	
	var getUrl = function(url) {
		return base + url;
	};
	var ajax = function(op) {
		if (op.cache == false) {
			if (op.data == null)
				op.data = '';
			op.data += 'nocache=' + new Date().getTime();
		}
		return J.ajax(op);
	};
	
	var callback;
	var prepare = function(op) {
		if (typeof op == 'function')
			callback = op;
		else if (typeof op == 'object' && typeof op.fn == 'function')
			callback = op.fn;
		else
			callback = null;
	};

	return {
		/** 弹出窗口的默认皮肤样式 */
		defaultSkin : 'chinaway',
		
		/** 页头的登录信息标签的ID */
		loginInfoId : "loginUserInfo_common",

		/** 刷新页头的登录信息 */
		refreshLoginInfo : function(infotype) {
			if(typeof common_refresh_login == 'function'){
				common_refresh_login();
			}
		},
		
		/** 登录成功的回调函数 */
		callback : function(user,infotype) {
			this.refreshLoginInfo(infotype);
			if (callback != null) {
				callback(user,infotype);
				callback = null;
			}
		},

		/** 登录认证，认证成功执行回调函数 */
		auth : function(op,infotype) {
			if (ie6 && document.readyState!="complete") return;	//IE下，页面未加载完，弹出窗口会报“无法打开Internet站点”
			
			var self = this;
			
			prepare(op);

			ajax({
				url : base+'user!checkLogin.action',//TODO:
				cache : false,
				ret : 'json',
				fn : function(data) {
					if (data.logged == true) {
						self.callback(data.user,infotype);
					}else{
						// 弹出登录窗口
						J.dialog.get({
							id : (op && op.id) || 'LoginDialog',
							skin : (op && op.skin) || self.defaultSkin,
							title : '盈彩网',
							link : getUrl('dialogLogin1.html'),
							nofoot : true,
							cover : true,
							width : 475,
							height : 268
						});
					}
				}
			});
		},
		
		/** 退出登录 */
		logout : function(callback,infotype){
			var self = this;
			ajax({
				url : base+'/logout.action',//TODO:
				cache : false,
				ret : 'text',
				fn : function(text) {
					if(text.indexOf('success') != -1){
						self.refreshLoginInfo(infotype);// 刷新页头登录信息
						if(callback != null && typeof callback == 'function')
							callback();
					}
				}
			});
		},
		
		/** ajax退出登录 */
		ajaxlogout : function(infotype){
			var self = this;
			ajax({
				url : base+'/ajaxlogout.action',//TODO:
				cache : false,
				ret : 'text',
				fn : function(text) {
					if(text.indexOf('success') != -1){
						self.refreshLoginInfo(infotype);// 刷新页头登录信息
					}
				}
			});
		},
		
		displaySpecialUserLoginWindow : function (user){
			var self = this;
			$$load({
				path:'http://s1.betzc.com/widgets/thinkbox/ver_1.0/thinkbox.js',
				charset:'utf-8',
				callback:function (){
				    var tempHtml = '';
				    tempHtml += '<div class="w400">';
			          tempHtml += '<div class="tccharwz"> 您的帐户已转移到新彩票平台：<span class="rcbold">彩客网</span><br/>您在彩客网的新账户名为：<span class="rcbold">'+user.caikeUserName+'</span></div>';
			          tempHtml += '<input type="button" class="lc_ybtbg" value="现在到彩客网投注登录" style="width:220px;" onclick="window.open(\'http://www.310win.com/\',\'_self\');">';
			        tempHtml += '</div>';
			        
			    	$floatHTML({
			    		height:201,
			    		width:420,
			    		html : tempHtml,
			    		title:'盈彩网',
			    		cover:true,
			    		afterClose:function(){
							window.setTimeout(function(){
								self.callback(user);
							},10);
			    		}
			    	});
			    }
			});

		}
		
	};
})();

$S = sso;
window.$S = sso;

function addBookmark(title,url) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}

function infoShowHideFundRemain(){
   	var fundRemainSpan=document.getElementById('sso_userRemainMoney');
    var fundRemain=fundRemainSpan.getProperty('sso_price');
       if("*****"==fundRemainSpan.innerHTML){
            fundRemainSpan.innerHTML=fundRemain+"元";
            fundRemainSpan.title="点击隐藏资金";
       }else{
            fundRemainSpan.innerHTML="*****";
            fundRemainSpan.title="点击查看资金";
       }
}

/*
var lhgcoreOK = lhgdialogOK = false;
var head = document.getElementsByTagName('head')[0];
var arr = head.getElementsByTagName('script');
if (arr != null) {
	if (arr.length != null) {
		for ( var i = 0, l = arr.length, url; i < l; i++) {
			url = arr[i].src;
			if (!lhgcoreOK && url.indexOf('lhgcore.min.js') !== -1)
				lhgcoreOK = true;
			if (!lhgdialogOK && url.indexOf('lhgdialog.js') !== -1)
				lhgdialogOK = true;
			if (lhgcoreOK && lhgdialogOK)
				break;
		}
	} else {
		var url = arr.src;
		lhgcoreOK = url.indexOf('lhgcore.min.js') !== -1;
		lhgdialogOK = url.indexOf('lhgdialog.js') !== -1;
	}
}
if (!lhgcoreOK || !lhgdialogOK) {
	var includeJS = function(scriptFile, fn) {
		var js = document.createElement('script');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', file);
		head.appendChild(js);

		if (window.ActiveXObject) {// 如果是IE浏览器
			js.onreadystatechange = function() {
				if (js.readyState == 'loaded' || js.readyState == 'complete') {
					fn();
				}
			};
		} else {// 如果是火狐及其他浏览器等
			js.onload = fn;
		}
	};
}
*/
