$(document).ready(function() {

   /**
    * Archive ajax handling
    * Use .live, not just .click, because content inside #archiveWindget, which
    * is exchanged via ajax, contains ".previous a" elements
    */
    $("#archiveWidget .previous a,#archiveWidget .next a").live("click",function() {
        //view ajax progress loader
        $("#ajaxArchiveProgress").ajaxStart(function() {
            $(this).show();
        });

        //load widget content via ajax
        $("#archiveWidget").load(
            //url is from <a> element
            $(this).attr("href")
        );

        $("#ajaxArchiveProgress").unbind();
        return false;
    });

    /**
     * ajax for comment preview
     */
    $("#preview").click(function() {
        //hide and register ajax loader
        $("#commentPreview").ajaxStart(function() {
            //remove error message after message textarea
            $("#message").next("ul").remove();
            $(this).html("<img src=\""+HomeWebApp.getBaseUrl()+"/images/ajax-loader.gif\" alt=\"ajaxLoader\">");
            $(this).show();
        }).hide();

        var msg = $.trim($("#message").val());
        if (msg == "") {
            alert("žádný text není vyplněn");
            return false;
        }
        
        //load message preview
        $("#commentPreview").load(
            HomeWebApp.getBaseUrl()+"/blog/preview/comment",
            { message : msg }
        );

        $("#commentPreview").unbind();
        return false;
    });

    /**
     * article rating
     */
    $("#articleRatigList li a").click(function(){
        //view ajax progress loader
        $("#ratingMessage").ajaxStart(function() {
            $(this).html("<img src=\""+HomeWebApp.getBaseUrl()+"/images/ajax-loader.gif\" alt=\"ajaxLoader\">");
        });

        $.getJSON($(this).attr("href"),function(data) {
            $("#ratingMessage").html(data.message);

            //upadate current rating
            if (data.average && data.votingCount) {
                $("#averageRating").html(data.average);
                $("#votingCount").html(data.votingCount);
            }

            //remove all li nodes without current rating node
            $("#articleRatigList li").not("#current-rating").remove();

            //change width by current rating
            $("#current-rating").width(Math.round(data.average*30)+"px");
        });

        $("#ratingMessage").unbind();
        return false;
    });
});

/**
 * blog entry namespace
 */
HomeWebApp.BlogEntry = {
    /**
     * on submit handler for comment form in blog entry page
     */
    addCommentFormSubmit: function(form) {
        //setup for ajax progress loader
        $("#commentPreview").ajaxStart(function() {
            $(this).html("<img src=\""+HomeWebApp.getBaseUrl()+"/images/ajax-loader.gif\" alt=\"ajaxLoader\">");
            $(this).show();
        }).ajaxStop(function() {
            $(this).hide();
            $(this).unbind();
        });

        return MKLib_JQuery_AjaxForm.onSubmit(form,{
            success: function() {
                location.reload();
            }
        });
    }
}