/*
 * The global OE object that will be used for all common functionality inside
 * the OE product line, 
 * 
 * @author DRS
 * @package OE
 * @version 1.0
 */

var OE = {
	
	/*
	 * The rotator object allows for a flexible panel with article content as well as mixed
	 * content such as audio and video in one playlist. All audio/video content is played
	 * via a FlowPlayer instance passed in at runtime.
	 * 
	 * @author DRS
	 * @package OE
	 * @version 1.0
	 */
		
	Rotator : {
		scrollableOrientation : 'bottom', //possible choices are top, bottom, left, right
		scrollablesPerPage : 4,
		currentIndex : 0,
		currentPage : 1,
		currentType : null,
		waitTime : 20000,
		waitingInstance : null,
		waitingVideoInstance : null,
		state : 'playing',
		items : [],
		flowplayerInstance : null,
		scrollableInstance : null,
		selectors : {
			player : '#videoPlayer_shell',
			videoContent : '#videoContent',
			article : '#articlePlayer_shell',
			articleContainer : '#article_',
			videoContainer : "#video_",
			playlist_items : 'div.entries > a'
		},
		setScrollableOrientation : function(val){
			this.scrollableOrientation = val;
		},
		setCurrentIndex : function(idx){
			this.currentIndex = idx;
		},
		addItem : function(item){
			OE.Rotator.items.push(item);
		},
		addItems : function(items){
			jQuery.each(items, function(i, item){
				OE.Rotator.addItem(item);
			});
		},
		getItem : function(idx){
			Log.info("OE.Rotator.getItem() called with index of "+idx);
			if(OE.Rotator.items.length >= idx){
				Log.info("OE.Rotator.getItem() OK - found item in list.");
				Log.info(OE.Rotator.items[idx]);
				return OE.Rotator.items[idx];
			}
			Log.warn("Unable to find item in index, returning null");
			return null;
		},
		getItems : function(){
			return OE.Rotator.items;
		},
		getVideoPath : function(file, show_directory){
			var path = "/media/video/"+show_directory+"/videos/"+file;
			Log.info("OE.Rotator.getVideoPath() called with file of "+file+", returning path of "+path);
			return path;
		},
		removePlaylistClicks: function(){
			items = jQuery(OE.Rotator.selectors.playlist_items).click(function(){
				OE.Rotator.stopCycle();
				return OE.Rotator.playItem(items.index(this));
			});
		},
		playItem : function(idx){
			var item = OE.Rotator.getItem(idx);
			
			jQuery(OE.Rotator.selectors.playlist_items).removeClass("current");
			jQuery(OE.Rotator.selectors.playlist_items+":eq("+idx+")").addClass("current");
			if(item.type == "article"){
				Log.info("OE.Rotator playItem() item type detected as article, hiding player control container.");
				
				OE.Rotator.stopVideo();
				jQuery(OE.Rotator.selectors.player).hide();
				var html = jQuery(OE.Rotator.selectors.articleContainer+item.image_id).html();
				jQuery(OE.Rotator.selectors.article).html(html).show();
			
			}else if(item.type == "video"){
				Log.info("OE.Rotator playItem() item type detected as video, hiding/clearing article control container...");
				
				jQuery(OE.Rotator.selectors.article).html('').hide();
				var html = jQuery(OE.Rotator.selectors.videoContainer+item.video_id).html();
				jQuery(OE.Rotator.selectors.videoContent).html(html).show();
				jQuery(OE.Rotator.selectors.player).show();
				
				Log.info("OE.Rotator playItem() showing player selector, setting timeouts for playVideo()");
				var fn = "OE.Rotator.playVideo('"+item.file+"', '"+item.show_directory+"');";
				Log.info("OE.Rotator playItem() setting timeout for: "+fn);
				OE.Rotator.waitingVideoInstance = setTimeout(fn, 400);
				
			}else{
				Log.error("OE.Rotator playItem() item type NOT detected, type is "+item.type);
			}
			return false;
		},
		stopVideo : function(){
			try{
				var state = OE.Rotator.flowplayerInstance.getState();
				/**
				 * The possible values for this are:
				 * 
				 * -1 = unloaded
				 *  0 = loaded
				 *  1 = unstarted
				 *  2 = buffering
				 *  3 = playing
				 *  4 = paused
				 *  5 = ended
				 */
				if( state == 2 || state == 3 || state == 4 ){
					Log.info("OE.Rotator.stopVideo detected video playing, attempting to stop it. { state: "+state+" }");
					OE.Rotator.flowplayerInstance.stop();
				}
			}
			catch(e){
				Log.error(e);
			}			
		},
		playVideo : function(vid, show){
			try{
				if(OE.Rotator.flowplayerInstance.isLoaded()){
					
					Log.info("OE.Rotator.playVideo() flow player instance appears to be loaded.");
					var vid = OE.Rotator.getVideoPath(vid, show);
					Log.info("OE.Rotator.playVideo() attempting to play "+vid);
					//before we actually start the video, hide its shell container.
					jQuery(OE.Rotator.selectors.videoContent).hide();
					OE.Rotator.flowplayerInstance.play(vid);
					//clear out our waiting times, if we have any DRS+ fixed playback bug 
					clearTimeout(OE.Rotator.waitingVideoInstance);
				}else{
					//give it another 400 milis to load.
					var fn = "OE.Rotator.playVideo('"+vid+"','"+show+"');";
					OE.Rotator.waitingVideoInstance = setTimeout(fn, 400);
				}
			}
			catch(e){
				Log.error(e);
			}
		},
		init : function(){
			//load our first item up, and get'r done
			//var item = OE.Rotator.getItem(0);
			Log.info("OE.Rotator Initializing...");
			Log.info("OE.Rotator Removing traditional click events from our playlist items...");
			OE.Rotator.removePlaylistClicks();
			OE.Rotator.start();
			//now lets set our orientation based on whats been specified..
			//lets remove the other possible choices just in case.. TODO
			//$(OE.Rotator.selectors.videoContainer).addClass(OE.Rotator.scrollableOrientation);
			//$(OE.Rotator.selectors.videoContainer).addClass(OE.Rotator.scrollableOrientation);
			//$(OE.Rotator.selectors.videoContainer).addClass(OE.Rotator.scrollableOrientation);
			jQuery(OE.Rotator.selectors.videoContainer).addClass(OE.Rotator.scrollableOrientation);
		},
		start : function(){
			Log.info("OE.Rotator.start() called, loading current index...");
			OE.Rotator.playItem(OE.Rotator.currentIndex);
			OE.Rotator.startCycle();
		},
		startCycle : function(){
			var fn = "OE.Rotator.advanceCycle();";
			OE.Rotator.waitingInstance = setTimeout(fn, OE.Rotator.waitTime);
		},
		advanceCycle : function(){
			Log.warn("Index: "+OE.Rotator.currentIndex);
			if(OE.Rotator.currentIndex >= OE.Rotator.items.length-1){
				OE.Rotator.currentIndex = 0;
				OE.Rotator.scrollableInstance.seekTo(0);
			}else{
				//only want to do this on every one EXCEPT the first clip
				OE.Rotator.currentIndex++;
				if(((OE.Rotator.currentIndex % OE.Rotator.scrollablesPerPage) == 0) && OE.Rotator.currentIndex != 1){
					OE.Rotator.scrollableInstance.seekTo(OE.Rotator.currentIndex);
				}
			}
			OE.Rotator.playItem(OE.Rotator.currentIndex);
			var fn = "OE.Rotator.advanceCycle();";
			OE.Rotator.waitingInstance = setTimeout(fn, OE.Rotator.waitTime);
		},
		stopCycle : function(){
			Log.info("OE.Rotator.stopCycle() called, cancelling timeouts set.");
			clearTimeout(OE.Rotator.waitingInstance);
		},
		is_article : function(href){
			if(href.indexOf(".jpg") != -1){
				return true;
			}else{
				return false;
			}
		}
	}
};