var fadeInTime = 250;
var fadeOutTime = 200;
var slideFadeOutTime = 1500;
var slideChangeTime = 4000;
function slideshow() {
    var current = $('#gallery-container img.current');
    current.addClass('previous');
    if (current.length == 0) current = $('#gallery-container img:last');
    var next;
    if (current.next().is('div')) next = $('#gallery-container img:first');
    else next = current.next();
    next.css({opacity: 0}).addClass('current').animate({opacity: 1}, slideFadeOutTime, function() {
        current.removeClass('previous current');
    });
}
$(function() {
    $('#navigation li a').addClass('fade');
    $('#navigation li a.fade').css({opacity: 0});
    $('#navigation li a').hover(
        function() {
            $(this).stop().animate({opacity: 1}, fadeInTime);
        },
        function() {
            $(this).stop().animate({opacity: 0}, fadeOutTime);
        });
    setInterval( "slideshow()", slideChangeTime);
    $('audio').css({display: 'none'});
    $('audio').each(function (index, elem) {
        var video = $('audio:eq(' + index + ')');
        video.wrap('<div id="mellieha-player-' + index + '" class="mellieha-player" />');
        var player = $('#mellieha-player-' + index);
        var artistAndTitle = $('audio').attr('title');
        var separator = artistAndTitle.search('//');
        var artist = artistAndTitle.slice(0,separator);
        var title = artistAndTitle.slice(separator + 2);
        player.append('<div class="mellieha-controls ui-corner-all" />');
        var controls = $('#mellieha-player-' + index + ' .mellieha-controls');
        controls.append('<div class="mellieha-control-play ui-state-default ui-corner-all ui-widget-content" style="padding: 4px; float: left; margin: 2px 5px 0 0;" ><span class="ui-icon ui-icon-play"></span></div>');
        controls.append('<p id="player-artist">' + artist + '</p>');
        controls.append('<p id="player-title">' + title + '</p>');
        controls.append('<div class="mellieha-control-timeline" style="clear: both; float: left; height: 5px; margin: 7px 5px 0 5px;"></div>');
        controls.append('<div class="mellieha-control-volume" style="float: left; width: 25px; margin: 7px 0 0 0;""></div>');
        // Play button
        $('#mellieha-player-' + index + ' .mellieha-control-play').click(function() {
            if (video.attr('paused') || video.attr('ended')) {
                $('#mellieha-player-' + index + ' audio').trigger('play');
                $('#mellieha-player-' + index + ' .mellieha-control-play span').removeClass('ui-icon-play');
                $('#mellieha-player-' + index + ' .mellieha-control-play span').addClass('ui-icon-pause');
            } else {
                $('#mellieha-player-' + index + ' audio').trigger('pause');
                $('#mellieha-player-' + index + ' .mellieha-control-play span').removeClass('ui-icon-pause');
                $('#mellieha-player-' + index + ' .mellieha-control-play span').addClass('ui-icon-play');
            }
        });
        video.bind('pause', function() {
            $('#mellieha-player-' + index + ' .mellieha-control-play span').removeClass('ui-icon-pause');
            $('#mellieha-player-' + index + ' .mellieha-control-play span').addClass('ui-icon-play');
        });
        video.bind('ended', function() {
            $('#mellieha-player-' + index + ' .mellieha-control-play span').removeClass('ui-icon-pause');
            $('#mellieha-player-' + index + ' .mellieha-control-play span').addClass('ui-icon-play');
        });
        video.bind('play', function() {
            $('#mellieha-player-' + index + ' .mellieha-control-play span').removeClass('ui-icon-play');
            $('#mellieha-player-' + index + ' .mellieha-control-play span').addClass('ui-icon-pause');
        });
        // Volume control
        $('#mellieha-player-' + index + ' .mellieha-control-volume').slider({max: 1.0, min: 0, step: 0.01});
        $('#mellieha-player-' + index + ' .mellieha-control-volume').bind('slide', function(event, ui) {
            $('#mellieha-player-' + index + ' audio').attr('volume', $('#mellieha-player-' + index + ' .mellieha-control-volume').slider('value'));
        });
        // Timeline control
        var timelinewidth = 100;
        $('#mellieha-player-' + index + ' .mellieha-control-timeline').css('width', timelinewidth);
        $('#mellieha-player-' + index + ' .mellieha-control-timeline').slider({min: 0, step: 0.01, range: "min"});
        video.bind("canplay", function() {
            var duration = video.attr('duration');
            var currentTime = video.attr('currentTime');
            var currentTimeMins = Math.floor(currentTime / 60);
            var currentTimeSecs = Math.round(currentTime % 60);
            if (currentTimeSecs.toString().length == 1) currentTimeSecs = '0' + currentTimeSecs;
            var durationMins = Math.floor(duration / 60);
            var durationSecs = Math.round(duration % 60);
            if (durationSecs.toString().length == 1) durationSecs = '0' + durationSecs;
            $('#mellieha-player-' + index + ' .mellieha-control-volume').slider('option', 'value', 1.0);
            $('#mellieha-player-' + index + ' .mellieha-control-timeline').slider('option', 'max', duration);
        });
        $('#mellieha-player-' + index + ' .mellieha-control-timeline').bind('slide', function(event, ui) {
            video.trigger('pause');
            video.attr('currentTime', $('#mellieha-player-' + index + ' .mellieha-control-timeline').slider('value'));
        });
        video.bind("timeupdate", function() {
            var duration = video.attr('duration');
            var currentTime = video.attr('currentTime');
            var currentTimeMins = Math.floor(currentTime / 60);
            var currentTimeSecs = Math.round(currentTime % 60);
            if (currentTimeSecs.toString().length == 1) currentTimeSecs = '0' + currentTimeSecs;
            var durationMins = Math.floor(duration / 60);
            var durationSecs = Math.round(duration % 60);
            if (durationSecs.toString().length == 1) durationSecs = '0' + durationSecs;
            $('#mellieha-player-' + index + ' .mellieha-control-timeline').slider('option', 'value', currentTime);
        });
        video.click(function() {
            var fullsize = $('#mellieha-player-' + index + ' .mellieha-option-fullsize').text();
            var newwidth = $('#mellieha-player-' + index + ' .mellieha-control-timeline').width() + (fullsize - $('#mellieha-player-' + index).width());
            $('#mellieha-background').fadeTo(0,0);
            $('#mellieha-player-' + index).animate({width: fullsize}, 1000);
            $('#mellieha-player-' + index + ' video').animate({width: fullsize}, 1000);
            $('#mellieha-player-' + index + ' .mellieha-control-timeline').animate({width: newwidth},1000);
            $('#mellieha-background').css('visibility', 'visible');
            $('#mellieha-background').css('display', 'block');
            $('#mellieha-background').fadeTo(1000,0.8);
        });
    });
});
