function addToPlaylist()
{
	var allList = new Array();
	var songName = "";
     var songId = "";
     var elementCount = 0;
	var selectedCount = 0;

     for (var i = 0; i < document.getElementById('playListContainer').elements.length; i++)
     {
     	elementCount++;
		str = document.getElementById('playListContainer').elements[i].value.split("::");
           songId = str[0];
           strValue = str[1];
           
     	if (document.getElementById('playListContainer').elements[i].checked == true)
           {
           	selectedCount++;
           	strStartPos = strValue.lastIndexOf("/");
                 songName = strValue.substring(strStartPos + 1);
           	allList[songName] = strValue;
                 document.getElementById(songId).style.fontWeight = '800';
           }
           else
		{
			document.getElementById(songId).style.fontWeight = '';
		}
     }

	// Clear any existing entries
	document.getElementById('plt').options.length = 0;
	var songCount = 0;	
	for(songName in allList) 
     {
		document.getElementById('plt').options[songCount] = new Option(songName, allList[songName]);
           songCount++;
	}
     
	if (selectedCount < elementCount)
     {
     	if (selectedCount > 0)
           {
			document.getElementById('selectAllButton').disabled = false;
			document.getElementById('clearSelectedButton').disabled = false;
           }
           else
           {
			document.getElementById('selectAllButton').disabled = false;
			document.getElementById('clearSelectedButton').disabled = true;
			document.getElementById('selectAllButton').focus();
           }
     }
     else if (selectedCount == elementCount)
     {
		document.getElementById('selectAllButton').disabled = true;
     	document.getElementById('clearSelectedButton').disabled = false;
		document.getElementById('streamButton').focus();           
     }


     if (songCount > 0)
     {
           document.getElementById('streamButton').disabled = false;           
     }
     else
     {
           document.getElementById('streamButton').disabled = true;
     }
}

function selectAll()
{
	for (var i = 0; i < document.getElementById('playListContainer').elements.length; i++)
	{
		document.getElementById('playListContainer').elements[i].checked = true;
	}
     document.getElementById('selectAllButton').disabled = true;
     document.getElementById('clearSelectedButton').disabled = false;
     document.getElementById('streamButton').disabled = false;
	document.getElementById('streamButton').focus();     
     addToPlaylist();
}

function clearSelected()
{
	for (var i = 0; i < document.getElementById('playListContainer').elements.length; i++)
	{
     	if (document.getElementById('playListContainer').elements[i].checked == true)
           {
			document.getElementById('playListContainer').elements[i].checked = false;
           }
	}
     document.getElementById('selectAllButton').disabled = false;     
	document.getElementById('clearSelectedButton').disabled = true;
     document.getElementById('streamButton').disabled = true;
	document.getElementById('selectAllButton').focus();
	addToPlaylist();     
}

function streamSelected()
{
	var streamString = "";
	var selectedCount = document.getElementById('plt').options.length;
	for (i = 0; i < selectedCount; i++)
     {
 		strValue = document.getElementById('plt').options[i].value;
		streamString += strValue;
           if (i < selectedCount - 1)
           {
			streamString += "::";
           }
     }
     
     document.location.href='pwap_streamFile.php?' + streamString;
}

function showInfo(songId, songURL)
{
	var dynamicHeightValue = 5;
     var dynamicMaxHeight = 60;
     var dynamicAnimationSpeed = 20;

	if (document.getElementById(songId + '_info').style.display == 'block')
     {
           shrinkHeight(songId, parseInt(document.getElementById(songId + '_info').style.height), 0, dynamicHeightValue, dynamicAnimationSpeed);
     }
     else
     {
		document.getElementById(songId + '_info').src = "pwap_fileInfo.php?x=small&file=" + songURL;
		document.getElementById(songId + '_info').style.display = 'block';
		document.getElementById(songId + '_close').style.display = 'block';
		document.getElementById(songId + '_details').style.display = 'block';           
		document.getElementById(songId + '_info').style.height = '0px';           
           growHeight(songId, 0, dynamicMaxHeight, dynamicHeightValue, dynamicAnimationSpeed);
     }
}

function showAllInfo(songId, songURL)
{
	window.open("pwap_fileInfo.php?x=all&file=" + songURL, "_blank");
}

function growHeight(elementId, value, maxValue, incrementValue, speedValue)
{
	if (parseInt(value) < parseInt(maxValue))
     {
     	var newValue = parseInt(value) + parseInt(incrementValue);
     	document.getElementById(elementId + '_info').style.height = newValue + "px";
           setTimeout("growHeight('" + elementId + "','" + newValue + "', '" + maxValue + "', '" + incrementValue + "', '" + speedValue + "')", speedValue);
     }
}

