// Set up classes
new QList(" ");
QList.prototype.ResetFocus = QList_ResetFocus;
QList.prototype.GetItemCount = QList_GetItemCount;
QList.prototype.AddItem = QList_AddItem;
QList.prototype.RemoveItem = QList_RemoveItem;
QList.prototype.IsSameItem = QList_IsSameItem;
QList.prototype.RemoveAllItems = QList_RemoveAllItems;
QList.prototype.ProcessItems = QList_ProcessItems;
QList.prototype.NextItem = QList_NextItem;
QList.prototype.PreviousItem = QList_PreviousItem;
QList.prototype.PlayNextItem = QList_PlayNextItem;
QList.prototype.PlayPreviousItem = QList_PlayPreviousItem;
QList.prototype.RemoveSelection = QList_RemoveSelection;
QList.prototype.UpdateSelection = QList_UpdateSelection;
QList.prototype.SelectItem = QList_SelectItem;
QList.prototype.GetItemPlayingName = QList_GetItemPlayingName;	
QList.prototype.GetCurrentItemId = QList_GetCurrentItemId;	
QList.prototype.GetCurrentItemIndex = QList_GetCurrentItemIndex;	
QList.prototype.ClearItem = QList_ClearItem;
QList.prototype.PlayItem = QList_PlayItem;
QList.prototype.FocusCurrentItem = QList_FocusCurrentItem;

QList.prototype.GetSelectIndex = QList_GetSelectIndex;
QList.prototype.GetPlayIndex = QList_GetPlayIndex;


var q = null;

	
var isIE = (navigator.appName.indexOf("Microsoft") != -1);

										  


// Queue List JavaScript 
function QList(id,elementType,className)
{
	this.id=id; 
	this.className = className;
	this.elementType = elementType;
	this.listObj = axs.id(id);


	// Used to provide identifier 
	this.uniqueId = 0;

	this.playObj = null;
	this.playIndex = -1;  // Used to hold if of element playing
	this.selectObj = null;
	this.selectIndex=-1;
	
	this.itemCount = 0;
	this.items = new Array();
}


function QList_GetItemCount()
{
	//out("ITEM COUNT: " + this.items.length)
	this.itemCount = this.items.length;
	return (this.items.length);
}

function QList_ResetFocus()
{
	if(this.itemCount > 0)
	{
	this.selectIndex=0;
	this.UpdateSelection();
	}
}

function QList_GetPlayIndex()
{

	return this.playIndex;
}

function QList_GetSelectIndex()
{
	return this.selectIndex;
}

function QList_AddItem(name,trackUrl)
{
	this.uniqueId++;
	var t = new Track(name,trackUrl,this.className,this.elementType+this.uniqueId);
	var mo = t.GetItem();
/*   // TRIED TO ADD DRAGON SUPPORT
	var itemSpan = document.createElement("a");
	itemSpan.className="linkItem";
//	itemSpan.setAttribute("id",this.uniqueuId);
	itemSpan.setAttribute("title",name);
	itemSpan.setAttribute("onclick","test()");
	itemSpan.setAttribute("href","http://mozilla.org");
	itemSpan.appendChild(mo);
	this.listObj.appendChild(itemSpan);
	*/
	
	this.listObj.appendChild(mo);
	this.items[this.itemCount]=t;
	this.itemCount++;
	// First new item will be tab focusable
	if(this.itemCount==1)
	{
		this.selectIndex=0;

		this.UpdateSelection();
	}
	// TODO Added this in so only this handler added for queue items
	if (this.elementType=="qi")
	{
		qId=this.elementType+this.uniqueId;
		$("#"+qId).hover(qMouseOver,qMouseOut);

		$("#"+qId).click(function(eventObj){
								  queueCommands.PlayItem();
								  });
	}


	return this.elementType+this.uniqueId;;
//	return t.iid;

}

function QList_UpdateSelection()
{
	//out("The selected index is: " + this.selectIndex)
	if(this.selectIndex==-1)
	{
		return;	
	}

	this.selectObj = this.items[this.selectIndex];
	this.items[this.selectIndex].SelectItem();
	
}


function QList_ProcessItems()
{
	var saveString="";
	for (var i=0; i<q.GetItemCount(); i++){
		saveString = saveString + q.items[i].name+"***"+q.items[i].url+"!!!";
	}
	out("SAVE STRING IS: " + saveString);
	return saveString;
}
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

function QList_RemoveItem()
{
	//out("SELECT INDEX: " + this.selectIndex);
	var itemCount = this.GetItemCount();
		if(itemCount==1)
		{
			this.selectIndex=0;	
			return;
		}
		else
		{
			deleteItemObj = axs.id(this.items[this.selectIndex].id);
			this.items.remove(this.selectIndex);
			this.listObj.removeChild(deleteItemObj);
		}
		if(this.selectIndex==itemCount-1)
		{
			this.selectIndex--;	
		}
		this.UpdateSelection();
}

function QList_PreviousItem()
{
	if(this.selectIndex>0)
	{
		this.RemoveSelection();
		this.selectIndex--;
		this.UpdateSelection();
		return true;
	}
	
	return false;
}

