var Ajax;
if (Ajax && (Ajax != null)) {
	Ajax.Responders.register({
	  onCreate: function() {
        if($('spinner') && Ajax.activeRequestCount>0)
          Effect.Appear('spinner',{duration:0.5,queue:'end'});
	  },
	  onComplete: function() {
        if($('spinner') && Ajax.activeRequestCount==0)
          Effect.Fade('spinner',{duration:0.5,queue:'end'});
	  }
	});
}

/***********************/
/** GLOBAL *************/
/***********************/
/*
function $(id){
	return document.getElementById(id);
}
*/
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;


Event.observe(window, 'load', function() {
	$$("a").each(function(tag){
		tag.onfocus = function(){
			this.blur();
		};
	});
	$$("form#searchForm input").each(function(tag){
		tag.observe('keyup', function(e){
			if(e.keyCode == Event.KEY_RETURN) $("searchForm").submit();
		});
	});
});

function invokeScript(divid){
	var obj = $(divid).childNodes;
	var len = obj.length;
	for(var i=0; i<len; i++){
		if(obj[i].tagName=="SCRIPT")
			eval(obj[i].text);   // execute the scripts.
	}
}

/************************/
/*** DIALOG *************/
/************************/

var dialogObj;

function dialog(e){

	var dialogId = "dialog";

	if(!dialogObj){
		dialogObj = new dialogInit(dialogId);
	}
	
	dialogObj.load(e);
	//dialogObj.position(dialogId, false, 100);
	dialogObj.position(dialogId, false, false);
	dialogObj.show();
	
	
	document.observe('keyup', function(e){
		if(e.keyCode == Event.KEY_ESC) dialogObj.close();
	});
	
	invokeScript("dialog");
	
}

function dialogInit(dialogId){

	// create blocker if not exist
	if(!$("blocker")){
		var newElm = document.createElement('div');
		newElm.setAttribute('id','blocker');
		//newDialog.innerHTML = e.responseText;
		document.body.appendChild(newElm);
	}else if($("blocker").style.display=="none"){
		$("blocker").style.display="block";
	}

	// create dialog if not exist
	if(!$(dialogId)){
		var newElm = document.createElement('div');
		newElm.setAttribute('id','dialog');
		document.body.appendChild(newElm);
	}else if($(dialogId).style.display=="none"){
		$(dialogId).style.display="block";
	}
	
	this.load = function(e){
		$(dialogId).innerHTML = e.responseText;
	}
	
	this.position = function(dialogId, leftOffset, topOffset) {
		if($(dialogId) != null) {
			if(typeof window.innerHeight != 'undefined') { // FF and Chrome
				$(dialogId).style.top = 
					Math.round(window.pageYOffset + 
					((topOffset)?topOffset:((window.innerHeight - $(dialogId).getHeight())/2))) + 'px';
				$(dialogId).style.left = 
					Math.round(window.pageXOffset + 
					((window.innerWidth - $(dialogId).getWidth()))/2) + 'px';
			} else { // IE8
				$(dialogId).style.top = 
					Math.round(document.documentElement.scrollTop + 
					((topOffset)?topOffset:((document.documentElement.clientHeight - $(dialogId).getHeight())/2))) + 'px';
				$(dialogId).style.left = 
					Math.round(document.documentElement.scrollLeft + 
					((document.documentElement.clientWidth - $(dialogId).getWidth()))/2) + 'px';
			}
		}
	}
	
	this.show = function(){
		// if it has been previously close
		if($(dialogId).style.display=="none"){		
			$('dialog').style.display="block";
			$('blocker').style.display="block";
		}
		this.state = "visible";
	}
	
	this.close = function(){
		$('dialog').style.display="none";
		$('blocker').style.display="none";
		this.state = "hidden";
	}
	
}

Event.observe(window, 'resize', function() {
	if(dialogObj)dialogObj.position("dialog", false, false);
});
Event.observe(window, 'scroll', function() {
	if(dialogObj)dialogObj.position("dialog", false, false);
});



/************************/
/*** MAIN ***************/
/************************/

