var Comments={
    reset: function() {
        this.format="json";
        this.maxResults=0;
        this.minDate="";
        this.maxDate="";
        this.page=1;
        this.idUser="";
        this.idSite="";
        this.idCategory="";
        this.idArticle="";
        this.username="";
        this.comment="";
        this.order="";
        this.callback="";
        this.jsonAPI="http://transversal.canal13.cl/comentarios/index.php?format=json";
    },
    parseResults: function(jsonResult) {
        for (var i=0;i<jsonResult.comments.length;i++) {
            var comment=jsonResult.comments[i];
            comment.getHTML=function() {
                var tmp;
                // Eliminar tags HTML
                tmp=this.comment.replace(/<[^>]+>/g,"");
                // Eliminar carriage return
                tmp=tmp.replace(/\r/g,"");
                // WordWrap cada 30 caracteres seguidos
                tmp=tmp.replace(/([^\s]{30})(?=[^\s])/g,"$1 ");
                // Reemplazar \n por <br /> y limitar a dos \n juntos como máximo
                tmp=tmp.replace(/\n\n[\n]+/g,"\n\n");
                tmp=tmp.replace(/\n/g,"<br />");
                // Reemplazar & ampersand por &amp;
                tmp=tmp.replace(/\&/g,"&amp;");
                return tmp;
            }
            comment.getText=function() {
                return this.comment;
            }
        }
        this.onLoadComplete(jsonResult);
    },
    getComments: function(idSite,idCategory,idArticle) {
        if (idSite) this.idSite=idSite;
        if (idCategory) this.idCategory=idCategory;
        if (idArticle) this.idArticle=idArticle;
        this.callback="Comments.parseResults";
        var url=this.prepareQuery();
        var head=document.getElementsByTagName("head")[0];
        var script=document.createElement("script");
        script.src=url;
        head.appendChild(script);
    },
    addComment: function(comment,username,idSite,idCategory,idArticle) {
        if (idSite) this.idSite=idSite;
        if (idCategory) this.idCategory=idCategory;
        if (idArticle) this.idArticle=idArticle;
        if (comment) this.comment=comment;
        else this.comment=" ";
        if (username) this.username=username;
        this.callback="Comments.onAddComplete";
        var url=this.prepareQuery(true);
        var head=document.getElementsByTagName("head")[0];
        var script=document.createElement("script");
        script.src=url;
        head.appendChild(script);
    },
    prepareQuery: function(addComment) {
        var query=this.jsonAPI;        
        if (this.callback) query+="&callback="+encodeURIComponent(this.callback);
        if (this.idSite) query+="&idSite="+encodeURIComponent(this.idSite);
        if (this.idCategory) query+="&idCategory="+encodeURIComponent(this.idCategory);
        if (this.idArticle) {
            if (this.idArticle.length>30) this.idArticle=this.idArticle.substr(0,30);
            query+="&idArticle="+encodeURIComponent(this.idArticle);
        }
        if (addComment) {
            if (this.idUser) query+="&idUser="+encodeURIComponent(this.idUser);
            if (this.username) query+="&username="+encodeURIComponent(this.username);
            if (this.comment) query+="&comment="+encodeURIComponent(this.comment);
        } else {
            if (this.maxResults) query+="&maxResults="+encodeURIComponent(this.maxResults);
            if (this.minDate) query+="&minDate="+encodeURIComponent(this.minDate);
            if (this.maxDate) query+="&maxDate="+encodeURIComponent(this.maxDate);
            if (this.page) query+="&page="+encodeURIComponent(this.page);
            if (this.order) query+="&order="+encodeURIComponent(this.order);
        }
        return query;
    },
    onLoadComplete: function(jsonResult) {},
    onAddComplete: function(jsonResult) {}
};
Comments.reset();