//var CategoryMod = Class.create();
CategoryMod= BaseModule.extend({
	initialize:function(){
		this.parent();
		
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="categories";
		this.contentId="categoriesContent";
		this.jstId="categoriesJST";
		this.op="bloghome.blog.getCategories";
		this.params={user_id:window.user_id};


	},
	bindEventFunction:function(){
		var elements=$(this.contentId).getElementsByTagName("LI");
		for(var i=0;i<elements.length;i++){
            var element=elements[i].firstChild;
			//elements[i].onclick=this.chooseCategory.bindAsEventListener(this);
            var catId=parseInt(element.id.substring(4));
            if(catId==0)
                element.href="/posts/";
            else
                element.href="/posts/#"+catId;
            element.target="_blank";
		
		}
	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=articleCategoriesList">分类管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	},
	chooseCategory:function(e){
		var el=Event.element(e);
		if(el.id){
			var id=el.id.substring(4);
			if(!el.postMod){
			//todo：修改模块名字
				//el.postMod=Class.create();
				el.postMod=Pager.extend({
					initialize:function(){
						this.parent();
						this.init();
						this.loadFunction();
					},
					init:function(){
						this.id="postsList";
						
						this.contentId="postsListContent";
						$(this.contentId).innerHTML="正在加载数据";
						this.jstId="postsListJST";
						this.op="bloghome.blog.getArticles";
						this.params={user_id:window.user_id,cat_id:id};

				}
			});
			}
			new el.postMod();
		
		}
		return false;
	
	}
}	
);
GiftMod=BaseModule.extend({
	initialize:function(){
		this.parent();
        this.init();
        setTimeout(this.loadFunction.bind(this), 100 );
    },
    init:function(){
        this.params={user_id:window.user_id};
		this.id="gift";
		this.contentId="giftContent";
		this.jstId="giftJST";
		
		this.op="bloghome.operate.getGift";
    }
});

RecentVisitorsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
        this.init();
        setTimeout(this.loadFunction.bind(this), 3000 );
    },
    init:function(){
		if(!$('recentVisitors'))
			this.createMod();
        this.params={user_id:window.user_id};
		this.id="recentVisitors";
		this.contentId="recentVisitorsContent";
		this.jstId="visitorsListJST";
		
		this.op="bloghome.operate.getRecentVisitors";
    },
	createMod:function(){

	    var html='<div class="mod modposition" id="recentVisitors">';
        html+='<div class="title" title="">';
        html+='<div class="leftwrap"></div>';
        html+='<div class="rightwrap"></div>';
        html+='<div class="toggle" title=""></div>';
        html+='<h2>最近访客</h2>';
        html+='</div>';
        html+='<div class="modcontent" id="recentVisitorsContent">';

  	    html+='<p>正在加载数据...</p>';
        html+='</div>';
        html+='</div>';
		if($("myFriends"))
			new Insertion.After('myFriends',html);
		else
			new Insertion.After('pInfo',html);

	}
});


//得到用户最近访问的商家历史
VisitShopsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
        this.init();
        setTimeout(this.loadFunction.bind(this), 3000 );
    },
    init:function(){
		this.id="visitShops";
		this.contentId="visitShopsContent";
		this.jstId="visitShopsListJST";
		this.op="bloghome.operate.getVisitShops";
		this.params={cookie_shopIds:window.cookie_shopIds};
    }
});



NewPostsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="newPosts";
		this.contentId="newPostsContent";
		this.jstId="newlistJST";
		this.op="bloghome.blog.getNewPosts";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});


