function getObj( name )
{
    if( document.getElementById )
    {
        return document.getElementById( name );
    }
    else
    {
        if( document.all )
        {
            return document.all[name];
        }
        else
        {
            if( document.layers ) return document.layers[name];
        }
    }

    return false;
}

var pathArtist = "";
var beginLoad = true;

function Player () {
    this.paused = true;
    this.stopped = true;
    this.sound = new Sound();
    this.position = 0;
    this.duration = 0;
    this.loadTrack( );
}

Player.prototype.setStopped = function(  ) {
    var o1 = getObj( "poslech" );
    var o2 = getObj( "phraje" );

    if (( o1 != null ) && ( o2 != null )) {
        o1.style.display = this.stopped ? '' : "none";
        o2.style.display = this.stopped ? "none" : '';
    }
}

Player.prototype.onTimerEvent = function() {
    if( this.stopped ) return;
    if( this.paused ) return;

    var position = this.sound.getPosition();
    if( !position ) position = 0;
    this.position = position;

    if( this.duration == 0 ) this.duration = this.sound.getDuration();
    if( !this.duration ) return ;

    if( this.duration <= this.position )
        this.onSoundComplete();

}


Player.prototype.onPlayButtonClick = function() {
    if( ord < 0 ) return ;
    if( songs[ord] == null ) return;

    if( this.paused ) {
        this.paused = false;
        if( this.stopped )
            this.sound.loadSound( dir + songs[ord]['file'], true);
        this.sound.start(this.position/1000, 1);
        this.stopped = false;
    } else {
        this.position = this.sound.getPosition();
        this.sound.stop();
        this.paused = true;
    }

    this.setStopped( )
}

Player.prototype.onForwardButtonClick = function() {
    if( songs[ord + 1] == null ) {
        if(( !this.paused ) && ( !this.stopped )) {
            ord = 0;
            this.loadTrack( );
            this.onStopButtonClick();
        }
        return;
    }
    this.position = 0;
    this.duration = 0;
    this.sound.start(this.duration/1000, 1);
    this.sound.stop();

    ord ++;
    this.loadTrack( );
    this.stopped = true;
    if(!this.paused) {
        this.paused = true;
        this.onPlayButtonClick();
    }
}

Player.prototype.onBackButtonClick = function() {
    if( songs[ord - 1] == null ) return;
    ord -= 2;
    this.onForwardButtonClick( );
}

Player.prototype.onStopButtonClick = function() {
    this.paused = true;
    this.stopped = true;
    this.position = 0;
    this.duration = 0;
    this.sound.start(this.duration/1000, 1);
    this.sound.stop();
    this.setStopped( )
}

Player.prototype.loadTrack = function( ) {
    if( songs[ord] == null ) return false;

    if( typeof document.getElementById( 'playerName') != "undefined" && document.getElementById( 'playerName') != null ) document.getElementById( 'playerName').innerHTML = songs[ord]["name"];
    if( typeof document.getElementById( 'playerInterpret') != "undefined" && document.getElementById( 'playerInterpret') != null ) document.getElementById( 'playerInterpret').innerHTML = songs[ord]["interpret"];
    if( typeof document.getElementById( 'playerInfo') != "undefined" && document.getElementById( 'playerInfo') != null ) {
        path = songs[ord]["path"];
        if( path != "" ) document.getElementById( 'playerInfo' ).className = 'block';
        else document.getElementById( 'playerInfo' ).className = 'no';
        pathArtist = path;
    }

    return true;
}

Player.prototype.onSoundComplete = function() {
    if( !this.paused ) this.onForwardButtonClick();
}