var activeSongId = "";
var songRowId = "";
function playPauseSong(songIdbtn){	
	if(activeSongId==songIdbtn){ // Pause or resume song
		if(player.isPaused()){
			$(activeSongId).src=$(activeSongId).src.replace(/play-selected-paused/,'pause-selected-playing');// replace play by a pause button
			player.resume();			
		}else if(player.isPlaying()){
			$(activeSongId).src=$(activeSongId).src.replace(/pause-selected-playing/,'play-selected-paused');// replace pause by a play button
			player.pause();
		}else{
			$(activeSongId).src=$(activeSongId).src.replace(/play-selected-paused/,'pause-selected-playing');// replace play by a pause button
			player.play($(activeSongId).getAttribute("songFile"));
		}
	}else{ // play song or switch song
	
		// get songRowId
		songRowId = getSongRowId(activeSongId);
		// deactivate previous song
		if(activeSongId && $(songRowId)){

			if(getSongRowId(activeSongId)!=getSongRowId(songIdbtn)){ // deactivate row if song is in different row
				$(songRowId).className = '';

				// reinitialise previous song
				$(activeSongId).src = $(activeSongId).src.replace(/pause-selected-playing/,'play-out').replace(/play-selected-paused/,'play-out').replace(/loading-selected-playing.gif/,'play-out.png');// replace by a play-out button
				var deleteBtn = activeSongId.replace(/Play/,'Delete');
				if($(deleteBtn))$(deleteBtn).src = $(deleteBtn).src.replace(/-selected/,'');
				var addBtn = activeSongId.replace(/Play/,'Add');
				if($(addBtn))$(addBtn).src=$(addBtn).src.replace(/-selected/,'');				
				//deactivate other mixes
				if(!isAmix(activeSongId)){
					var AmixId = getAmix(activeSongId);
					if($(AmixId)){
						$(AmixId).src = $(AmixId).src.replace(/play-selected-out/,'play-out');// replace by a play-out button
						var AmixDeleteBtn = AmixId.replace(/Play/,'Delete');
						if($(AmixDeleteBtn))$(AmixDeleteBtn).src = $(AmixDeleteBtn).src.replace(/-selected/,'');
						var AmixAddBtn = AmixId.replace(/Play/,'Add');
						if($(AmixAddBtn))$(AmixAddBtn).src=$(AmixAddBtn).src.replace(/-selected/,'');
					}
				}
				if(!isCmix(activeSongId)){
					var CmixId = getCmix(activeSongId);
					if($(CmixId)){
						$(CmixId).src = $(CmixId).src.replace(/play-selected-out/,'play-out');// replace by a play-out button
						var CmixDeleteBtn = CmixId.replace(/Play/,'Delete');
						if($(CmixDeleteBtn))$(CmixDeleteBtn).src = $(CmixDeleteBtn).src.replace(/-selected/,'');
						var CmixAddBtn = CmixId.replace(/Play/,'Add');
						if($(CmixAddBtn))$(CmixAddBtn).src=$(CmixAddBtn).src.replace(/-selected/,'');
					}
				}
				if(!isDmix(activeSongId)){
					var DmixId = getDmix(activeSongId);
					if($(DmixId)){
						$(DmixId).src = $(DmixId).src.replace(/play-selected-out/,'play-out');// replace by a play-out button
						var DmixDeleteBtn = DmixId.replace(/Play/,'Delete');
						if($(DmixDeleteBtn))$(DmixDeleteBtn).src = $(DmixDeleteBtn).src.replace(/-selected/,'');
						var DmixAddBtn = DmixId.replace(/Play/,'Add');
						if($(DmixAddBtn))$(DmixAddBtn).src=$(DmixAddBtn).src.replace(/-selected/,'');
						var DmixSongToolbox = DmixId.replace(/PlayBtn/,'Toolbox');
					}
				}
			}else{				
				if(player.isPaused())$(activeSongId).src = $(activeSongId).src.replace(/play-selected-paused/,'play-selected-out').replace(/loading-selected-playing.gif/,'play-selected-out.png');
				if(player.isPlaying())$(activeSongId).src = $(activeSongId).src.replace(/pause-selected-playing/,'play-selected-out').replace(/loading-selected-playing.gif/,'play-selected-out.png');
			}
		}
		
		/***** activate this song *********/			
		try {
			activeSongId = songIdbtn;		
			player.play($(activeSongId).getAttribute("songFile"));

			songRowId = getSongRowId(activeSongId);
			$(songRowId).className = 'selected';
			
			// active this song's icons
			//$(activeSongId).src = $(activeSongId).src.replace(/play(-selected)?-over/,'pause-selected-playing');
			$(activeSongId).src = $(activeSongId).src.replace(/play(-selected)?-over.png/,'loading-selected-playing.gif');
			var deleteBtn = activeSongId.replace(/Play/,'Delete');
			if($(deleteBtn))$(deleteBtn).src = $(deleteBtn).src.replace(/delete-out/,'delete-selected-out');
			var addBtn = activeSongId.replace(/Play/,'Add');
			if($(addBtn))$(addBtn).src = $(addBtn).src.replace(/add-out/,'add-selected-out');
			
			if(!/.mix/.test(getSongRowId(activeSongId))){ // activate other only if in the same row
				if(!isAmix(activeSongId)){
					var AmixId = getAmix(activeSongId);
					if($(AmixId)){
						$(AmixId).src = $(AmixId).src.replace(/play-out/,'play-selected-out');
						var AmixDeleteBtnId = AmixId.replace(/Play/,'Delete');
						if($(AmixDeleteBtnId))$(AmixDeleteBtnId).src = $(AmixDeleteBtnId).src.replace(/delete-out/,'delete-selected-out');
						var AmixAddBtnId = AmixId.replace(/Play/,'Add');
						if($(AmixAddBtnId))$(AmixAddBtnId).src = $(AmixAddBtnId).src.replace(/add-out/,'add-selected-out');	
					}
				}
				if(!isCmix(activeSongId)){
					var CmixId = getCmix(activeSongId);
					if($(CmixId)){
						$(CmixId).src = $(CmixId).src.replace(/play-out/,'play-selected-out');
						var CmixDeleteBtnId = CmixId.replace(/Play/,'Delete');
						if($(CmixDeleteBtnId))$(CmixDeleteBtnId).src = $(CmixDeleteBtnId).src.replace(/delete-out/,'delete-selected-out');
						var CmixAddBtnId = CmixId.replace(/Play/,'Add');
						if($(CmixAddBtnId))$(CmixAddBtnId).src = $(CmixAddBtnId).src.replace(/add-out/,'add-selected-out');	
					}
				}
				if(!isDmix(activeSongId)){
					var DmixId = getDmix(activeSongId);
					if($(DmixId)){
						$(DmixId).src = $(DmixId).src.replace(/play-out/,'play-selected-out');
						var DmixDeleteBtnId = DmixId.replace(/Play/,'Delete');
						if($(DmixDeleteBtnId))$(DmixDeleteBtnId).src = $(DmixDeleteBtnId).src.replace(/delete-out/,'delete-selected-out');
						var DmixAddBtnId = DmixId.replace(/Play/,'Add');
						if($(DmixAddBtnId))$(DmixAddBtnId).src = $(DmixAddBtnId).src.replace(/add-out/,'add-selected-out');	
					}
				}
			}	
		}catch (err){			
			alert('This sample will be available soon, please try later.');
			return
		}		
	}
}

