/**
 * Function to build righthand navigational episode lists for episode pages for show.
 *
 * @param season number. this should be an integer.
 * @param xml URL of the XML file where information about episodes are allocated in. This is a required field. XML file MUST be on the same server as the episode list.
 * @param css URL of the CSS file if custom style should be applied to the widgetd. Not required.
 */
function EpisodeList(season, xml,css) {
	var obj = this;
	$.ajax({type: "GET",url: xml, dataType: "xml", success: function(xml) { obj.initXML(xml,season); } });	
	if(css) {
	    $("head").append("<link>");
		$("head").children(":last").attr({ rel: "stylesheet", type: "text/css", href: css });
	}
}
EpisodeList.prototype =  {
	totalSeasons : -1,
	/*  Default HolderID. Changes this If you need to have the episode focus on different URL  */
	holderID : "episodelist",
	/*
	 * InitalizesXML File and generates HTML
	 */
	initXML : function(xml,focusedSeason) {
		var obj = this;
		$("#"+this.holderID).append('<h3>'+obj.cleanStr($(xml).find("contentNav").attr("headerCopy")) +'</h3>');
		$("section",xml).each(function(i){
			$("#"+obj.holderID).append('<div id="el-season-' + i + '"/>');
			$("#el-season-"+ i).append('<h5 class="seasonTitle">'+obj.cleanStr($(this).attr("textTitle"))+'</h5>');
			$("#el-season-"+ i).append('<ul/>');
			$('contentItem',this).each(function(j){
				$('#el-season-'+i+' ul').append('<li><a href="'+obj.cleanStr($(this).attr('url'))+'">'+obj.cleanStr($(this).attr("title"))+'</a></li>');
			});
			totalSeasons = i+1;
		});
		this.initMenu(focusedSeason);
	},
	/*
	 * Initializes menu, including onclick event.
	 */
	initMenu : function(focusedSeason) {
		var obj = this;
		for(var i=0;i<totalSeasons;i++) {
			$('#el-season-'+i).attr({"class":((i+1)==focusedSeason ? "on" : "off")});	
			$('#el-season-'+i + " .seasonTitle").unbind(); // make sure to unbind so click events don't loop!
			$('#el-season-'+i + " .seasonTitle").click(function(){ 			
				obj.initMenu(parseInt($(this).parent("div").attr("id").replace("el-season-","")) + 1);
			});
		}
	},
	/*
	 * Cleans up String so it doesn't break html in attributes
	 */
	cleanStr : function(str) {
		return str;
	}
}