var audioEl;
window.onload = function(){

	

	//window.plugins.powerManagement.acquire(function(){},function(){});

	var links = document.getElementsByClassName("button-wrapper");
	audioEl= document.getElementById("audio");

	for(var i = 0; i<links.length; i++){
		links[i].addEventListener("click",songClicked);
	}
	audioEl.addEventListener("ended", function(){
		if (document.getElementById("shuffle").childNodes[1].classList.contains("checkImgC")){
			songClicked.call(links[Math.floor(Math.random() * links.length)]);
		}else{
			resetIcons();
		}
	},false);

	var shuffleBtn = document.getElementById("shuffle");
	var repeatBtn = document.getElementById("repeat");
	shuffleBtn.addEventListener("click", function(){
		if (this.childNodes[1].classList.contains("checkImg")){
			repeatBtn.childNodes[1].classList.remove("checkImgC");
			repeatBtn.childNodes[1].classList.add("checkImg");
			audioEl.loop = false;
			this.childNodes[1].classList.remove("checkImg");
			this.childNodes[1].classList.add("checkImgC");
		}else{
	
			this.childNodes[1].classList.remove("checkImgC");
			this.childNodes[1].classList.add("checkImg");
		}
	});

	
	repeatBtn.addEventListener("click", function(){
		if (this.childNodes[1].classList.contains("checkImg")){
			shuffleBtn.childNodes[1].classList.remove("checkImgC");
			shuffleBtn.childNodes[1].classList.add("checkImg");
			audioEl.loop = true;
			this.childNodes[1].classList.remove("checkImg");
			this.childNodes[1].classList.add("checkImgC");
		}else{
			audioEl.loop = false;
			this.childNodes[1].classList.remove("checkImgC");
			this.childNodes[1].classList.add("checkImg");
		}
	});

};

function resetIcons(){
	var playIcons = document.getElementsByClassName("button-wrapper");
	for(var x = 0; x<playIcons.length; x++){
		playIcons[x].childNodes[0].classList.remove("btnStop");
		playIcons[x].childNodes[0].classList.add("btnPlay");
	}
}

function songClicked(event){

	audioEl.pause();
	if ((this.childNodes[0].classList+"").indexOf("btnStop")<0){
		resetIcons();

		var mp3URL = this.getAttribute("data-mp3");
		audioEl.src = "mp3/" + mp3URL;
		this.childNodes[0].classList.remove("btnPlay");
		this.childNodes[0].classList.add("btnStop");
		audioEl.play();
	}else{
		resetIcons();

	}
}