aoe.player={};
aoe.player.Player=function(pname,purl,pfiles,pdomObj,autoplay,playall,shuffle,repeat,repeat_track){
    var url=purl;
    var files=pfiles;
    var name=pname;
    var domObj=pdomObj;
    this.state=null;
    this.volume=null;
    var swfObj;
    var onStateChange=null;
    var onPlaytimeUpdate=null;
    var onLoadedUpdate=null;
    var onSongChange=null;
    var onVolumeUpdate=null;
    var currentSong=null;
    this.play=function(){
        swfObj.GotoFrame(aoe.player.Player.ACTION_PLAY);
    }
    this.pause=function(){
        swfObj.GotoFrame(aoe.player.Player.ACTION_PAUSE);
    };
    this.stop=function(){
        swfObj.GotoFrame(aoe.player.Player.ACTION_STOP);
    };
    this.playTrack=function(nr){
        this.stop();
        swfObj.SetVariable("_root.newTrack",nr);
        swfObj.GotoFrame(aoe.player.Player.ACTION_PLAYTRACK);
    };
    this.setVolume=function(volume){
        if(volume>0&&volume<=100){
            swfObj.SetVariable("_root.newVolume",volume);
            swfObj.GotoFrame(aoe.player.Player.ACTION_SETVOLUME);
        }
    };
    this.volumeUp=function(){
      this.setVolume(this.volume+10);
    };
    this.volumeDown=function(){
      this.setVolume(this.volume-10);
    };
    this.togglePlay=function(){
        if(this.isPlaying()){
            this.pause();
        }else {
            this.play();
        }
    };
    this.prev=function(){
        swfObj.GotoFrame(aoe.player.Player.ACTION_PREV);
    };
    this.next=function(){
        swfObj.GotoFrame(aoe.player.Player.ACTION_NEXT);
    };
    this.isPlaying=function(){
        return this.state==aoe.player.Player.PLAYER_STATE_PLAYING;
    };
    this.playPosition=function(pos){
        swfObj.SetVariable("_root.newPosition",pos);
        swfObj.GotoFrame(aoe.player.Player.ACTION_PLAYPOSITION);
    };
    this.setOnStateChange=function(func){
        onStateChange=func;
    };
    this.setState=function(pState){
        this.state=pState;
        if(onStateChange!==null){
            onStateChange(pState);
        }
    };
    this.setOnPlaytimeUpdate=function(func){
        onPlaytimeUpdate=func;
    };
    this.updatePlaytime=function(played,full){
       if(!played)played=0;
       if(!full)full=0;
       if(onPlaytimeUpdate!==null){
           onPlaytimeUpdate(played,full);
       }
    };
    this.setOnLoadedUpdate=function(func){
        onLoadedUpdate=func;
    };
    this.updateLoaded=function(loaded){
       if(!loaded)loaded=0;
       if(onLoadedUpdate!==null){
           onLoadedUpdate(loaded);
       }
    };
    this.getSong=function(){
      return currentSong;
    };
    this.setSong=function(song){
       var prevSong=currentSong;
       currentSong=song;
       if(onSongChange!==null){
           onSongChange(song,prevSong);
       }
    };
    this.setOnSongChange=function(func){
        onSongChange=func;
    };
    this.updateVolume=function(volume){
        this.volume=volume;
        if(onVolumeUpdate!==null){
            onVolumeUpdate(volume);
        }
    };
    this.setOnVulemeUpdate=function(func){

    };
    var filesstr='';
    if(files!==null){

        for(var i=0;i<files.length;i++){
            filesstr+="&files"+i+"="+files[i];
        }
    }
    if(shuffle){
        filesstr+="&shuffle_enabled=true";
    }
    if(typeof playall!='undefined'){
        filesstr+="&playall_enabled="+((playall)?"true":"false");
    }
    var swfEmb;
    var s=document.createElement('script');
    if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
    s.language='JavaScript';
        s.text='function '+name+'_DoFSCommand(cmd,args){\n'
	+'aoe.util.getInstance(aoe.util.getID(\''+name+'\')).fscommand(cmd, args)\n'
        +'}';
   document.getElementsByTagName('head')[0].appendChild(s);
var s2=document.createElement('script');
s2.language='JavaScript';
s2.setAttribute("for",name);
s2.setAttribute("event",'FSCommand(command,args)');
s2.text=name+'_DoFSCommand(command, args)';
document.getElementsByTagName('head')[0].appendChild(s2);

        swfEmb = document.createElement("embed");
        swfEmb.setAttribute("src","/f/player/simpleplayer.swf");
        swfEmb.setAttribute("loop","false");
        swfEmb.setAttribute("menu","false");
        swfEmb.setAttribute("quality","low");
        swfEmb.setAttribute("scale","noborder");
        swfEmb.setAttribute("wmode","transparent");
        swfEmb.setAttribute("width","1");
        swfEmb.setAttribute("height","1");
        swfEmb.setAttribute("swLiveConnect","true");
        swfEmb.setAttribute("id",name);
        swfEmb.setAttribute("name",name);
        swfEmb.setAttribute("allowScriptAccess","sameDomain");
        swfEmb.setAttribute("type","application/x-shockwave-flash");
        swfEmb.setAttribute("pluginspage","http://www.macromedia.com/go/getflashplayer");
        swfEmb.setAttribute("flashvars","aoe_sid=" + aoe.util.getFE('aoe:sid').value + '&f_name='+name+'&aoe_lang=de&'+filesstr);
        swfObj=swfEmb;
    }else {
        s.type="text/javascript";
        var st=document.createTextNode('function '+name+'_DoFSCommand(cmd,args){\n'
	+'aoe.util.getInstance(aoe.util.getID(\''+name+'\')).fscommand(cmd, args)\n'
        +'}');
        s.appendChild(st);
        document.getElementsByTagName('head')[0].appendChild(s);
        swfEmb = document.createElement("embed");
        swfEmb.setAttribute("src","/f/player/simpleplayer.swf");
        swfEmb.setAttribute("loop","false");
        swfEmb.setAttribute("menu","false");
        swfEmb.setAttribute("quality","low");
        swfEmb.setAttribute("scale","noborder");
        swfEmb.setAttribute("wmode","transparent");
        swfEmb.setAttribute("width","1");
        swfEmb.setAttribute("height","1");
        swfEmb.setAttribute("swLiveConnect","true");
        swfEmb.setAttribute("id",name);
        swfEmb.setAttribute("name",name);
        swfEmb.setAttribute("allowScriptAccess","sameDomain");
        swfEmb.setAttribute("type","application/x-shockwave-flash");
        swfEmb.setAttribute("pluginspage","http://www.macromedia.com/go/getflashplayer");
        swfEmb.setAttribute("flashvars","aoe_sid=" + aoe.util.getFE('aoe:sid').value + '&f_name='+name+'&aoe_lang=de'+filesstr);
        swfObj=swfEmb;
    }
     
    aoe.util.setIntance(swfObj,this);
    domObj.appendChild(swfObj);
};
aoe.player.Player.PLAYER_STATE_STOPPED=0;
aoe.player.Player.PLAYER_STATE_LOADING=1;
aoe.player.Player.PLAYER_STATE_PLAYING=2;
aoe.player.Player.PLAYER_STATE_PAUSED=3;

aoe.player.Player.ACTION_PLAY=1;
aoe.player.Player.ACTION_PAUSE=2;
aoe.player.Player.ACTION_STOP=5;
aoe.player.Player.ACTION_PLAYTRACK=3;
aoe.player.Player.ACTION_SETVOLUME=4;
aoe.player.Player.ACTION_NEXT=6;
aoe.player.Player.ACTION_PREV=7;
aoe.player.Player.ACTION_PLAYPOSITION=8;

aoe.player.Song=function(ptitle,partist,palbum,pindex,plength){
    var title=ptitle;
    var artist=partist;
    var album=palbum;
    var index=pindex;
    var length=plength;
    this.getTitle=function(){return title;};
    this.getAlbum=function(){return album;};
    this.getArtist=function(){return artist;};
    this.getIndex=function(){return index;};
    this.getLength=function(){return length;};
};
