WatchResource=RequestBase.extend({
	initialize:function(){
		this.parent();
    	this.watchStatus = false;
    	this.visitorId = window.visitor_id;
    	this.resId = window.res_id;
    	this.resType = window.res_type;
    	this.loading=false;
    	//this.WATCHCACHE = new Array();
        if(!this.visitorId)
            return;
    	this.init();
	},
	init:function(){
		$("watchBtn").onclick = this.alterWatchStatus.bindAsEventListener(this);
		this.judgeWatchStatus();
	},
	judgeWatchStatus:function(){

    	if(typeof(res_id)!="undefined"&&typeof(visitor_id)!="undefined"&&res_type<5){
			this._post("yakbo.operate.judgeWatchStatus",this.onJudgeWatchStatus.bind(this),{resId:this.resId,resType:this.resType});
		}else{
			$("watchBtn").innerHTML = "我要关注";
			return true;
		}
	},
	onJudgeWatchStatus:function(xmlhttp){
		var rsp = this.getHttpElements(xmlhttp);
		var stat = rsp.getAttribute("stat");
		if(stat == "ok"){
			nodes = rsp.childNodes;
			this.watchStatus = nodes[0].getAttribute("judgeResult");
			if(this.watchStatus == "watched"){
				$("watchBtn").innerHTML = "取消关注";
				this.watchStatus = true;
				return true;
			}else{
				if(this.watchStatus == "nowatch"){
					$("watchBtn").innerHTML = "我要关注";
					this.watchStatus = false;
					return true;
				}
				this.watchStatus = false;
				return false;
			}
		}
	},
	alterWatchStatus:function(){
		if(!this.visitorId){
			alert("对不起,只有登陆用户才有权使用关注功能!");
			return;
		}
		//if(this.WATCHCACHE[this.resId]){
    	//	alert("您现在正在关注");
    	//	return;
    	//}
		if(this.loading)
    		return;
    	this.loading=true;
		this._post("yakbo.operate.alterWatchStatus",this.onAlterWatchStatus.bind(this),{resId:this.resId,resType:this.resType,watchStatus:this.watchStatus});
	},
	onAlterWatchStatus:function(xmlhttp){
		var rsp = this.getHttpElements(xmlhttp);
		var stat = rsp.getAttribute("stat");
		if(stat == "ok"){
			nodes = rsp.childNodes;
			if(!this.watchStatus){
				if(nodes[0].getAttribute('result')=='watched'){
	        		alert("对不起，您正在关注！");
	        	}
	        	else if(nodes[0].getAttribute('result')=='top'){
	        		alert("对不起，您关注数目已经到达上限！");
	        	}
	        	else if(nodes[0].getAttribute('result')=='false'){
	        		alert("对不起，由于网络原因，添加关注失败！");
	        	}
	        	else{
	        		if(nodes[0].getAttribute('result')=='success'){
		        	    alert("恭喜，您正在关注！");
		        	    //var wtNum = $("num_watches").innerHTML;
		        	    //var totalNums = parseInt(wtNum)+1;
		        	    $("watchBtn").innerHTML="取消关注";
		        	    this.watchStatus = true;
		        	    //$("num_watches").innerHTML=totalNums;
	        	    }
	        	   // this.WATCHCACHE[this.resId]=1;

	        	}
        	}else{
        		if(nodes[0].getAttribute('result') == 'success'){
	    			alert("您已经取消关注");
	    			//var watNum = $("num_watches").innerHTML
	    			//var totalNum = parseInt(watNum)-1;
	    			$("watchBtn").innerHTML="我要关注";
	    			//$("num_watches").innerHTML=totalNum;
	    			//this.WATCHCACHE[this.resId] = false;
	    			this.watchStatus = false;
    			}
    			if(nodes[0].getAttribute('result') == 'failure'){
    				alert("对不起,由于网络原因,取消关注失败!");
    			}
        	}
        	this.loading = false;
		}
	}
});
//var watchResource=new WatchResource();
//
var ReplyGB=PopDiv.extend({
      initialize:function(){
	 	this.parent();
	 	this.id="gb_reply";
	 	this.clickId="";
	 	this.width=350;
	 	this.height=300;
	 	this.init();
	 	this.setTitle("留言回复");
	 	this.setStatus("填写回复内容");
	 	this.inited=false;
	 	this.loading=false;
        this._userId=null;
        this._gbId =null;

	 },  
	 getHTML:function(){
		var html='<textarea id="reply_content" cols=35 rows=5></textarea>';
        html += '<input type="button" id="gb_reply_submit" name="gb_reply_submit" value="回复"/>';
		return html;
	 },
	 initEvent:function(){
	 	if($("gb_reply_submit"))
	 		$("gb_reply_submit").onclick=this.gbReply.bindAsEventListener(this);
	 },
	 gbReply:function(){
        var c = $('reply_content').value;
	 	this.loading=true;
	 	this.setStatus("正在提交........");
	 	this._post("172baby.console.gbReply",this.onGbReply.bind(this),{content:c,user_id:this._userId,gb_id:this._gbId});
	 },
	show:function(el){
        var a = el.id.split('_');
        this._userId=a[1];
        this._gbId=a[2];
        var o = Position.cumulativeOffset(el);
  		this.setContentHTML(this.getHTML());
  		this.htmlElement.style.left=(o[0]-160)+"px";
  		this.htmlElement.style.top =(o[1]+18)+"px";
  		this.htmlElement.style.display="";
  		this.initEvent();
    },
	onGbReply:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
            this.hide();
            window.gbMod.initialize();
        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setContentHTML(errorMsg);
           this.setStatus(errorMsg);
        }

	}

});
AddContact=PopDiv.extend({
	 initialize:function(){
	 	this.parent();
	 	this.id="contactSetDiv";
	 	this.clickId="addContact";
	 	this.width=300;
	 	this.height=250;
	 	this.init();
	 	this.setTitle("好友设置");
	 	this.setStatus("添加好友是您和其它用户交流的好途径之一");
	 	this.inited=false;
	 	this.loading=false;

	 },
	 getHTML:function(){
	 if(typeof(visitor_id)=="undefined"){
	 	this.inited=true;
	 	var html="请首先登录才能添加好友";
	 	return html;
	 }
	 if(visitor_id==user_id){
	 	this.inited=true;
	 	var html="您不能将自己添加为好友";
	 	return html;
	 }
	 if(!this.inited){
	 	var html="正在加载...";
	 	return html;
	 }

 	 var userName=user_name;
	 	if(!this.relation){
	 		var html='<h2 class="poptitle">点击确定按钮将'+userName+'加为好友</h2>';
	    	html+='将其他用户加入您的好友列表，这样您就可以及时的看到好友的文章和照片.';
	 		html+='<h2 class="poptitle">您是否愿意...</h2>';
	 		html+='<div class="contentDiv">';
			html+='<input name="isContact" value="3" type="radio" id="friend_radio" checked="true"';
			html+='>加为好友?<br>';
  			//html+='<input name="isContact" value="4" type="radio" id="family_radio"';
  			//html+='>加为家人?(可选项)';
  			html+='<input type="submit" name="" value="确定" class="button" id="set_contact_btn">';
			html+='</div>';
	 		return html;
	 	}
	 	var str="";
	 	if(this.relation==2)
	 		str="好友";
	 	else if(this.relation==3)
	 		str="朋友";
	 	else if(this.relation==4)
	 		str="家人";
	 	var html2='<h2 class="poptitle">'+userName+'现在是您的'+str+'</h2>';
	 	if(this.relation==0)
	 	    var html2='<h2 class="poptitle">'+userName+'现在不是您的好友</h2>';
	 	//html2+='您只可以将某一个用户加入到您的好友或是家人中的一个分组中，不能同时加到两个分组里，,您还可以将其从朋友中删除(他仍将保留在您的联系人列表中，除非您将其彻底删除).';
	 	//html2+='<h2 class="poptitle">点击下面按钮从你的好友列表中删除</h2>';
	 	/*html2+='<div class="contentDiv">';
		html2+='<input name="isContact" value="3" type="radio" id="friend_radio"';
		if(this.relation==3)
		    html2+=' checked="true"';
		html2+='>修改为好友?<br>';
  		html2+='<input name="isContact" value="4" type="radio" id="family_radio"';
  		if(this.relation==4)
		    html2+=' checked="true"';
  		html2+='>修改为家人?';
        */
  		//html2+='<p style="margin-left:130px;margin-top:10px"><input type="submit" name="" value="删除" class="button" id="del_contact_btn"></p>';
		html2+='</div>';
		return html2;
	 },
	 initEvent:function(){
	 	if($("set_contact_btn"))
	 		$("set_contact_btn").onclick=this.setContact.bindAsEventListener(this);
	 	if($("del_contact_btn"))
	 		$("del_contact_btn").onclick=this.delContact.bindAsEventListener(this);
	 },
	 setContact:function(){
	 	if(this.loading)
	 		return;
	 	var relation=2;
	 	if($("friend_radio").checked)
	 		relation=$("friend_radio").value;
	 	else if($("family_radio").checked)
	 		relation=$("family_radio").value;
	 	if(parseInt(this.relation)==parseInt(relation)){
	 		this.setStatus("您没有做任何修改");
	 		return;
	 	}
	 	this.loading=true;
	 	this.setStatus("正在提交........");
	 	this._post("bloghome.operate.editRelation",this.onSetRelation.bind(this),{user_id:window.user_id,user_name:window.user_name,relation:relation});

	 },
	 delContact:function(){
	 	if(this.loading)
	 		return;
	    this.loading=true;
	 	this.setStatus("正在提交........");
	 	this._post("bloghome.operate.delRelation",this.onDelRelation.bind(this),{user_id:window.user_id});
	 },
	show:function(e){
  		this.position(e);
  		this.setContentHTML(this.getHTML());
  		this.htmlElement.style.display="";
  		this.initEvent();
  		if(!this.inited)
  			this.getRelation();
    },
    getRelation:function(){
    	this._post("bloghome.operate.getRelation",this.onGetRelation.bind(this),{user_id:window.user_id});
    },
	onGetRelation:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
           this.inited=true;
           this.relation=parseInt(rsp.getAttribute("relation"));
           this.setContentHTML(this.getHTML());
           this.initEvent();
           this.statusElement.style.bottom="4px";

        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setContentHTML(errorMsg);
           this.setStatus(errorMsg);
        }

	},
	onDelRelation:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
           this.relation=0;
           this.setContentHTML(this.getHTML());
           this.initEvent();
           this.statusElement.style.bottom="4px";
           this.setStatus("删除成功");
        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setStatus(errorMsg);
           this.setStatus("删除失败");
        }
        this.loading=false;
	},
	onSetRelation:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
           var relation=rsp.getAttribute("relation");
           this.relation=relation;
           this.setContentHTML(this.getHTML());
           this.initEvent();
           this.statusElement.style.bottom="4px";
           this.setStatus("设置成功");
        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setStatus(errorMsg);
           this.setStatus("设置失败");
        }
        this.loading=false;
	}
});

