$(function(){
	$(".grayTips").each(function(){ //遍历每个文本框
		var objTextBox=$(this);
		var oldText=$.trim(objTextBox.val());
		objTextBox.css("color","#888");
		objTextBox.focus(function(){
			if(objTextBox.val()!=oldText){
				objTextBox.css("color","#000");
			} else{
				objTextBox.val("").css("color","#888");
			}
		});
		objTextBox.blur(function(){
			if(objTextBox.val()==""){
				objTextBox.val(oldText).css("color","#888");
			}
		});
		objTextBox.keydown(function(){
			objTextBox.css("color","#000");
		});
	});
});

// 验证码 //
function _vCode(ele) {var vimg;if (ele!="" && ele!=undefined) {vimg = ele;} else {vimg = 'vCode';}$("#"+vimg).attr('src','/util/vCode.php?'+(new Date()).valueOf());}
// 跳转 //
function go(href) {location.href=href;}
// 显示 twitter内容 //
function twitter(id) {Boxy.load("/twitter/detail.php?id="+id,{modal:true});}
function addtwitter() {
	var content = $("#twitter_content").val();
	if (content=="" || content=="你正在做什么？") {
		$("#tempmsg").html("<span style=\"colorred\">内容不能为空!</span>");
	} else if (content.length<3) {
		$("#tempmsg").html("<span style=\"colorred\">输入内容太短!</span>");
	} else {
		$("#twitters").html("<span style=\"colorred\">正在提交中...</span>");
		$.post("/twitter/ajax.twitter.php", {content:content},
		function(data) {
			$("#twitters").html(data);
			$("#twitter_content").attr("value",'');
			$.get("/my/ajax.newsfeed.php", {pagesize:20},
			function(data) {
				$("#newsfeeds").html(data);
			});
		});
	}
}
// 博客分类 //
function blog_category(fr) {Boxy.load("/blog/ajax.category.php?fr="+fr,{modal:true});}
function hide_slowly(ele,timeout){if (timeout==undefined){timeout=2000;}setTimeout('$("'+ele+'").hide(500)',timeout);}
// 字符串长度 //
function strLen(str) {
	var charset = document.charset;
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1;
	}
	return len;
}
// 计算输入框剩余字符长度 //
function textCounter(obj, showid, maxlimit) {
	var len = strLen(obj.value);
	if(len > maxlimit) {
		obj.value = getStrbylen(obj.value, maxlimit);
		$('#'+showid).text(0);
	} else {
		$('#'+showid).text(maxlimit - len);
	}
}
// 长得字符串长度 //
function getStrbylen(str, len) {
	var num = 0;
	var strlen = 0;
	var newstr = "";
	var obj_value_arr = str.split("");
	for(var i = 0; i < obj_value_arr.length; i ++) {
		if(i < len && num + byteLength(obj_value_arr[i]) <= len) {
			num += byteLength(obj_value_arr[i]);
			strlen = i + 1;
		}
	}
	if(str.length > strlen) {
		newstr = str.substr(0, strlen);
	} else {
		newstr = str;
	}
	return newstr;
}
// 取得字节长度 //
function byteLength (sStr) {
	aMatch = sStr.match(/[^\x00-\x80]/g);
	return (sStr.length + (! aMatch ? 0 : aMatch.length));
}
// 加某人为好友 //
function friend_add(fid) {Boxy.load("/util/friend.php?opt=add&objid="+fid,{modal:true});}
// 给某人发送消息 //
function msg(sendto) {$.messager.setpause(1);Boxy.load("/message/i.php?sendto="+sendto,{modal:true});}
// 显示登录界面 //
function login() {Boxy.load("/sign/sign.php",{modal:true});}
// 显示注册界面 //
function register() {Boxy.load("/sign/sign.php?display=register",{modal:true});}
// 复选框选择1全选0全不选-1反选 //
function checkall(obj,flag) {
	$("input[name='" + obj +"']").each(function(){
		$(this).attr("checked", flag<0 ? !this.checked : flag);
	});
}

