/**
 * Keyboard Command Section
 */
axs.debug=1;

// Application commands
var appCommands={
	//Initialize things after the page is fully loaded
	init:function()
	{	
		eObj=axs.id("editArtist");
		axs.keyreg("control+m",appCommands.ToggleMute,{des:"Toggle players audio on / off"});
		axs.keyreg("control+space",appCommands.PlayQueue,{des:"Play"});
		axs.keyreg("control+right",appCommands.PlayNextQueue,{des:"Move to next queue item"});
		axs.keyreg("control+left",appCommands.PlayPreviousQueue,{des:"Move to previous queue item"});
		axs.keyreg("control+s",appCommands.SaveQueue,{des:"Save the current queue"});
		axs.keyreg("enter",appCommands.SearchArtist,{node:eObj,des:"Searches For Currently Typed Artist"});
	},
	// Toggle the mute on and off for the playing video
	ToggleMute:function(){
		ToggleVolume();
	},
	// Generate test data to fill out tracks in queue
	GenerateTestData:function(){
		appCommands.AddTrack("Extraordinary","RbAVmj8E3sM");
		appCommands.AddTrack("Never Said","XnxN27NX-Qw");
		appCommands.AddTrack("Floor","fUr0nVbBxEw");
		appCommands.AddTrack("One Pure Thought","eAev1ZjE3dI");
	},
	// Save queue to server
	SaveQueue:function(){
//		dialogAlert('Save Queue to Youtube', 'You have worked hard on your queue.  Lets save it forever on youtube');
		processQueue();
	},
	// Move to next item in queue
	PlayNextQueue:function(){
		if((q.GetPlayIndex()) != (q.GetItemCount()-1) )
		{
			userControlled=true;
			changeSong(1);
		}
		else
		{
			updateStatusAlert("At end of queue");
			return;	
		}
	},
	// Move to previous item in queue
	PlayPreviousQueue:function()
	{
		if(q.GetPlayIndex() != 0)
		{
			userControlled=true;
			changeSong(-1);
		}
		else
		{
			updateStatusAlert("At start of queue");
			return;	
		}
	},
	// Play currently focused item in queue
	PlayQueue:function(){

			if(q.IsSameItem()==true)
			{
				if(getPlayerState()==1)
				{
					pause();	
					return;
				}
				if(getPlayerState()==2)
				{
					play();
					return;
				}
			}
		q.PlayItem();
	},
	// Search for songs based on artist name
	SearchArtist:function()
	{
	 	var artistObj = document.getElementById("editArtist");
		var artistName = artistObj.value;
		ytvb.listVideos("all",artistName);
	},
	// TODO Unnecessary?
	FocusTrack:function()
	{

		var focusObj = axs.id(tracks.GetCurrentItemId());
		out("FocusItem called:");
		out(focusObj);
		if(focusObj)
		{
			//focusObj.setAttribute("tabindex","0");
			window.setTimeout(function () {focusObj.focus();},0);  // focusItem must be in scope
		}
	},
	// Add Track to list
	AddTrack:function(songName,id)
	{
		var text = SanitizeText(songName);
		/*
		var itemSpan = document.createElement("a");
		itemSpan.className="linkItem";
		itemSpan.setAttribute("id",id);
		itemSpan.setAttribute("title",songName);
		itemSpan.setAttribute("onclick","test()");
		itemSpan.setAttribute("href","http://mozilla.org");
		textNode = document.createTextNode(text);
		itemSpan.appendChild(textNode);
		footerDiv.appendChild(itemSpan);
		*/
		
		
		

		var group = id.split("/");
		var id = group[group.length-1];  // used to just get URL
		itemId = tracks.AddItem(text,id);
		
		// TODO Important Section to work on
	   $("#"+itemId).click(function(eventObj) {
		appCommands.AddQueue(this.getAttribute("id"),this.getAttribute("title"),this.getAttribute("url"));
		   });
	   
	   $("#"+itemId).mouseover(function(eventObj) {
		tracks.SelectItem(this.getAttribute("id"));
		var spanA = this.firstChild;
		spanA.className = "addIcon";
		   });
	   
	   $("#"+itemId).mouseout(function(eventObj) {
	//	tracks.ClearItem(this.getAttribute("id"));
		var spanC = this.firstChild;
		spanC.className = "";
		   }); 
	},
	// Add Item to Queue
	AddQueue:function(qId,qTitle,qUrl)
	{
		$("#"+qId).fadeOut();	
		//out(qId + " is qid");
		qId = q.AddItem(qTitle,qUrl);
		

		window.setTimeout(function(){tracks.RemoveItem();},MEDIUMSPEED);
		queueCommands.ReviewItems();
	
	
	/*

		*/		




		

		


	}
}


