
$(document).ready(function()
{

    $('#nyro-message').hide();

    $.fn.nyroModal.settings.processHandler = function(settings) {
      var from = settings.from;
      if (from && from.href && from.href.indexOf('http://www.youtube.com/watch?v=') == 0) {
        var width = 640, height = 385;
        $.nyroModalSettings({
          type: 'swf',
          height: 385,
          width: 640,
          url: from.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
        });
      }
    };

    if ($('#nyro-message').html() != null && $('#nyro-message').html() != '')
    {
        $.nyroModalManual({
            width: 360, // default Width If null, will be calculate automatically
            height: 140, // default Height If null, will be calculate automatically
            bgColor: '#000000',
            title: 'Informacja', // Modal title
            content: $('#nyro-message').html()
        });
    }
    /*****************************  pictures in background *****************************/
    var picts = {
    "pic_duration": 14,
    "list": ["/gfx/bk/1.jpg","/gfx/bk/2.jpg","/gfx/bk/3.jpg","/gfx/bk/4.jpg","/gfx/bk/5.jpg"] };

    for(i = 0; i < picts.list.length; i++) {
        var img = new Image();
        img.src = picts.list[i];
    }
    
    $("#image-bk").css("background-image", "none");

    $.timer(8, function(timer) {
        var image = picts.list[Math.round(new Date().getTime()/(1000.0 * picts.pic_duration + 1) ) % picts.list.length];
        if($.browser.msie && $.browser.version=="6.0") {
            $("#image-bk").css( {
                            backgroundPosition: "50% 0",
                            backgroundImage: "url('"+ image +"')"
                });
        } else {
            $("#image-bk")
                .fadeOut(1000, function() {
                    $(this)
                        .css( {
                            backgroundImage: "url('"+ image +"')"
                            } )
                        .fadeIn(1000);
                });
        }
        timer.reset(picts.pic_duration * 1000);
    });
    $('a.flash').click(function(e) {
        e.preventDefault();
        $.nyroModalManual({
            type: 'swf',
            width: '720',
            height: '480',
            url: $(this).attr("href")
        });
        return false;
    });

    $('a.iframed').click(function(e) {
        e.preventDefault();
        $.nyroModalManual({
            type: 'iframe',
            forceType: 'iframe',
            width: '720',
            height: '480',
            url: $(this).attr("href")
        });
        return false;
    });
    
    $('a#close-strap').click(function(e) {
        e.preventDefault();
        $('#bottom-strap').animate({
            bottom: '-200px'
        },500);
        return false;
    });
    $('#bottom-strap').css({bottom: '-200px'});
    $('#bottom-strap #cover').hide();
    $('#bottom-strap').animate({
        bottom: '0px'
    },800, function() {
        $('#bottom-strap #cover').fadeIn('slow');
    });

    /***************************** player *****************************/
    if ($('#player_container').length > 0)
    {
        var player_html = "<ul id='player_controls'>"
                        +   "<li id='jplayer_play'>play</li>"
                        +   "<li id='jplayer_pause'>pause</li>"
                        +   "<li id='jplayer_volume_min'>min volume</li>"
                        +   "<li id='jplayer_volume_max'>max volume</li>"
                        + "</ul>"
                        + "<div id='play_time'></div>"
                        + "<div id='total_time'></div>"
                        + "<div id='jplayer_progress'>"
                        +   "<div id='jplayer_load_bar'>"
                        +     "<div id='jplayer_play_bar'></div>"
                        +   "</div>"
                        + "</div>"
                        + "<div id='jplayer_volume_bar'>"
                        +   "<div id='jplayer_volume_bar_value'></div>"
                        + "</div>"
                        + "<div id='current_songtitle'></div>";
        $('#player_container').append(player_html);
        
        var player_directory = '/files/player/';
        var playItem = 0;

        var myPlayList = [
            {artist: 'irek-dudek', name:"Spadam stąd", filename:"spadam-stad.mp3"}
        ];

        $("#jquery_jplayer").jPlayer({
            ready: function() {
                displaySong();
                playListInit(false); // Parameter is a boolean for autoplay.
            },
            swfPath: "/js",
            nativeSupport: true,
            errorAlerts:true,
            oggSupport: false
        })
        .jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
            var myPlayedTime = new Date(playedTime);
            var ptMin = (myPlayedTime.getMinutes() < 10) ? "0" + myPlayedTime.getMinutes() : myPlayedTime.getMinutes();
            var ptSec = (myPlayedTime.getSeconds() < 10) ? "0" + myPlayedTime.getSeconds() : myPlayedTime.getSeconds();

            var myTotalTime = new Date(totalTime);
            var ttMin = (myTotalTime.getMinutes() < 10) ? "0" + myTotalTime.getMinutes() : myTotalTime.getMinutes();
            var ttSec = (myTotalTime.getSeconds() < 10) ? "0" + myTotalTime.getSeconds() : myTotalTime.getSeconds();
            $("#play_time").text(ptMin+":"+ptSec);
            $("#total_time").text(ttMin+":"+ttSec);
        })
/*
        .jPlayer("onSoundComplete", function() {
            playListNext();
        });
        $("#ctrl_prev").click( function() {
            playListPrev();
            return false;
        });

        $("#ctrl_next").click( function() {
            playListNext();
            return false;
        });
*/
        function displaySong() {
            $("#current_songtitle").html(myPlayList[playItem].name);
            /*$("#player_picture").fadeOut(180, function() {
                $(this)
                    .css('background-image', "url('" + player_directory + myPlayList[playItem].artist + ".jpg')")
                    .fadeIn(50);
            });*/
        }

        function playListInit(autoplay) {
            if(autoplay) {
                playListChange( playItem );
            } else {
                playListConfig( playItem );
            }
        }

        function playListConfig( index ) {
            playItem = index;
            $("#jquery_jplayer").jPlayer("setFile", player_directory + myPlayList[playItem].filename);
            displaySong();
        }

        function playListChange( index ) {
            playListConfig( index );
            $("#jquery_jplayer").jPlayer("play");
        }

        function playListStar(star, autoplay) {
            var index;
            for (index = 0; index < myPlayList.length; index++) {
                if (myPlayList[index].artist == star)
                {
                    playItem = index;
                    break;
                }
            }
            if(autoplay) {
                playListChange( index );
            } else {
                playListConfig( index );
            }
        }
        /*
        function playListNext() {
            var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
            playListChange( index );
        }

        function playListPrev() {
            var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
            playListChange( index );
        }
        */
    }
});