NewCommentsMod=BaseModule.extend({
	initialize:function(){
		this.needSession=false;
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.id="newComments";
		this.contentId="newCommentsContent";
		this.jstId="newlistJST";
		this.op="bloghome.blog.getNewComments";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=viewPostComments&style=receive">回复管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});

MyLinksMod=BaseModule.extend({
	initialize:function(){
		
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="myLinks";
		this.contentId="myLinksContent";
		this.jstId="linksJST";
		this.op="bloghome.blog.getLinks";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=linksList">链接管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});
PInfoMod=BaseModule.extend({
	initialize:function(){
		
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="pInfo";
		this.contentId="pInfoContent";
		this.jstId="pInfoJST";
		this.op="bloghome.blog.getPInfo";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=editProfile">个人资料管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	},
	bindEventFunction:function(){
        
        var w=$("pInfoContent").offsetWidth-22;
        var element=$("add_resource_btn");
        var eleSrc=element.src;
        if(eleSrc.indexOf("p_")>0&&w>260)
                element.src=eleSrc.replace('small','medium');
        else if(eleSrc.indexOf('blog_icon')>0&&isOwner)
                element.src='/imgs/no_avastar_login.gif';
        if(w>500)
                w=500;
        element.removeAttribute('width');
        element.removeAttribute('height');
        element.style.width=w+'px';
		if(isOwner){
            if($("add_resource_btn")){
                var element=$("add_resource_btn");
			    element.title="点击换一张图片在这儿显示?";
			    new SetBlogIcon("add_resource_btn");
            }
            //插入常用菜单
            var html='<a href="/console.php?op=newPost" style="float:left;width:48%;" target="_blank"><img src="/imgs/icons/16/control/post.gif"/>发表文章</a>';
            html+='<a href="/console.php?op=newResources" style="float:left;width:48%" target="_blank"><img src="/imgs/icons/16/control/photo.gif"/>上传照片</a><br clear="all"/>';
            html+='<a href="/console.php?op=viewPostComments&style=receive" style="float:left;width:48%" style="_blank"><img src="/imgs/icons/16/control/comment.gif"/>回复管理</a>';
            html+='<a href="javascript:;" id="private_articles_btn" style="float:left;width:48%"><img src="/imgs/icons/16/control/private.gif"/>隐私文章</a><br clear="all"/>';
            new Insertion.Bottom("pInfoContent",html);
            $("private_articles_btn").onclick=function(){
            var el=$("private_articles_btn");
			var id="private";
			if(!el.postMod){
			//todo：修改模块名字
				//el.postMod=Class.create();
				el.postMod=Pager.extend({
					initialize:function(){
						this.parent();
						this.init();
						this.loadFunction();
					},
					init:function(){
						this.id="postsList";
						
						this.contentId="postsListContent";
						$(this.contentId).innerHTML="正在加载数据";
						this.jstId="postsListJST";
						this.op="bloghome.blog.getArticles";
						this.params={user_id:window.user_id,cat_id:id};

				}
			});
			}
			new el.postMod();
            }
		}
	}
});

TopPostsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="topPosts";
		this.contentId="topPostsContent";
		this.jstId="topPostsJST";
		this.op="bloghome.blog.getTopPosts";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=postsList&showStatus=-1">文章管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});
MyFriendsMod=BaseModule.extend({
	initialize:function(){
		
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="myFriends";
		this.contentId="myFriendsContent";
		this.jstId="userJST";
		this.op="bloghome.blog.getContacts";
		this.params={user_id:window.user_id};
	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=contactsList">好友管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});
MyGroupsMod=BaseModule.extend({
	initialize:function(){
		
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="myGroups";
		this.contentId="myGroupsContent";
		this.jstId="myGroupsJST";
		this.op="bloghome.blog.getUserGroups";
		this.params={user_id:window.user_id};

	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=myJoinedGroups">圈子管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});

StatsMod=BaseModule.extend({
	initialize:function(){
		
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="stats";
		this.contentId="statsContent";
		this.jstId="statsJST";
		this.op="bloghome.blog.getStats";
		this.params={user_id:window.user_id};
	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});


MusicMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="music";
		this.contentId="musicContent";
		this.jstId="musicJST";
		this.op="bloghome.blog.getMusic";
		this.params={user_id:window.user_id};
	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=musicsList">音乐管理</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});


OtherArticlesMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="otherArticles";
		//this.contentId="otherArticlesContent";
        this.contentId="sameCatList";
		this.jstId="articleListJST";
		this.op="bloghome.blog.getOtherArticles";
		this.params={user_id:window.user_id,article_id:window.res_id,subdomain:window.subdomain,cat_id:window.cat_id};

	}
});


ArticleFavUsersMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="articleFavUsers";
		this.contentId="articleFavUsersContent";
		this.jstId="userJST";
		this.op="bloghome.blog.getArticleFavUsers";
		this.params={user_id:window.user_id,article_id:window.res_id,subdomain:window.subdomain,cat_id:window.cat_id};

	}
});

LastUpdateBlogsMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="lastUpdateBlogs";
		this.contentId="lastUpdateBlogsContent";
		this.jstId="userJST";
		this.op="bloghome.blog.getLastUpdateBlogs";
		this.params={};
	},
	insertManageFunction:function(){
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});

PrevNextArticleMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="prevNextArticle";
		this.contentId="prevNextArticleContent";
		this.jstId="prevNextArticleJST";
		this.op="bloghome.blog.getPrevNextArticle";
		this.params={user_id:window.user_id,article_id:window.res_id,global_cat_id:global_cat_id};
        if(typeof(anon)!='undefined')
            this.params.anon=1;

	},
	bindEventFunction:function(){
        if(typeof(anon)!='undefined')
            return;
		var data=this.data['article'];
		var html='<div id="articlesNav" class="mod modposition" style="border-top:1px solid"><div id="articlesNavContent" class="modcontent">';
        for(var i=0;i<data.length;i++){
			var article=data[i];
			if(article.type!="prev"&&article.type!="next"){
				html+=this._getPrevNextHtml(article);
            }    
		}
		html+='<br clear="all"/></div></div>';
		new Insertion.After("relatedArticles",html);	
	},
	_getPrevNextHtml:function(article){
		var type=article.type;
		var str=type.substring(0,4);
        prefix="";
		if(str=="site"){
			prefix="全站导航";
            title="全站文章顺序浏览";
        }    
		else{
			prefix=global_cat_name;
            title="精彩推荐:"+global_cat_name+"的文章顺序浏览";
        }    
		if (type.indexOf("prev")>0)
			prev=true;
		else
			prev=false;
        prefix="《"+prefix+"》";
		var html="";	

		if(prev){
			if(article.id=="0")
				html+='<a style="float:left;width:40%">这是第一篇文章</a>';
			else
				html+=' <a href="'+article.link+'" title="上一篇:'+article.intro+'" style="position:relative;float:left;width:40%;">'+'<strong>«</strong>&nbsp;'+article.topic+'</a>';	
		    html+='<span style="width:20%;float:left;text-align:center" title="'+title+'">'+prefix+'</span>';
        }else{
			if(article.id=="0")
				html+='<a style="float:left;width:40%;text-align:right">这是最后一篇文章</a>';
			else
				html+=' <a href="'+article.link+'" title="下一篇:'+article.intro+'" style="position:relative;float:left;width:40%;text-align:right">'+article.topic+'&nbsp;<strong>»</strong></a>';	
		}
		return html;	
	}
});

RelatedArticlesMod=BaseModule.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="relatedArticles";
		//this.contentId="relatedArticlesContent";
        this.contentId="relatedList";
		this.jstId="relatedArticlesJST";
		this.op="bloghome.blog.getRelatedArticles";
		this.params={user_id:window.user_id,article_id:window.res_id};

	},
    parseData:function(xmlhttp){
        this.parent(xmlhttp);
        var dataNode=xmlhttp.responseXML.getElementsByTagName("data");
        this.morelink=dataNode[0].getAttribute("morelink");        
    },
    bindEventFunction:function(){
        if(this.morelink){
            this.morelink=this.morelink.replace("_","&");
            this.morelink="http://www.bloghome.cn/"+this.morelink;
            //this.morelink=this.morelink.replace(".","&");
            var html='<a href="'+this.morelink+'" target="_blank" class="showmore">更多</a>';
            new Insertion.Bottom("relatedList",html);
        }
    }
});

