﻿package local.display
{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.net.*;
	import flash.geom.*;
	
	import local.display.SimpleMP3Track;
	
	public class PillMP3Player extends MovieClip
	{
		public static const XML_PATH: String = "songlist.xml";
		
		public var xml: XML;
		
		private var track: SimpleMP3Track = new SimpleMP3Track();
		
		public function PillMP3Player(loadXML: Boolean = true) : void
		{
			track.equalizer = equalizer;
			volume.track    = track;
			
			volume.Initialize();
			
			if (loadXML)
				startXML();
			
			addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage);
		}
		
		// plays the mp3 from whatever MP3Song button was clicked
		public function playMP3(path: String) : void
		{
			track.playMP3(path);
		}
		
		private function startXML() : void
		{
			// start the xml loading, when it's done xmlComplete will be called
			var xmlLdr: URLLoader = new URLLoader();
			xmlLdr.addEventListener(Event.COMPLETE, xmlComplete);
			xmlLdr.load(new URLRequest(XML_PATH));
		}
		
		private function xmlComplete(event: Event) : void
		{
			// remove the event listener from the xmlLdr in the startXML function
			event.target.removeEventListener(Event.COMPLETE, xmlComplete);
			
			// set the xml to the var we'll use to access it
			xml = new XML(event.target.data);
			
			playMP3(xml.@path);
		}
		
		private function removedFromStage(event: Event) : void
		{
			track.resetSound();
		}
	}
}