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

    $.fn.nyroModal.settings.addImageDivTitle = false;
    $.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/')
        });
      }
    };

    /***************************** bluesletter signup *****************************/
    var clear_field = {
        'color': '#ccc',
        'font-style': 'italic'
    };
    var filled_field = {
        'color': '#000',
        'font-style': 'normal'
    };

    $('#signup #email').css(clear_field);

    $('#signup #email').focus(function() {
        $(this).val('').css(filled_field);
    });

    /***************************** stars pictures in background *****************************/
    var stars = {
    "pic_duration": 20,
    "files_directory": '/gfx/bk/',
    "list": [
        {"filename":"asp.jpg",      "name":"Asylum Street Spankers",           "description":"27. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"eb.jpg",       "name":"Eden Brent",                       "description":"29. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"esbm1.jpg",    "name":"Eric Sardinas and Big Motor",      "description":"29. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"esbm2.jpg",    "name":"Eric Sardinas and Big Motor",      "description":"29. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"id10.jpg",     "name":"Irek Dudek - Dudek Bluesy",        "description":"30. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"janiva.jpg",   "name":"Janiva Magness",                   "description":"27. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"jbu10-2.jpg",  "name":"James Blood Ulmer",                "description":"30. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"ren.jpg",      "name":"Rick Estrin (The Nightcats)",      "description":"30. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"rp.jpg",       "name":"Rod Piazza & The Mighty Flyers",   "description":"29. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"sd2.jpg",      "name":"Shakin` Dudi",                     "description":"29. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"sj.jpg",       "name":"Samuel James",                     "description":"28. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"storm.jpg",    "name":"Storm Warning",                    "description":"27. Rawa Blues Festival",    "author":"foto: Lukasz Rak"},
        {"filename":"vr.jpg",       "name":"Vernon Reid",                      "description":"25. Rawa Blues Festival",    "author":"foto: Lukasz Rak"}

    ] };

    for(i = 0; i < stars.list.length; i++) {
        var img = new Image();
        img.src = stars.files_directory + stars.list[i].filename;
    }

    $("#image-background").css("background-image", "none");

    $.timer(200, function(timer) {
        $("#image-background")
            .fadeOut(1000, function() {
                var image = stars.list[Math.round(new Date().getTime()/(1000.0 * stars.pic_duration + 1) ) % stars.list.length];
                $(this)
                    .css("background-image", "url('"+ stars.files_directory + image.filename +"')")
                    .fadeIn(350, function() {
                        /*$("#star-info")
                            .html("<strong>" + image.name + "</strong><br/>" + image.description + "<br/>" + image.author)
                            .css({
                                width: '0',
                                opacity: 0
                                })
                            .animate({
                                opacity: 1,
                                width: "22%"
                                }, 1500)
                            .fadeTo(4000,1)
                            .animate({
                                opacity: 0
                                });*/
                    });
            });
        timer.reset(stars.pic_duration * 1000);
    });

    /***************************** tickets *****************************/

    if ($('#tickets_sum').length > 0)
    {
        update_tickets_sum = function() {
            $('#tickets_sum').val(
                  (isNaN(parseInt($('#lodge').val())) ? 0 : parseInt($('#lodge').val())) * $('#h_lodge').val()
                //+ (isNaN(parseInt($('#vip1').val()))  ? 0 : parseInt($('#vip1').val()))  * $('#h_vip1').val()
                + (isNaN(parseInt($('#vip2').val()))  ? 0 : parseInt($('#vip2').val()))  * $('#h_vip2').val()
                + (isNaN(parseInt($('#red').val()))   ? 0 : parseInt($('#red').val()))   *  $('#h_red').val()
                + (isNaN(parseInt($('#blue').val()))  ? 0 : parseInt($('#blue').val()))  * $('#h_blue').val()
                + (isNaN(parseInt($('#stand').val())) ? 0 : parseInt($('#stand').val())) * $('#h_stand').val()
            );
        };
        update_tickets_sum();
        $('#tickets input').change(update_tickets_sum);
    }

    if ($('#tickets_payment_sum').length > 0)
    {
        update_tickets_payment_sum = function () {
            if ($('#prepay').attr('checked'))
            {
                $('#tickets_payment_sum').val(parseInt($('#tickets_totals').val()) + parseInt($('#h_prepay').val()));
            }
            else if ($('#postpay').attr('checked'))
            {
                $('#tickets_payment_sum').val(parseInt($('#tickets_totals').val()) + parseInt($('#h_postpay').val()));
            }
            else
            {
                $('#tickets_payment_sum').val("");
            }
        };
        update_tickets_payment_sum();
        $('input[type=radio]').click(update_tickets_payment_sum);
    }

    $('.stars-main a').click(function (e) {
        var star = $(this).attr('rel');
        e.preventDefault();

        $('#stars-box-start').hide();
        $('#stars-box-' + star).fadeIn('fast');
        playListStar(star, true);

        return false;
    });
    $('.stars-box-start-back').click(function (e) {
        e.preventDefault();

        $('.short-stars').hide();
        $('#stars-box-start').fadeIn('fast');
        return false;
    });
    /***************************** player *****************************/
    if ($('#player_container').length > 0)
    {
        var player_html = "<ul id='player_controls'>"
                        +   "<li id='player_play'>play</li>"
                        +   "<li id='player_pause'>pause</li>"
                        +   "<li id='player_volume_min'>min volume</li>"
                        +   "<li id='player_volume_max'>max volume</li>"
                        +   "<li id='ctrl_prev'>previous</li>"
                        +   "<li id='ctrl_next'>next</li>"
                        + "</ul>"
                        + "<div id='play_time'></div>"
                        + "<div id='total_time'></div>"
                        + "<div id='player_progress'>"
                        +   "<div id='player_progress_load_bar'>"
                        +     "<div id='player_progress_play_bar'></div>"
                        +   "</div>"
                        + "</div>"
                        + "<div id='player_volume_bar'>"
                        +   "<div id='player_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: 'james-blood-ulmer-n-vernon-reid',                 name:"Survivors of the Hurricane", filename:"jbu-survivors_of_the_hurricane.mp3"},
            {artist: 'james-blood-ulmer-n-vernon-reid',                 name:"Commit a Crime",             filename:"jbu-commit_a_crime.mp3"},
            {artist: 'rick-estrin-and-the-nightcats-with-charlie-baty', name:"Big Time",                   filename:"nightcats-big_time.mp3"},
            {artist: 'nora-jean-bruso',                                 name:"Telling Me What To Do",      filename:"njb-telling-me-what-to-do.mp3"},
            {artist: 'irek-dudek',                                      name:"Spadam Stad",                filename:"id-spadam-stad.mp3"}
        ];

        $("#jquery_jplayer").jPlayer({
            ready: function() {
                displaySong();
                playListInit(false); // Parameter is a boolean for autoplay.
            },
            swfPath: "/js"
        })
        .jPlayerId("play", "player_play")
        .jPlayerId("pause", "player_pause")
        .jPlayerId("stop", "player_stop")
        .jPlayerId("loadBar", "player_progress_load_bar")
        .jPlayerId("playBar", "player_progress_play_bar")
        .jPlayerId("volumeMin", "player_volume_min")
        .jPlayerId("volumeMax", "player_volume_max")
        .jPlayerId("volumeBar", "player_volume_bar")
        .jPlayerId("volumeBarValue", "player_volume_bar_value")
        .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);
        })
        .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").setFile(player_directory + myPlayList[playItem].filename);
            displaySong();
        }

        function playListChange( index ) {
            playListConfig( index );
            $("#jquery_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 );
        }
    }

    $("a.star-item").tooltip({ 
        effect: 'slide', 
        tipClass: 'star-tooltip-info', 
        position: 'bottom center',
        delay: 3
    });
    
/**************** jam session clubs localisation ****************/
/*
    $("#jam-session-map").css({
        width: "540px",
        height: "460px",
        border: "solid 1px #000000",
        marginTop: "20px",
        marginLeft: "auto",
        marginRight: "auto"
    }).gMap({ markers: [
            { address: "Al. Korfantego 35, Katowice",
                  html: "<strong>Rawa Blues Festival - Spodek</strong><br/>al. Korfantego 35, Katowice",
                  popup: true},
            { address: "Plac Sejmu Slaskiego 2, Katowice",
                  html: "<strong>Jazz Club Hipnoza</strong><br/>pl. Sejmu Slaskiego 2, Katowice"},
            { address: "Warszawska 37, Katowice",
                  html: "<strong>Klub Marchołt</strong><br/>ul. Warszawska 37, Katowice"}
            ],
            zoom: 14 });
*/
});


