function initLogin() {
	$("#login .showRegDialog").click(function (e) {
		tb_remove();
		setTimeout(function () {
                        $("#regDialog").click();
                }, 300);
		return false;
	});

}
function initReg() {
	$("#reg .showLoginDialog").click(function (e) {
		tb_remove();
		setTimeout(function () {
                        $("#loginDialog").click();
                }, 300);
		return false;
	});

	$("#validateCode").click(function (e) {
		refreshCode();
	});
}
function refreshCode(){
	var codeKey = new Date().getTime();
	$("#validateCode").attr("src",'/img.php?'+codeKey);
}
function initAll(){
	create_fav_html();
	$(".dialogCancel").click(function (e) {
		tb_remove();
		return false;
	});
	$("#validateCode").click(function (e) {
		refreshCode();
	});
	checkLogin();
    initLogin();            
    initReg();  
}
function initRegAndLog(){
	$(".dialogCancel").click(function (e) {
		tb_remove();
		return false;
	});
	$("#validateCode").click(function (e) {
		refreshCode();
	});
	checkLogin();
}
function loginSub(formName){
	var data = $("#"+formName).serialize();
	jQuery.post("/login.php", data, function (result) {
		if (result.err) {
			alert('账户或者密码错误');
			$("#un").focus();
		} else {
			checkLogin();
		}
	}, "json");
	return false;
}
function regSub(){
	jQuery.post("/reg.php", $("#regForm").serialize(), function (result) {
		if (result.err) {
			if(result.err == '-1'){
				alert('请填写正确的邮箱作为账户');
				$("#userName").focus();
				return false;
			}else if(result.err == '-2'){
				alert('密码不符合规则，请正确填写');
				$("#password").focus();
				return false;
			}else if(result.err == '-3'){
                                alert('两次密码不一致');
				$("#rePassword").focus();
                                return false;
                        }else if(result.err == '-4'){
                                alert('昵称不符合规则');
				$("#nickName").focus();
                                return false;
                        }else if(result.err == '-5'){
                                alert('验证码不正确');
				$("#code").focus();
				$("#validateCode").click();
                                return false;
                        }else if(result.err == '-7'){
                                alert('Email已经存在');
				$("#userName").focus();
                                return false;
                        }else if(result.err == '-8'|| result.err == '-9'){
                                alert('注册失败');
                                return false;
                        }else if(result.err == '-6'){
                                alert('验证码不正确');
				$("#validateCode").click();
				$("#code").focus();
                                return false;
                        }
		} else {
			checkLogin();
			tb_remove();
		}
	}, "json");
	return false;
}
function checkLogin(){
        var societyguester='';
	var nickname = '';
	var uid = '';
	var contents = '';
	var logintime = 0;
	var loginip = '';
        var allcookie = document.cookie.split(';');
        for(var i=0;i<allcookie.length;i++){
                var cookieI=allcookie[i].split('=');
                if(cookieI.length>1){
			var cookieTrimValue = jQuery.trim(cookieI[0]);
			if(cookieTrimValue=='societyguester')societyguester=allcookie[i].split('=')[1];
			else if(cookieTrimValue=='nickname')nickname=allcookie[i].split('=')[1];
			else if(cookieTrimValue=='uid')uid=allcookie[i].split('=')[1];
			else if(cookieTrimValue=='logintime')logintime=allcookie[i].split('=')[1];
			else if(cookieTrimValue=='loginip')loginip=allcookie[i].split('=')[1];
                }
        }
	if(societyguester=='' || nickname=='' || uid==''){
		/*
		contents+='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>';
		contents+='<td><a id="loginDialog" href="#TB_inline?width=450&height=280&inlineId=login" class="thickbox"><img src="/images/login_sub.gif"></a></td>';
		contents+='<td><a id="regDialog" href="#TB_inline?width=450&height=280&inlineId=reg" class="thickbox"><img src="/images/regster_sub.gif"></a></td>';
		contents+='</tr></table>';
		*/
		contents+='<form name="loginForm_show" id="loginForm_show" onsubmit="return loginSub(\'loginForm_show\')">';
		contents+='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>';
		contents+='<label>邮箱：</label></td><td><input name="userName" id="un" type="text" class="login_show" /></td>';
		contents+='<td><label>密码：</label></td><td><input name="password" id="pwd" type="password" class="login_show" /></td>';
		contents+='<td><input type="checkbox" id="autoLogin" name="autoLogin" value="1" checked="checked" /></td><td><label>自动登录</label></td>';
		contents+='<td><input type="submit" style="cursor:pointer;background:url(/images/login_sub.gif);width:39px;height:19px;border:0px" value="" /></td>';
		contents+='<td><a id="regDialog" href="#TB_inline?width=450&height=280&inlineId=reg" onclick="refreshCode();" title="注册用户" class="thickbox"><img src="/images/regster_sub.gif"></a></td>';
		contents+='</tr></table>';
		contents+='</form>';
	}else{
		contents+='<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><label>';
		contents+='欢迎您，'+getValueFromCookie('nickname')+'，开源吧后续功能正在开发中，敬请期待，谢谢您的关注';
		//if(logintime!=0)contents+='&#160;&#160;&#160;上次登录：'+logintime;
		//if(loginip!='')contents+=' | '+loginip;
		contents+='</label><img src="/images/logout.gif" style="cursor:pointer;margin-top:3px;margin-left:10px" onclick="logOut()" />';
		contents+='</td></tr></table>';
	}
	$("#top_common_login").html(contents);
	//document.getElementById('top_common_login').innerHTML=contents;
}
function logOut(){
	var temp=document.cookie.split(";");   
	var loop3;   
	var ts; 
	for (loop3=0;loop3<temp.length;loop3++){   
		ts=temp[loop3].split("=")[0];
		delCookie(ts,'.kaiyuanba.cn');
	}
	checkLogin();
	init_thickbox();
}
function delCookie(name,c_domain){
	expires=new Date();
	expires.setTime(expires.getTime()-1);
	document.cookie=name+"=; expires="+expires.toGMTString()+"; maxage=0 ; path=/ ; domain="+c_domain;
}
function getValueFromCookie(name){
	var curUser='';
	var allcookie = document.cookie.split(';');
	for(var i=0;i<allcookie.length;i++){
		var cookieI=allcookie[i].split('=');
		if(cookieI.length>1&&jQuery.trim(cookieI[0])==name){
			curUser=unescape(decodeURIComponent(allcookie[i].split('=')[1]));
		}
	}
	return curUser;
}

