function refreshNumComments(div,url,id,type){
    $j(document).ready(
                        function()
                        {
                                $j.post(url,{id:id,commentType:type},function(data){
                                                                                        $j("#"+div).html(data);
                                                                                        savingComment = false;
                                                                                        $j('.button_add_comment').css("cursor","pointer");
                                                                                    });
                        }
    );


}

/**
 * refreshProp
 * add 1 to the prop and refresh the value
 * @param string div this is the div to be refresh
 * @param string url this is the url where ajax will ask for the prop
 * @param number userID ID of the user who prop
 * @param number commentID ID of the comment to be prop
 * @param string commentType Type of the comment, internal value
 */
function refreshProp(div,url,userID, commentID, commentType){
    $j(document).ready(
                        function()
                        {
                                $j.post(url,{user_id:userID,comment_id:commentID,object_type:commentType},function(data){$j("#"+div).html(data);});
                        }
    );
}

/**
 * refreshFlag
 * set the flag for a comment
 * @param string div this is the div to be refresh
 * @param string url this is the url where ajax will ask for the prop
 * @param number userID ID of the user who flag
 * @param number commentID ID of the comment to be flag
 * @param string commentType Type of the comment, internal value
 */
function refreshFlag(div,url,userID, commentID, commentType){
    $j(document).ready(
                        function()
                        {
                                $j.post(url,{user_id:userID,comment_id:commentID,object_type:commentType},function(data){$j("#"+div).html(data);});

                        }
    );


}

/**
 * This function gets a block of comments
 * @param int Page Number
 */
 function getPage(numPage,url,div){
     //first we must change the value of the current page. To do it, we save it in a  cookie
     $j.cookie('current_comment_page',numPage , {path: "/"} );
     //Now just call the show action with the new page
     $j(document).ready(
         function(){
             $j.post(
                     url,
                     {
                         id:$j('#cmt_object_id').val(),
                         commentType:$j('#cmt_object_type').val(),
                         current_page:numPage
                     },
                     function(data){
                         $j("#"+div).html(data);
                     }
             );
         }
     );


 }

/**
* This function shows the previous page
*/
function previousPage(){
    //first we get the current page
    var numPage = $j.cookie('current_comment_page');
    if (numPage > 1){
        numPage--;
        getPage(numPage,'/comment/show',"commentDiv");
    }
    else{
        return;
    }
}

function nextPage(){
    //first we get the current page
    var numPage = $j.cookie('current_comment_page');
    var maxPages = $j.cookie('num_comment_pages');
    if (numPage == null)
        numPage=1;
    if(numPage<maxPages){
        numPage++;
        getPage(numPage,'/comment/show',"commentDiv");
    }
    else{
        return;
    }

}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

