
function onYouTubePlayerReady(playerId) {
    $('#player-container').flash(function() {
        this.addEventListener("onStateChange", "onPlayerStateChange")
    });
}
function onPlayerStateChange(newState) {
    if (newState == 0) {
        var next = getNextSong(parseInt($("#current_song").attr('rel')));
        if (next != null) {
            playSong(next);
        }
        else {
            playerQuit();
        }
    }
}

function getPrevSong(index) {
    return (index != null && index > 0 ? index - 1 : null);
}

function getNextSong(index) {
    return (index != null && index < playlist.length - 1 ? index + 1 : null);
}

function getSongByCode(code) {
    var index;
    for (index = 0; index < playlist.length; index++) {
        if (code == playlist[index].code) {
            return index;
        }
    }
    return null;
}

function playSong(index) {
    if (index != null) {
        var prev = getPrevSong(index);
        var next = getNextSong(index);

        $('#player').html('').flash({
            swf: 'http://www.youtube.com/v/' + playlist[index].code + '&hl=pl_PL&rel=0&autoplay=1&fs=1&hd=1&showsearch=0&showinfo=0&iv_load_policy=3&enablejsapi=1',
            width: 500,
            height: 405,
            id: "player-container",
            params: {
                allowscriptaccess: "always",
                allowfullscreen: "true"
            }
        }).show();
        $('#current_song')
            .hide()
            .attr('rel', index)
            .html(playlist[index].title + ' (' + playlist[index].length + ') <span class="rating">' + playlist[index].rating + ' (' + playlist[index].rating_number + ')</span> <span class="watches">' + playlist[index].views + '</span>')
            .delay(600).fadeIn();
        if (prev != null) {
            $('a#play_prev').hide().attr({
                'href': '#',
                'rel': playlist[prev].code,
                'title': playlist[prev].title
            }).css("background", "url('/gfx/thumbnails/" + playlist[prev].code + ".jpg') left top no-repeat")
            .html('<span class="label">'+ playlist[prev].title + ' (' + playlist[prev].length + ')</span>').delay(1200).fadeIn('slow');
        } else {
            $('a#play_prev').html('').hide();
        }

        if (next != null) {
            $('a#play_next').hide().attr({
                'href': '#',
                'rel': playlist[next].code,
                'title': playlist[next].title
            }).css("background", "url('/gfx/thumbnails/" + playlist[next].code + ".jpg') left top no-repeat")
            .html('<span class="label">'+ playlist[next].title + ' (' + playlist[next].length + ')</span>').delay(1500).fadeIn('slow');
        } else {
            $('a#play_next').html('').hide();
        }
    }
}

function playerQuit() {
    $('#darken-bk').fadeTo('fast', 0, function() {
        $('#darken-bk').fadeOut();
        $('#player').html('').fadeOut();
        $('a#play_prev').html('').fadeOut();
        $('a#play_next').html('').fadeOut();
        $('#current_song').html('').fadeOut();
    });
}

$(document).ready(function()
{
    var abitshorterstring = ""; $(".notareallyneededtext").text(abitshorterstring);

    $('a.song').click(function () {
        var index = getSongByCode($(this).attr('rel'));
        
        $('html').animate({
            scrollTop: $("#main").offset().top
        }, 200, function() {
            $('#darken-bk').show().fadeTo('slow', 0.8, function() {
                playSong(index);
            });
        });
        return false;
    });

    if (user_message != null)
    {
        $('#user-message').html(user_message).fadeIn('slow').delay(4000).fadeOut('slow');
    }

    $('#darken-bk').click(function() {
        playerQuit();
    });
    
    $('#add-comment').submit(function() { // comment-fix
        if ($('#add-comment #comment-nick').val() == "" || $('#add-comment #comment-contents').val() == "" || $('#add-comment #comment-fix').val() == "") {
            alert("Nick/imię, treść komentarza oraz wpisanie '4' jako zabezpieczenie antyspamowe są wymagane.");
            return false;
        }
    });
});

