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();
	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 : null,
		
		/** 页头的登录信息标签的ID */
		loginInfoId : "loginUserInfo_common",

		/** 刷新页头的登录信息 */
		refreshLoginInfo : function(infotype) {
			if (this.loginInfoId != null) {
				var obj = document.getElementById(this.loginInfoId);
				if (obj != null) {
					ajax( {
						url : base+'/user!info.action?itype='+infotype,
						cache : false,
						fn : function(text) {
							obj.innerHTML = text;
						}
					});
				}
			}
		},
		
		/** 登录成功的回调函数 */
		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 : (op && op.cover),
							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);// 刷新页头登录信息
					}
				}
			});
		}
		
		
	};
})();

$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;
		}
	};
}
*/