PostsListMod=Pager.extend({
	initialize:function(){
		this.parent();
		this.init();
		this.loadFunction();
	},
	init:function(){
		this.needSession=false;
		this.id="postsList";
		this.contentId="postsListContent";
		this.jstId="postsListJST";
		this.op="bloghome.blog.getArticles";
		
		var sh=window.location.hash;
		if(sh.length>0){
			catId=sh.substring(1);
		}else{
			catId=0;
		}
		this.params={user_id:window.user_id,cat_id:catId};

	},
	bindEventFunction:function(){
		if(this.total>0){
			this.CACHE[this.cacheKey+this.currentPage]=$(this.contentId).innerHTML;
			this.initPager();
		}
		fixImgs($("postsList"));
	},
	insertManageFunction:function(){
		if(typeof(in_main)=="undefined")
			return false;
		var html='<div class="subtitle subtitborder" style="border-top:none;border-bottom:none"><a href="javascript:;" id="'+this.id+'SettingBtn">设置</a><span style="margin:0 4px">|</span><a href="/console.php?op=newPost">发表文章</a></div>';
		new Insertion.Before($(this.contentId),html);
		if(typeof(modSetting)!="undefined")
			$(this.id+'SettingBtn').onclick=modSetting.show.bindAsEventListener(modSetting);
	}
});

