define(["area"],function(e){var t=Class.extend({init:function(e){var t=this;this.enabled=!0,this.extension=Detect.canPlayMP3()?"mp3":"ogg",this.sounds={},this.game=e,this.currentMusic=null,this.areas=[],this.musicNames=["village","beach","forest","cave","desert","lavaland","boss"],this.soundNames=["loot","hit1","hit2","hurt","heal","chat","revive","death","firefox","achievement","kill1","kill2","noloot","teleport","chest","npc","npc-end"];var n=function(){var e=_.size(t.soundNames);log.info("Loading sound files..."),_.each(t.soundNames,function(n){t.loadSound(n,function(){e-=1,e===0&&(Detect.isSafari()||r())})})},r=function(){t.game.renderer.mobile||(log.info("Loading music files..."),t.loadMusic(t.musicNames.shift(),function(){_.each(t.musicNames,function(e){t.loadMusic(e)})}))};!Detect.isSafari()||!Detect.isWindows()?n():this.enabled=!1},toggle:function(){this.enabled?(this.enabled=!1,this.currentMusic&&this.resetMusic(this.currentMusic)):(this.enabled=!0,this.currentMusic&&(this.currentMusic=null),this.updateMusic())},load:function(e,t,n,r){var i=e+t+"."+this.extension,s=document.createElement("audio"),o=this;s.addEventListener("canplaythrough",function(e){this.removeEventListener("canplaythrough",arguments.callee,!1),log.debug(i+" is ready to play."),n&&n()},!1),s.addEventListener("error",function(e){log.error("Error: "+i+" could not be loaded."),o.sounds[t]=null},!1),s.preload="auto",s.autobuffer=!0,s.src=i,s.load(),this.sounds[t]=[s],_.times(r-1,function(){o.sounds[t].push(s.cloneNode(!0))})},loadSound:function(e,t){this.load("audio/sounds/",e,t,4)},loadMusic:function(e,t){this.load("audio/music/",e,t,1);var n=this.sounds[e][0];n.loop=!0,n.addEventListener("ended",function(){n.play()},!1)},getSound:function(e){if(!this.sounds[e])return null;var t=_.detect(this.sounds[e],function(e){return e.ended||e.paused});return t&&t.ended?t.currentTime=0:t=this.sounds[e][0],t},playSound:function(e){var t=this.enabled&&this.getSound(e);t&&t.play()},addArea:function(t,n,r,i,s){var o=new e(t,n,r,i);o.musicName=s,this.areas.push(o)},getSurroundingMusic:function(e){var t=null,n=_.detect(this.areas,function(t){return t.contains(e)});return n&&(t={sound:this.getSound(n.musicName),name:n.musicName}),t},updateMusic:function(){if(this.enabled){var e=this.getSurroundingMusic(this.game.player);e?this.isCurrentMusic(e)||(this.currentMusic&&this.fadeOutCurrentMusic(),this.playMusic(e)):this.fadeOutCurrentMusic()}},isCurrentMusic:function(e){return this.currentMusic&&e.name===this.currentMusic.name},playMusic:function(e){this.enabled&&e&&e.sound&&(e.sound.fadingOut?this.fadeInMusic(e):(e.sound.volume=1,e.sound.play()),this.currentMusic=e)},resetMusic:function(e){e&&e.sound&&e.sound.readyState>0&&(e.sound.pause(),e.sound.currentTime=0)},fadeOutMusic:function(e,t){var n=this;e&&!e.sound.fadingOut&&(this.clearFadeIn(e),e.sound.fadingOut=setInterval(function(){var r=.02;volume=e.sound.volume-r,n.enabled&&volume>=r?e.sound.volume=volume:(e.sound.volume=0,n.clearFadeOut(e),t(e))},50))},fadeInMusic:function(e){var t=this;e&&!e.sound.fadingIn&&(this.clearFadeOut(e),e.sound.fadingIn=setInterval(function(){var n=.01;volume=e.sound.volume+n,t.enabled&&volume<1-n?e.sound.volume=volume:(e.sound.volume=1,t.clearFadeIn(e))},30))},clearFadeOut:function(e){e.sound.fadingOut&&(clearInterval(e.sound.fadingOut),e.sound.fadingOut=null)},clearFadeIn:function(e){e.sound.fadingIn&&(clearInterval(e.sound.fadingIn),e.sound.fadingIn=null)},fadeOutCurrentMusic:function(){var e=this;this.currentMusic&&(this.fadeOutMusic(this.currentMusic,function(t){e.resetMusic(t)}),this.currentMusic=null)}});return t});