// Javascript used for song catalog refinement.
//

window.onload=function()
{
	// check if DOM is available
	if(!document.getElementById || !document.createTextNode){return;}
	// check if there is a "No JavaScript" message
	var nojsmsg=document.getElementById('nojs');
	if(!nojsmsg){return;}

	// If we made it this far, we're javascript capable, so let's hide the 
	// no javascript message and show the Refine button.
	nojsmsg.style.display='none'; 
	document.getElementById('refineButton').style.display='block';

	buildLists(true);
}

function showRefineForm()	{
	document.getElementById('refineForm').style.display='block';			
	document.getElementById('refineButton').style.display='none';
}
function hideRefineForm()	{
	document.getElementById('refineForm').style.display='none';			
	document.getElementById('refineButton').style.display='block';			
}

// more readable indexes into our keyword array
var KW_SELECTED=0;
var KW_WORD=1;
var KW_COUNT=2;

function buildLists(firstTime)	{

	var kwtype=document.getElementById('keyword-search');
	var ulav=document.getElementById('keyword-list');
	var ulsel=document.getElementById('keyword-selected');

	var selCount=0;
	var shownCount=0;

	clearChildren(ulav);
	clearChildren(ulsel);
	
	for (var i=0; i < keywords.length; i++)	{

		// If this is the first time through, set any previously selected items.
		if (("|"+kwString+"|").indexOf("|"+keywords[i][KW_WORD]+"|") != -1 && firstTime)	{
			keywords[i][KW_SELECTED]=1;
		}

		// build the "available" list.
		if (keywords[i][KW_SELECTED]==0)	{
			// we're only going to show at most 10 items that match the characters
			// in the type ahead box.
			if (keywords[i][KW_WORD].indexOf(kwtype.value.toLowerCase())==0
			&& shownCount < 10	)	{	
				shownCount++;
				var li = document.createElement('li');
				li.id="kw-"+i;
				li.onclick=function() {setSelection(this,1)};
				var txt = document.createTextNode(keywords[i][KW_WORD]+" ("+keywords[i][KW_COUNT]+")");
			    li.appendChild(txt);
		    	ulav.appendChild(li);
			}
		} 

		// build the "selected" list.
		else { // keywords[i][KW_SELECTED] !=0
			selCount++;
			var li = document.createElement('li');
			li.id="kw-"+i;
			li.onclick=function() {setSelection(this,0)};
			var txt = document.createTextNode(keywords[i][KW_WORD]+" ("+keywords[i][KW_COUNT]+")");
	    	li.appendChild(txt);
	    	ulsel.appendChild(li);
		}
	}

	// if nothing is selected add one entry to show that.
	if (selCount==0) 	{
		var li = document.createElement('li');
		var txt = document.createTextNode("- No Keyword Filtering -");
    	li.appendChild(txt);
    	ulsel.appendChild(li);
	}
	
	// if we didn't show anything, it may be because an unavailable sequence 
	// was typed into the keyword search box. Remove the last letter and rebuild.
	if (shownCount==0 && kwtype.value.length > 0)	{
		var a=kwtype.value;
		a=a.substring(0,a.length-1);
		kwtype.value=a;
		buildLists(false);
	}

}

function clearChildren(parent)	{
	while (parent.hasChildNodes())	{
		parent.removeChild(parent.childNodes[0]);			
	}
}
		
function setSelection(li,on)	{
	var i=li.id.substring(3);
	keywords[i][KW_SELECTED]=on;
	buildLists(false);
//	rebuildList();		
}

function kwTextAction()	{
	buildLists(false);
}
		
function submitRefine()	{
	//alert('going to submit');
	var sel=document.getElementById('keyword');
	clearChildren(sel);
	var	selCount=0;
	for (var i=0; i < keywords.length; i++)	{
		if (keywords[i][KW_SELECTED]==1)	{
			selCount++;
			var opt = document.createElement('option');
			opt.value = keywords[i][KW_WORD];
			opt.selected="selected";
			var txt = document.createTextNode(keywords[i][KW_WORD]);
	    	opt.appendChild(txt);
	    	sel.appendChild(opt);
		}
	}
	return true;
}
		
function keywordHelp()	{
	alert(
	'Type a few letters in the search box to see what keywords are available. '
	+'(Keywords were gathered from the Title, Genre, Description, Mood, '
	+'Instrumentation, and Lyrics fields of all the songs.)\n\n'
	+'Click on as many keywords as you like from the \"available\" list to add them to '
	+'your \"selected\" list (Clicking on keywords in your \"selected\" list will unselect '
	+'them).\n\nWhen you\'ve got the list of keywords you like, click the \"Refine\" '
	+'button. You will get back all songs with any of those words. '
	);
}


function swallowEnterKey (field, event) {
	var kc = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (kc == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i]) break;
		// wrap around if there is no next field
		var nextfield = (i + 1) % field.form.elements.length;
		field.form.elements[nextfield].focus();
		return false;
	} 
	else
	return true;
}      