//var addContact=new AddContact();
//发送短消息
SendMessage=PopDiv.extend({
	 initialize:function(){
	 	this.parent();
	 	this.id="sendMessageDiv";
	 	this.clickId="sendMessage";
	 	this.width=300;
	 	this.height=250;
	 	this.init();
	 	this.setTitle("发站内信(短消息)");
	 	this.setStatus("站内信只有收件人才会看到");
	 	this.inited=false;
	 	this.setContentHTML(this.getHTML());
	 	this.loading=false;
	 },
	 getHTML:function(){
	 	if(typeof(visitor_id)=="undefined"){
     		var html="请登录后留言";
     		return html;
     	}
	 	if(typeof(visitor_name)!="undefined"){
     	      senderName = visitor_name;
     	}else{
     	  	  senderName = "";
     	}


     	var html='<div id="msgcontent" class="contentDiv"><span class="reason">发送人：<input type="text"  id="message_sender_name" value="'+senderName+'" class="inputtext"/><br />';
     	html+='标&nbsp&nbsp&nbsp题：<input  type="text" id="subject" value="" class="inputtext"/><br />';
        html+='内&nbsp&nbsp&nbsp容：<textarea id="message_content"  rows="3" class="inputtext"></textarea><br />';
        html+='<p style="margin-left:140px;margin-top:8px"><input id="submit_send" type="button" value="提交" class="button"/><input id="cancel_send" type="button" value="取消" class="button"></p></span></div>';
        html+='<div id="send_result_message" style="display:none"></div>';
	 	return html;

	 },
	 initEvent:function(){
	 	if($("submit_send"))
	 		$("submit_send").onclick=this.userSendMessage.bindAsEventListener(this);
	 	if($("cancel_send"))
	 		$("cancel_send").onclick=this.cancelSend.bindAsEventListener(this);
	 },
	 userSendMessage:function()
    {
    	  if(this.loading)
    	  	return;
          var senderName=$("message_sender_name").value;
          var subject=$("subject").value;
          var content=$("message_content").value;
          senderName=senderName.trim();
          subject=subject.trim();
          content=content.trim();
          if(senderName.length<1){
          	alert("发送人不能为空，请输入您的姓名！");
          	return;
          }
          if(subject.length<1){
          	alert("请输入标题！");
          	return;
          }
          if(content.length<1){
          	alert("请输入内容！");
          	return;
          }
          this.loading=true;
          this._post("bloghome.operate.sendMessage",this.onUserSendMessage.bind(this),{sender_name:senderName,subject:subject,content:content,to_user_id:window.user_id});
          this.setStatus("正在发送站内信...");
    },
    cancelSend:function()
    {
        this.htmlElement.style.display="none";
    },
    show:function(e){
  		this.position(e);
  		this.setContentHTML(this.getHTML());
  		this.htmlElement.style.display="";
  		this.initEvent();
    },
    onUserSendMessage:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
           this.setStatus("发送成功");
           setTimeout(this.clearMsg.bind(this),1000);
        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setStatus(errorMsg);
		   alert(errorMsg);
           this.setStatus("发送失败");
        }
        this.loading=false;
	},
	 clearMsg: function(){
        this.setContentHTML(this.getHTML());
        this.initEvent();
    }
});
//var sendMessage=new SendMessage();

