//Plays all checked items on /music/collection.jsp or if no selections were
//made, plays the entire collection.
function playSelection(userName) {
	var referer = encodeURIComponent(window.location);
    var checkedCount = 0;
    var params = "";

    var mediaForm = null;
    if (document.mediaForm != undefined) {
      mediaForm = document.mediaForm;
    } else {
      mediaForm = document.collectionForm;
    }
	if (mediaForm.mediaName == undefined) {
        return showWarning(playSelectionEmpty, 0, "");
    }
    
    if (mediaForm.mediaName.length != undefined) {
        for (var i = 0; i < document.mediaForm.mediaName.length; i++) {
            if (mediaForm.mediaName[i].checked == true) {
                checkedCount++;
                params += "&mediaName=" + document.mediaForm.mediaName[i].value;
            }
        }
    }
    // there is only one mediaName element in the form
    else if (mediaForm.mediaName.checked == true) {
        checkedCount++;
        params += "&mediaName=" + document.mediaForm.mediaName.value;
    }    

    if (checkedCount == 0) {
        if (mediaForm.collectionName.value != "Shoebox") {
            // Play all items in collection
		
            params += "&collectionName=" + mediaForm.collectionName.value;
            playCollection(params, userName);
        }
        else {
            // Play all items in current All Tracks view (which contains songs in different collections)
            params += "&collectionName=" + mediaForm.collectionName.value;
            if (mediaForm.mediaName.length) {              
                for(var i = 0; i < mediaForm.mediaName.length; i++) {
                    params += "&mediaName=" + mediaForm.mediaName[i].value;
                }
            }
            else {
                params += "&mediaName=" + mediaForm.mediaName.value;
            }
            params += "&user=" + userName;
            

            var locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getSelectionM3U" + params;
            window.location = locationString;
        }
    }
    else {
        var locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getSelectionM3U&collectionName=" + mediaForm.collectionName.value + params + "&user=" + userName;
        /*
                var shuffle = $("shuffle").innerHTML;
                if (shuffle == "ON") {
                        locationString += "&shuffle";
                }
        */
	 window.location = locationString;
    }
}

function playEditSong(collectionName, mediaName, userName){
    var locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getSelectionM3U&collectionName=" + collectionName + "&mediaName=" + mediaName + "&user=" + userName;    
    window.location = locationString;
}

function playSingle( mediaName, userName ) {
    var locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getSelectionM3U&collectionName=" + document.mediaForm.collectionName.value;
    locationString += "&mediaName=";
    locationString += mediaName;
    locationString += "&user=";
    locationString += userName;    
    window.location = locationString;
}

// Plays all collections indicated by params or the single collection on /music/collection.jsp
function playCollection(params, userName) {
	var locationString = "";
	if ((params == "") || (params == undefined)) {
		locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getCollectionM3U&collectionName=" + document.mediaForm.collectionName.value;
	}
	else {	
		locationString = "/gemini/service/com.casero.abode.media.audio.RootAudioController?v=getCollectionM3U" + params;
	}
  
    locationString = locationString + "&user=" + userName;
	/*
	var shuffle = $("shuffle").innerHTML;
	if (shuffle == "ON") {
		locationString += "&shuffle";
	}
	*/

    window.location = locationString;
}

// Plays all checked collections on /music/index.jsp page.
function playCollections(userName) {
	
	var params = "";
	var checkedCount = 0;
	if (document.collectionForm.collectionName.length != undefined) {
        for (var i = 0; i < document.collectionForm.collectionName.length; i++) {
            if (document.collectionForm.collectionName[i].checked == true) {
                checkedCount++;
                params += "&collectionName=" + document.collectionForm.collectionName[i].value;
		

            }
        }
    }
    // there is only one collectionName element in the form
    else if (document.collectionForm.collectionName.checked == true) {
        checkedCount++;
        params += "&collectionName=" + document.collectionForm.collectionName.value;

    }

	//If no selections were made add all checkboxes on the page
	if (checkedCount == 0) {
		if (document.collectionForm.collectionName.length != undefined) {
	        for (var i = 0; i < document.collectionForm.collectionName.length; i++) {
                params += "&collectionName=" + document.collectionForm.collectionName[i].value;
	        }
	    }
        else if (document.collectionForm.collectionName) {
            params += "&collectionName=" + document.collectionForm.collectionName.value;
        }
	}
	params += "&userName="+userName;
	var myAjax = new Ajax.Updater('', '/music/checkEmptyPlayLists.jsp', {method: 'get',
				 parameters: params, onComplete: checkResponse }); 
}    

function checkResponse (req){
	var responseText = req.responseText.valueOf();
	// Fix me: why I am getting string of length 12.
	// Tmp fix for Music empty List
	if(responseText.length <= 12){
		showWarning("The selected play list is empty. You must select a play list that has songs.",
						0, false);
		return;
	}
	var index = responseText.lastIndexOf("=");
	var userName = responseText.substring(index+1);
	index = responseText.lastIndexOf("&");
	var index1 = responseText.indexOf("&");
	var params = responseText.substring(index, index1);
	playCollection(params, userName); 
}

function toggleShuffle() {
	var currentValue = $("shuffleText").innerHTML;

	if (currentValue == "ON") {
		$("shuffleText").innerHTML = "OFF";
		$("shuffle").style.background = "#F29E60";
	}
	else {
		$("shuffleText").innerHTML = "ON";
		$("shuffle").style.background = "#C21D23";
	}
}

function updateSortField(collectionName, sortField) {		
    window.location.href = "/music/collection.jsp?collectionName=" + collectionName + "&sortField=" + sortField;
}