function QList_NextItem()
{
	if(this.selectIndex < (this.GetItemCount() - 1))
	{
		this.RemoveSelection();
		this.selectIndex++;
		this.UpdateSelection();
		return true;
	}
	
	return false;
}


function QList_GetItemPlayingName()
{
	
	return this.items[this.playIndex].name;	
}

function QList_GetCurrentItemId()
{
	//console.log("CurrentId: "+this.selectIndex);
	return this.items[this.selectIndex].id;	
}


function QList_GetCurrentItemIndex(objId)
{
	for(var i=0;i<this.GetItemCount();i++)
	{
		if (this.items[i].id == objId)	
		{
			return i;	
		}
	}
	return -1;
}

function QList_IsSameItem()
{
	if(this.playIndex == this.selectIndex)
	{
		return true;	
	}	
}
function QList_PlayNextItem()
{
	
}

function QList_PlayPreviousItem()
{
}
function QList_FocusCurrentItem()
{
//	console.log("The focused index is: " + this.selectIndex)
	this.items[this.selectIndex].FocusItem();
}

function QList_PlayItem(amtChange)
{
	userControlled=true;
	if(this.playObj)
	{
		this.playObj.ClearPlayItem();	
	}
	if(IsPlaying)
	{
	
		stop();	
	}



		if(amtChange)
		{
		this.playIndex=this.playIndex+amtChange;
		}
		else
		{
		this.playIndex=this.selectIndex;	
		}
		if(this.playIndex != -1)
		{
		this.playObj = this.items[this.playIndex];
		//out("SIndex: "+this.selectIndex);
		//out("PIndex: "+this.playIndex);
		this.playObj.PlayItem();	
		}
		this.selectIndex = this.playIndex;
		this.UpdateSelection();
}

function QList_ClearItem(i)
{	
	this.RemoveSelection();
	
	/*
	this.selectObj = axs.id(i);
	this.selectObj.setAttribute("tabindex","-1");
			if(UseProperties)
		{
	this.selectObj.setAttribute("aria-selected","false");
		}
		*/
}

function QList_RemoveSelection()
{
	if(this.selectIndex>-1)
	{
		this.items[this.selectIndex].UnselectItem();
	}
}

// Code sample from http://oranlooney.com/functional-javascript/
function clone(obj) {
    // A clone of an object is an empty object 
            // with a prototype reference to the original.

    // a private constructor, used only by this one clone.
            function Clone() { } 
    Clone.prototype = obj;
    return new Clone();
}

function QList_SelectItem(i)
{
	out("Select This Index: "+i);
	// Clear last selection
	this.RemoveSelection();
//	out("COMPARE ITEM INDEX: "+ this.GetCurrentItemIndex(i));
	this.selectIndex=this.GetCurrentItemIndex(i);
	this.UpdateSelection();
}

function QList_RemoveAllItems()
{
	while(this.listObj.hasChildNodes())
	{
		this.listObj.removeChild(this.listObj.lastChild);
	}
	this.itemCount=0;
}



function scrollItemOld(i)
{

/*
	//For visibility
	if (selectId >= (8*scrollId))
	{
		scrollId++;
		itemObj.scrollIntoView(false);
	}
	if (scrollId>1)
	{
		if((7*scrollId%selectId)==0)
		{
//			scrollId--;
//			itemObj.scrollIntoView(true);
		}
	}
	*/
}


function qMouseOver(eventObj)
{
	
	//	out("MouseOver Q Item" + eventObj)
		q.SelectItem(this.getAttribute("id"));
		var spanA = this.firstChild;
//		spanA = spanA.nextSibling.nextSibling;
		spanA.className = "deleteIcon";



	//	$("#"+this.getAttribute("id")+" span[className=playColumn]").addClass('deleteIcon');	
		
		$(".deleteIcon").click(function(event){
//		$("#"+qId+":nth-child(1)").click(function(event){																					   
		out("delete column clicked");
	   event.preventDefault();
	   event.stopPropagation();

		window.setTimeout(function(){q.RemoveItem();},MEDIUMSPEED);
	   $(this).fadeOut;
	   cName = this.className;
	   out(cName);


		return false;

		 });

}


function qMouseOut(eventObj)
{
		//q.ClearItem(this.getAttribute("id"));

		
//		spanA = spanA.nextSibling.nextSibling;
//		spanA.className = "";
		$("#"+this.getAttribute("id") + " span[className=deleteIcon]").unbind("click");
//		$("#"+this.getAttribute("id") + " span[className=playColumn deleteIcon]").removeClass('deleteIcon');	
		var spanA = this.firstChild;
		spanA.className="";
}


// Processing functions
function Random()
{
	var rNum = Math.random();
	return rNum;
}

function Shuffle()
{
	var i=0;
	var queueList = axs.id("listQueue");
	var queueLength = queueList.length-1;

	for(i=0;i<queueLength;i++)
	{
		var oldOption = queueList.options[i];
		var oldText = oldOption.text;
		var oldValue = oldOption.value;
		var newIndex = Math.round(Random()*queueLength);
		var swapOption = queueList.options[newIndex];
		if(newIndex!=i)
		{
		queueList.options[i] = new Option(swapOption.text, swapOption.value);
		queueList.options[newIndex].value = oldValue;
		queueList.options[newIndex].text = oldText;
		}
	}
}