//收藏

AddFavorite=RequestBase.extend({
	initialize:function(){
		this.parent();
		this.judgeFavorite();
		this.loading=false;
	},
	bind:function(id){
    	$(id).onclick = this.addToFavorite.bindAsEventListener(this);
    },
    //判断一个人是否收藏了某个资源
    judgeFavorite:function(){
    	if(typeof(res_id)!="undefined"&&res_type<5&&typeof(visitor_id)!="undefined"&&visitor_id&&!isOwner){
    		this.loading=true;
    	    this._post("bloghome.operate.getFavorite",this.onGetFavorite.bind(this),{res_id:res_id,res_type:res_type});
    	}
    },
     onGetFavorite:function(xmlhttp){
    	if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
        	var result=parseInt(rsp.getAttribute("result"));
        	//如果已经收藏
        	if(result==1){
        		if($("addFavBtn"))
        			$("addFavBtn").innerHTML="取消收藏";
        	}

        }else{
        	var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
            var errorMsg = err.getAttribute("msg");
            alert("错误信息:"+errorMsg);
        }
        this.loading=false;
    },
    //第三个参数为点击的元素
    addFavorite:function(id,res_type,element){
		if(this.loading)
    		return;
    	//如果是点击的取消收藏
    	this.element=element;
    	this.id=id;
    	this.res_type=res_type;
    	if(element&&element.innerHTML=="取消收藏")
    		del=1;
    	else
    		del=0;
        this._post("bloghome.operate.addFavorite",this.onAddFavorite.bind(this),{res_id:id,res_type:res_type,del:del});
    },
    onAddFavorite:function(xmlhttp){
    	if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
        	var result=parseInt(rsp.getAttribute("result"));

        	if(result==0){
        		alert("对不起，收藏失败！");
        	}
        	else if(result==-1){
        		alert("对不起，您不能收藏自己的！");
        	}
        	//
        	else if(result==-2){
        		alert("您已经收藏！取消收藏请点击取消收藏");
        		this.element.innerHTML="取消收藏";
        	}
        	//1表示收藏成功,2表示删除收藏成功
        	//TODO:解析数字
        	else if(result==1){
        		alert("收藏成功");
        		this.element.innerHTML="取消收藏";
        		if(ele=$("fav_"+this.res_type+"_"+this.id))
        			ele.innerHTML=parseInt(ele.innerHTML)+1;
        	}
        	else if(result==2){
        		alert("删除收藏成功");
        		this.element.innerHTML="收藏";
        		if(ele=$("fav_"+this.res_type+"_"+this.id))
        			ele.innerHTML=parseInt(ele.innerHTML)-1;
        	}
        }
        else{
        	var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
            var errorMsg = err.getAttribute("msg");
            alert("错误信息:"+errorMsg);
        }
        this.loading=false;
    }
});

