/**
 * Pwr-on Co.
 *
 * @package    Pwron_Blog
 * @copyright  Copyright (c) 2011 Pwron Co.
 * @license    http://pwr-on.com/license.txt
 * @demo       http://pwr-on.com/blog 
 * @mail       info@pwr-on.com 
 */
BlogVoting = Class.create();
BlogVoting.prototype = {
    initialize : function(){}, 
    voting : function(element, pValue){
        var form = element.parentNode;
        var url = form.getAttribute('action');
        new Ajax.Request(url, {
            parameters: {
                value: pValue
            },            
            onSuccess: function(transport) {
                try {
                    var jsonResponse=transport.responseText.evalJSON();
                    if(jsonResponse.error){
                        alert(jsonResponse.errorTxt);
                        return false;
                    }
                    var item = form.select('div.rating')[0];
                    item.setStyle({
                        width:20*jsonResponse.value+'%'
                        });                    
                } catch(e) {
                //console.log(e.message);
                }
            }.bind(this)
        });        
    }
}


BlogConnect= Class.create();
BlogConnect.prototype = {
    initialize : function(url){
        this.url = url;
    }, 
    doIt : function(element, blogId){
        new Ajax.Request(this.url, {
            parameters: {blog_id: blogId},            
            onSuccess: function(transport) {
                try {
                    var jsonResponse=transport.responseText.evalJSON();
                    if (jsonResponse.value=='0'){
                        element.setAttribute('class', 'b-disconnect');
                    } else if  (jsonResponse.value=='1'){
                        element.setAttribute('class', 'b-connect');
                    }
                } catch(e) {
                    console.log(e);
                }
            }.bind(this)
        });        
        
    }
}

/**
 * BlogCommentForm
 **/
BlogCommentForm = Class.create();
BlogCommentForm.prototype = new VarienForm();

BlogCommentForm.prototype.initialize = (function(super_initialize) {
    return function(formId, editorForm, newUrl, editUrl) {
        super_initialize.call(this, formId, false);
        this.editorForm=editorForm;
        this.newUrl=newUrl;
        this.editUrl=editUrl;
        if (this.form) {
            this.errorElement   = null;
            this.successElement = null;
            this.form.observe('submit', this.submit.bindAsEventListener(this))
        }
    };
})(VarienForm.prototype.initialize);

BlogCommentForm.prototype.currReply = 0;

BlogCommentForm.prototype.replyComment = function(commentReplyId) {
    if (commentReplyId ==null) {
        this.replyComment(0);
        return;
    }
    this.form.writeAttribute('action',this.newUrl);
    this.currReply = commentReplyId;
    var rep;
    if (commentReplyId) {
        rep = $('comment-reply-'+commentReplyId);
        this.form.select('#comment_parent_comment_id')[0].value = commentReplyId;
    } else {
        rep = $('comment-reply');
        this.form.select('#comment_parent_comment_id')[0].value = null;
    }
    this.form.select('#comment_id')[0].value = null;
    this.form.select('#description')[0].value = null;
    var form = $("form-comment-wrap");
    Effect.SlideDown(form, {
        duration: 1
    });
    rep.appendChild($("form-comment-wrap"));
    Effect.Morph(form);
};


BlogCommentForm.prototype.editComment = function(commentEditId, url) {
    this.form.writeAttribute('action',this.editUrl);
    var rep;
    if (commentEditId) {
        rep = $('comment-edit-'+commentEditId);
        this.form.select('#comment_parent_comment_id')[0].value = commentEditId;
    } else {
        rep = $('comment-reply');
        this.form.select('#comment_parent_comment_id')[0].value = null;
    }
    this.form.select('#comment_id')[0].value = commentEditId;
    var that = this;
    new Ajax.Request(url, {
        onSuccess: function(transport) {
            try {
                var jsonResponse=transport.responseText.evalJSON();
                if(jsonResponse.error){
                    alert(jsonResponse.errorTxt);
                    return false;
                }
                that.form.select('#description')[0].value = jsonResponse.description;
                that.editorForm.upPreview(jsonResponse.format);
                var form = $("form-comment-wrap");
                Effect.SlideDown(form, {
                    duration: 1
                });
                rep.appendChild($("form-comment-wrap"));
                Effect.Morph(form);
            } catch(e) {
                console.log(e.message);
            }
        }.bind(this)
    });
    
    
};

