function launch_m3u() {
	var url = "code/m3u.php?ids="
	var ids = ""
	var children = document.getElementById("playlist_covers").getElementsByTagName("td")
	for(var i = 0;i < children.length;i++) {
		if (children[i].style.textDecoration!="line-through") ids = ids + children[i].id.substr(4).replace(/_/g,'') + ","
	}
	playlist_win = window.open('http://www.amgtapestry.com/radio/'+url+ids,'playlist','width=0,height=0')	//http://www.amgtapestry.com/radio/
	playlist_win.blur()
	window.focus()
}

function editPlaylist(oObj,tr) {
	if (document.getElementById(tr).style.textDecoration!="line-through") {
		document.getElementById(tr).style.color = "#ccc"
		document.getElementById(tr).style.textDecoration= "line-through"
		oObj.innerHTML = "+"
	} else {
		document.getElementById(tr).style.color = "#333"
		document.getElementById(tr).style.textDecoration= "none"
		oObj.innerHTML = "x"
	}
}


function doTabs(type) {
	switch(type) {
		case 'search':
		document.getElementById("search_form").style.display="block"
		document.getElementById("main").style.height="200px"
		document.getElementById("main_head").style.backgroundImage="url(images/coll_on.gif)"
		document.getElementById("main").innerHTML=document.getElementById("search_results").innerHTML
		break
		case 'desc':
		document.getElementById("search_form").style.display="none"
		document.getElementById("main").style.height="auto"
		document.getElementById("main_head").style.backgroundImage="url(images/desc_on.gif)"
		document.getElementById("main").innerHTML=document.getElementById("desc").innerHTML
		break
	}
}

function doOptions() {
	switch(document.getElementById("options").style.visibility) {
		case 'visible':
			document.getElementById("options").style.visibility = 'hidden'
			break
		case 'hidden':
			document.getElementById("options").style.visibility = 'visible'
			break
		default:
			document.getElementById("options").style.visibility = 'visible'
			break
	}	
}

function expand(id) {
	var	oObj = document.getElementById(id)
	oObj.style.display = (oObj.style.display == "block") ? "none" : "block"
}

/****DESCRIPTOR TREE LOGIC****/
function descBuild() {
	var ids = ""
	var children = document.getElementById("main").getElementsByTagName("INPUT")
	for(var i = 0;i < children.length;i++) {
		if ((children[i].checked)&&(children[i].id.substr(0,1)=='D')) ids = ids + children[i].id+","
	}
	ids = ids.replace("+","%20")			//convert to tapestry ids
	ids = ids.substring(0,ids.length-1)		//remove trailing comma
	doPlaylist(ids)
}

function doParent(parentInput) {
	var checkVal = parentInput.checked	
	var parentId = parentInput.id
	var children = document.getElementById(parentId+'_child').getElementsByTagName("input")
	for(var i = 0;i < children.length;i++) {	
		children[i].checked=checkVal
	}
	updateParent(parentId)
}

function doChild(childInput,parentId) {
	var checkVal = childInput.checked	
	var oParent = document.getElementById(parentId)
	if (!checkVal) oParent.checked = checkVal
	updateParent(parentId)
}

function updateParent(parentId) {
	var trueCnt = 0
	var children = document.getElementById(parentId+'_child').getElementsByTagName("input")
	for (var i = 0;i < children.length;i++) {	
		if (children[i].checked) trueCnt++
	}

	if (trueCnt > 0) {
		document.getElementById(parentId+'_title').style.color = "#CB9135"
		document.getElementById(parentId).checked = (trueCnt == children.length) ? true : false
	} else {
		document.getElementById(parentId).checked = false
		document.getElementById(parentId+'_title').style.color = "#fff"
	}
}

/*******AJAX*******/
var xmlHttp = false
var div = ""
var timer = ""

function doPlaylist(ids) { 
	if (ids.length > 0) { 	
		div = "playlist"
		var diversity = document.forms["option_form"].mix_diversity.value
		var count = document.forms["option_form"].mix_count.value
		if (count > "9999" || count < "1") {
			alert("Invalid playlist count value")
			return
		}

		document.getElementById("playlist_head").style.display="block"
		document.getElementById("playlist").style.display="block"
		document.getElementById("playlist").innerHTML="<div id=\"load\"><img src=\"images/loading_playlist.gif\"  alt=\"building playlist...\" width=\"240px\" height=\"97px\" /></div>"

		for (var i=0; i < document.option_form.mix_mode.length; i++) {
			if (document.option_form.mix_mode[i].checked) {
				var opts = document.option_form.mix_mode[i].value
			}
		}
		
		for (var i=0; i < document.option_form.mix_diversity.length; i++) {
			if (document.option_form.mix_diversity[i].checked) {
				var diversity = document.option_form.mix_diversity[i].value
			}
		}

		var url="code/playlist.php?ids="+ids+"&opts="+opts+"&diversity="+diversity+"&count="+count
		xmlreqGET(url)
	} else { 
		document.getElementById("playlist").innerHTML="No ID"
	} 
} 

function doSearch (q) { 	
	if (q=='') return
	div = "search_results"
	document.getElementById("search_button").style.backgroundImage="url(./images/search_btn_hit.gif)"
	xmlreqGET("code/search.php?q="+escape(q))	
} 

function xmlreqGET(url) { 
	if (window.XMLHttpRequest) { // Mozilla, etc. 
		xmlHttp=new XMLHttpRequest()
		xmlHttp.onreadystatechange = stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	} else if (window.ActiveXObject) { // IE 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
		if (xmlHttp) { 
		xmlHttp.onreadystatechange = stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send()
		} 
	} 
}

function stateChanged() { 
	if (xmlHttp.readyState == 4) { 
		if (xmlHttp.status == 200 || xmlHttp.status == 304) { 
			var response = xmlHttp.responseText
			document.getElementById(div).innerHTML=response
			if (div=="search_results") {
				document.getElementById("search_button").style.backgroundImage="url(./images/search_btn.gif)"
				document.getElementById("main").innerHTML=document.getElementById("search_results").innerHTML
				document.getElementById("main").scrollTop=0
			} else {
				document.getElementById("listen").style.display='block'
			}
		} 
	} 
} 