window.resourceFav=new AddFavorite();
function addFav(resource_id,resource_type,element){
	resourceFav.addFavorite(resource_id,resource_type,element);
}

if(typeof(EIP)!="undefined"&&typeof(eipOptions)!="undefined"&&isOwner){
	eipOptions.each(function(e){
		new EIP(e[0],e[1]);
	});

}

//标签
 AddResourceTags=PopDiv.extend({
	 initialize:function(){
	 	this.parent();
	 	this.id="addResourceTagDiv";
	 	this.clickId="add_tag_btn";
	 	this.width=300;
	 	this.height=250;
	 	this.init();
	 	this.setTitle("添加标签");
	 	this.setStatus("在这里您可以方便对添加标签");
	 	this.inited=false;
	 	this.setContentHTML(this.getHTML());
	 },
	 getHTML:function(){
	  		var html="";
     	  	html+='<div id="tags_content" class="contentDiv">用逗号隔开标签名字:<br /><input id="tag_name" cols="20" rows="3" value="" /><br />';
          	html+='<input class="button" id="submit_tag" type="submit" value="提交" />&nbsp;<input class="button" id="cancel_add_tag" type="submit" value="取消"><br />';
          	html+='<a href="javascript:;" id="display_user_tags" class="popLink">从已有的标签中选择</a><br/><div id="user_all_tags_list" class="userTags" style="*height:100%"></div></div><br clear="all"/>';
	 		return html;

	 },
	 initEvent:function(){
	 	if($("submit_tag"))
	 		$("submit_tag").onclick=this.addTag.bindAsEventListener(this);
	 	if($("cancel_add_tag"))
	 		$("cancel_add_tag").onclick=this.hide.bindAsEventListener(this);
	 	if($("display_user_tags"))
	 		$("display_user_tags").onclick=this.displayUserTags.bindAsEventListener(this);
	 },
     displayUserTags:function()
     {
    	  if($("user_all_tags_list").innerHTML.length<1){
    	  	this._post("bloghome.tag.getTags",this.onGetUserTags.bind(this),{user_id:window.user_id});
    	  }else{
    	    $("user_all_tags_list").style.display="block";
      		$("display_user_tags").innerHTML="请选择标签:";
    	  }

     },
     //添加标签
     addTag:function()
     {
          var tagNames=$("tag_name").value;
          if (tagNames.trim()==""){
          	alert("对不起，标签名不能为空");
          	return false;
          }

          this._post("bloghome.tag.addResourceTag",this.onAddTags.bind(this),{tag:tagNames,res_id:res_id,res_type:res_type,subdomain:window.subdomain});
          $("user_all_tags_list").style.display="none";
          $("display_user_tags").innerHTML="从已有的标签中选择";
          $("display_user_tags").onclick=this.displayUserTags.bindAsEventListener(this);
     },

     onGetUserTags:function (xmlhttp){
     	this.parseData(xmlhttp);
     	var data=this.data['tag'];


        if (data.length>0) {
            var html="";
             $("display_user_tags").innerHTML="请选择用户标签:";
            for(var i=0;i<data.length;i++){
            	 html+='<a href="javascript:;" id="userTag_'+data[i].id+'" class="popLink">&nbsp;<span id="userTagValue'+data[i].id+'">'+data[i].name+'</span>&nbsp;</a>'

            }
            $("user_all_tags_list").innerHTML=html;

        } else {
        	$("tagsTipMsg").innerHTML="";
        	$("user_all_tags_list").innerHTML="没有可供显示的标签！";
        }

        var elements=$$("#user_all_tags_list a");
        if(elements){
        	elements.forEach(function(el){
        		el.onclick=this.chooseTag.bindAsEventListener(this);
        	}.bind(this));
        }
    },
    /**
     * 选择标签
     */
    chooseTag:function(e){
    	var tag=$("tag_name");
    	this.inputTags=new Array();

    	if(tag.value.trim()!=""){
     		var tagArray=tag.value.split(",");
      		for(var i=0;i<tagArray.length;i++){
        		if(tagArray[i].trim()!=""){
          		this.inputTags[this.inputTags.length]=tagArray[i];
        		}
      		}
    	}

    	var element=Event.element(e).parentNode;
    	var tag_id=(element.id).substring(8);
    	var tagName=$("userTagValue"+tag_id).innerHTML;
    	if(!this.isHaveTag(tagName)){
      		this.inputTags[this.inputTags.length]=tagName;
      		$("userTagValue"+tag_id).style.color="#c45c5c";
    	}
    	else{
      		$("userTagValue"+tag_id).style.color="#0063DC";
      		var userTagst=new Array();
      		for(var i=0;i<this.inputTags.length;i++){
        		if(this.inputTags[i]!=tagName){
          			userTagst[userTagst.length]=this.inputTags[i];
        		}
       		}
     		this.inputTags=userTagst;
    	}

    	var str="";
    	for(var i=0;i<this.inputTags.length;i++){
      		str+=this.inputTags[i]+",";
   		}
    	str=str.substr(0,str.length-1);
    	$("tag_name").value=str;
  	},
  	//判断是否已经添加了tag
    isHaveTag:function(tagName){
    	if(!this.inputTags)
      		return false;
    	for(var i=0;i<this.inputTags.length;i++){
      		if(this.inputTags[i]==tagName){
        		return true;
      		}
    	}
    	return false;
    },
    onAddTags:function(xmlhttp)
    {
     if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
        	this.setStatus("标签添加成功");
        	//解析数据
        	this.parseData(xmlhttp);
        	var data=this.data['tag'];
        	if(data.length>0){
        		var html="";
        		for(var i=0;i<data.length;i++){
        			html+='<li id="tag_'+data[i].id+'"><a href="'+data[i].link+'">'+data[i].name+'</a>';
        			html+='[<a href="javascript:;" class="delTag" onclick="deleteTag('+data[i].id+')" id="del_tag_'+data[i].id+'">&nbsp;X&nbsp;</a>]';
            		html+='</li>';
        		}

        		var eles=$$("#tags_list li");

        		if(eles&&eles.length)
        			new Insertion.Bottom("tags_list",html);
        		else
        			$("tags_list").innerHTML=html;

        	}
        }

        setTimeout(this.clearMsg.bind(this),2000);
    },
    //删除标签
    deleteTag:function (id){
    	if(confirm("确定要删除此标签吗？")){
    		this._post("bloghome.tag.delResourceTag",this.onDelTags.bind(this),{res_id:res_id,res_type:res_type,tag_id:id});
    	}
    },
    //删除标签后的返回结果
    onDelTags:function (xmlhttp){
        if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
        	var tag_id=rsp.getAttribute("tagid");
            var tag = $('tag_'+tag_id);
            if(tag){
            	tag.parentNode.removeChild(tag);
            	alert("标签已经成功删除！");
            }
        } else {
            var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
            var errorMsg = err.getAttribute("msg");
        }
    },
    // 提交成功后显示
    clearMsg: function(){
    	$("tag_name").value="";
    },
    show:function(e){
  		this.position(e);
  		this.htmlElement.style.display="";
  		this.initEvent();
    },
    //
    parseData:function(xmlhttp){
		var dataNode=xmlhttp.responseXML.getElementsByTagName("data");
		var nodeName=dataNode[0].getAttribute("name");
		this.name=nodeName;
		//考虑分页的情况
		var total=dataNode[0].getAttribute("total");

		if(total)
			this.total=parseInt(total);

		var perPage=dataNode[0].getAttribute("perpage");
		if(perPage)
			this.perPage=parseInt(perPage);
		var data=[];
		var nodes=xmlhttp.responseXML.getElementsByTagName(nodeName);

		if(nodes){
			var da=[];
			for(var i=0;i<nodes.length;i++){
				var node=nodes[i];
				var cnodes=node.childNodes;
				var d=[];
				for(var j=0;j<cnodes.length;j++){
					var name=cnodes[j].tagName;
					if(cnodes[j].firstChild)
						var value=cnodes[j].firstChild.data;
					else
						var value="";
					d[name]=value;

				}
				//var icon=nodes[i].childNodes[5].firstChild.data;
				da[da.length]=d;
				//if(d.id)
				//	da[d.id]=d;
			}
			data[nodeName]=da;
		}
		this.data=data;
	}


});

TagsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
        this.canTag=false;
		if(res_type>4&&res_type!=11)
			return false;
        if(res_type!=1&&res_type!=5&&res_type<100)
		    this.loadFunction();
        else
            this.bindEventFunction();
	},
	init:function(){
		this.id="tags";
		this.contentId="tagsContent";
		this.jstId="tagsJST";
		this.op="bloghome.tag.getResourceTags";
		this.params={user_id:window.user_id,res_id:window.res_id,subdomain:window.subdomain,res_type:window.res_type};
	},
	parseData:function(xmlhttp){
		this.parent(xmlhttp);
		var dataNode=xmlhttp.responseXML.getElementsByTagName("data");

		this.canTag=dataNode[0].getAttribute("cantag");
	},
	//绑定标签的点击事件
	bindEventFunction:function(){
		if(this.canTag||(typeof(isOwner)!="undefined"&&isOwner)||isAdmin){
			var html='<a href="#" class="showmore" id="add_tag_btn">添加标签&gt;&gt; </a>';
			new Insertion.After("tags_list",html);
			if(!window.addResourceTags){
				window.addResourceTags=new AddResourceTags();
				window.deleteTag=addResourceTags.deleteTag.bind(addResourceTags);
			}
			$("add_tag_btn").onclick=addResourceTags.show.bindAsEventListener(addResourceTags);
			//
			var elements=$$("#tags_list li");
			if(elements){
				elements.forEach(function(el){
					var id=el.id.substring(4);
					html='[<a href="javascript:;" class="delTag" onclick="deleteTag('+id+')" id="del_tag_'+id+'">&nbsp;X&nbsp;</a>]';
					new Insertion.Bottom(el.id,html);
				});

			}

		}
	}
});
//如果是在具体文章，照片，视频页面，加载模块
if(typeof(res_id)!="undefined"&&res_type&&typeof(commentsList)=='undefined'){
		new TagsMod();
}

