document.write('<script type="text/javascript" src="/js/cleditor/jquery.cleditor.js"></script>');
document.write('<script type="text/javascript" src="/js/sexybookmarks.js"></script>');

var loader = '<img class="loader" src="/gfx/loading.gif" />';
var loader2 = '<div class="loaderWrapper"><img class="loader" src="/gfx/loader.gif" /></div>';
var cleditor;

function loading(name)
{
    $('input[name=' + name + ']').hide().parent().append(loader);
}

function complete(name)
{
    $('input[name=' + name + ']').show().parent().find('.loader').remove();
}

function vote(joke_id, rate)
{
    var id = $("#joke-" + joke_id);

    jQuery.ajax
    ({
        type: 'post',
        url: '/joke/vote/joke_id/' + joke_id + '/rate/' + rate,
        beforeSend: function()
        {
            $(id).find(".rate").prepend(loader2);
        },
        success: function(data, textStatus)
        {
            $(id).find(".rate .loaderWrapper").remove();
            $(id).find(".rate .tipWrapper").fadeIn(400);
            if (data == 0)
            {
                //$(id).find(".rate").html("Zaloguj, aby ocenić dowcip!");
                $('#notLoggedDialog').jqm({ajax:'/page/notLoggedDialog', modal:true, overlay:65});
                $('#notLoggedDialog').jqmShow();
                $(id).find(".rate .tip").html("Zaloguj się, aby ocenić dowcip.");
                $(id).find(".rate .tipWrapper").delay(3000).fadeOut(400);
                return 0;
            }

            if (data == -1)
            {
                $(id).find(".rate .tip").html("Oddałeś/aś już głos na ten dowcip.");
                $(id).find(".rate .tipWrapper").delay(3000).fadeOut(400);
                return -1;
                //return($(id).find(".rate").html("Oddałeś już głos na ten dowcip."));
            }

            if (data == -2)
            {
                $(id).find(".rate .tip").html("Nie możesz oddać głosu na swój dowcip.");
                $(id).find(".rate .tipWrapper").delay(3000).fadeOut(400);
                return -2;
                //return($(id).find(".rate").html("Nie możesz oddać głosu na swój dowcip."));
            }

            eval("var result = " + data)
            $(id).find(".votes_ayes").text(result.votes_ayes);
            $(id).find(".votes").text(result.votes);
            $(id).find(".rate").html('<div class="success">Dziękujemy! Głos został oddany.</div>');
        }
    });
}

function add2favorites(joke_id)
{
    var id = $("#joke-" + joke_id);

    jQuery.ajax
    ({
        type: 'post',
        url: '/favorite/add/joke_id/' + joke_id,
        beforeSend: function()
        {
            $(id).find(".fav").prepend(loader2);
        },
        success: function(data, textStatus)
        {
            $(id).find(".fav .loaderWrapper").remove();
            $(id).find(".fav .tipWrapper").fadeIn(400);
            if (data == 0)
            {
                $('#notLoggedDialog').jqm({ajax:'/page/notLoggedDialog', modal:true, overlay:65});
                $('#notLoggedDialog').jqmShow();
                $(id).find(".fav .tip").html("Zaloguj się, aby dodać do ulubionych.");
                $(id).find(".fav .tipWrapper").delay(3000).fadeOut(400);
                //$(id).find(".fav").replaceWith("Zaloguj, aby dodać do ulubionych.");
                return 0;
            }

            if (data == -1)
            {
                $(id).find(".fav a").removeClass().addClass("fav_error").attr('title','Ten dowcip znajduje się już na Twojej liście ulubionych.').removeAttr("href");
                $(id).find(".fav .tip").html("Ten dowcip znajduje się już na Twojej liście ulubionych.");
                $(id).find(".fav .tipWrapper").delay(3000).fadeOut(400);
                return -1;
                //return($(id).find(".fav").replaceWith(""));
            }

            $(id).find(".fav a").removeClass().addClass("fav_success");
            $(id).find(".fav .tip").html("Dowcip został dodany do Twojej listy ulubionych.");
            $(id).find(".fav .tipWrapper").delay(3000).fadeOut(400);
            //$(id).find(".fav").replaceWith("Dowcip został dodany do Twojej listy ulubionych.");
        }
    });
}

function deleteFromFavorites(joke_id)
{
    var id = $("#joke-" + joke_id);

    jQuery.ajax
    ({
        type: 'post',
        url: '/favorite/delete/joke_id/' + joke_id,
        success: function(data, textStatus)
        {
            //$(id).find(".fav").replaceWith("Dowcip został usunięty z Twojej listy ulubionych.");
            $(id).find('.header .actionsWrapper').html("<b>Wybrany dowcip został usunięty z Twojej listy ulubionych.</b>");
            setTimeout(function() {$(id).fadeOut();}, 2000);
        }
    });
}

$(document).ready(function()
{
    
    $(".fav").delegate('.fav_delete','click mouseover mouseleave', function(e){
        if (e.type == 'mouseover') {
            $(this).parent('.fav').find('.tipWrapper').fadeIn(400).find('.tip').html('Na pewno chcesz usunąć ten dowcip z ulubionych?');
        } else if(e.type != 'click') {
            //$(this).parent('.fav').find('.tipWrapper').fadeOut(400);
        }
    });

    $(".tipWrapper").html('<div class="tip"></div><div class="tipArrow"></div>');

    $(".input_advice").focus(function()
    {
        $(this).parents('.row_container').find('.focus').show();
    });

    $(".input_advice").focusout(function()
    {
        $(this).parents('.row_container').find('.focus').hide();
    });

    $(".visual").cleditor
    ({
          width:        600,
          height:       250,
          controls:     "bold italic underline",
          useCSS:       false, // use CSS to style HTML when possible (not supported in ie)
          docCSSFile:   "", // CSS file used to style the document contained within the editor.
          bodyStyle:    "margin:4px; font:10pt Arial,Verdana; cursor:text"
    })

    cleditor = $(".cleditor-comment").cleditor
    ({
          width:        450,
          height:       220,
          controls:     "bold italic underline",
          useCSS:       false, // use CSS to style HTML when possible (not supported in ie)
          docCSSFile:   "", // CSS file used to style the document contained within the editor.
          bodyStyle:    "margin:4px; font:10pt Arial,Verdana; cursor:text"
    })[0];

    $('#smiley img').click(function()
    {
        var img = $(this);
        cleditor.execCommand('insertimage', img.attr('src'));
        return false;
    });

    $("#facebook_widget").hover(function()
    {
        $("#facebook_widget").stop(true, false).animate({right:"0"}, "medium");
    },function()
    {
        $("#facebook_widget").stop(true, false).animate({right:"-237"}, "medium");
    },500);
});