UserDefinedMod=BaseModule.extend({
	initialize:function(id){
		
		this.parent();
		this.id=id;
		this.loadFunction();
	},
	loadFunction:function(){
		this._post("bloghome.blog.getUserDefined",this.onLoad.bind(this),{id:this.id});

	},
	onLoad:function(xmlhttp){
		var rsp=xmlhttp.responseXML.getElementsByTagName("rsp")[0];
		var html=rsp.firstChild.data;
		if(typeof(this.id)=='undefined')
			this.id='';
		userTemplate.createUserDefined($("userDefined"+this.id),html,this.id);
		userTemplate.userDefinedSaved=true;		
	}
});
CalendarMod=BaseModule.extend(
{
    initialize: function()
    {	this.parent();
    	this.needSession=false;
    	this.user_id=window.user_id;
    	var date=new Date();
    	this.year=date.getFullYear();
    	this.month=date.getMonth()+1;
    	this.day=date.getDate();
    	if(!window.CACHE)
    		window.CACHE={};
    	if(!window.CACHE["calendar"])
    		window.CACHE["calendar"]=new Array();
    		//每天的文章的缓存
    	if(!window.CACHE["day_article"])
    		window.CACHE["day_article"]=new Array();	
    	this.dayNames=new Array("S", "M", "T", "W", "T", "F", "S");
    	this.dayNamesShort=new Array("一", "二", "三", "四", "五", "六", "日");
    	this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    	this.monthNames = new Array("一月", "二月", "三月", "四月", "五月", "六月",
                            "七月", "八月", "九月", "十月", "十一月", "十二月");
        this.startDay=0;
        this.today=new Date();
        //当前点击的日期
        this.currentDate=false;
        this.contentId="postsListContent";
		this.jstId="postsListJST";
        this.init();
        this.loading=false; 
        this.isFirst=true;                  		
    },
    init:function(){
    	this._createMonthUI();
    
    },
    _createMainUI:function(calendar){
    	var html= "<table class=\"calMonth\" id=\"calTable\">";
		html += "<thead>";
        html += "<tr class=\"calMonthNav\">";
    	html += "<th class=\"calMonthBackward\"><a href=\"javascript:;\" title=\"prevMonthName\" id=\"prevMonth\">&laquo;</a>"  +"</th>";
    	html += "<th class=\"calMonthCurrent\" colspan=\"5\" id=\"calendarMonth\">"+this.year+"年&nbsp;"+this.monthNames[this.month-1]+"</th>";
    	html += "<th class=\"calMonthForward\"><a href=\"javascript:;\" title=\"nextMonthName\" id=\"nextMonth\">&raquo;</a>"+"</th>";
    	html += "</tr>";

    	html += "<tr class=\"calMonthHeader\">";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay)%7] +"\">" + this.dayNamesShort[(this.startDay)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+1)%7] +"\">" + this.dayNamesShort[(this.startDay+1)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+2)%7] +"\">" + this.dayNamesShort[(this.startDay+2)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+3)%7] +"\">" + this.dayNamesShort[(this.startDay+3)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+4)%7] +"\">" + this.dayNamesShort[(this.startDay+4)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+5)%7] +"\">" + this.dayNamesShort[(this.startDay+5)%7] +"</th>";
    	html += "<th scope=\"col\" abbr=\"" + this.dayNames[(this.startDay+6)%7] +"\">" + this.dayNamesShort[(this.startDay+6)%7] +"</th>";
    	html += "</tr>";
		html += "</thead>";
		html +="<tbody id=\"calendarBody\">";
		html+=calendar;
		html += "</tbody>";
    	html += "</table>";
    	html += '<div id="calendarTip" style="display:none">正在加载数据..</div>';
    	$("calendarContent").innerHTML=html;
    	this.registerEvent();
    	$("prevMonth").onclick=this.getPrevMonth.bindAsEventListener(this);
    	$("nextMonth").onclick=this.getNextMonth.bindAsEventListener(this);   
    },
    getPrevMonth:function(e){
    	if(this.loading)
    		return;
    	var a=this.adjustDate(this.month-1,this.year);
    	this.month=a[0];
    	this.year=a[1];
    	this._createMonthUI();
    },
    getNextMonth:function(e){
    	if(this.loading)
    		return;
    	var a=this.adjustDate(this.month+1,this.year);
    	this.month=a[0];
    	this.year=a[1];
    	this._createMonthUI();
    },
    //产生某个月的视图
    _createMonthUI:function(){
    	if(!window.CACHE["calendar"][this.year+"_"+this.month]){
			this._post('bloghome.blog.getCalendar',this.onGetCalendar.bind(this), {user_id:this.user_id,year:this.year,month:this.month});
			this.loading=true;
    	}//end if
    	else{
    		var s=window.CACHE["calendar"][this.year+"_"+this.month];   	
    		this._createMainUI(s);    	
    		this.registerEvent();
    	}
    },
    onGetCalendar:function(xmlhttp){
    	if (xmlhttp.status != 200) {
            return;
        }
        var text=xmlhttp.responseText;
        this._createMainUI(text);
        var n=window.CACHE["calendar"].length;
        var m=this.year+"_"+this.month;
    	window.CACHE["calendar"][n]=window.CACHE["calendar"][m]=text;
    	this.registerEvent();
    	this.loading=false;
    	
    },
    showCalendarArticle:function(e){
    	var element=Event.element(e);
    	var date=element.id;
    	document.body.scrollTop=0;
    	if(!window.CACHE["day_article"][date]){
    		this.currentDate=date;
    		//blogAPI.getCalendarArticle(this.user_id,date,this.onGetCalendarArticle.bindAsEventListener(this));
    		this._post('bloghome.blog.getCalendarArticle', this.onGetCalendarArticle.bindAsEventListener(this), {user_id:this.user_id,date:date});
    		
    	}
    	else{
    		$("postsListContent").innerHTML=window.CACHE["day_article"][date];
    	}
    },
    onGetCalendarArticle:function(xmlhttp){
    	if (xmlhttp.status != 200) {
            return;
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1)
        {
            return new Array();
        }
        var status=new Array();
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        if(stat=="ok"){
        	this.parseData(xmlhttp);
        	
        	this.data._MODIFIERS=TT_Filters;
			var result = TrimPath.processDOMTemplate(this.jstId, this.data);
        	$("postsListContent").innerHTML=result;
        	var n=window.CACHE["day_article"].length;
        	window.CACHE["day_article"][n]=window.CACHE["day_article"][this.currentDate]=$("postsListContent").innerHTML;
        }
    },
    registerEvent:function(){
    	var elements=document.getElementsByPrefix(new String(this.year));
    	for(var i=0;i<elements.length;i++)
    		elements[i].onclick=this.showCalendarArticle.bindAsEventListener(this);
    },
    adjustDate:function(month, year)
    {
        var a = new Array();
        a[0] = month;
        a[1] = year;

        while (a[0] > 12)
        {
            a[0] -= 12;
            a[1]++;
        }

        while (a[0] <= 0)
        {
            a[0] += 12;
            a[1]--;
        }
        return a;
    },
    getDaysInMonth:function (month, year)
    {
        if (month < 1 || month > 12)
        {
            return 0;
        }

        d = this.daysInMonth[month - 1];

        if (month == 2)
        {

            if (year%4 == 0)
            {
                if (year%100 == 0)
                {
                    if (year%400 == 0)
                    {
                        d = 29;
                    }
                }
                else
                {
                    d = 29;
                }
            }
        }

        return d;
    }
}    	
);
document.getElementsByPrefix = function(prefix) {
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
  
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var eid = child.id;
    if(eid&&eid.indexOf(prefix)>-1)
        elements.push(child);
  }
  
  return elements;
}

