var fep_clipboard;
function initFullEpisode(){
	$('.full-episode-page').each(function(){
		//
		// Share and info buttons in the player pane.
		//
		$(this).find('.player-pane').each(function(){
			$(this).find('a.share').click(function(){
				if(typeof(video_player)!='undefined'){
					video_player.openPanel('SHARE');
				}
				return false;
			});
			$(this).find('a.info').click(function(){
				if(typeof(video_player)!='undefined'){
					video_player.openPanel('INFO');
				}
				return false;
			});
		});

		//
		// Carousel
		//
		$(this).find('.carousel').each(function(){
			$(this).disableTextSelect();
			var carousel = new FepCarousel($(this));
		});

		//
		// Share (for the clipboard)
		//
		$(this).find('.sharePromo').each(function(){
			ZeroClipboard.setMoviePath('/shadow_sitewide/flash/zero_clipboard.swf');
			var cb = new ZeroClipboard.Client();
			var input = $(this).find('input[type=text]');
			input.click(function(){
				$(this).select();
			});
			cb.setText(input.attr('value'));
			cb.setHandCursor(true);
			cb.glue('copy_button');
		});
	});
}

/**
 * This class basically manages the toggling of the currently opened tab.
 * FepCarouselTab contains the bulk of the logic.
 *
 * @param jQuery inst A jQuery instance of a carousel DOM.
 */
function FepCarousel(inst){
	var carousel = this;
	this._inst = inst;
	this._tabs = new Array();
	this._seasons = new Array();
	this._content = $('#content');
	this._overlay_box = this._content.find('.overlay-box');

	// This finds all the tabs and assumes it has a correlated content node with
	// an id similar to the tab (whatever_tab) with _tab replaced by _content:
	// whatever_content.
	this._inst.find('.tabs .tab').each(function(){
		var tabImg = $(this).find('img');
		var tabImgSrc = $(this).find('img').attr('src');
		var tabImgOnSrc = tabImgSrc.replace('_grey', '_white');
		var tab = new FepCarouselTab(carousel, $(this), $('#content'));
		carousel._tabs.push(tab);

		if($(this).hasClass('tab-selected')){
			carousel._current_tab = tab;
			tabImg.attr('src', tabImgOnSrc);
		}
	});
	
	
	this._inst.find('.seasons .season').each(function(){
		var season = new FepCarouselSeason(carousel, $(this), $('#content'));
		carousel._seasons.push(season);
		
		if($(this).hasClass('season-selected')){
			carousel._current_season = season;
		}
	});
	
}

FepCarousel.prototype = {
	_inst:null,
	_tabs:null,
	_current_tab:null,
	_current_season:null,
	_current_episode:null,
	_total_episodes:0,
	_current_index:-1,
	_overlay_box:null,
	_inside_episode:false,
	_inside_overlay:false,
	_overlay_timeout:null,
	
	open_tab:function(tab){
		this._current_tab.close();
		this._current_tab = tab;
	},
	
	open_season:function(season){
		this._current_season.close();
		this._current_season = season;
	},
	
	get_episodes:function(json, feedUrl, season_name) {
		var thisref = this;
		var feed;
		var season;
		$(document).ajaxSend(function(){
			$('.scroll').empty();
			$('.scroll').append('<img class="loading" src="/sitewide/images/full_episodes/loader.gif"/>')
		});
		if (feedUrl != null) {
			feed = feedUrl;
		} else {
			$.each(json.seasons, function(i,season){
				week = season.name
			});
			(season_name != null)? season = season_name : season = week;
			feed = '/feeds/full-episode/show/'+ json.showId +'/season/'+season;
		} 
		thisref.epAjaxCall = $.ajax({
	    	url:feed,
	      	type: "GET",
			async:false,
	      	dataType: "html",
			timeout:(30 * 1000),
	      	success: function(html){
				$('.scroll').empty();
				$('.scroll').append(html);
				$(".episode").bind("mouseover", function(e){
	        		thisref.episode_over($(this));
					thisref.episode_out($(this));
					return false;
	    		});
	      	},
			error: function( objAJAXRequest, strError ){
				 thisref._epAjaxCall.abort();
			}
	   	});
	},
	
	episode_over:function(source){
		this._inside_episode = true;
		this._overlay_box.attr('href', source.attr('href'));
		this._overlay_box.find('.overlay').remove();
		this._overlay_box.append(source.find('.overlay').clone());
		this._overlay_box.addClass('overlay-box-over');
		this._overlay_box.css('left', source.position().left+45);
	},
	
	episode_out:function(source){
		this._inside_episode = false;
	},
	
	overlay_over:function(source){
		this._inside_overlay = true;
	},
	overlay_out:function(source){
		this._inside_overlay = false;
	},
	content_mouseover:function(source){
		if(!this._inside_overlay && !this._inside_episode){
			this._overlay_box.removeClass('overlay-box-over');
		}
	}
};
/**
 * This class manages the contents of a tab.
 *
 * @param FepCarousel carousel The parent instance of FepCarousel.
 * @param jQuery tab_node A jQuery instance of the node which represents the tab.
 * @param jQuery content_node A jQuery instance with the 'content' of a tab.
 */
