//var playlist_win = null
//var playlistInterval

function launch_m3u(user,ids) {
	if (document.URL.search('&local')==-1) {
		var url = "code/m3u.php?user="+user+"&ids="
	} else {
		var url = "code/m3u.php?user="+user+"&local&ids="
	}
	
	if (ids==undefined) {
		var ids = ""
		var children = document.getElementById("list").getElementsByTagName("tr")
		for(var i = 0;i < children.length;i++) {
			if (children[i].style.textDecoration!="line-through") ids = ids + "T" + children[i].id.substr(4).replace(/_/g,' ') + ","
		}
	}
	
	playlist_win = window.open('http://www.amgtapestry.com/demo/'+url+ids,'playlist','width=0,height=0')	//http://12.27.1.208:85/delphi/
	playlist_win.blur()
	window.focus()

/*
	if (!playlist_win) {
		playlist_win = window.open('http://www.amgtapestry.com/demo/'+url+ids,'playlist','width=0,height=0')
		playlist_win.blur()
		window.focus()
		//playlistInterval = setInterval("launch_m3u()",5000)
	} else {
		clearInterval(playlistInterval)
		playlist_win.close()
		playlist_win = null
	}
*/
}

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 'coll':
		document.getElementById("nav").style.display="block";
		document.getElementById("main").style.height="366px"
		document.getElementById("main_head").style.backgroundImage="url(images/coll_on.gif)"
		//document.getElementById("main").innerHTML='<img src="images/loading_coll.gif" id="load" alt="loading collection..." width="240px" height="97px" />'
		document.getElementById("main").innerHTML=document.getElementById("collection").innerHTML
		break
		case 'desc':
		//document.getElementById("options").style.visibility = 'hidden';
		document.getElementById("nav").style.display="none";
		document.getElementById("main").style.height="auto"
		document.getElementById("main_head").style.backgroundImage="url(images/desc_on.gif)"
		//document.getElementById("main").innerHTML='<img src="images/loading_desc.gif" id="load" alt="loading descriptors..." width="240px" height="97px" />'
		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"
	/*
	if (oObj.style.display == "block") {
		oObj.style.display = "none"
	} else {
		oObj.style.display = "block"
	}
	*/
}

/****DESCRIPTOR TREE LOGIC****/
function descBuild(user) {
	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,user)
}

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
		/*
		if (trueCnt == children.length) {
			document.getElementById(parentId).checked = true
		} else {
			document.getElementById(parentId).checked = false
		}
		*/
	} else {
		document.getElementById(parentId).checked = false
		document.getElementById(parentId+'_title').style.color = "#fff"
	}
}

/*******AJAX*******/
var xmlHttp = false

function doPlaylist(ids,user,playback) { 
	if (ids.length > 0) { 	
		var count = document.forms["option_form"].mix_count.value
		if (count > "9999" || count < "1") {
			alert("Invalid playlist count value")
			return
		}

		document.getElementById("playlist").style.display="block"
		document.getElementById("playlist").innerHTML="<img src=\"images/loading_playlist.gif\" id=\"load\" alt=\"building playlist...\" width=\"240px\" height=\"97px\" />"

		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
			}
		}
		var url="code/playlist.php?ids="+ids+"&user="+user+"&opts="+opts+"&count="+count
		xmlreqGET(url)
	} else { 
		document.getElementById("playlist").innerHTML="No ID"
	} 
} 

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("playlist").innerHTML=xmlHttp.responseText
		} 
	} 
} 

/*
var ajaxId
var xmlreqs = new Array();

function CXMLReq(type, xmlhttp, id) { 
	this.type = type; 
	this.xmlhttp = xmlhttp; 
	this.id = id;
} 

function doTabs(type) { 
	switch(type) {
		case 'coll':
			document.getElementById("nav").style.display="block";
			document.getElementById("main_head").style.backgroundImage="url(images/coll_on.gif)"
			document.getElementById("main").innerHTML="<img src=\"images/loading_coll.gif\" id=\"load\" alt=\"loading tracks...\" width=\"240px\" height=\"97px\" />"
			document.getElementById("main").style.height = "366px"
			var url="code/collection.php"
			break;
		case 'desc':
			document.getElementById("nav").style.display="none";
			document.getElementById("main_head").style.backgroundImage="url(images/desc_on.gif)"
			document.getElementById("main").innerHTML="<img src=\"images/loading_desc.gif\" id=\"load\" alt=\"loading descriptors...\" width=\"240px\" height=\"97px\" />"
			document.getElementById("main").style.height = "auto"
			var url="code/desc.php"
			break;
		default:
			return 0;
			break;
	}
	sendGet(url,"main")	
} 

function doPlaylist(ids) { 
	if (ids.length > 0) { 
		document.getElementById("playlist_head").style.display = "inline"
		document.getElementById("playlist").style.display="block";
		document.getElementById("playlist").innerHTML="<img src=\"images/loading_playlist.gif\" id=\"load\" alt=\"building playlist...\" width=\"240px\" height=\"97px\" />"
		var url="code/playlist.php?ids="+ids
		sendGet(url,"playlist")
	} else { 
		document.getElementById("playlist_head").style.display = "inline"
		document.getElementById("playlist").innerHTML="No ID"
	} 
} 

function stateChanged() { 
	if (typeof(window['xmlreqs']) == "undefined") return; 
	//alert(xmlreqs[0].id)
	//if (xmlreqs[0].xmlhttp.readyState!=3) alert(xmlreqs[0].xmlhttp.readyState)
	for (var i=0; i < xmlreqs.length; i++) { 
		if (xmlreqs[i].xmlhttp.readyState == 4) { 
			if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
			//if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) { 
				var response = xmlreqs[i].xmlhttp.responseText
				var id = xmlreqs[i].id;
				xmlreqs.splice(i,1); 
				i--; 
				document.getElementById(id).innerHTML=response
			} else {
				xmlreqs.splice(i,1);
				i--;
			} 
		} 
	}
} 

function sendGet(url,id) {
	var xmlhttp=false;
	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(); 
		} 
	}
	var xmlreq = new CXMLReq('', xmlhttp, id);
	xmlreqs.push(xmlreq);
} 
*/