function getDateTime(dateVal){
	var date = new Date();
	date.setTime(dateVal);
	return date.getFullYear()+'-'+date.getMonth()+'-'+date.getDate();
}
function setHomepage(site) {
	if (document.all) {
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage(site);
	}else if (window.sidebar) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}catch (e) {
				alert("该操作被浏览器拒绝，假如想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage',site);
	}
} 
function openNewWindow(url){
	window.open(url,'_blank');
}
(function() {
    var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
        .text($backToTopTxt).attr("title", $backToTopTxt).click(function() {
            $("html, body").animate({ scrollTop: 0 }, 120);
    }), $backToTopFun = function() {
        var st = $(document).scrollTop(), winh = $(window).height();
        (st > 0)? $backToTopEle.show(): $backToTopEle.hide();    
        //IE6下的定位
        if (!window.XMLHttpRequest) {
            $backToTopEle.css("top", st + winh - 166);    
        }
    };
    $(window).bind("scroll", $backToTopFun);
    $(function() { $backToTopFun(); });
})();

$(function(){$("img").lazyload({effect:"fadeIn"});});
function loginSubForDialog(){
	var data = $("#loginForm").serialize();
	jQuery.post("/login.php", data, function (result) {
		if (result.err) {
			alert('账户或者密码错误');
			$("#userNameForDialog").focus();
		} else {
			checkLogin();
			tb_remove();
		}
	}, "json");
	return false;
}