function FepCarouselTab(carousel, tab_node, content_node){
	this._carousel = carousel;
	this._tab = tab_node;
	this._tabImg = this._tab.find('img');
	this._show_name = this._tab.attr('href');
	this._tabImgSrc = this._tab.find('img').attr('src');
	this._tabImgOnSrc = this._tabImgSrc.replace('_grey', '_white');
	this._tabFeedUrl = this._tab.attr('id');
	this._current_tab = this._tab.find('tab-selected');
	this._content = content_node;
	this._episodes = this._content.find('.episodes');
	this._current_episode;
	this._base_name = this._tab.attr('id').replace('_tab', '');
	this._total_episodes = this._episodes.find('.episode').size();

	// Check if we're opened by default.
	this._opened = this._tab.hasClass('tab-selected');
	
	if(this._opened){
		//this._current_episode = this._episodes.find('.episode-selected');
		//this._episodes.stop().scrollTo(this._current_episode, FepCarouselTab.SCROLL_PROPS);
		this._init_browsing();
	}else{
		// This is so we init the scroll when the content is shown for the first time.
		this._scroll_on_open = true;
	}

	var thisref = this;

	// Tabs.
	this._tab.click(function(){
		thisref.open();
		return false;
	});

	// Scroll arrows.
	this._content.find('.browse-left').each(function(){
		$(this).click(function(){
			thisref.browse_left($(this));
			return false;
		});
	});
	
	this._content.find('.browse-right').each(function(){
		$(this).click(function(){
			thisref.browse_right($(this));
			return false;
		});
	});

	// Episode overlays.
	this._overlay_box = this._content.find('.overlay-box');
	this._overlay_box.mouseover(function(){
		thisref._carousel.overlay_over($(this));
		return false;
	});
	
	this._overlay_box.mouseover(function(){
		thisref._carousel.overlay_out($(this));
		return false;
	});
	this._content.find('.episode').each(function(){
		$(this).mouseover(function(){
			thisref._carousel.episode_over($(this));
			return false;
		});
		$(this).mouseover(function(){
			thisref._carousel.episode_out($(this));
			return false;
		});
	});
	this._content.mouseover(function(){
		thisref._carousel.content_mouseover($(this));
		return false;
	});
};

FepCarouselTab.SCROLL_PROPS = {duration:500,offset:{left:0,top:0}};

