﻿var loginhtml = '<div class="ts_wrap ts_login" id="loginIn" style="display:none;"><div id="ts_main"><h3>会员登录</h3><a href="javascript:void(0);" onclick="ub();" class="btn_close" title="关闭">关闭</a><dl class="form_list"><dt>用户名</dt><dd><input type="text" class="input_txt input_0" id="tusername"/></dd><dt>密码</dt><dd id="tempTip"><input type="password" class="input_txt input_0" id="tuserpwd"/></dd> <dt></dt><dd><input type="checkbox" id="rememberlogin" /><label for="rememberlogin">记住登录</label></dd><dt></dt><dd><input type="button" value="确定" class="btn_common_1" onclick="login()"/>&nbsp;<input type="button" value="取消" class="btn_common_3" onclick="ub();" /></dd></dl><div class="clear"></div> </div></div>';
//搜索
var currentSearchTab = "clubtitle";
function switchSearchType(t, key) {
	if (key == null) {
		var liobj = $(t).parent();
		var key = liobj.attr("key");
	}

	if (currentSearchTab == key) return;
	$("[key=" + currentSearchTab + "]").removeClass("t_menu_current");
	currentSearchTab = key;
	$("[key=" + currentSearchTab + "]").addClass("t_menu_current");
	$("#searchType").val(currentSearchTab);
	$("#searchKey")[0].select();
	if (currentSearchTab == "blog") $("#searchForm")[0].action = "/Blog/Search";
	else if (currentSearchTab == "member") $("#searchForm")[0].action = "/User/CustomerIndex";
	else if (currentSearchTab == "clubtitle") $("#searchForm")[0].action = "/Club/SearchTopic/";
}

function parseSearchSubmit() {
	return $("#searchKey").val() != '';
}

function initSearch() {
	var u = (self.location + '').toLowerCase();
	var _sb = u.indexOf("/blog/search") != -1;
	var _sc = u.indexOf("/user/customerindex") != -1 && u.getQueryString("key") != '';
	var _st = u.indexOf("/club/searchtopic") != -1;

	if (_sb) switchSearchType(null, 'blog');
	else if (_sc) switchSearchType(null, 'member');
	else if (_st) switchSearchType(null, 'clubtitle');

	if (_sb || _sc || _st) {
		$("#searchKey").val(u.getQueryString("key"));
	}
}

$(initSearch);