//举报
Report=PopDiv.extend({
	 initialize:function(){
	 	this.parent();
	 	this.id="reportDiv";
	 	this.clickId="reportBtn";
	 	this.width=300;
	 	this.height=250;
	 	this.init();
      this.resId = window.res_id;
      this.resType = window.res_type;
	 	this.setTitle("举报");
	 	this.setStatus("在这里您可以举报违规的资源");
	 	this.inited=false;
	 	this.setContentHTML(this.getHTML());
	 },
	 getHTML:function(){
     	var html='<div id="report_content" class="contentDiv">';
        html+='请说明原因:：<textarea id="report_reason_content"  rows="3" class="inputtext"></textarea><br />';
        html+='<input id="submit_report" type="button" value="提交" class="button"/><input id="cancel_report" type="button" value="取消" class="button"></span></div>';
        html+='<div id="report_result_message" style="display:none"></div>';
	 	return html;

	 },
	 initEvent:function(){
	 	if($("submit_report"))
	 		$("submit_report").onclick=this.reportBadResource.bindAsEventListener(this);
	 	if($("cancel_report"))
	 		$("cancel_report").onclick=this.cancelSend.bindAsEventListener(this);
	 },
	 reportBadResource:function()
    {
          var content=$("report_reason_content").value;
          content=content.trim();
          if(content.length<1){
          	alert("请输入举报原因！");
          	return;
          }
          this._post("bloghome.operate.report",this.onUserReport.bind(this),{res_Id:this.resId,reason:content,res_type:this.resType});
          $("report_result_message").innerHtml="正在提交...";
          $("report_result_message").style.display="";
    },
    cancelSend:function()
    {
        this.htmlElement.style.display="none";
    },
    show:function(e){
  		this.position(e);
  		this.setContentHTML(this.getHTML());
  		this.htmlElement.style.display="";
  		this.initEvent();
    },
    onUserReport:function(xmlhttp){
		 if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if (stat == "ok") {
           this.setStatus("举报成功");
           setTimeout(this.clearMsg.bind(this),2000);
        }
       else{
           var err = rsp.firstChild;
           var errorCode = err.getAttribute("code");
           var errorMsg = err.getAttribute("msg");
           this.setStatus(errorMsg);
        }
	},
	 clearMsg: function(){
        this.setContentHTML(this.getHTML());
        this.initEvent();
    }
});
//var Report=new Report();
var elements=$$(".title h2");
if(elements){
	elements.each(function(element){
		element.onclick=modToggle;
	});
}
StatsLoadder=RequestBase.extend({
	initialize:function(){
        if(user_id=="10954")
            alert(user_id);
		this.parent();
    	this.init();
        this.needSession=false;
	},
	init:function(){
	//博客首页

		if(typeof(in_main)!="undefined"||typeof(in_blog)!="undefined"||typeof(in_photo_main)!="undefined"){
        var elements=document.getElementsByTagName('span');
			var catIds=new Array();
			var postIds=new Array();
			var topIds=new Array();
			var photoIds=new Array();
			var videoIds=new Array();
			var magaIds=new Array();


			if(elements){
				for(var i=0;i<elements.length;i++){
					var id=elements[i].id;
					if(id){
					  if(id.substring(0,3)=="cat")
					  	catIds[catIds.length]=id.substring(4);
					  else if(id.substring(0,7)=="topread")
					  	topIds[topIds.length]=id.substring(8);
					  else if(id.substring(0,11)=="postcomment")
					  	postIds[postIds.length]=id.substring(12);
					  else if(id.substring(0,8)=="photohit")
					  	photoIds[photoIds.length]=id.substring(9);	
					  else if(id.substring(0,8)=="videohit")
					  	videoIds[videoIds.length]=id.substring(9);
					  else if(id.substring(0,7)=="magahit")
                        magaIds[magaIds.length]=id.substring(8);
					}
				}
			}
			if(typeof(in_main)!="undefined")
				type=this.type="main";
			else if(typeof(in_photo_main)!="undefined")
				type=this.type="photo";	
			else type=this.type="blog";
             if(typeof(in_tuya_main)!="undefined")
				type=this.type="tuya";	

			cat_id=catIds.join(",");
			post_id=postIds.join(",");
			top_id=topIds.join(",");
			photo_id=photoIds.join(",");
			video_id=videoIds.join(",");
			maga_id=magaIds.join(",");
			this._post("yakbo.operate.loadStats",this.onLoadStats.bind(this),{user_id:user_id,type:type,cat_id:cat_id,post_id:post_id,top_id:top_id,photo_id:photo_id,video_id:video_id,maga_id:maga_id});
		}

	},

	onLoadStats:function(xmlhttp){
		var text=xmlhttp.responseText;
		eval(text);
	}

});

