package com.terry.engine {
	import flash.display.*;          
	import flash.media.*; 
  import flash.events.*;
	import flash.utils.Dictionary;
	
	public class music{	
		//For Music stuff
		public static function init():void {
			currentsong = "nothing"; musicfade = 0;//no music, no amb
			initefchannels(); currentefchan = 0;
			
			numplays = 0;
			numeffects = 0;
			numsongs = 0;
		}
		
		public static function play(t:String):void {
			if (currentsong !=t) {
				if (currentsong != "nothing") {
					//Stop the old song first
					musicchannel.stop();
					musicchannel.removeEventListener(Event.SOUND_COMPLETE, loopmusic);
				}
				if (t != "nothing") {
			    currentsong = t;
					musicchannel = musicchan[songindex[t]].play(0);
					
					musicchannel.addEventListener(Event.SOUND_COMPLETE, loopmusic);
				}else {	
			    currentsong = "nothing";
				}
			}
    }   
		
		public static function loopmusic(e:Event):void { 
			musicchannel.removeEventListener(Event.SOUND_COMPLETE, loopmusic);
			if (currentsong != "nothing") {

				musicchannel = musicchan[songindex[currentsong]].play();
				musicchannel.addEventListener(Event.SOUND_COMPLETE, loopmusic);
			}
		}
		
		public static function stopmusic(e:Event):void { 
			musicchannel.removeEventListener(Event.SOUND_COMPLETE, stopmusic);
			musicchannel.stop();
			currentsong = "nothing";
		}
		
		public static function stop():void { 
			musicchannel.removeEventListener(Event.SOUND_COMPLETE, stopmusic);
			musicchannel.stop();
			currentsong = "nothing";
		}
		
		public static function fadeout():void { 
			if (musicfade == 0) {
			  musicfade = 31;
			}
		}
		
		public static function processmusicfade():void {
			musicfade--;
			if (musicfade > 0) {
				musicchannel.soundTransform = new SoundTransform(musicfade / 30);
			}else {
			  musicchannel.stop();
			  currentsong = "nothing";
			}
		}
		
		public static function processmusicfadein():void {
			musicfadein--;
			if (musicfadein > 0) {
				musicchannel.soundTransform = new SoundTransform((60-musicfadein) / 60 );
			}else {
				musicchannel.soundTransform = new SoundTransform(1.0);
			}
		}
		
		
   	public static function processmusic():void {
			if (musicfade > 0) processmusicfade();
			if (musicfadein > 0) processmusicfadein();
		}
		
		public static var musicchan:Array = new Array();	
		public static var musicchannel:SoundChannel;
		public static var currentsong:String;
		public static var musicfade:int;
		public static var musicfadein:int;
		
		public static function processmute():void {
			if (key.justPressed("M") && game.mutebutton<=0) {
				game.mutebutton = 2; if (game.muted) { game.muted = false; }else { game.muted = true;}
			}
			if (game.mutebutton > 0 && !key.pressed("M")) game.mutebutton--;
			
			if (game.muted) {
				if (game.globalsound == 1) {
					game.globalsound = 0; SoundMixer.soundTransform = new SoundTransform(0);
				}
			}
			
			if (!game.muted && game.globalsound == 0) {
				game.globalsound = 1; SoundMixer.soundTransform = new SoundTransform(1);
			}
		}
		
		//Play a sound effect! There are 16 channels, which iterate
		public static function initefchannels():void {
			for (var i:int = 0; i < 16; i++) efchannel.push(new SoundChannel);
		}
		
		public static function playef(t:String, offset:int = 0):void {
			var temptransform:SoundTransform = new SoundTransform(volumelevels[effectindex[t]]);
			efchannel[currentefchan] = efchan[effectindex[t]].play(offset);
			efchannel[currentefchan].soundTransform = temptransform;
			currentefchan++;
			if (currentefchan > 15) currentefchan -= 16;
		}
		
		public static function addeffect(t:String, vol:Number = 1.0):void {
			effectindex[t] = numeffects;
			volumelevels.push(vol);
			numeffects++;
		}
		
		
		
		public static function addsong(t:String, vol:Number = 1.0):void {
			songindex[t] = numsongs;
			songvolumelevels.push(vol);
			numsongs++;
		}
		
		public static var effectindex:Dictionary = new Dictionary;
		public static var volumelevels:Vector.<Number> = new Vector.<Number>;
		public static var numeffects:int;
		
		public static var songindex:Dictionary = new Dictionary;
		public static var songvolumelevels:Vector.<Number> = new Vector.<Number>;
		public static var numsongs:int;
		
		public static var currentefchan:int;
		public static var efchannel:Array = new Array();
		public static var efchan:Array = new Array();
		public static var numplays:int;
	}
}