String.prototype.strip = function() { return $.trim(this); }
String.prototype.len = function() { return this.strip().replace(/[\u4e00-\u9fa5]/g, "aa").length; }
String.format = function() { if (arguments.length == 0) { return null; } var str = arguments[0]; for (var i = 1; i < arguments.length; i++) { var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); str = str.replace(re, arguments[i]); } return str; }
String.prototype.getQueryString = function(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null; }
function chkValLen(val, minLen, maxLen) { var len = val.len(); if (len < minLen) return false; if (len > maxLen) return false; return true; }
$.fn.check = function(mode) { var mode = mode || 'on'; return this.each(function() { switch (mode) { case 'on': this.checked = true; break; case 'off': this.checked = false; break; case 'toggle': this.checked = !this.checked; break; } }); };
function ub() { isShow = false; $.unblockUI(); clearTimeout(timeOutFunc) };
var isShow = false; //提示框是否已经显示
var interValWait; //等待时的Interval
var timeOutFunc; //自动隐去提示框
//显示提示框
function showBlock(id) {
	var obj = $("#" + id);
	var c = { border: "none", width: obj.width(), height: obj.height(), padding: "0px" };
	$.blockUI({ message: obj, css: c });
	//$.resetCss(obj.width(), obj.height());
};
function eleBlock(eleid, id) {
	var obj = $("#" + id);
	var c = { border: "none", padding: "0px" };
	$("#" + eleid).block({ message: obj, css: c });
};
//提示框
(function($) {
	var status = { correct: "tip_1", alert: "tip_4", quest: "tip_3", error: "tip_2", loading: "tip_0" };

	function config(stat, msg, callBack) {

		switch (stat) {
			case status.error:
			case status.correct:
			case status.alert:
				{
					$(".tip_button").show();
					$(".tip_button .btn_common_3").hide();
					$(".tip_button .btn_common_1").unbind().click(function() { ub(); });
					if (callBack != undefined) {
						$(".tip_button .btn_common_1").unbind().click(callBack);
					}
				} break;
			case status.loading:
				{
					$(".tip_button").hide();
				} break;
			case status.quest:
				{
					$(".tip_button .btn_common_3").show();
					$(".tip_button .btn_common_1").unbind().click(callBack);
				} break;

		} $("#msgContent").html(msg);
		$("#tipPic").attr("class", "tip_pic " + stat);
		if (isShow == false) {
			$(".tip_button .btn_common_3").click(function() { ub(); });
			showBlock("tipMessage");
			isShow = true;
		}
		else {
			//$.resetCss($("#tipMessage").width(), $("#tipMessage").height());
		}



	};

	//提示框
	$.alert = function(info) { config(status.alert, info); };
	//确认操作框
	$.confirm = function(info, yesCallBack) { config(status.quest, info, yesCallBack); };
	//提示出错
	$.error = function(info) { config(status.error, info); };
	//提示正确
	$.correct = function(info, callBack) { config(status.correct, info, callBack); };
	//请求
	$.load = function(info) { config(status.loading, info); };
	//$ajax0574("blog/delcomment",{cid:1},2000,null,"正在删除评论,请稍候...")
	$.ajaxCommon = function(url, data, callback, isOnlyCallBack) {

		$.ajax({
			url: url + (url.indexOf("?") == -1 ? "?" : "&") + "s=" + Math.random(),
			type: 'POST',
			dataType: 'json',
			data: data,
			error: function(a) {
				$.alert('系统繁忙,请等会再操作...'); //timeOutFunc = setTimeout("ub()", 3000);

			},
			success: function(json) {
				if (json.IsCorrect) {
					if (isOnlyCallBack) { callback(json); return; }
					if (json.Redirect != null && json.Redirect != "") {
						$.alert(json.Info);
						window.location.href = json.Redirect;

					}
					else {
						callback(json);
						//timeOutFunc = setTimeout("ub()", 3000);
					}
				}
				else {

					if (isOnlyCallBack) { callback(json); return; }
					$.alert(json.Info);

					callback(json);
				}
				return;
			}
		});
	},
    $.ajax0574 = function(url, data, timeout, callBack, msg) {
    	var timeOut = timeout || 2000;
    	if (msg != undefined) {
    		currentCallBack = function() { $.ajax0574(url, data, timeout, callBack); };
    		var temp;
    		if (timeOut > 2000) temp = timeOut;
    		else temp = 600;
    		setTimeout(currentCallBack, temp);
    		$.load(msg);
    		return;
    	}

    	$.ajax({
    		url: url + (url.indexOf("?") == -1 ? "?" : "&") + "s=" + Math.random(),
    		type: 'POST',
    		dataType: 'json',
    		data: data,
    		timeout: "10000",
    		error: function(a, b) {
    			$.alert('系统繁忙,请等会再操作...'); //timeOutFunc = setTimeout("ub()", 3000);

    		},
    		success: function(json) {
    			if (json.IsCorrect) {
    				if (json.Redirect != null && json.Redirect != "") {
    					$.correct(json.Info, function() { window.location.href = json.Redirect; });

    				}
    				else {
    					$.correct(json.Info);
    					if (callBack != undefined) callBack(json);
    					//timeOutFunc = setTimeout("ub()", 3000);
    				}
    			}
    			else {
    				$.error(json.Info);
    			}
    			return;
    		}
    	});
    }

})(jQuery);
var currentCallBack;
//确认是否登录
function chkLogin(callback) {

	currentCallBack = callback;
	if (userid<=0) {
		showBlock("loginIn");
		return false;
	}
	else {
		currentCallBack();
		return true;
	}
}
//登录框的登录
function login() {

	var username = $('#tusername').val();
	var userpwd = $('#tuserpwd').val();
	if (username.length < 3 || username.length > 20) { showInfo("用户名必须在3-20个字符之间!"); fadeOutInfo(); return false; }
	if (userpwd.length < 5 || userpwd.length > 20) { showInfo("密码必须在5-20个字符之间!"); fadeOutInfo(); return false; }
	$.ajaxCommon("/user/login", { userid: username, userpwd: userpwd, remember: $("#rememberlogin")[0].checked }, loginedShow, true);
}
//登录成后或失败后的操作
function loginedShow(json) {
	if (json.IsCorrect) {
		var result = eval("(" + json.Info + ")");
		if (result.userid > 0) {
			userid = result.userid;
		} ub();
		currentCallBack();
	}
	else {
		showInfo(json.Info); fadeOutInfo();
	}

}
//登录框中的提示信息
function showInfo(msg) {
	$("#tempTip").append($("<p id='login_info'>" + msg + "</p>")).fadeIn('normal');
}
function fadeOutInfo() {
	setTimeout(function() { $("#login_info").remove(); }, 3000);
}
//检查登陆框
function checkLoginForm() {
	if ($("#username").val() == '') { $.alert("请输入用户名"); return; }
	if ($("#userpass").val() == '') { $.alert("请输入密码"); return; }

	document.forms['loginform'].submit();
}
var friendhtml = '<div class="ts_wrap ts_friend" id="friendAdd" style="display:none"><div id="ts_main"><h3>加{1}为朋友</h3><a href="javascript:void(0);" onclick="ub();" class="btn_close" title="关闭">关闭</a>' +
  '<div class="ts_content"><div class="ts_img"><a href="/users/{3}"><img src="{2}" /></a></div>' +