/*
var xmlreqs = new Array(); 

function CXMLReq(type, xmlhttp) { 
	this.type = type; 
	this.xmlhttp = xmlhttp; 
} 

function xmlreqGET(url) { 
	document.getElementById("nav").style.display="block";
	document.getElementById("main_head").style.backgroundImage="url(images/coll_on.gif)"
	document.getElementById("main").innerHTML="<img src=\"images/loading_coll.gif\" id=\"load\" alt=\"loading tracks...\" width=\"240px\" height=\"97px\" />"
	document.getElementById("main").style.height = "366px"
	var url="code/collection.php"

	var xmlhttp=false; 
	if (window.XMLHttpRequest) { // Mozilla, etc. 
		xmlhttp=new XMLHttpRequest(); 
		xmlhttp.onreadystatechange = xmlhttpChange; 
		xmlhttp.open("GET",url,true); 
		xmlhttp.send(null); 
	} else if (window.ActiveXObject) { // IE 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		if (xmlhttp) { xmlhttp.onreadystatechange = xmlhttpChange; 
			xmlhttp.open("GET",url,true); 
			xmlhttp.send(); 
		} 
	} 
	var xmlreq = new CXMLReq('', xmlhttp); 
	xmlreqs.push(xmlreq); 
} 

function xmlreqPOST(url,data) { 
	var xmlhttp=false; 
	if (window.XMLHttpRequest) { // Mozilla etc. 
		xmlhttp=new XMLHttpRequest(); 
		xmlhttp.onreadystatechange=xmlhttpChange; 
		xmlhttp.open("POST",url,true); 
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlhttp.send(data); 
	} else if (window.ActiveXObject) { // IE 
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		if (xmlhttp) { 
			xmlhttp.onreadystatechange=xmlhttpChange; 
			xmlhttp.open("POST",url,true); 
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			xmlhttp.send(data); 
		} 
	} 
	var xmlreq = new CXMLReq('', xmlhttp); 
	xmlreqs.push(xmlreq); 
} 

function xmlhttpChange() { 
	if (typeof(window['xmlreqs']) == "undefined") return; 
	for (var i=0; i < xmlreqs.length; i++) { 
		if (xmlreqs[i].xmlhttp.readyState == 4) { 
			if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) { // 200 OK // get response info here before splicing - see below on creating an xml object 
				xmlreqs.splice(i,1); i--;
				alert("test");
			} else { // error 
				xmlreqs.splice(i,1); i--; 
			} 
		} 
	} 
}


*/



























/*
var ajaxId
var xmlreqs = new Array();

function CXMLReq(type, xmlhttp, id) { 
	this.type = type; 
	this.xmlhttp = xmlhttp; 
	this.id = id;
} 

function doTabs(type) { 
	switch(type) {
		case 'coll':
			document.getElementById("nav").style.display="block";
			document.getElementById("main_head").style.backgroundImage="url(images/coll_on.gif)"
			document.getElementById("main").innerHTML="<img src=\"images/loading_coll.gif\" id=\"load\" alt=\"loading tracks...\" width=\"240px\" height=\"97px\" />"
			document.getElementById("main").style.height = "366px"
			var url="code/collection.php"
			break;
		case 'desc':
			document.getElementById("nav").style.display="none";
			document.getElementById("main_head").style.backgroundImage="url(images/desc_on.gif)"
			document.getElementById("main").innerHTML="<img src=\"images/loading_desc.gif\" id=\"load\" alt=\"loading descriptors...\" width=\"240px\" height=\"97px\" />"
			document.getElementById("main").style.height = "auto"
			var url="code/desc.php"
			break;
		default:
			return 0;
			break;
	}
	sendGet(url,"main")	
} 

function doPlaylist(ids) { 
	if (ids.length > 0) { 
		document.getElementById("playlist_head").style.display = "inline"
		document.getElementById("playlist").style.display="block";
		document.getElementById("playlist").innerHTML="<img src=\"images/loading_playlist.gif\" id=\"load\" alt=\"building playlist...\" width=\"240px\" height=\"97px\" />"
		var url="code/playlist.php?ids="+ids
		sendGet(url,"playlist")
	} else { 
		document.getElementById("playlist_head").style.display = "inline"
		document.getElementById("playlist").innerHTML="No ID"
	} 
} 

function stateChanged() { 
	if (typeof(window['xmlreqs']) == "undefined") return; 
	//alert(xmlreqs[0].id)
	//if (xmlreqs[0].xmlhttp.readyState!=3) alert(xmlreqs[0].xmlhttp.readyState)
	for (var i=0; i < xmlreqs.length; i++) { 
		if (xmlreqs[i].xmlhttp.readyState == 4) { 
			if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
			//if (xmlreqs[i].xmlhttp.status == 200 || xmlreqs[i].xmlhttp.status == 304) { 
				var response = xmlreqs[i].xmlhttp.responseText
				var id = xmlreqs[i].id;
				xmlreqs.splice(i,1); 
				i--; 
				document.getElementById(id).innerHTML=response
			} else {
				xmlreqs.splice(i,1);
				i--;
			} 
		} 
	}
} 

function sendGet(url,id) {
	var xmlhttp=false;
	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(); 
		} 
	}
	var xmlreq = new CXMLReq('', xmlhttp, id);
	xmlreqs.push(xmlreq);
} 
*/