//用户首页留言本
GuestbookMod=Pager.extend({
	initialize:function(){
		this.parent();
		this.init();
		setTimeout(this.loadFunction.bind(this), 2000 );
		//this.loadFunction();
	},
	init:function(){
		this.needSession=true;
		this.id="guestbook";
        this.contentId="guestbookContent";
		this.jstId="guestbookJST";
		this.op="bloghome.operate.getGuestbooks";
        this.perPage = 10;
        this.total=0;
		this.params={to_user_id:window.user_id,visitor_id:window.visitor_id,perpage:this.perPage,total:this.total};

	    //处理form
	    if(window.visitor_name){
	  	  $("guestName").value=window.visitor_name;
	    }

	    $("addMessageBtn").onclick=this.addGuestbook.bindAsEventListener(this);
	    //$("resetMessageBtn").onclick=this.reset.bindAsEventListener(this);
	},

    delGuestbook:function(gb_id){
	 	this._post("bloghome.operate.delGuestbook",this.onDelGuestbook.bind(this),{gb_id:gb_id,to_user_id:window.user_id});
    },
	onDelGuestbook:function(xmlhttp){
		if (xmlhttp.status != 200)
        {
            //状态提示
            return new Array();
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1)
        {
            return new Array();
        }
        var status=new Array();
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        var total=rsp.getAttribute("total");
        if(stat=="ok"){
            alert("恭喜，删除成功！");
		    this.initialize();
        }
	    else{
	    	var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
			
            var errorMsg = err.getAttribute("msg");
            
			if(errorCode=="303"){//出错
                alert("出错了，请联系管理员172baby@gmail.com！");
            }
				
	    }
	},

    addGuestbook:function(){
        var user_id=window.visitor_id;
		var to_user_id=window.user_id;

	 	var text=$("gbMsgContent").value;
	 	
	 	var tmpText=text.trim();
	 	tmpText=tmpText.replace("&nbsp;",""); 
	 	tmpText=tmpText.replace("<br>","");
	 	if(tmpText.trim().length<1){
	 		alert("请输入回复内容，至少1个字");
	 		return false;
	 	}
	 	text = encodeURIComponent(text);
	 	var username=$("guestName").value;
	 	//增加验证判断
	 	if(username.trim().length<2){
	 		alert("请输入用户名");
	 		return false;
	 	}
        if($("guestbook_form"))
	 	    $("guestbook_form").style.display="none";
	 	$("tipAddGuestbooks").style.display="block";
        var secret = ($('secret_post').checked)?1:0;

	 	var params={user_id:user_id,text:text,username:username,to_user_id:to_user_id,secret:secret };
	 	this._post("bloghome.operate.addGuestbook",this.onAddGuestbook.bind(this),params);
	},
	//清空留言框的内容
	reset:function(){
		$("gbMsgContent").value="";
	},
	onAddGuestbook:function(xmlhttp){
		if (xmlhttp.status != 200)
        {
            //状态提示
            return new Array();
        }
        var elements = xmlhttp.responseXML.getElementsByTagName("rsp");
        if (elements == null || elements.length != 1)
        {
            return new Array();
        }
        var status=new Array();
        var rsp = elements[0];
        var stat = rsp.getAttribute("stat");
        var total=rsp.getAttribute("total");
        if(stat=="ok"){
		    this.initialize();
		    $("gbMsgContent").value="";
        }
	    else{
	    	var err = rsp.firstChild;
            var errorCode = err.getAttribute("code");
			
            var errorMsg = err.getAttribute("msg");
            
			if(errorCode=="111"){//如果是未登录用户
                //alert("你还没有你登录呢，请登录后再留言吧！");
				if(!window.qLogin){
					window.qLogin=new QuickLogin();
					qLogin.show();
					qLogin.bindFunc(this.quickLogin.bind(this));
				}
			}
			if(errorCode=="303"){//回复出错
                alert("留言时出错了，请联系管理员172baby@gmail.com！");
            }
			if(errorCode=="110"){//回复出错
                alert(errorMsg);
            }
				
	    }
        if($("guestbook_form"))
		    $("guestbook_form").style.display="block";
	 	$("tipAddGuestbooks").style.display="none";
	
	},
    replyUI:function(el){
        el = $(el);
        if( this._gbReply ) this._gbReply.hide();
        else this._gbReply = new ReplyGB();
        this._gbReply.show(el);

    },

    quickLogin:function(params){
		$("guestName").value=params["userName"];
		window.visitor_id=params["userId"];
		
		this.addGuestbook();
	}
});


window.modules={postsList:PostsListMod,
			categories:CategoryMod,
			newPosts:NewPostsMod,
			newComments:NewCommentsMod,
			myLinks:MyLinksMod,
			pInfo:PInfoMod,
			topPosts:TopPostsMod,
			myFriends:MyFriendsMod,
			stats:StatsMod,
			myGroups:MyGroupsMod,
			music:MusicMod,	
			recentVisitors:RecentVisitorsMod,	
			gift:GiftMod,	
			guestbook:GuestbookMod,	
			visitShops:VisitShopsMod,	
			//otherArticles:OtherArticlesMod,
			articleFavUsers:ArticleFavUsersMod,
			lastUpdateBlogs:LastUpdateBlogsMod,
			prevNextArticle:PrevNextArticleMod,
			commentForm:CommentsMod,
			//relatedArticles:RelatedArticlesMod,
			calendar:CalendarMod,
			userDefined:UserDefinedMod}
