/**
 * 游戏官网通用头尾共享js
 * 2011-9-23
 * @Aliang
 */

jQuery(function(){
	$("#CG_loginLink").mouseenter(function(){
		$("#CG_loginBox").show();
	});

	$("#CG_loginBox").mouseleave(function(){
		$(this).hide();
		typeof loginIframeBck != 'undefined' && loginIframeBck.hide();
	});
 
	if(typeof objTop!='undefined'){
		objTop.initUserStatus();
		$('#logoutById').click(objTop.logout);
		$("#CG_loginForm").submit(objTop.login);
		$('.CG_setHome').click(objTop.setHomePage);
		$('.CG_addFav').click(objTop.addFav);
	}

});

//兼容以前版本
var top_uuzu = function(options){

	var _this = this;
	this.settings = {
		proxyScript:'/proxy.php',//跨域代理
		levelUrl:'https://passport.uuzu.com/userlevel/userlevel_new.php',//在线时间接口
		loginUrl:'https://passport.uuzu.com/loginAjax_new.php',//登录接口
		onUserLogin:null,//状态改变时的回调函数
		onUserLogout:null,
		homepage:'http://www.uuzu.com',
		sitename:'游族网页游戏平台'
	};

	$.extend(this.settings, options);

	this.initUserStatus = function(){		
		if($.cookie("uuzu_UNICKNAME")) {			
			$("#CG_username").text($.cookie("uuzu_UNICKNAME"));
			$("#CG_loginBox").hide();
			typeof loginIframeBck != 'undefined' && loginIframeBck.hide();
			$("#CG_loginStatusBar0").hide();
			$("#CG_loginStatusBar").show();
			$('#CG_usernameUserId').html($.cookie("uuzu_UNICKNAME"));
			// console.log($('#CG_usernameUserId').html());
			_this.updateOnlineTime();
			_this.updateOnlineTimeTimer = setInterval(_this.updateOnlineTime,300000);
		}else{
			$("#CG_loginStatusBar0").show();
			$("#CG_loginStatusBar").hide();
			if(_this.updateOnlineTimeTimer!=null) clearInterval(_this.updateOnlineTimeTimer);
		}		
	}

	//取指定游戏最近登陆的服务器信息
	this.getUserLastServer = function(game_id, callback){
		$.getJSON("/proxy.php",{url:'http://passport.uuzu.com/api/get_user_server_list.php',game_id:game_id},function(data){
			if(data!=null){
				var game_info = data.pop();
				var server = game_info.server.sort(function(a,b){
					if(a.login_time>b.login_time) return 1;
					else if(a.login_time<b.login_time) return -1;
					else return 0;
				});

				var server = server.pop();

				if($.isFunction(callback)) callback(server);
			}
		});
	}

	//更新在线时间
	this.updateOnlineTime = function(){
		if($.cookie("uuzu_UNICKNAME")==null){
			$.cookie("uuzu_TIMER",null,{ path: '/', expires: -1,domain:'uuzu.com' });
			return null;
		} else {
			var date=new Date();
			$.post(_this.settings.proxyScript,{url:_this.settings.levelUrl,action:'changetimer'},function(data) {
				$.cookie("uuzu_USERPIC",data.user_pic,{ path: '/', expires: 0,domain:'uuzu.com' });
			},'json');
		}
	}

	this.login = function(){
		var username = $("#CG_loginForm input[name=username]").val();
		var password = $("#CG_loginForm input[name=password]").val();		
		if(username=='' || password==''){
			alert("请输入用户名和密码");
			return false;
		}
		$.getJSON(_this.settings.proxyScript,{url:_this.settings.loginUrl,type:'login',username:username,password:password},function(data){
			if(data.status==1){
				$("#CG_username").text(data.nickname);
				$("#CG_loginBox").hide();
				typeof loginIframeBck != 'undefined' && loginIframeBck.hide();
				typeof limitLeft !=='undefined' && $('#CG_usernameUserId').html(limitLeft($.cookie('uuzu_UNICKNAME'),6,'..'));
				$("#CG_loginStatusBar0").hide();
				$("#CG_loginStatusBar").show();

				if(typeof(_this.settings.onUserLogin)=='function') _this.settings.onUserLogin();
			}else{							
				alert(data.message);
			}
			$("#CG_loginForm input[name=password]").val('');
		});
		return false;
	}

	this.setHomePage = function(){
		try{
			this.style.behavior='url(#default#homepage)';
			this.setHomePage(_this.settings.homepage);
		}
		catch(e){
			if(window.netscape) {
				try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
				} 
				catch (e)  {
					alert("此操作被浏览器拒绝！"); 
				}
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage',_this.settings.homepage);
			 }
		}
	}

	this.addFav = function(){
		if($.browser.msie){
			window.external.AddFavorite(_this.settings.homepage,_this.settings.sitename);
		}else if($.browser.safari){
			window.sidebar.addPanel(_this.settings.sitename, _this.settings.homepage, "");
		}else {
			alert('此操作被浏览器拒绝！');
		}
		return false;
	}
	this.logout=function(){
		$.cookie("uuzu_UNICKNAME",null,{ path: '/', expires: -1,domain:'uuzu.com' });
		$.cookie("uuzu_UAUTH",null,{ path: '/', expires: -1,domain:'uuzu.com' });
		$('#CG_loginStatusBar0').show();
		$('#CG_loginStatusBar').hide();
		if(typeof(_this.settings.onUserLogout)=='function') _this.settings.onUserLogout();
	}
}


//if(!objTop) var objTop = new top_uuzu();