/* message notify start */
(function (jQuery){
	this.version = '@1.3';this.pause_status=0; this.layer = {'width' : 345, 'height': 227};this.title = '信息提示';this.time = 4000;this.anims = {'type' : 'slide', 'speed' : 600};this.timer1 = null;
	this.inits = function(title, text){
		if($("#messagenotify").is("div") || this.pause_status==1){ return; }
		$(document.body).prepend('<div id="messagenotify" style="z-index:100;width:'+this.layer.width+'px;height:'+this.layer.height+'px;position:absolute; display:none;background-image:url(/images/message/msgalert.png); bottom:0; right:0; overflow:hidden;"><div style="border-top:none;width:100%;height:auto;font-size:12px;padding-top:22px;padding-left:30px;"><div style="width:160px;padding:3px;font-weight:bold;float:left;">'+title+'</div><div id="messagenotify_close" style="padding:3px;cursor:pointer;">&nbsp;&nbsp;</div><div id="messagenotify_content" style="padding:8px 5px 0px 3px;font-size:12px;width:180px;color:#1f336b;text-align:left;overflow:hidden;">&nbsp;&nbsp;&nbsp;&nbsp;'+text+'</div></div></div>');
		$("#messagenotify_close").click(function(){setTimeout('this.close()', 1);});
		$("#messagenotify").hover(function(){clearTimeout(timer1);timer1 = null;},function(){timer1 = setTimeout('this.close()', time);});
	};
	this.show = function(title, text, time){if($("#messagenotify").is("div") || this.pause_status==1){ return; }this.inits(title, text);if(time>=0)this.time = time;switch(this.anims.type){case 'slide':$("#messagenotify").slideDown(this.anims.speed);break;case 'fade':$("#messagenotify").fadeIn(this.anims.speed);break;case 'show':$("#messagenotify").show(this.anims.speed);break;default:$("#messagenotify").slideDown(this.anims.speed);break;}
	if($.browser.is=='chrome'){	setTimeout(function(){$("#messagenotify").remove();this.inits(title, text);$("#messagenotify").css("display","block");},this.anims.speed-(this.anims.speed/5));}this.rmmessage(this.time);};
	this.lays = function(width, height){if($("#messagenotify").is("div")){ return; }if(width!=0 && width)this.layer.width = width;if(height!=0 && height)this.layer.height = height;}
	this.anim = function(type,speed){if($("#messagenotify").is("div") || this.pause_status==1){ return; }if(type!=0 && type)this.anims.type = type;if(speed!=0 && speed){switch(speed){case 'slow' : ;break;case 'fast' : this.anims.speed = 200; break;case 'normal' : this.anims.speed = 400; break;default:this.anims.speed = speed;}}}
	this.rmmessage = function(time){if(time>0){timer1 = setTimeout('this.close()', time);}};
	this.close = function(){switch(this.anims.type){case 'slide':$("#messagenotify").slideUp(this.anims.speed);break;case 'fade':$("#messagenotify").fadeOut(this.anims.speed);break;case 'show':$("#messagenotify").hide(this.anims.speed);break;default:$("#messagenotify").slideUp(this.anims.speed);break;};setTimeout('$("#messagenotify").remove();', this.anims.speed);}
	this.setpause = function(status) {clearInterval(g_blinkid);g_blinkid=0;document.title=g_blinktitle;this.pause_status=status;};
	this.pause = function() {return this.pause_status;}
	jQuery.messager = this;
	return jQuery;
})(jQuery);
/* message notify end */

/*消息提醒*/
var g_blinkid = 0;
var g_blinkswitch = 0;
var g_blinktitle = $("title").html();
var g_inputtime = 0;

function blinkNewMsg() {
	var now    = new Date();
	var nowtime = now.getTime();
	if (nowtime - g_inputtime > 5000 && $.messager.pause()==0) {
		document.title = g_blinkswitch % 2 ? "【　　　】-" + g_blinktitle : "【新消息】-" + g_blinktitle;
	}
	g_blinkswitch++;
}

function checkNewMsg() {
	if (is_signed()==true && $.messager.pause()==0) {
		$.get('/message/new.php?'+(new Date()).valueOf(),{act:'message'},function(data){
			checkNewMsgShow(data);
		});
	}
	setTimeout(checkNewMsg, 20000);
}

var g_oldmsg = g_oldsysmsg = g_oldbbs = g_oldbbsreply = g_oldcomment = g_oldreply = 0;
function checkNewMsgShow(req) {
	if (req==0 || $.messager.pause()==1) {
		return false;
	} else {
		/*发现新消息的到来*/
		$.messager.anim('show', 1000);
		$.messager.show('消息提示：', 'Hi '+$Y.username+',您有'+req+'个新短消息未读,请注意查收!<br><br>&nbsp;&nbsp;<a href="###" onclick="msg(\'open\');$.messager.close();">马上查看</a>',8000);
		g_blinkid = setInterval(blinkNewMsg, 1000);
	}
}
// 判断是否已经登录系统 //
function is_signed(){if (typeof($Y)!='undefined' && typeof($Y.id)=='number') {if($Y.id>0){return true;}else{return false;}}}
// 检测是否有新消息 //
if (is_signed()==true){setTimeout(checkNewMsg, 20000);}

function sign_logout() {
	$.get("/sign/ajax.logout.php",{c:1},
	function(data) {
		if ($.trim(data)=='success') {
			location.href="/my/index.php";
		}
	});
}

function newsfeed_reply(id) {
	temp_msgid = $("input[name='temp_newsfeedid']").val();
	if ($("#reply_"+id).css('display')=='none') {
		// 防止打开B时A还在打开状态,将原来的关闭 //
		if (temp_msgid>0 && temp_msgid!=id) {
			$("#reply_"+temp_msgid).hide('slow');
			$("#__content_"+temp_msgid).html('');
		}
		$("#reply_"+id).show('slow');
		$("input[name='temp_newsfeedid']").attr("value",id);
		content_editor = new webEditor("content_editor" , document.getElementById("__content_"+id) , 350, 35);
		content_editor.init("");
		$.get("/my/ajax.comment4newsfeed.php",{newsfeedid:id},
		function(data) {
			$("#newsfeed_comments_"+id).html(data);
		});
	} else {
		$("#reply_"+id).hide('slow');
	}
}
function newsfeed_reply_submit(projectid,relationid,newsfeedid) {
	var request_url;
	var content = content_editor.getHtml();
	if (content=="") {
		alert('请输入回复内容！');
	} else {
		if (projectid==3) {
			request_url = "/space/ajax.photo_comment.php";
		} else if (projectid==2) {
			request_url = "/space/ajax.blog_comment.php";
		} else if (projectid==6) {
			request_url = "/twitter/ajax.twitter.php";
		}

		$("#newsfeed_comments_"+newsfeedid).html('<span style="color:red">回复提交中......</span>');
		$.post(request_url,{content:content,relationid:relationid,newsfeed:1},
		function(data) {
			$("#newsfeed_comments_"+newsfeedid).html(data);
			$("#__content_"+newsfeedid).attr('value','');
		});

	}
}