'<dl><dt>{0}</dt><dd><input type="button" class="btn_common_1" value="关闭" onclick="ub()"/></dd>' +
'</dl><div class="clear"></div></div></div></div>';
//添加好友
function addFriend(id) {
	$('#friendAdd').remove();

	chkLogin(function() {
		$.ajaxCommon("/User/FriendAdd", { friendid: id }, function(data) {
			if (data.IsCorrect) {
				var t = data.Info.split('#');

				$('body').append(String.format(friendhtml, t[0], t[1], t[2], t[3]));
				showBlock("friendAdd");
			}
			else {
				$.error(data.Info);
			}
		});
	});
}
function sendMessage(username) {

	chkLogin(function() {
		var url = "/User/MessageWrite/" + username;

		self.location.href = url;
	});
}
$(document).ready(function() {
	$("body").prepend(loginhtml);
	if ((self.location + '').indexOf("localhost") > 0) {
		$("div").ajaxError(function(a, b, c, d) {
			var v = self;
			v.document.open();
			v.document.write(b.responseText);
			v.document.close();
		});
	}
});


//--Added @ 2009年2月25日16:33:33--from club.js
//--Common--
var tabGroup = {}
var tabClassGroup = {}
var currentTabClass = "t_menu_current";
function swTab(g, i) {
	if (tabGroup[g] == undefined || tabGroup[g] == i) return;
	cl = tabClassGroup[g];

	$("[tg=" + g + "_t][idx=" + tabGroup[g] + "]").removeClass(cl);
	$("[tg=" + g + "][idx=" + tabGroup[g] + "]").hide();
	tabGroup[g] = i;
	$("[tg=" + g + "_t][idx=" + tabGroup[g] + "]").addClass(cl);
	$("[tg=" + g + "][idx=" + tabGroup[g] + "]").show();
}
function regTab(g, curIdx, cl) {
	tabGroup[g] = curIdx;
	if (cl == null) tabClassGroup[g] = currentTabClass;
	else tabClassGroup[g] = cl;
}
//--end--
