VideoManager = function(){
	var currentVideoListMode = "";
	var currentSearchCriteria = "";
	var currentSearchFields = "";
	var currentSortFields = "";
	var currentNodeId = "";
	var currentPage = 1;
	
	return {
		init : function(){	
			//Load node 2 ie "Formula 1" by default
			VideoManager.updateVideoList(2,1,'');			 
		},

		setSearchFields : function(fields){	
			this.currentSearchFields = fields;
		},
		
		setSortFields : function (fields){
			this.currentSortFields = fields;
		},

		//Edit a video in a popup dialog
		editVideo : function(videoid){
			//link = 'videoadmin/edit/id/' + videoid;
			//Application.showDialog('Edit Video', link);			
			VideoEdit.showDialog(videoid);
			urchinTracker('/video/edit');
		},
		
		excludeVideo : function(videoid){
			link = Application.baseUrl + 'videoadmin/exclude/videoid/' + videoid;
			//Application.showDialog('Exclude Video', link);			
			var mgr = new Ext.UpdateManager('message-details');				
					mgr.update({
						 url: link,
						 callback: function(el,res) {
							Message.msg('Video','Excluded Successfully');			
				   			urchinTracker('/video/exclude');
						 },
						 scope: this, //(optional scope) 
						 discardUrl: false, 
						 nocache: true,
						 timeout: 30,
						 scripts: false
						 });
		},
				
		queueVideo : function(videoid){
			link = Application.baseUrl + 'videoadmin/queue/videoid/' + videoid;
			var mgr = new Ext.UpdateManager('message-details');				
					mgr.update({
						 url: link,
						 callback: function(el,res) {
								Message.msg('Video','Queued Successfully');			
								urchinTracker('/video/queued');
						 },
						 scope: this, //(optional scope) 
						 discardUrl: false, 
						 nocache: true,
						 timeout: 30,
						 scripts: false
						 });
		},
		
	
		showComments : function (videoid) {
			Application.loadInTab('Comments -', 'http://formulatube.com/videos/comments/videoid/'+ videoid);
   			urchinTracker('/video/showComments');
		},
		
		addVideo : function(){			
			Ext.MessageBox.prompt('Add Video', 'Please enter video URL', function(btn, text){
				if (btn == 'ok') {
					if (text != ""){
						link = 'videos/add?url=' + encodeURI(text);
						Application.showDialog('Add Video', link);	
					} else {
						Ext.MessageBox.alert('Add Video', 'You must enter a URL to add a video');
					}
				}
			});    								
  			urchinTracker('/video/add');
		},
		
		addVideoByTag : function(){			
			Ext.MessageBox.prompt('Add Video', 'Please enter tag that will searched on', this.loadVideoByTag);    								
  			urchinTracker('/video/addByTag');			
		},
		
		suggestVideo : function(){			
			Ext.MessageBox.prompt('Suggest Video', 'Please enter video URL', this.videoSuggested);    								
  			urchinTracker('/video/suggest');
		},
		updateList : function() {
			//set the current video list information
			if (this.currentVideoListMode == "category") {
				this.updateVideoList(this.currentNodeId, this.currentPage);
			} else if (this.currentVideoListMode == "search") {
				this.updateVideoListFromSearch(this.currentSearchString, this.currentPage);
			}
						
			
		},
		
		
		
		updateVideoList : function(nodeid, page) {
			//set the current video list information
			this.currentVideoListMode = "category";
			this.currentNodeId = nodeid;
			this.currentPage = page;
	        var mgr = new Ext.UpdateManager('videos-wrapper');				
			mgr.update({
				 url: 'videos/getListByNode',
				 params: {node: nodeid,
				 		  sortfields: this.currentSortFields,
				 		  page: page}, // or a URL encoded string
				 callback: this.videoListLoaded,
				 scope: this, //(optional scope) 
				 discardUrl: false, 
				 nocache: false,
				 text: 'Loading...',
				 timeout: 30,
				 scripts: true
				 });
		},
		
		updateVideoListFromSearch : function(searchstring, page) {
			this.currentVideoListMode = "search";
	        this.currentSearchString = searchstring;
	        this.currentPage = page;
	        var mgr = new Ext.UpdateManager('videos-wrapper');				
			mgr.update({
				 url: 'videos/getListFromSearch',
				 params: {searchstring: searchstring,
				 		  fields : this.currentSearchFields,
				 		  sortfields: this.currentSortFields,
				 		  page: page}, // or a URL encoded string
				 callback: this.videoListLoaded,						 
				 scope: this, //(optional scope) 
				 discardUrl: false, 
				 nocache: false,
				 text: 'Searching...',
				 timeout: 30,
				 scripts: false
				 });
		},
		
		videoListLoaded : function(){
			Ratings.init();
		},
								
		
		loadVideoByTag : function(btn, text){
			if (btn == 'ok') {
				if (text != ""){
					link = 'videos/addByTag?tag=' + encodeURI(text);
					Application.showDialog('Add Video', link);	
				} else {
					Ext.MessageBox.alert('Add Video', 'You must enter a TAG to add a video');
				}
			}
		},
		
		videoSuggested : function(btn, text){
			if (btn == 'ok') {
				if (text != ""){
					link = 'videos/suggest?url=' + encodeURI(text);
					Ext.Ajax.request({
						url : link,
						success: function() {
							Ext.MessageBox.alert('Thanks', 'The video has been added for inclusion on the site.');
							//Trigger another request to run in background to force update of suggested videos
							Ext.Ajax.request({url: 'videos/addSuggestedVideos'});
						},
						failure: function() {
							Ext.MessageBox.alert('Thanks', 'Unfortunatley there was an error adding your suggestion.');
						}							
					});
					
				} else {
					Ext.MessageBox.alert('Add Video', 'You must enter a URL to add a video');
				}
			}
		}					
								
		
	}
}();
Ext.EventManager.onDocumentReady(VideoManager.init, VideoManager, true);