﻿function SeasonNavigator(id)
{
	this.MAX_DISPLAYED_SEASONS = 5;
	this.container = $('#' + id);
	this.seasonLinks = $('a.seasonLink', this.container);
	this.seasonLinks.click(this._onSeasonSelected.bindEventListener(this));
	this.setNavArrows(this.container);

	this.startofrange = 0;
	this.endofrange = 0;
	this.maxseasonindex = this.seasonLinks.length;
	this.maxseasonindex = this.maxseasonindex - 1;
	
	this.setSeasonsVisible(this.container);

	$(".seasonnavleftarrow img", this.container).click(this._onPreviousSelected.bindEventListener(this));
	$(".seasonnavrightarrow img", this.container).click(this._onNextSelected.bindEventListener(this));
}

SeasonNavigator.prototype._onSeasonSelected = function(evt)
{
	var link = evt.target;
	this.onSeasonSelected(eval("(" + link.lang + ")"));
	evt.target.blur();
	
	$("#seriesnav ul li a").removeClass("seasonselected");
	$("#seriesnav ul li a").addClass("seasonunselected");
	$("#" + link.id).addClass("seasonselected");
	return false;
}
SeasonNavigator.prototype._onPreviousSelected = function(evt)
{
	if (this.startofrange > 0)
	{
		this.startofrange --;
		this.endofrange--;
		this.renderSeasons();
	}
}
SeasonNavigator.prototype._onNextSelected = function(evt)
{
	if (this.endofrange < this.maxseasonindex)
	{
		this.startofrange ++;
		this.endofrange++;
		this.renderSeasons();
	}
}
SeasonNavigator.prototype.setNavArrows = function(container)
{
	if (this.seasonLinks.length <= this.MAX_DISPLAYED_SEASONS)
	{
		$(".seasonnavleftarrow img", container).hide();	
		$(".seasonnavrightarrow img", container).hide();	
	}
}
SeasonNavigator.prototype.setSeasonsVisible = function(container)
{
	//only happens if there are too many seasons.
	if (this.seasonLinks.length > this.MAX_DISPLAYED_SEASONS)
	{
		var selectedseason = $("#hdnCurrentSelectedSeasonNumber").val();

		eval('var maxseasondetails = ' + this.seasonLinks[this.maxseasonindex].lang);
		eval('var minseasondetails = ' + this.seasonLinks[0].lang);
			
		if (selectedseason == maxseasondetails.SeasonNumber)
		{
			//work backwards
			this.endofrange = this.maxseasonindex;
			this.startofrange = ((this.endofrange - this.MAX_DISPLAYED_SEASONS) + 1);
		}
		else if (selectedseason == minseasondetails.SeasonNumber)
		{
			this.startofrange = 0;
			this.endofrange = ((this.startofrange + this.MAX_DISPLAYED_SEASONS) - 1);
		}
		else 
		{
			//really nasty, just sets the start and end ranges to the selected index and upwards
			var selectedindex = $("#hdnCurrentSelectedSeasonIndex").val();
			
			if (((selectedindex + this.MAX_DISPLAYED_SEASONS) - 1) > this.maxseasonindex) this.endofrange = this.maxseasonindex;
			else ((selectedindex + this.MAX_DISPLAYED_SEASONS) - 1);
			
			this.startofrange = selectedindex;
		}	
		
		this.renderSeasons();
	}
}
SeasonNavigator.prototype.renderSeasons = function()
{
		//hideall
		for (i = 0; i <= this.maxseasonindex; i ++) $('#' + this.seasonLinks[i].id).hide();
		
		//unhide visible
		for (i = this.startofrange; i <= this.endofrange; i ++) $('#' + this.seasonLinks[i].id).show();

		//finally hide the arrows is necessary
		if (this.startofrange == 0) $(".seasonnavleftarrow img", this.container).hide();	
		else $(".seasonnavleftarrow img", this.container).show();
		
		if (this.endofrange == this.maxseasonindex) $(".seasonnavrightarrow img", this.container).hide();
		else $(".seasonnavrightarrow img", this.container).show();
}
SeasonNavigator.prototype.onSeasonSelected = function(evt)
{
	alert("");
}