var paging = {
    params:false,
    object: "",
    id: false,
    init: function(params) {
        this.id=params.id;
        this.object=params.object;
        this.params = this.decode_params(params.hash);
        if(typeof(this.params.page) != "undefined") {
            this.getNewPage(this.params.page);
        }
        if(typeof(this.params.comment) != "undefined") {
            var link=$("#comment-"+params.comment);
            if ($(link).length>0) scroll(0, $(link).attr("offsetTop"));
        }
        var $this = this;
        $(this.object).find(".Pager a").bind("click", function(){
            $this.onPagingClick($this, this);
        });

    },
    onPagingClick: function($this, object) {
        var href=$(object).attr("href");
        if(typeof(href) != "undefined") {
            pparams=$this.decode_params(href);
            $this.getNewPage(pparams.page);
        }
        return true;
    },
    getNewPage: function(nPage) {
        var $this = this;
        $.get(
          '/ajax/comments.php',
          {
            id: this.id,
            ajax: 'Y',
            page: nPage
          },
          function(data){
              $this.onAjaxSuccess($this, data)
          }
        );
    },
    onAjaxSuccess: function($this, data) {
        $($this.object).html(data);
		/*if (false && $.browser.opera) {setTimeout(function() {
			var page=$($this.object).find(".Pager:first span").text();
			console.log(page);
			$(".PaperTitle").attr("id", "page_"+page);
			document.location = document.location},
		1000);}*/
        /*if(typeof($this.params.comment) != "undefined") {
            var link=$($this.object).find("#comment-"+$this.params.comment);
            if ($(link).length>0) scroll(0, $(link).attr("offsetTop"));
        }*/
        //setTimeout('document.location = document.location', 3000);
        //$($this.object).ready(function(){alert(1);document.location = document.location;});
        /*document.location = document.location;
        document.location = document.location;*/
        /*else {*/
			var page=$($this.object).find(".Pager:first span").text();
			$(".PaperTitle").attr("id", "page_"+page);
			document.location = document.location;
		/*}*/
        articleComments.init({id: $this.id});
    },
    decode_params: function(hash) {
        var params = {}, vars = hash.substr(1).split('_');
        var i=0;
        while (i<=vars.length) {
            params[vars[i]] = vars[i+1];
            i=i+2;
        }
        //for (var i in vars) { var pair = vars[i].split('='); params[pair[0]] = pair[1]; }
        return params;
    }
}
var commentForm = {
    init: function(params){
            this.object=params.object;
            var $this = this;
            $(this.object).find("#Comments .item .ans-form a:nth-child(1)").bind("click", function(){
                $this.onReAnswerClick($this, this);
            });
            $(this.object).find("#comments-reply-block a").bind("click", function(){
                $this.onAnswerClick($this, this);
            });
            $(this.object).find("#post-comment").bind("submit", function(){
                $this.onSubmitForm($this, this);
            });
            /*$(this.object).find("#post-comment input[type=submit]").bind("click", function(){
                $(this).attr("disabled", "disabled");
            });*/
     },
    /*onReAnswerClick: function($this, object){
        var item = $(object).parents(".item");
        var id=$(item).find(".comment-id").text();
        $($this.object).find(".Answer:not(#def-comments-answer-block)").remove();
        $($this.object).find("#def-comments-answer-block").hide();
        $(item).after("<div class=\"Answer\">"+$("#def-comments-answer-block").html()+"</div>");
        $($this.object).find(".Answer:not(#def-comments-answer-block) input[name=answer_to]").attr("value", id);
        return false;
    },*/
    onReAnswerClick: function($this, object){
        var item = $(object).parents(".item");
        var id=$(item).find(".comment-id").text();
        $($this.object).find("#Comments .item .ans-form a").show();
        $($this.object).find(".simpleAnswer").remove();
        $(object).after("<div class=\"simpleAnswer\">"+$("#simpleRecomment").html()+"</div>");
        $($this.object).find(".simpleAnswer input[name=answer_to]").attr("value", id);
        $($this.object).find(".simpleAnswer .reset").bind("click", function(){
                $this.onResetClick($this, this);
         });
        $(this.object).find(".simpleAnswer form").bind("submit", function(){
                $this.onSubmitForm($this, this);
        });
        /*$(this.object).find(".simpleAnswer form input[type=submit]").bind("click", function(){
                $(this).attr("disabled", "disabled");
        });*/
        $(object).hide();
        return false;
    },
    onAnswerClick: function($this, object){
        $($this.object).find(".Answer:not(#def-comments-answer-block)").remove();
        $($this.object).find("#def-comments-answer-block").show();
        return false;
    },
    onResetClick: function($this, object){
       var formblock=$(object).parents(".simpleAnswer");
       $(formblock).prev("a").show();
       $(formblock).remove();
       return false;
    },
    onSubmitForm: function($this, object) {
        $(object).find("input[type=submit]").attr("disabled", "disabled");
        return true;
        //return false;
    }
}
var articleComments = {
    init: function(params) {
        params=$.extend({hash: '', id: 0, object:'.Comments'}, params);
        commentForm.init(params);
        paging.init(params);
    }
}