FepCarouselTab.prototype = {
	_carousel:null,
	_tab:null,
	_show_name:null,
	_tabFeedUrl:null,
	_tabImg:null,
	_tabImgSrc:null,
	_tabImgOnSrc:null,
	_content:null,
	_episodes:null,
	_opened:false,
	_fetchingEpisode:false,
	_base_name:'',
	_scroll_on_open:false,
	_current_episode:null,
	_total_episodes:0,
	_current_index:-1,
	_overlay_box:null,
	_inside_episode:false,
	_inside_overlay:false,
	_overlay_timeout:null,
	_epAjaxCall:null,
	
	open:function(){
		if(!this._opened){
			this._opened = true;
			this._fetchingEpisodes = true;
			this._tab.addClass('tab-selected');
			this._tab.find('img').attr('src', this._tabImgOnSrc);

			if(this._scroll_on_open || this._fetchingEpisodes){
				this._scroll_on_open = false;
				this._current_episode = this._episodes.find('.episode:first');
				this._episodes.stop().scrollTo(this._current_episode, FepCarouselTab.SCROLL_PROPS);
				this._init_browsing();
			}
			this._carousel.open_tab(this);
			this.change_shows();
		}
	},
	
	close:function(){
		if(this._opened){
			this._opened = false;
			this._tab.removeClass('tab-selected');
			this._tab.find('img').attr('src', this._tabImgSrc);
		}
	},
	
	change_shows:function() {
		var thisref = this;
		if (this._show_name == 'the daily show' || this._show_name == 'the colbert report') {
			$('.browseSeason').empty().html('Browse by Week');
		} else {
			$('.browseSeason').empty().html('Browse by Season');
		}
		$.getJSON(this._tabFeedUrl, function(json){
			thisref._carousel.get_episodes(json, null, null);
			thisref.change_seasons(json);
        });		
	},
	
	change_seasons:function(json) {
		var thisref = this;
		$('.season').remove();
		$.each(json.seasons, function(i,season){
			$('.seasons').append('<a id="'+season.carouselHTMLURL+'" class="season" href="#">'+season.name+'</a>');
		});
		
		this._carousel._inst.find('.seasons .season').each(function(){
			var season = new FepCarouselSeason(thisref._carousel, $(this), thisref._content);
			thisref._carousel._seasons.push(season);
			$(".season:last").addClass('season-selected');
			if($(this).hasClass('season-selected')){
				thisref._carousel._current_season = season;
			}
		});
	},
	
	_init_browsing:function(){
		var eps = this._episodes.find('.episode');
		this._total_episodes = eps.size();
		this._current_index = eps.index(this._current_episode);
	},
	
	browse_left:function(button){
		var i = this._current_index - 1;
		if(!button.hasClass('browse-disabled') && i>=0){
			this._current_episode = this._episodes.find('.episode:eq('+i+')');
			this._episodes.stop().scrollTo(this._current_episode, FepCarouselTab.SCROLL_PROPS);
			this._init_browsing();
		}
	},
	
	browse_right:function(button){
		var i = this._current_index + 1;
			
		if(!button.hasClass('browse-disabled') && i<this._total_episodes){
			if(i!=0){
				this._current_episode = this._episodes.find('.episode:eq('+i+')');
				this._episodes.stop().scrollTo(this._current_episode, FepCarouselTab.SCROLL_PROPS);
				this._init_browsing();
			}
		}
	}
};

function FepCarouselSeason(carousel, season_node, content_node){
	this._carousel = carousel;
	this._season = season_node;
	this._content = content_node;
	this._seasonFeedUrl = this._season.attr('id');
	this._seasons = this._content.find('.seasons')
	this._current_season = this._seasons.find('.season-selected');
	this._episodes = this._content.find('.episodes');
	this._current_episode = this._episodes.find('.episode-selected');
	this._total_episodes = this._episodes.find('.episode').size();
	
	//remember me.
	var thisref = this;
	
	// Seasons.
	this._season.click(function(){
		thisref.open();
		return false;
	});
};


FepCarouselSeason.prototype = {
	_carousel:null,
	_season:null,
	_content:null,
	_seasonFeedUrl:null,
	_current_season:null,
	_current_episode:null,
	_epAjaxCall:null,
	
	open:function(season_node){
		var thisref = this;
		if (this._season.attr('id') != this._current_season.attr('id')) {
			this._current_episode = this._episodes.find('.episode:eq(0)');
			this._episodes.stop().scrollTo(this._current_episode, FepCarouselTab.SCROLL_PROPS);
			this._carousel._current_index =0;	
		}
		this._season.addClass('season-selected');
		this._carousel.open_season(this);
		this._carousel.get_episodes(null, this._seasonFeedUrl, null);
	},
	
	close:function(){
		this._season.removeClass('season-selected');
	}
	
};