
var Qism_song_i=0;function QismPlayer(id,resource){var self=this;this._id=id;this._el=$('#'+id);this._resource=resource;this._playlist=[];this._player=null;this.current=null;this.lastUpdate=null;this.init=function(){if(this._resource!=null){this.fetchRemotePlaylist();}else{$('#'+this._id).find('a').each(function(){Qism_song_i++;var button=$('<a>&nbsp;</a>');self._playlist[Qism_song_i]={'title':$(this).text(),'url':$(this).attr('href'),'domelement':$(button),};$(this).removeClass('play');$(this).removeClass('nojs');$(button).attr('href','#');$(button).attr('rel','song'+Qism_song_i);$(button).addClass('play').addClass('control-button');$(button).bind('mousedown',function(){$(button).css('background-position','1px 1px');});$(button).bind('mouseup',function(){$(button).css('background-position','0 0');});$(button).bind('click',function(){self.play(this.rel);return false;});$(this).before('<p class="download-link"><a href="'+this.href+'" title="Download mp3">&nbsp;</a></p>');prog=$('<p class="progress"></p>');$(this).after(prog);self._playlist[Qism_song_i].domelement_bar=$('<span class="bar">&nbsp;</span>');$(prog).html(self._playlist[Qism_song_i].domelement_bar);self._playlist[Qism_song_i].domelement_pos=$('<p class="pos"></p>');$(this).before(self._playlist[Qism_song_i].domelement_pos);$(this).before(button);});}}
this.fetchRemotePlaylist=function(){Hg.send(this._resource,{onSuccess:function(xhr,c){self._playlist=eval('('+xhr.responseText+')');self.initDOM();}});}
this._loadSong=function(i){this._player=soundManager.createSound({id:'song'+i,url:self._playlist[i].url,whileplaying:self.showPosition,onfinish:self.onfinish,});this.current=i;}
this.initDOM=function(){for(i=0;i<this._playlist.length;i++){this._el.append('<li>'
+' <a href="#" rel="song'+i+'" class="play">'+this._playlist[i].title+'</a> <span id="status_song'+i+'"></span>'
+' </li>');this._el.find('nojs').removeClass('nojs');this._el.find('.play').bind('click',{'player':this},function(e){e.data.player.play(this.rel);});}}
this.play=function(songid){var sid=parseInt(songid.replace('song',''));if(this.current==sid){if(this._player.paused){$(this._playlist[sid].domelement).removeClass('play').removeClass('pause').addClass('playing');this._player.resume();}else{if(this._player.playState==0){$(this._playlist[sid].domelement).removeClass('play').addClass('playing');this._player.play();}else{this.pause(songid);}}}else{if(this._player!=null){this._player.unload(songid);$(self._playlist[self.current].domelement_bar).width('0');$(self._playlist[self.current].domelement_pos).text('');$(this._playlist[self.current].domelement).removeClass('playing').removeClass('pause').addClass('play');}
this._loadSong(sid);this._player.play();$(this._playlist[sid].domelement).removeClass('play').removeClass('pause').addClass('playing');}}
this.pause=function(songid){var sid=parseInt(songid.replace('song',''));$(this._playlist[sid].domelement).removeClass('play').removeClass('playing').addClass('pause');this._player.pause();}
this.onfinish=function(){$(self._playlist[self.current].domelement).removeClass('playing').addClass('play');if(self._playlist.length>1){current=self.current+1;if(current>self._playlist.length-1){current=0;}
self.play('song'+current);}}
this.showPosition=function(){var d=new Date();if(d-self.lastUpdate>500){sid=self.current;$(self._playlist[self.current].domelement_pos).text('['+self.getTime(this.position,true)+'/'+self.getTime(this.duration,true)+']');$(self._playlist[self.current].domelement_bar).width(((this.position/this.duration)*100)+'%');self.lastUpdate=d;}}
this.getTime=function(nMSec,bAsString){var nSec=Math.floor(nMSec/1000);var min=Math.floor(nSec/60);var sec=nSec-(min*60);return(bAsString?(min+':'+(sec<10?'0'+sec:sec)):{'min':min,'sec':sec});}
this.init();}