// Toolbar Commands
var toolbarCommands={
	//Initialize commands after the page is fully loaded
	init:function(){		
		tObj=axs.id("customToolbar");
		axs.keyreg("left",toolbarCommands.PreviousButton,{node:tObj,des:"Move to previous button"});
		axs.keyreg("right",toolbarCommands.NextButton,{node:tObj,des:"Move to next button"});
		axs.keyreg("enter",toolbarCommands.Execute,{node:tObj,des:"Executes action"});
		axs.keyreg("space",toolbarCommands.Execute,{node:tObj,des:"Executes action"});
	},
	//Move to previous enabled toolbar button
	PreviousButton:function(){
		  buttonid = getPrevButtonID();  // This is an author defined function
		  if(UseStates)
		  {
		  tObj.setAttribute("aria-activedescendant", buttonid); 	
		  }
	},
	//Move to next enabled toolbar button
	NextButton:function(){
		buttonid = getNextButtonID();   // This is an author defined function
		if(UseStates)
		{
		tObj.setAttribute("aria-activedescendant", buttonid); 
		}
	},
	//Calls through to button's onclick handler
	Execute:function(){
		ExecuteButtonAction(getCurrentButtonID());
	}
}	




var trackCommands= 
{
	init:function()
	{
		lObj=axs.id("listTracks");
		axs.keyreg("up",trackCommands.PreviousTrack,{node:lObj,des:"Move to previous track"});
		axs.keyreg("down",trackCommands.NextTrack,{node:lObj,des:"Move to next track"});		
		axs.keyreg("right",trackCommands.MoveTrack,{node:lObj,des:"Move track into queue"});				
	},
	//Move to previous track
	PreviousTrack:function()
	{
		if (tracks.PreviousItem())
		{

			appCommands.FocusTrack();
		//	tracks.FocusCurrentItem();
		}
		else
		{
			
		// TODO UPDATE STATUS	
		}
	},
	//Move to next track
	NextTrack:function()
	{
		if (tracks.NextItem())
		{
//			console.log("calling track next item");
			appCommands.FocusTrack();
			
		}
		else
		{
				//TODO UPDATE STATUS
		}
	},
	MoveTrack:function()
	{
		track = axs.id(tracks.GetCurrentItemId());
		
		appCommands.AddQueue(track.getAttribute("id"), track.getAttribute("title"), track.getAttribute("url"));
		window.setTimeout(function(){appCommands.FocusTrack();},350);

	}
}

// AXS Keyboard Section
var queueCommands=
{
	//Initialize commands after the page is fully loaded
	init:function()
	{
		qObj = axs.id("listQueue");
		axs.keyreg("up",queueCommands.PreviousItem,{args:[""],node:qObj,des:"Move to previous button"});
		axs.keyreg("down",queueCommands.NextItem,{args:[""],node:qObj,des:"Move to next button"});
		axs.keyreg("enter",queueCommands.PlayItem,{args:[""],node:qObj,des:"Executes current action"});
		axs.keyreg("space",queueCommands.PlayItem,{args:[""],node:qObj,des:"Executes current action"});
		axs.keyreg("delete",queueCommands.DeleteItem,{args:[""],node:qObj,des:"Executes current action"});

	},
	ReviewItems:function()
	{
	   if(q.GetItemCount()>=1)
	   {
		   	enableToolbar();	
		}
		else
		{
			disableUI();	
		}
		if(q.GetSelectIndex()>0)
		{
			enablePreviousButton();	
		}
		else
		{
			disablePreviousButton();	
		}
		if(q.GetSelectIndex()==(q.GetItemCount()-1))
		{
			disableNextButton();											
		}
	},
	//Move to previous song in queue
	PreviousItem:function()
	{
		if(		q.PreviousItem()==false)
		{
			updateStatusAlert("At start of queue");			
			return;
		}

		queueCommands.FocusItem();
		queueCommands.ReviewItems();
	},
	//Move to next song in queue
	NextItem:function()
	{
		if(q.NextItem()==false)
		{
			updateStatusAlert("At end of queue");
			return;
		}
		var focusObj = axs.id(q.GetCurrentItemId());
		out("FocusItem called:");
//		console.log(focusObj);
		if(focusObj)
		{
			//focusObj.setAttribute("tabindex","0");
			window.setTimeout(function () {focusObj.focus();},0);  // focusItem must be in scope
		}
//		queueCommands.FocusItem();
		queueCommands.ReviewItems();	
	},
	FocusItem:function()
	{
		var focusObj = axs.id(q.GetCurrentItemId());
		if(UseKeyboard)
		{
		focusObj.setAttribute("tabindex","0");
		if(focusObj)
		{
			window.setTimeout(function () {focusObj.focus();},0);  // focusItem must be in scope
		}
		}
	},
	//Calls through to button's onclick handler
	PlayItem:function()
	{
		//Specify that user caused this action to occur
		userControlled = true;
		appCommands.PlayQueue();
		queueCommands.ReviewItems();
	},
	DeleteItem:function()
	{
		q.RemoveItem();	
		queueCommands.FocusItem();
		queueCommands.ReviewItems();
	}
}	

//TODO Put back once demo done.  

//When the window fires an onload event, call the function init()
axs.ae(window,'load',appCommands.init);
axs.ae(window,'load',toolbarCommands.init);
//Once window has loaded fire up list keyboard commands
axs.ae(window,'load',trackCommands.init);
axs.ae(window,'load',queueCommands.init);



function printKey(e)
{
//	console.log(e);
	if (e.charCode==32)
	{
		alert("space");	
	}
}