/**
 * Recaps: recaps episode loader
 */

var Recaps = Class.create();
Recaps.prototype = {
	initialize : function(){
		this.episodes = $$('.recaps .recap-info');
		this.currentEpisode = -1;

		// Get episode number from URL
		var url = window.location.toString();
		var ep  = parseInt(url.gsub(/^.*#episode/, ''));

		// Load episode
		// If nothing found in URL or the namber is outrange, send 0
		if( (url == ep) || isNaN(ep) || ( ep > this.episodes.length ) ) {
			this.showEpisode(0);
		} else {
			this.showEpisode(ep);
		}
	},

	/**
	 * Show episode by number
	 * @param ep: episode number. 0 to load the last episode 
	 */
	showEpisode : function(ep){
		this.episodes.each(function(elm, ind){
			if(ind+1 == ep || ( ep == 0 && ind == this.episodes.length-1 )) {
				elm.show();
				this.currentEpisode = ind+1;
			} else {
				elm.hide();
			}
		}.bind(this));
	},

	/**
	 * Get current episode
	 */
	getCurrentEpisode : function(){
		return this.currentEpisode;
	},

	/**
	 * Get current season
	 */
	getCurrentSeason : function(){
		var currentSeason = window.location.toString().gsub(/^.*season(\d+?)\/.*$/,'#{1}');
		if(isNaN(currentSeason)){
			return -1;
		} else {
			return currentSeason;
		}
	}
	
}
var seasonRecaps = new Recaps();

var so = new SWFObject("/_ui/flash/main.swf?xmlPath=/_xml/main.xml", "simpsons_recap", "700", "628", "9", "#fffff");
so.addParam("wmode", "transparent");
so.write("nav_recap");
