var AUDIO_STATE = {
	play:"playing",
	stop:"stopped",
	pause:"paused"
}

var AudioManager = {
	instances:[],
	addAudio:function(instanceName,myAudioInstance){
		this.instances[instanceName] = myAudioInstance;
	},
	removeAudio:function(instanceName){
		//
	},
	getInstance:function(instanceName){
		return this.instances[instanceName]
	}
}

var MyAudio = {
	instanceName:"test",
	created:false,	
	currentIndex:-1,
	audioInstance:null,
	state:AUDIO_STATE.stop,
	previousSelectedIndex:0,
	playlist:{},

	init:function(instanceName,playlist){
		this.instanceName = instanceName;
		AudioManager.addAudio(instanceName,this);
		this.playlist = playlist;
	},
	create:function(){
		if(this.created){
			this.audioInstance.pause();

		}
		

		document.getElementById("songTitle").innerHTML = playList[this.currentIndex].artist;
		document.getElementById("songArtist").innerHTML = playList[this.currentIndex].title;


		if(typeof Audio == "undefined"){
			this.audioInstance = null;
			this.audioInstance = document.createElement("audio");
			this.audioInstance.setAttribute('src',playList[this.currentIndex].src + ".ogg");
		}else{
			this.audioInstance = null;
			this.audioInstance = new Audio(playList[this.currentIndex].src + ".ogg");
			this.audioInstance.play();
		}
		document.getElementById("songLoading").innerHTML = "Loading Please Wait";

		this.audioInstance.addEventListener("canplaythrough",function(e){
			document.getElementById("songLoading").innerHTML = "";
			okayToVirtualize = true;
		},true);
		this.created = true;
	},
	destroy:function(){
		this.audioInstance.pause();
		this.audioInstance = null;
	},
	play:function(){	
		
		switch(this.state){
			case AUDIO_STATE.stop:
				if(this.currentIndex == -1)
					this.currentIndex = 0;
				this.create();
				this.audioInstance.play();
			break;
			case AUDIO_STATE.pause:
				this.audioInstance.play();
			break;
			case AUDIO_STATE.play:
				this.create();
				this.audioInstance.play();
			break;
		}
		this.highlightCurrentSong();
		this.state = AUDIO_STATE.play;
	},
	playIndex:function(index){
		this.currentIndex = index;
		this.create();
		this.highlightCurrentSong();
		this.audioInstance.play();
		this.previousSelectedIndex = index;
	},
	pause:function(){
		this.audioInstance.pause();
		this.state = AUDIO_STATE.pause;
	},
	stop:function(){
		this.audioInstance.pause();
		this.state = AUDIO_STATE.stop;
	},
	previous:function(){
		if(this.currentIndex == -1)
			this.currentIndex = 1;
		if(this.currentIndex - 1 < 0)
			this.currentIndex = this.playlist.length;
		this.playIndex(this.currentIndex - 1);
	},
	next:function(){
		if(this.currentIndex + 1 >= this.playlist.length)
			this.currentIndex = -1;
		this.playIndex(this.currentIndex + 1);
	},
	muteUnmute:function(){
		if(this.audioInstance.volume == 0)
			this.audioInstance.volume = 1;
		else
			this.audioInstance.volume = 0;
		
	},
	mute:function(){
		this.audioInstance.volume = 0;
	},

	highlightCurrentSong:function(){
		var previousSelectedRow = document.getElementById("playlist_row" + this.previousSelectedIndex);

		if(previousSelectedRow){
			previousSelectedRow.className = previousSelectedRow.className.replace('selectedRow',"");
		}

		var currentRow = document.getElementById("playlist_row" + this.currentIndex);
		currentRow.className += " selectedRow";
	},
	initPlaylist:function(){
		var newElements = "";

		for ( var i in this.playlist){
			newElements += "<li id='playlist_row"+i+"' onclick='AudioManager.getInstance(\""+this.instanceName+"\").playIndex("+i+")'>" + this.playlist[i].artist + " - " + this.playlist[i].title + "</li>";
		}
		document.getElementById("playlist").innerHTML = newElements;

	}
}	

