/**
 * @author Oren.Chen
 */
JSoul.$(function(){
	JSoul("#commContent").initComment("#pageControl", "#feedback");
});
JSoul.fn.extend({
    /**
     * 初始化用户留言对象
	 * @param {String} pageSelector 分页对象选择表达式
	 * @param {String} postSelector 发表留言对象选择表达式
     * @return JSoul
     */
	initComment: function(pageSelector, postSelector){
		var main = this.extend({
			page: JSoul(pageSelector).find("dl").initPageControl(this),
			post: JSoul(postSelector).initPostForm(this),
			turnPage: function(html){
				this.innerHtml(html);
			},
			insertMsg: function(html){
				this.innerHtml(html + this.innerHtml());
			}
		});
		return this;
	},
    /**
     * 分页对象
     * @param {JSoul} main 内容列表对象
     * @return JSoul
     */
	initPageControl: function(main){
		var self = this.extend({
			fresh: function(html){
				this.innerHtml(html);
				this.initPageControl(main);
			},
			links: this.find("a").bindEvent({
				onclick: function(e){
					$.$xJson(this.href, function(json){
						if(json.data){
							main.turnPage(json.data);
						}
						if(json.Pagination){
							self.fresh(json.Pagination);
						}
					}, null);
					return false;	
				}
			})
		});
		return this;
	},
    /**
     * 用户评论验证
     * @param {JSoul} main 内容列表对象
     * @return JSoul
     */
    initPostForm: function(main){
		var self = this.bindEvent({
			onsubmit: function(e){
				if(this["comment"].value.trim().length < 6){
					alert("评论内容不能少于6个字！");
					this["comment"].focus();
				}else{
					var form = this;
					$.$xJson(this.action, function(json){
						if(json == 1){
							var msg = "<dt><span>[刚刚]</span>我 说：</dt><dd><p>"+form["comment"].value+"</p></dd>";
							main.insertMsg(msg);
							form["comment"].value = "";
						}else if(json == 5){
							alert("你还没有登陆，请先登录！");
						}else{
							alert("发表留言失败");
						}
					}, {act:"post",comment:this["comment"].value.trim()});
					
				}
				return false;
			}
		});
        return this;
    }
});