//---------------------------------------------------------------------
// 
//  copyright 2008, 2009 adam marks.  marks@divinia.com
// 
//  This script is free software: you can redistribute it and/or
//  modify it under the terms of the GNU General Public License as
//  published by the Free Software Foundation, either version 3 of the
//  License, or (at your option) any later version.
//
//  This script is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  General Public License for more details.
//
//---------------------------------------------------------------------

var audio_page;

// global hook that the jwplayer calls when it has loaded
function playerReady(obj) 
{
    var id = obj['id'];
    var version = obj['version'];
    var client = obj['client'];
    //alert('the videoplayer '+id+' has been instantiated');
    var player = document.getElementById(id);

    audio_page = new AudioPage(player);
}


function time_monitor(obj)
{
    audio_page.time_monitor(obj.position, obj.duration);
}


function state_monitor(obj)
{
    audio_page.state_monitor(obj.oldstate, obj.newstate);
}


function AudioPage(player)
{
    this.player = player;
    this.current_track_number = -1;
    this.playing = false;

    this.status_playing = $$('#tracks .status_playing').first();

    this.player_position = $$('#tracks .position').first();
    this.player_duration = $$('#tracks .duration').first();

    // determine number of tracks
    this.num_tracks = $$('#tracks .track').size();

    // track hooks
    var self = this;

    $$('#tracks .track .title a').each(function(node)
    {
        var link_node = node;
        node.onclick = self.click_track.bind(self, link_node);
    });


    // player hooks
    this.player.addModelListener('TIME', 'time_monitor');
    this.player.addModelListener('STATE', 'state_monitor');

    // window spacebar hook
    window.onkeydown = self.keydown.bind(self);
}



AudioPage.prototype.click_track = function(node)
{
    // scrape track number from the node id.
    var a = node.id.match(/track_(\d+)/);
    var track_number = 0;
    if (a)
    {
        track_number = parseInt(a[1]);
    }

    if (this.current_track_number == track_number)
    {
        // if the track was already selected, toggle playback.
        this.toggle_playback();
    }
    else
    {
        //  if the track was not already selected, select it and play.
        this.select_track(track_number);
    }

    // dont want to see the anchor in the url...although someday we
    // might.
    return false;
};


AudioPage.prototype.select_track = function(track_number)
{
    var track_node = $('track_' + track_number);

    if (!track_node) return;

    // get the status slot node
    var status_slot = track_node.select('.status_slot').first();

    // get the position slot node
    var position_slot = track_node.select('.position_slot').first();

    // get the duration slot node
    var duration_slot = track_node.select('.duration_slot').first();

    this.current_track_number = track_number;

    this.player.sendEvent('ITEM', track_number); // this starts playback too
    this.playing = true;
    status_slot.update(this.status_playing);
    this.player_position.update();
    this.player_duration.update();
    position_slot.update(this.player_position);
    duration_slot.update(this.player_duration);
};


AudioPage.prototype.time_monitor = function(position, duration)
{
    if (this.playing)
    {
        this.player_position.update(format_time(position));
        this.player_duration.update(format_time(duration));
    }
};


AudioPage.prototype.state_monitor = function(oldstate, newstate)
{
    //alert("oldstate: " + oldstate + " newstate: " + newstate);

    if (oldstate == 'PLAYING' && newstate == 'COMPLETED')
    {
        this.select_track((this.current_track_number | 0)  + 1);
    }
};



AudioPage.prototype.keydown = function(e)
{
    var code;
    e = e || window.event;
    if (e.keyCode) 
    {
        code = e.keyCode;
    } 
    else if (e.which) 
    {
        code = e.which;
    }

    // space
    if (code == 32) 
    {
        e.cancelBubble = true;
        e.returnValue = false;

        if (e.stopPropagation) 
        {
            e.stopPropagation();
            e.preventDefault();
        }

        this.toggle_playback();

        return false;
    }
    // left
    else if (code == 37)
    {
        this.select_previous_track();
    }
    // right
    else if (code == 39)
    {
        this.select_next_track();
    }
};



AudioPage.prototype.toggle_playback = function()
{
    // if havent started playing yet, start first track.
    // else toggle.
    if (this.current_track_number == -1)
    {
        return this.select_track(0);
    }


    // get the .track node
    var track_node = $('track_' + this.current_track_number);

    // get the status slot node
    var status_slot = track_node.select('.status_slot').first();

    // get the position slot node
    var position_slot = track_node.select('.position_slot').first();

    // get the duration slot node
    var duration_slot = track_node.select('.duration_slot').first();

    if (this.playing)
    {
        // pause
        this.player.sendEvent('PLAY', false);
        this.playing = false;
        status_slot.update();
    }
    else
    {
        // play
        this.player.sendEvent('PLAY', true);
        this.playing = true;
        status_slot.update(this.status_playing);
    }
};



AudioPage.prototype.select_previous_track = function()
{
    if (this.current_track_number <= 0)
    {
        this.select_track(0);
    }
    else
    {
        this.select_track(this.current_track_number - 1);
    }
};



AudioPage.prototype.select_next_track = function()
{
    if (this.current_track_number == -1)
    {
        this.select_track(0);
    }
    else if (this.current_track_number == this.num_tracks - 1)
    {
        this.select_track(this.num_tracks - 1);
    }
    else
    {
        this.select_track(this.current_track_number + 1);
    }
};





function format_time(time)
{
    var hours = Math.floor(time / 3600);

    var minutes = Math.floor((time - (hours * 3600)) / 60);

    var seconds = Math.floor(time - (hours * 3600) - (minutes * 60));

    var out = '';

    if (hours > 0) out += hours + ':';

    out += minutes + ':';
    
    if (seconds < 10) out += '0';
    
    out += seconds;

    return out;
}