function getSongRowId(activeSongId){
	var songRowId	= activeSongId.replace(/PlayBtn/,''); // song is on its own row
	if(!$(songRowId)){
		songRowId = songRowId.replace(/.mix/,''); // song is in the same row
	}
	return songRowId;
}

function setPauseButton(){
	$(activeSongId).src=$(activeSongId).src.replace(/loading-selected-playing.gif/,'pause-selected-playing.png');
}

function resetPlayer(){
	$(activeSongId).src=$(activeSongId).src.replace(/pause-selected-playing/,'play-selected-paused');
}

function isSameSong(previousSongId, nextSongId){
	return previousSongId.replace(/.mix/,'')==nextSongId.replace(/.mix/,'');
}

function isAmix(songId){
	return songId.indexOf('Amix')>=0;
}

function isCmix(songId){
	return songId.indexOf('Cmix')>=0;
}

function isDmix(songId){
	return songId.indexOf('Dmix')>=0;
}

function getAmix(songId){
	return songId.replace(/.mix/,'Amix');
}

function getCmix(songId){
	return songId.replace(/.mix/,'Cmix');
}

function getDmix(songId){
	return songId.replace(/.mix/,'Dmix');
}

function resetSearchCriteras(){
	$("searchForm").songtitle.value="";
	$("searchForm").artist.value="";
	$("searchForm").lyrics.value="";
	$("searchForm").title.value="";
	$("searchForm").language.selectedIndex=0;
	$("searchForm").vocals.selectedIndex=0;
	$("searchForm").eraFrom.selectedIndex=0;
	$("searchForm").eraTo.selectedIndex=0;
	$("searchForm").tempoFrom.selectedIndex=0;
	$("searchForm").tempoTo.selectedIndex=0;
	$("searchForm").durFrom.selectedIndex=0;
	$("searchForm").durTo.selectedIndex=0;
	$("searchForm").key.selectedIndex=0;
	$("searchForm").genre.selectedIndex=0;
}

function validateSelectMusicBox(){
	if(document.forms.selectMusicBox.mbId.value==""){
		alert("Please select a Song List or create a new one");
		return false
	}
	return true;
}

function validateCreateMusicBox(){
	if(document.forms.createMusicBox.mbName.value==""){
		alert("Please enter a name");
		return false
	}
	return true;
}

function validateRenameMusicBox(){
	if(document.forms.renameMusicBox.mbName.value==""){
		alert("Please enter a name");
		return false
	}
	return true;
}

function validateRegistration(){
	if(!document.forms.subscribe.terms.checked){
		alert("Please accept the Terms & Conditions and the Privacy Policy");
		return false
	}
	return true;
}