//加载好习惯数据
HabitLoadder=RequestBase.extend({
	initialize:function(){
		this.parent();
    	this.init();
        this.needSession=false;
	},
	init:function(){
	//博客首页

		if(typeof(in_main)!="undefined"&&$("habitMain")){
			this._post("yakbo.kid.getUserHabitInfo",this.onLoadHabit.bind(this),{user_id:user_id});
		}

	},

	onLoadHabit:function(xmlhttp){
		if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1) {
            return;
        }
        var rsp = elements[0];


        //var stat = rsp.getAttribute("stat");
		var stat = rsp.getAttribute("stat");

		if(stat == "ok"){
			var habitName=rsp.getAttribute("name");
			var habitId=rsp.getAttribute("id");
			var n=rsp.getAttribute("n");
			var html='<table width="100%"><tr><td>'+user_name+'正在养成<a href="/habits/'+habitId+'.html">'+habitName+"</a>的好习惯,Ta已经得到"+n+"朵<img src='/imgs/red-flower-48x48.png' width='22'>了,<a href='/habits/"+habitId+".html'>给Ta些鼓励吧！</a><br>";
			var nodes = rsp.childNodes;
			if(nodes && nodes.length>0){
				for(var i=0;i<nodes.length;i++){
					var node=nodes[i];
					var date=node.childNodes[0].firstChild.data;
					var content=node.childNodes[1].firstChild.data;
					var result=node.childNodes[2].firstChild.data;
					html+=date+':'+content+"&nbsp;获得了"+result+"朵小红花<img src='/imgs/red-flower-48x48.png' width='22'><br/>";
				}
			}
			html+='</td><td><a href="http://www.172baby.com/site.php?op=habitIndex"><img src="/imgs/habitlogo.gif"></a></td></tr></table>';
			$("habitContent").innerHTML=html;
		}else{


		}
	}//end 函数

});
//var statsLoadder=new StatsLoadder();
//填充数据函数
function fillstats(type,data){
	if(type=="post"){
		for(var i=0;i<data.length;i++){
			var d=data[i];
			if($("postcomment_"+d[0])){
				if($("postread_"+d[0]))
					$("postread_"+d[0]).innerHTML=d[1];
				$("postcomment_"+d[0]).innerHTML=d[2];
				$("postfav_"+d[0]).innerHTML=d[3];
			}
		}
	}
	else if(type=="cat"){
		for(var i=0;i<data.length;i++){
			var d=data[i];
			if($("cat_"+d[0])){
				$('cat_'+d[0]).innerHTML=d[1];
			}

		}
	}
	else if(type=="top"){
		//for(var i=0;i<data.length;i++){
			var d=data;
			if($("topread_"+d[0])){
				$("topread_"+d[0]).innerHTML=d[1];
				$("topcomment_"+d[0]).innerHTML=d[2];
				$("topfav_"+d[0]).innerHTML=d[3];
			}
		//}
	}
	else if(type=="summary"){
		var d=data;
			if($("total_reads")){
				$("baby_coin").innerHTML="<img src='/imgs/control/sns/baby-coin.png' width=10/> "+d.coin+"";
				$("smart_score").innerHTML=d.score;
				$("total_reads").innerHTML=d.total_reads;
				$("total_posts").innerHTML=d.total_posts;
				$("total_photos").innerHTML=d.total_photos;
				$("total_videos").innerHTML=d.total_videos;
				$("total_comments").innerHTML=d.total_comments;
				$("total_photo_comments").innerHTML=d.total_photo_comments;
				$("total_video_comments").innerHTML=d.total_video_comments;
				$("total_guestbooks").innerHTML=d.total_guestbooks;
                if( d.level == 1 )
                    $("vip_type").innerHTML="园长";
                else if( d.level == 2 )
                    $("vip_type").innerHTML="教师";
                else if( d.vip_type =='normal'){
                    $("vip_type").innerHTML="普通用户";

                }else{
                    if( d.vip_type == 4 ) $("vip_type").innerHTML="终身VIP";
                    else{ 
                        if (d.days_remain>84)var aaa = 84.5;
                        else if(d.days_remain>-1) var aaa = d.days_remain;
                        if( d.vip_type ==1 ){ var vipTypeHtml ="银卡VIP<br/>"; }
                        else if( d.vip_type ==3 ) var vipTypeHtml ="金卡VIP<br/>";
                        $("vip_type").innerHTML=vipTypeHtml ;
                        if(d.days_remain>-1)  
                        {
                            vipTypeHtml = '<label>&nbsp;VIP 剩余天数：</label><span class="percent" id="percentGreen" title="还剩下'+d.days_remain+'天"><span class="volume" style="width: '+aaa+'px;"></span></span>';
                            $("vip_type").parentNode.innerHTML+=vipTypeHtml ;
                        }
                    }
                }

                    if(d.days_remain==-1 || d.level==1 || d.level==2){
                        //只有SuperVIP用户才显示
                        $("photo_quota").innerHTML+="已用空间"+d.photo_usage+" M";
                        $("video_quota").innerHTML+="已用空间"+d.video_usage+" M";
                    }else{
                        var prs = d.photo_quota - d.photo_usage; var vrs = d.video_quota - d.video_usage;
                        var prsp = parseInt((prs/d.photo_quota)*84.5);var vrsp=parseInt((vrs/d.video_quota )*84.5);
                        prsp=prsp<1?1:prsp;vrsp=vrsp<1?1:vrsp;
                        //只有VIP用户才显示
                        $("photo_quota").innerHTML+='<span class="percent" id="percentBlue" title="还剩空间'+prs+'M"><span class="volume" style="width: '+prsp+'px;"></span></span>';
                        $("video_quota").innerHTML+='<span class="percent" id="percentOrange" title="还剩空间'+vrs+'M" style=""><span class="volume" style="width: '+vrsp+'px;"></span></span>';
                    }

			}

	}
    else if(type=="photo"){
		var d=data;
        for(var i=0;i<data.length;i++){
            var d=data[i];
			if($("photohit_"+d[0])){
				$("photohit_"+d[0]).innerHTML=d[1];
				$("photocomment_"+d[0]).innerHTML=d[2];
		
				
			}
        }   
	
	}
	else if(type=="video"){
		var d=data;
        for(var i=0;i<data.length;i++){
            var d=data[i];
			if($("videohit_"+d[0])){
				$("videohit_"+d[0]).innerHTML=d[1];
				$("videocomment_"+d[0]).innerHTML=d[2];
		
				
			}
        }   
	
	}
	else if(type=="maga"){
		var d=data;
        for(var i=0;i<data.length;i++){
            var d=data[i];
			if($("magahit_"+d[0])){
				$("magahit_"+d[0]).innerHTML=d[1];
				$("magacomment_"+d[0]).innerHTML=d[2];
		
				
			}
        }   
	
	}
}

