<?xml version="1.0" encoding="utf-8"?>
<tvWindow xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" cornerRadius="7" title="Search Results" width="570" height="375" borderAlpha="1" show="onShow(event)" resize="carousel.reset(0)" creationComplete="doInit()">
<mx:Script>
	<![CDATA[
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	[Bindable]
	public var tvMaster:Master;
	[Bindable]
	private var nextWord:String;
	[Bindable]
	private var previousWord:String;	
	private var curSong:Number = 0;
	  
	  
		[Embed(source="assets/icon_left.png")]
		private const ICON_LEFT:Class;
		
		[Embed(source="assets/icon_right.png")]		
		private const ICON_RIGHT:Class;
		
		[Embed(source="assets/icon_left_disabled.png")]
		private const ICON_LEFT_DISABLED:Class;
		
		[Embed(source="assets/icon_right_disabled.png")]
		private const ICON_RIGHT_DISABLED:Class;	  
	  
	public function addVideo():void {
	  var tmpVid:Object = new Object();
	  
      if (tvMaster.myQueue == null)
        tvMaster.myQueue = new ArrayCollection;
        
      if (tvMaster.myQueue.length == 0)
        tvMaster.tvNetwork.sendData("QUEUE FULL\n");
       
      if (resGrid.selectedIndex >= 0) {
      	tmpVid.video = tvMaster.songList[resGrid.selectedIndex].artist + " - " + tvMaster.songList[resGrid.selectedIndex].title;
      	tmpVid.vid   = tvMaster.songList[resGrid.selectedIndex].vid;
        tvMaster.myQueue.addItem(tmpVid);
        tvMaster.masterObject.chatWindow.htmlText += "<FONT COLOR=\"#0033CC\"><P>[" + new Date().toLocaleTimeString() + "] " + resGrid.selectedItem.artist + " - " + resGrid.selectedItem.title + " Has been added to your queue.</P></FONT>";
        }
        
      focusManager.setFocus(resGrid);
      }
      
	private function onShow(event:FlexEvent):void
		{
			
			if (resGrid.selectedIndex == 0)
				prevVid.setStyle("icon", ICON_LEFT_DISABLED);
			else
				prevVid.setStyle("icon", ICON_LEFT);
			if (resGrid.selectedIndex >= tvMaster.songList.length - 1)
				nextVid.setStyle("icon", ICON_RIGHT_DISABLED); 				
			else 
				nextVid.setStyle("icon", ICON_RIGHT); 			
			carousel.reset(resGrid.selectedIndex);
			Alert.show("On Show?");
		}      
		
    private function doInit():void {
      this.addEventListener(KeyboardEvent.KEY_DOWN,doKey,false,0x0,true);
      resGrid.selectedIndex = 0;
      carousel.reset(0);
      }
      
   private function doKey(event:KeyboardEvent):void {
   	 if (event.keyCode == 27)
   	   dispatchEvent(new FlexEvent("closeWindow"));
     }
    private function showPrev():void {
	  resGrid.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,true,false,0.0,38));		
      }
      
    private function doPrev(iX:Number):void {
	  if (iX > 0) {
		nextVid.setStyle("icon", ICON_RIGHT); 
		//resGrid.selectedIndex--;
		//focusManager.setFocus(resGrid);
		carousel.rotateRight(iX);
				
		if (resGrid.selectedIndex <= 0)
		  prevVid.setStyle("icon", ICON_LEFT_DISABLED);
			} 
		}
      
	private function showNext():void {
	  resGrid.dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,true,false,0.0,40));
	  }
	  
    private function doNext(iX:Number):void {
	  if (iX < tvMaster.songList.length - 1) {
	    prevVid.setStyle("icon", ICON_LEFT);
				
	    if (iX >= tvMaster.songList.length - 1)
            nextVid.setStyle("icon", ICON_RIGHT_DISABLED); 
		} 
	  } /* End showNext() */
	  
	  private function findSong():void {
	  	tvMaster.masterObject.searchBox.text = searchBox.text;
	  	tvMaster.masterObject.findSong();
	  	curSong = 0;
	  }
	  private function doChange():void {
	  	var i:Number;
	  	var x:Number;
	  	var tA:Object;
	  	if (resGrid.selectedIndex < curSong) {
	  	  x = curSong - resGrid.selectedIndex;
	  	  if (x > 1)
	  	    carousel.reset(resGrid.selectedIndex + 1);
	  	    carousel.rotateRight(resGrid.selectedIndex);
	  	  /*
	  	  for (i = 0;i < x;i++) {
	  	  	tA = new Object();
	  	  	tA.dir = 0;
	  	  	tA.end = curSong - i - 1;
	  	  	tvMaster.SSA.push(tA);
	  	    }
	  	    */
	  	  }
	  	else {
	  	  x = resGrid.selectedIndex - curSong;
	  	  if (x > 1)
	  	    carousel.reset(resGrid.selectedIndex - 1);
	  	    carousel.rotateLeft(resGrid.selectedIndex);
	  	    /*
	  	  for (i = 0;i < x;i++) {
	  	  	tA = new Object();
	  	  	tA.dir = 1;
	  	  	tA.end = curSong + i + 1;
	  	  	tvMaster.SSA.push(tA);
	  	    }
	  	  	*/
	  	  }
	  	curSong = resGrid.selectedIndex;
	  	//carousel.animate();
	    }
	]]>
  </mx:Script>
  <mx:VBox width="100%" height="100%">
    <mx:VBox width="100%" height="100%" horizontalAlign="center" horizontalCenter="0"  backgroundColor="#000000" id="mySpot" color="#ffffff" verticalAlign="bottom">
      <Carousel id="carousel" gallery="{resGrid}" tvMaster="{tvMaster}" width="100%" height="100%" verticalCenter="0" horizontalCenter="0" doubleClick="addVideo()" doubleClickEnabled="true" />	
      <mx:HBox>
	    <NavButton icon="{ICON_LEFT}" id="prevVid" toolTip="{previousWord}" click="showPrev()" />			
        <NavButton icon="{ICON_RIGHT}" id="nextVid" toolTip="{nextWord}" click="showNext()" />	
	  </mx:HBox>
  	  <mx:Label id="vidName" text="{resGrid.selectedItem.artist} - {resGrid.selectedItem.title}" width="100%" height="20" styleName="photoName"  textAlign="center" />
    </mx:VBox>
    <mx:HBox horizontalAlign="center" width="100%">
	  <mx:TextInput width="467" enter="findSong()" id="searchBox"/>	
	  <mx:Button label="Search" click="findSong()"  width="69"/>
    </mx:HBox>
    <mx:DataGrid x="5" y="475" width="100%" height="100%" id="resGrid" dataProvider="{tvMaster.songList}" doubleClick="addVideo()" doubleClickEnabled="true" cornerRadius="5" color="#000000" change="doChange()">
      <mx:columns>
        <mx:DataGridColumn headerText="Artist" dataField="artist" width="175" />
	    <mx:DataGridColumn headerText="Title" dataField="title"/>
	    <mx:DataGridColumn headerText="Genre" dataField="genre" width="75"/>
  	    <mx:DataGridColumn headerText="Length" dataField="length" width="60"/>
      </mx:columns>
    </mx:DataGrid>			
  </mx:VBox>
</tvWindow>