document.write('<script type="text/javascript" src="/js/jwysiwyg/jquery.ui.js"></script>');
document.write('<script type="text/javascript" src="/js/jwysiwyg/jquery.wysiwyg.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.validation.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.jqmodal.js"></script>');
document.write('<script type="text/javascript" src="/js/sexybookmarks.js"></script>');

function adult_check (check)
{
    if (check != 1)
    {
        $('#intro').jqm({ajax:'/templates/intro.tpl', modal:true, overlay:80});
        $('#intro').jqmShow();
    }
}

function vote (id, rate)
{
    $.ajax
    ({
       type     : 'post',
       url      : '/demot',
       data     : 'act=demot_vote&id=' + id + '&rate=' + rate,
       beforeSend  : function ()
       {
           $('#demot-' + id).find('.rate_it').html('<img src="/gfx/loading.gif" />');
       },
       success  : function (data)
       {
           if (data == 0)
           {
               $('#demot-' + id).find('.rate_it').html('Zaloguj się aby oddać głos!');
               $('#alert').jqm({ajax:'/templates/alerts/votes.tpl', modal:true, overlay:80});
               $('#alert').jqmShow();
           }
           else if (data == -1)
           {
               $('#demot-' + id).find('.rate_it').html('Oddałeś już wcześniej głos.');
           }
           else
           {
               $('#demot-' + id).find('.rate_it').html('Głos został oddany');

               var vote = data.split('|');
               
               $('#demot-' + id).find('.vote_ayes').text(vote[0]);
               $('#demot-' + id).find('.vote_total').text(vote[1]);
           }
       }
    });
}

function addtofavorites (id)
{
    $.ajax
    ({
       type     : 'post',
       url      : '/demot',
       data     : 'act=demot_favorites&id=' + id,
       beforeSend  : function ()
       {
           $('#demot-' + id).find('.favorites').html('<img src="/gfx/loading.gif" />');
       },
       success  : function (data)
       {
           if (data == 0)
           {
                $('#demot-' + id).find('.favorites').html('Zaloguj aby dodać do ulubionych!');
                $('#alert').jqm({ajax:'/templates/alerts/favorites.tpl', modal:true, overlay:80});
                $('#alert').jqmShow();
           }
           else if (data == -1)
           {
               $('#demot-' + id).find('.favorites').html('Pornodemotywator jest już w ulubionych.');
           }
           else
           {
               $('#demot-' + id).find('.favorites').html('Zapisano w ulubionych.');
           }
       }
    });
}

function deletefavorites (id)
{
    $.ajax
    ({
       type     : 'post',
       url      : '/demot',
       data     : 'act=demot_deletefavorites&id=' + id,
       beforeSend  : function ()
       {
           $('#demot-' + id).find('.favorites').html('<img src="/gfx/loading.gif" />');
       },
       success  : function (data)
       {
           $('#demot-' + id).find('.favorites').html('Usunięto z ulubionych.');
       }
    });
}

$(document).ready(function()
{
    $('#comments_textarea').wysiwyg();

    $('#comments_submit').click(function()
    {
        var length = $('#comments_textarea').val().replace(/\<br\>|&nbsp;/gi, '').length;

        if (length < 3)
        {
            alert('Treść komentarza jest za krótka. Komentarz musi mieć minimum 3 znaki.');
            return false;
        }
        else if (length > 1000)
        {
            alert('Treść komentarza jest za długa. Komentarz może mieć maksymalnie 1000 znaków. Twój komentarz ma ' + length + ' znaków. Skróć komentarz i dodaj go raz jeszcze.');
            return false;
        }

        $(this).attr('disabled', 'disabled');

        return true;
    });

    $('#comments_smiley img').click(function()
    {
        var src = $(this).attr('src');

        $('#comments_textarea').wysiwyg('insertImage', src);
        return false;
    });

    $('.comment_delete').click(function()
    {
        if (confirm("Czy napewno chcesz usunąć komentarz?"))
        {

            var comment = $(this).parents('.comment:first');
            var id = $(comment).attr('id').match(/[0-9]+/);

            $.ajax
            ({
               type     : 'post',
               url      : '/demot',
               data     : 'act=comment_delete&id=' + id,
               success  : function (data)
               {
                   $(comment).html(data);
               }
            });
        }
    });

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

});