if(typeof(in_main)!="undefined"&&typeof(is_config)=='undefined'){
	modSetting.initBind();
}
//修改首页的图片
 if(typeof(in_main)!="undefined"&&in_main&&typeof(is_config)=="undefined"){
	 
       if($("postsList"))
              window.onload=function(){new HabitLoadder();fixImgs($("postsList"));};
 }
 if(typeof(in_main)!="undefined"&&typeof(gbMod)=="undefined")
    window.gbMod=new GuestbookMod();

//编辑照片标题
if(typeof(in_photo_main)!="undefined"){

		if(window.isOwner){
			var elements=$$("#photosList .photoset");
			if(elements){
				elements.each(function(el){
					var id=el.id;
					id=id.substring(6);
					new EIP('photo_t_'+id, {
						params:{user_id:window.user_id,res_id:id,type:"photoTitle"}
					});
			
				});
		
			  }
		}	  
}
var winOnLoad = window.onload;
  window.addContact=new AddContact();
    window.sendMessage=new SendMessage();
    window.watchResource=new WatchResource();
window.onload = function(){
    
  
	if( winOnLoad ) winOnLoad();
//    var Report=new Report();
	//验证
    //setTimeout("window.statsLoadder=new StatsLoadder()",1200);
	if(typeof(authId)!="undefined"){
		//如果是自己，管理员，园长或者收费用户
		//if(visitor_id&&(isOwner||isVipUser)){
        if(visitor_id){
			for(i=0;i<authId.length;i++)
				$(authId[i]).style.display="block";

		}else{
			for(i=0;i<authId.length;i++){
				
				$(authId[i]).style.display="block";
				if(!visitor_id)
					$(authId[i]).innerHTML="请<a href='http://www.172baby.com'>登录</a>后查看";
				else{
					if(typeof(authFunc)!="undefined"){
						authFunc();
						var html=user_name+"老师共有"+total+"张照片，只有<a href=\"http://help.172baby.com/posts/44.html\">VIP用户</a>才可以访问更多照片,点<a href=\"http://help.172baby.com/posts/44.html\">这里</a>了解什么是VIP用户";
						new Insertion.Top(authId[i],html);
					}else{
						if(authId[i]=="mediaList")
							$(authId[i]).innerHTML=user_name+"老师共有"+total+"个影音，只有<a href=\"http://help.172baby.com/posts/44.html\">VIP用户</a>才可以访问更多,点<a href=\"http://help.172baby.com/posts/44.html\">这里</a>了解什么是VIP用户";
						else
							$(authId[i]).innerHTML="只有<a href=\"http://help.172baby.com/posts/44.html\">VIP用户</a>才可以访问更多,点<a href=\"http://help.172baby.com/posts/44.html\">这里</a>了解什么是VIP用户";
					}
					
				}

			}
		}

	}

        if(user_id=="10954")
            alert(user_id);
    new StatsLoadder();
}

