function track(id, name, file)
{
    this.id = id;
    this.name = name;
    this.file = file;
    this.nextTrack = null;
}

function player()
{
    this.playerWindow = null;
    this.tracks = new Array();
    this.currentTrack = null;
    this.ctrack = null;

    this.addTrack = function(track)
    {
        var i = this.tracks.push(track) - 1;

        this.ctrack = this.tracks[i];

        if(i > 0)
        {
            this.tracks[i - 1].nextTrack = this.ctrack;
        }
        this.ctrack.nextTrack = this.tracks[0];
    };

    this.play = function(index)
    {
        if(index != null)
        {
            this.currentTrack = this.tracks[index];
        }

        if(this.currentTrack == null)
        {
            this.currentTrack = this.tracks[0];
        }

        try
        {
            this.playerWindow.tl.play(index);
        }
        catch(e)
        {
            this.createPlayerWindow();
        }
    };

    this.pause = function()
    {
        try
        {
            this.playerWindow.tl.pause();
        }
        catch(e)
        {
        }
    };

    this.stop = function()
    {
        try
        {
            this.playerWindow.tl.stop();
        }
        catch(e)
        {
        }
    };

    this.next = function()
    {
        if(this.currentTrack.nextTrack === null)
        {
            this.currentTrack = this.tracks[0];
        }
        else
        {
            this.currentTrack = this.currentTrack.nextTrack;
        }
        this.createPlayerWindow();

        try
        {
            this.playerWindow.tl.next();
        }
        catch(e)
        {
        }
    };

    this.prev = function()
    {
        for(var i = 0; i < this.tracks.length; i++)
        {
            if(this.tracks[i].nextTrack && this.tracks[i].nextTrack.id == this.currentTrack.id)
            {
                this.currentTrack = this.tracks[i];
                break;
            }
        }

        this.createPlayerWindow();

        try
        {
            this.playerWindow.tl.prev();
        }
        catch(e)
        {
        }
    };

    this.updateTrack = function(id)
    {
        for(var i = 0; i < this.tracks.length; i++)
        {
            if(this.tracks[i].id == id)
            {
                this.currentTrack = this.tracks[i];
                this.updateTrackName();
                break;
            }
        }
    };

    this.updateTrackName = function()
    {
        if(document.getElementById('player-trackName').innerHTML !== this.currentTrack.name)
        {
            document.getElementById('player-trackName').innerHTML = this.currentTrack.name;
        }
    };

    this.createPlayerWindow = function()
    {
        this.playerWindow = window.open("/player?track="+this.currentTrack.id, "Biggboss Player", "scrollbars=no,toolbar=no,location=no,resizable=0,width=350,height=250");
    };

    this.renderTracks = function()
    {
        var content = "";
        for(var i = 0; i < this.tracks.length && i < 3; i++)
        {
            content += "<div class=\"player-track\"><a href=\"\" id=\"player-track-"+this.tracks[i].id+"\" onclick=\"pl.play("+i+");return false;\" title=\"" + this.tracks[i].name + "\">" + this.tracks[i].name + "</a></div>";

        }
        $("#player-tracks").html(content);
    };
}

function trackList()
{
    this.sm = soundManager;
    this.tracks = new Array();
    this.currentTrack = null;

    this.playerUpdater = null;

    this.addTrack = function(track)
    {
        var i = this.tracks.push(track) - 1;

        this.currentTrack = this.tracks[i];

        if(i > 0)
        {
            this.tracks[i - 1].nextTrack = this.currentTrack;
        }

        this.currentTrack.nextTrack = this.tracks[0];

        this.sm.createSound('track-'+track.id, '/songs/' + track.file);
    };

    this.play = function(index)
    {
        this.sm.stopAll();
        if(index != null)
        {
            if(this.tracks[index].id != this.currentTrack.id)
            {
                this.sm.stop('track-'+this.currentTrack.id);
            }
            this.currentTrack = this.tracks[index];
        }

        this.sm.play('track-'+this.currentTrack.id,{onfinish: function() { tl.next(); } } );
        this.updateTrackName();
    };

    this.pause = function()
    {
        this.sm.pause('track-'+this.currentTrack.id);
    };

    this.stop = function()
    {
        this.sm.stop('track-'+this.currentTrack.id);
    };

    this.next = function()
    {
        this.sm.stop('track-'+this.currentTrack.id);

        if(this.currentTrack.nextTrack === null)
        {
            this.currentTrack = this.tracks[0];
        }
        else
        {
            this.currentTrack = this.currentTrack.nextTrack;
        }

        this.sm.play('track-'+this.currentTrack.id,{onfinish: function() { tl.next(); } } );
        this.updateTrackName();
    };

    this.prev = function()
    {
        this.sm.stop('track-'+this.currentTrack.id);

        for(var i = 0; i < this.tracks.length; i++)
        {
            if(this.tracks[i].nextTrack && this.tracks[i].nextTrack.id == this.currentTrack.id)
            {
                this.currentTrack = this.tracks[i];
                break;
            }
        }
        this.sm.play('track-'+this.currentTrack.id,{onfinish: function() { tl.next(); } } );
        this.updateTrackName();
    };

    this.updateTrackName = function()
    {
        if(this.currentTrack)
        {
            clearInterval(this.playerUpdater);

            this.playerUpdater = setInterval("tl.updatePlayer()", 500);
        }
        else
        {
            clearInterval(this.playerUpdater);
        }

        if (document.getElementById('player-trackName').innerHTML !== this.currentTrack.name)
        {
            document.getElementById('player-trackName').innerHTML = this.currentTrack.name;
        }

        try
        {
            window.opener.pl.playerWindow = window;
            window.opener.pl.updateTrack(this.currentTrack.id);
        }
        catch(e)
        {
        }
    };

    this.updatePlayer = function()
    {
        try
        {
            window.opener.pl.playerWindow = window;
            window.opener.pl.updateTrack(this.currentTrack.id);
        }
        catch(e)
        {
        }
    };

    this.renderTracks = function()
    {
        var content = "";
        for(var i = 0; i < this.tracks.length; i++)
        {
            content += "<div class=\"player-track\"><a href=\"\" id=\"player-track-"+this.tracks[i].id+"\" onclick=\"tl.play("+i+");return false;\" title=\"" + this.tracks[i].name + "\">" + this.tracks[i].name + "</a></div>";

        }

        document.getElementById('player-tracks').innerHTML = content;
    };
}