function shrinkHeight(elementId, value, minValue, decrementValue, speedValue)
{
	if (parseInt(value) > parseInt(minValue))
     {
     	var newValue = parseInt(value) - parseInt(decrementValue);
     	document.getElementById(elementId + '_info').style.height = newValue + "px";
           setTimeout("shrinkHeight('" + elementId + "','" + newValue + "', '" + minValue + "', '" + decrementValue + "', '" + speedValue + "')", speedValue);
     }
     else
     {
     	document.getElementById(elementId + '_info').style.display = 'none';
		document.getElementById(elementId + '_close').style.display = 'none';
		document.getElementById(elementId + '_details').style.display = 'none';           
     }
}

function addAnimation(songId, checkedValue, songName)
{
	if (checkedValue == true)
     {
		songId = 'song_' + songId;
		var w = document.getElementById(songId).offsetWidth;
		var h = document.getElementById(songId).offsetHeight; 

		var divObj = document.createElement("DIV");
		divObj.setAttribute("ID", "streamAnimationBox");
           divObj.innerHTML = songName;
		document.getElementById("mp3DocumentBody").appendChild(divObj);
     
		leftTopCoord = findPos(document.getElementById(songId));

		document.getElementById("streamAnimationBox").style.position = "absolute";
		document.getElementById("streamAnimationBox").style.border = "2px #4769A3 dotted";
		document.getElementById("streamAnimationBox").style.fontWeight = "bold";           
		document.getElementById("streamAnimationBox").style.background = "transparent";
		document.getElementById("streamAnimationBox").style.backgroundColor = "#CCD4E7";
		document.getElementById("streamAnimationBox").style.opacity = ".5";
		document.getElementById("streamAnimationBox").style.filter = "alpha(opacity=50)";                            
		document.getElementById("streamAnimationBox").style.top = leftTopCoord[1];
		document.getElementById("streamAnimationBox").style.left = leftTopCoord[0];
		document.getElementById("streamAnimationBox").style.width = w;
		document.getElementById("streamAnimationBox").style.height = h;          
		document.getElementById("streamAnimationBox").style.display = "block";

		doAnimation(w, h, leftTopCoord[0], leftTopCoord[1], 15, 0)
     }	
     else
     {
		var bodyObj = document.getElementById("mp3DocumentBody");
		var animationObj = document.getElementById("streamAnimationBox");

		bodyObj.removeChild(animationObj);
     }
}

// Taken from quirksmode: http://www.quirksmode.org/js/findpos.html
function findPos(obj) 
{
	var curleft = 0;
     var curtop = 0;

	if (obj.offsetParent) 
     {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
           {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}

	return [curleft,curtop];
}

function doAnimation(width, height, leftPos, topPos, incValue, iValue)
{
		var animationSpeed = 35;
           
           // Get Stream Button coordinates
		sbw = document.getElementById("streamButton").offsetWidth;
           sbh = document.getElementById("streamButton").offsetHeight;
           sbCoord = findPos(document.getElementById("streamButton"));

           stopWidth = sbw;
           stopHeight = sbh;
           stopTop = sbCoord[1];
           stopLeft = sbCoord[0];
           
           if (iValue <= incValue)
           {
			document.getElementById("streamAnimationBox").style.width = (iValue * (sbw - width) / incValue) + width;
			document.getElementById("streamAnimationBox").style.height = (iValue * (sbh - height) / incValue) + height;
			document.getElementById("streamAnimationBox").style.left = (iValue * (stopLeft - leftPos) / incValue) + leftPos;
			document.getElementById("streamAnimationBox").style.top = (iValue * (stopTop - topPos) / incValue) + topPos;

			document.getElementById("streamAnimationBox").style.opacity = 1 / (iValue * .2);
			document.getElementById("streamAnimationBox").style.fontSize = 100  - (iValue * 4) + "%";                 
			//document.getElementById("streamAnimationBox").style.filter = "alpha(opacity=" + (100 - (iValue * 10)) + ")";                            
                 
                 iValue++;
                 setTimeout("doAnimation(" + width + ", " + height+ ", " + leftPos + ", " + topPos + ", " + incValue + ", " + iValue + ")", animationSpeed);                 
           }
           else
           {
			var bodyObj = document.getElementById("mp3DocumentBody");
			var animationObj = document.getElementById("streamAnimationBox");

			bodyObj.removeChild(animationObj);
           }
}
