/*
 * HTML5 MP3 Player
 * @version 1.0.1
 * @author Patrick Woldberg
 * Copyright (c) 2010 DMXzone
 */
(function($,sm){sm.debugMode=false;sm.useConsole=true;sm.flashVersion=9;sm.flashLoadTimeout=3000;sm.flash9Options.usePeakData=true;var PLUGIN_NAME="dmxHTML5MP3Player",VERSION="1.0.1",CLS_INFO=PLUGIN_NAME+"Info",CLS_PLAYLIST=PLUGIN_NAME+"Playlist",CLS_CURRENT=PLUGIN_NAME+"Current",CLS_PLAYING=PLUGIN_NAME+"Playing",CLS_PLAY_PAUSE=PLUGIN_NAME+"PlayPause",CLS_ACTIVE=PLUGIN_NAME+"Active",CLS_BTN_PLAY=PLUGIN_NAME+"BtnPlay",CLS_BTN_PAUSE=PLUGIN_NAME+"BtnPause",CLS_BTN_STOP=PLUGIN_NAME+"BtnStop",CLS_BTN_PREV=PLUGIN_NAME+"BtnPrev",CLS_BTN_NEXT=PLUGIN_NAME+"BtnNext",CLS_BTN_MUTE=PLUGIN_NAME+"BtnMute",CLS_BTN_SHUFFLE=PLUGIN_NAME+"BtnShuffle",CLS_BTN_REPEAT=PLUGIN_NAME+"BtnRepeat",CLS_BTN_PLAY_PAUSE=PLUGIN_NAME+"BtnPlayPause",CLS_BTN_PLAYLIST=PLUGIN_NAME+"BtnPlaylist",CLS_POS_SLIDER=PLUGIN_NAME+"PositionSlider",CLS_POS_HANDLE=PLUGIN_NAME+"PositionHandle",CLS_POS_BAR=PLUGIN_NAME+"PositionBar",CLS_LOAD_BAR=PLUGIN_NAME+"LoadBar",CLS_PAN_SLIDER=PLUGIN_NAME+"PanSlider",CLS_PAN_HANDLE=PLUGIN_NAME+"PanHandle",CLS_VOL_SLIDER=PLUGIN_NAME+"VolumeSlider",CLS_VOL_SLIDEV=PLUGIN_NAME+"VolumeSliderV",CLS_VOL_HANDLE=PLUGIN_NAME+"VolumeHandle",CLS_VOL_BAR=PLUGIN_NAME+"VolumeBar",CLS_VOL=PLUGIN_NAME+"Volume",CLS_VOL_MUTE=PLUGIN_NAME+"VolumeMute",CLS_VOL_LOW=PLUGIN_NAME+"VolumeLow",CLS_VOL_MED=PLUGIN_NAME+"VolumeMed",CLS_VOL_HIGH=PLUGIN_NAME+"VolumeHigh";CLS_PEAK_LEFT=PLUGIN_NAME+"PeakLeft";CLS_PEAK_LEFT_V=PLUGIN_NAME+"PeakLeftV";CLS_PEAK_LEFT_BAR=PLUGIN_NAME+"PeakLeftBar";CLS_PEAK_RIGHT=PLUGIN_NAME+"PeakRight";CLS_PEAK_RIGHT_V=PLUGIN_NAME+"PeakRightV";CLS_PEAK_RIGHT_BAR=PLUGIN_NAME+"PeakRightBar";$.fn[PLUGIN_NAME]=function(options){var isMethodCall=(typeof options=="string"),args=Array.prototype.slice.call(arguments,1);if(isMethodCall&&options.substring(0,1)=="_"){return this}return this.each(function(){var instance=$.data(this,PLUGIN_NAME);if(!instance&&!isMethodCall){$.data(this,PLUGIN_NAME,new $[PLUGIN_NAME](this,options))._init()}(instance&&isMethodCall&&$.isFunction(instance[options])&&instance[options].apply(instance,args))})};$[PLUGIN_NAME]=function(element,options){this.element=$(element);this.options=$.extend({},options);this.sound=null;this.sounds={}};$[PLUGIN_NAME].defaults={swfPath:"",autoNext:true,autoPlay:false,loop:true,volume:100,pan:0,playlist:[],shuffle:false,onPlay:null,onPause:null,onStop:null,onPrevious:null,onNext:null,onUpdate:null,onChange:null};$[PLUGIN_NAME]._config={version:VERSION};$[PLUGIN_NAME]._diag={isReady:false,isPlaying:false,isMuted:false,index:-1};$[PLUGIN_NAME].count=0;$[PLUGIN_NAME].timeFormat={showHour:false,showMin:true,showSec:true,padHour:false,padMin:true,padSec:true,sepHour:":",sepMin:":",sepSec:""};$[PLUGIN_NAME].convertTime=function(mSec){var f=$[PLUGIN_NAME].timeFormat,d=new Date(mSec),h=d.getUTCHours(),m=d.getUTCMinutes(),s=d.getUTCSeconds(),sh=(f.padHour&&h<10)?"0"+h:h,sm=(f.padMin&&m<10)?"0"+m:m,ss=(f.padSec&&s<10)?"0"+s:s;return((f.showHour)?sh+f.sepHour:"")+((f.showMin)?sm+f.sepMin:"")+((f.showSec)?ss+f.sepSec:"")};$[PLUGIN_NAME].prototype={_init:function(){var c,self=this,e=this.element,exec=function(m){return function(e){e.preventDefault();self[m].apply(self)}};c=this.config=$.extend({},$[PLUGIN_NAME].defaults,this.options,$[PLUGIN_NAME]._config);c.diag=$.extend({},$[PLUGIN_NAME]._diag);this.element.data(PLUGIN_NAME+".config",c);$[PLUGIN_NAME].count++;c.id=PLUGIN_NAME+$[PLUGIN_NAME].count;c.sound={volume:c.volume,pan:c.pan,onfinish:function(){e.find("."+CLS_POS_BAR).width("0%");if(c.autoNext){self.next.apply(self)}},onid3:function(){self._updateInfo.apply(self)},onstop:function(){e.find("."+CLS_POS_BAR).width("0%");e.find("."+CLS_POS_HANDLE).css("left",0);self._updateInfo()},onload:function(s){if(s){e.find("."+CLS_LOAD_BAR).width("100%")}else{(console&&console.log&&console.log("Error loading sound: "+this.url))}self._updateInfo.apply(self)},whileloading:function(){var t=this.bytesTotal,p=(t>0)?100*this.bytesLoaded/t:0;e.find("."+CLS_LOAD_BAR).width(p+"%");self._updateInfo.apply(self)},whileplaying:function(){var d=this.loaded?this.duration:this.durationEstimate,p=(d>0)?100*this.position/d:0;e.find("."+CLS_POS_BAR).width(p+"%");e.find("."+CLS_POS_HANDLE).css("left",p+"%");e.find("."+CLS_PEAK_LEFT+" ."+CLS_PEAK_LEFT_BAR).width((this.peakData.left*100)+"%");e.find("."+CLS_PEAK_RIGHT+" ."+CLS_PEAK_RIGHT_BAR).width((this.peakData.right*100)+"%");e.find("."+CLS_PEAK_LEFT_V+" ."+CLS_PEAK_LEFT_BAR).height((this.peakData.left*100)+"%");e.find("."+CLS_PEAK_RIGHT_V+" ."+CLS_PEAK_RIGHT_BAR).height((this.peakData.right*100)+"%");e.trigger(PLUGIN_NAME+".playing",[this]);self._updateInfo.apply(self)}};e.find("."+CLS_INFO).each(function(){$(this).data(PLUGIN_NAME+".tpl",$(this).html())});e.find("."+CLS_POS_SLIDER).mousedown(function(evt){var slider=this;self.setPosition2(Math.max(0,Math.min(1,(evt.pageX-$(slider).offset().left)/$(slider).width())));$(document).bind("mousemove."+PLUGIN_NAME,function(evt){self.setPosition2(Math.max(0,Math.min(1,(evt.pageX-$(slider).offset().left)/$(slider).width())))}).bind("mouseup."+PLUGIN_NAME,function(evt){$(document).unbind("."+PLUGIN_NAME);self.setPosition2(Math.max(0,Math.min(1,(evt.pageX-$(slider).offset().left)/$(slider).width())))})});e.find("."+CLS_VOL_SLIDER).click(function(evt){var v=Math.round(((evt.pageX-$(this).offset().left)/$(this).width())*100);self.setVolume.apply(self,[v])});e.find("."+CLS_VOL_SLIDEV).click(function(evt){var v=100-Math.round(((evt.pageY-$(this).offset().top)/$(this).height())*100);self.setVolume.apply(self,[v])});e.find("."+CLS_VOL_BAR).width(c.volume+"%");if(self.config.loop){e.find("."+CLS_BTN_REPEAT).addClass(CLS_ACTIVE)}if(self.config.loop){e.find("."+CLS_BTN_REPEAT).addClass(CLS_ACTIVE)}e.find("."+CLS_BTN_PLAY).click(exec("play"));e.find("."+CLS_BTN_STOP).click(exec("stop"));e.find("."+CLS_BTN_PAUSE).click(exec("pause"));e.find("."+CLS_BTN_PREV).click(exec("previous"));e.find("."+CLS_BTN_NEXT).click(exec("next"));e.find("."+CLS_BTN_MUTE).click(exec("toggleMute"));e.find("."+CLS_BTN_SHUFFLE).click(function(evt){evt.preventDefault();self.config.shuffle=!self.config.shuffle;if(self.config.shuffle){e.find("."+CLS_BTN_SHUFFLE).addClass(CLS_ACTIVE)}else{e.find("."+CLS_BTN_SHUFFLE).removeClass(CLS_ACTIVE)}});e.find("."+CLS_BTN_REPEAT).click(function(evt){evt.preventDefault();self.config.loop=!self.config.loop;if(self.config.loop){e.find("."+CLS_BTN_REPEAT).addClass(CLS_ACTIVE)}else{e.find("."+CLS_BTN_REPEAT).removeClass(CLS_ACTIVE)}});e.find("."+CLS_BTN_PLAY_PAUSE).click(exec("togglePause"));e.find("."+CLS_BTN_PLAYLIST).click(function(evt){evt.preventDefault();e.find("."+CLS_PLAYLIST).slideToggle()});this.element.bind(PLUGIN_NAME+".play",function(){self.element.find("."+CLS_PLAYING).removeClass(CLS_PLAYING);self.element.find("."+CLS_BTN_PLAY_PAUSE).addClass(CLS_PLAYING);self.element.find("."+CLS_CURRENT).addClass(CLS_PLAYING)}).bind(PLUGIN_NAME+".pause",function(){self.element.find("."+CLS_PLAYING).removeClass(CLS_PLAYING)}).bind(PLUGIN_NAME+".stop",function(){self.element.find("."+CLS_PLAYING).removeClass(CLS_PLAYING)});var toggle=e.find("."+CLS_PLAY_PAUSE);if(toggle.length>0){toggle.find("."+CLS_BTN_PAUSE).hide();this.element.bind(PLUGIN_NAME+".play",function(){toggle.find("."+CLS_BTN_PLAY).hide();toggle.find("."+CLS_BTN_PAUSE).show()}).bind(PLUGIN_NAME+".pause",function(){toggle.find("."+CLS_BTN_PLAY).show();toggle.find("."+CLS_BTN_PAUSE).hide()}).bind(PLUGIN_NAME+".stop",function(){toggle.find("."+CLS_BTN_PLAY).show();toggle.find("."+CLS_BTN_PAUSE).hide()})}if(this.config.onPlay){e.bind(PLUGIN_NAME+".play",function(){if(typeof self.config.onPlay=="string"){eval(self._parse(self.config.onPlay))}else{self.config.onPlay.call(self)}})}if(this.config.onPause){e.bind(PLUGIN_NAME+".pause",function(){if(typeof self.config.onPause=="string"){eval(self._parse(self.config.onPause))}else{self.config.onPause.call(self)}})}if(this.config.onStop){e.bind(PLUGIN_NAME+".stop",function(){if(typeof self.config.onStop=="string"){eval(self._parse(self.config.onStop))}else{self.config.onStop.call(self)}})}this.element.bind(PLUGIN_NAME+".mute",function(evt,mute){if(mute){$(this).find("."+CLS_BTN_MUTE).addClass(CLS_ACTIVE)}else{$(this).find("."+CLS_BTN_MUTE).removeClass(CLS_ACTIVE)}});this.element.bind(PLUGIN_NAME+".volume",function(evt,volume){e.find("."+CLS_VOL_SLIDER+" ."+CLS_VOL_BAR).width(volume+"%");e.find("."+CLS_VOL_SLIDER+" ."+CLS_VOL_HANDLE).css("left",volume+"%");e.find("."+CLS_VOL_SLIDEV+" ."+CLS_VOL_BAR).height(volume+"%");e.find("."+CLS_VOL_SLIDEV+" ."+CLS_VOL_HANDLE).css("bottom",volume+"%");e.find("."+CLS_VOL).removeClass(CLS_VOL_MUTE+" "+CLS_VOL_LOW+" "+CLS_VOL_MED+" "+CLS_VOL_HIGH);if(volume>80){e.find("."+CLS_VOL).addClass(CLS_VOL_HIGH)}else{if(volume>40){e.find("."+CLS_VOL).addClass(CLS_VOL_MED)}else{if(volume>0){e.find("."+CLS_VOL).addClass(CLS_VOL_LOW)}else{e.find("."+CLS_VOL).addClass(CLS_VOL_MUTE)}}}});this._updateInfo();sm.onready(function(){if(sm.supported()){self._ready.apply(self)}else{alert("SoundManager 2 failed to initialize!")}})},_ready:function(){var url,self=this,c=this.config,d=c.diag,p=c.playlist;if(typeof p=="string"){this.query=p;c.playlist=[];this.element.find(p).each(function(i){c.playlist.push(this.href);$(this).click(function(e){e.preventDefault();if(i!=self.config.diag.index){self._setSound(i,true)}else{self.togglePause()}})})}p=c.playlist;if(p.length>0){for(var i=0;i<p.length;i++){url=p[i];if(!(url in this.sounds)){this.sounds[url]=sm.createSound({id:c.id+"_"+i,url:url,volume:c.volume})}}this._setSound(0,c.autoPlay)}d.isReady=true;this.element.trigger(PLUGIN_NAME+".ready")},mute:function(){this._mute(true)},next:function(){var n,s=this.sound,c=this.config,d=c.diag,p=c.playlist,l=p.length;if(l>1){n=c.shuffle?Math.floor(Math.random()*l):d.index+1;if(n>=l&&c.loop){n=0}if(n<l){(s&&s.stop());this._setSound(n,d.isPlaying);if(c.onNext){if(typeof c.onNext=="string"){eval(this._parse(c.onNext))}else{c.onNext()}}}}},pause:function(){var s=this.sound;(s&&s.pause());this.element.find("."+CLS_BTN_PLAY).removeClass(CLS_ACTIVE);this.config.diag.isPlaying=false;this.element.trigger(PLUGIN_NAME+".pause")},play:function(index){if(index!=null&&index>=0&&index<this.config.playlist.length){this._setSound(index,true)}else{if(!this.config.diag.isPlaying){this._play()}}},previous:function(){var n,s=this.sound,c=this.config,d=c.diag,p=c.playlist,l=p.length;if(l>1){n=c.shuffle?Math.floor(Math.random()*l):d.index-1;if(n<0&&c.loop){n=l-1}if(n>=0){(s&&s.stop());this._setSound(n,d.isPlaying);if(c.onPrevious){if(typeof c.onPrevious=="string"){eval(this._parse(c.onPrevious))}else{c.onPrevious()}}}}},setPan:function(value){var s=this.sound,p=this.config.sound.pan;p=Math.max(-100,Math.min(100,value));(s&&s.setPan(p));this.element.trigger(PLUGIN_NAME+".pan",[p])},setPosition:function(value){var s=this.sound;(s&&s.setPosition(value))},setPosition2:function(value){var s=this.sound,d=s.loaded?s.duration:s.durationEstimate;if(s.playState==0){return}(s&&d>0&&s.setPosition(value*d))},setVolume:function(value){var s=this.sound,v=this.config.sound.volume;v=this.config.sound.volume=Math.max(0,Math.min(100,value));(s&&s.setVolume(v));this._updateInfo();this.element.trigger(PLUGIN_NAME+".volume",[v])},stop:function(){var s=this.sound;(s&&s.stop());this.element.find("."+CLS_BTN_PLAY).removeClass(CLS_ACTIVE);this.config.diag.isPlaying=false;this.element.trigger(PLUGIN_NAME+".stop")},toggleMute:function(){this._mute(!this.config.diag.isMuted)},togglePause:function(){if(this.sound){if(this.config.diag.isPlaying){this.pause()}else{this.play()}}},unmute:function(){this._mute(false)},_mute:function(m){var s=this.sound;if(m){(s&&s.mute())}else{(s&&s.unmute())}this.config.diag.isMuted=m;this._updateInfo();this.element.trigger(PLUGIN_NAME+".mute",[m]);this.element.trigger(PLUGIN_NAME+".volume",[m?0:this.config.sound.volume])},_play:function(){var s=this.sound,c=this.config;if(s){if(s.paused){s.resume()}else{s.play(c.sound)}c.diag.isPlaying=true;this.element.trigger(PLUGIN_NAME+".play");this.element.find("."+CLS_BTN_PLAY).addClass(CLS_ACTIVE)}},_setSound:function(n,p){var s=this.sound,c=this.config,d=c.diag,e=this.element;(s&&s.stop());(s&&!s.loaded&&s.unload());d.index=n;this.sound=this.sounds[c.playlist[n]];s=this.sound;s.setVolume(d.volume);this._mute(d.isMuted);var t=s.bytesTotal||0,l=(t>0)?100*s.bytesLoaded/t:0;e.find("."+CLS_LOAD_BAR).width(l+"%");e.find("."+CLS_POS_BAR).width("0%");this._updateInfo();if(this.query){e.find(this.query).removeClass(CLS_CURRENT).eq(n).addClass(CLS_CURRENT)}if(p){this._play()}e.trigger(PLUGIN_NAME+".change");if(c.onChange){if(typeof c.onChange=="string"){eval(this._parse(c.onChange))}else{c.onChange()}}},_updateInfo:function(){var self=this;this.element.find("."+CLS_INFO).each(function(){$(this).html(self._parse.call(self,$(this).data(PLUGIN_NAME+".tpl")))});if(this.config.onUpdate){if(typeof this.config.onUpdate=="string"){eval(this._parse(this.config.onUpdate))}else{this.config.onUpdate()}}},_parse:function(str){var c=this.config,d=c.diag,s=c.sound,snd=this.sound,ct=$[PLUGIN_NAME].convertTime;return(str.replace(/##index##/g,d.index).replace(/##nr##/g,d.index+1).replace(/##title##/g,this._getTitle()).replace(/##time##/g,ct(snd?snd.position:0)).replace(/##remaining##/g,ct(snd?snd.duration-snd.position:0)).replace(/##duration##/g,ct(snd?snd.duration:0)).replace(/##position##/g,snd?this._perc(snd.position,snd.duration):0).replace(/##volume##/g,d.isMuted?0:s.volume).replace(/##pan##/g,s.pan).replace(/##peakLeft##/g,sm.features.peakData?(snd?snd.peakData.left:0):0).replace(/##peakRight##/g,sm.features.peakData?(snd?snd.peakData.right:0):0))},_perc:function(value,total){return Math.round(100*value/total)||0},_getTitle:function(){var s=this.sound;if(this.query){var e=this.element.find(this.query).eq(this.config.diag.index);if(e.attr("title")!=""){return e.attr("title")}if(e.text()!=""){return e.text()}}return s?s.id3.title||s.id3.TIT2||s.url.substring(s.url.lastIndexOf("/")+1):"No sound loaded..."}}})(jQuery,soundManager);
