//---------------------------------------------------------------------
// 
//  copyright 2008 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();

    // 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');
}



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]);
    }

    // get the .track node
    var track_node = node.ancestors().find(function(a)
    {
        if (a.hasClassName('track')) return true;
        return false;
    });

    // 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.current_track_number == track_number)
    {
        // if the track was already selected, toggle playback

        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);
        }
    }
    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);
    }
};



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;
}


