/**
* @fileoverview Adds functionality to eastenders episode finder form. Will hide any date options that are in the future.
*
* Usage: Common.addEvent(window, "load", function() {d = new FinderForm();}, true);
*
**/
 
/**
* @constructor
*/
function FinderForm (opts) {
	this.init(opts);
}

FINDER_FORM_OBJECTS = []; // Array of all FinderForm objects on page, used for accessing the objects via events

FinderForm.prototype = {	
	
	/**
	* Inititiate a new FinderForm object
	* 
	* @param {Object} opts An object containing an associative array of options to overwrite the deafults.
	*
	* @option yearId 	- String. The ID of the year select box. Defaults to "year".
	* @option monthId 	- String. The ID of the month select box. Defaults to "month".
	* @option date 		- Date [JavaScript Object]. The date which options will be hidden if later than. Defaults to current.
	*
	*/
	init : function (opts) {
			
		// Default options
		this.options = {
			date : new Date(),
			yearId : "year",
			monthId : "month"
		}
			
		// Set any var's passed on init
		if (opts) {
			c.setOptions(opts);
		};
		
		// Cache lookups for year select box, it's options and their length.
		this.year = Common.$(this.options.yearId);
		this.yearOptions = this.year.getElementsByTagName("option");
		this.yearOptionsLength = this.yearOptions.length; 
		
		// Cache lookups for month select box, it's options and their length.
		this.month = Common.$(this.options.monthId);
		this.monthOptions = this.month.getElementsByTagName("option");
		this.monthOptionsLength = this.monthOptions.length;
		
		// Do year to start
		this.doYear();
		
		// Add this to FINDER_FORM_OBJECTS array and cache position in temp variable
		var temp = FINDER_FORM_OBJECTS.push(this) - 1;
		
		// Add onchange handler to the year select element
		Common.addEvent(this.year, "change", function() {FINDER_FORM_OBJECTS[temp].doYear();}, true);
	
	},
	
	/**
	* Rebuild the options in the month selectbox. Either with full 12 months or the amount of months to date
	*
	* @param {Integer} monthOptionsLength The amount of month options to create
	* @param {Object} selectedMonth	The current selected month option (HTML Element).
	*
	*/
	createMonthOptions : function(monthOptionsLength, selectedMonth) {
				
		if (!selectedMonth) selectedMonth = monthOptionsLength;
		
		// Delete existing month options
		this.month.innerHTML = ""; 
	
		// Rebuild them! 
		for (var i=1;i <= monthOptionsLength; i++) {
		
			var thisEl =  document.createElement("option");
			
			var monAbr = this.getMonAbr(i);
			
			thisEl.value = monAbr[0];
			thisEl.innerHTML = monAbr[1];
			
			if (selectedMonth == i) thisEl.selected = true;
			
			this.month.appendChild(thisEl);
		}
		
		// Resest Variables
		this.monthOptions = this.month.getElementsByTagName("option");
		this.monthOptionsLength = this.monthOptions.length; 
		
	},	
	
	
	/**
	* Find out if the selected year is current year. Show / hide month options
	*/		
	doYear : function () {
	
		// Get selected year option
		var thisEl = this.yearOptions[this.getSelectedOption(this.yearOptions, this.yearOptionsLength)];
		
		// If its this year ...
		if (thisEl.value == this.options.date.getFullYear()) {
			this.createMonthOptions(this.options.date.getMonth() + 1);
		} else {
			this.createMonthOptions(12, this.getSelectedOption(this.monthOptions, this.monthOptionsLength) + 1);
		}

	},
		
	/**
	* Pass an integer, returns array with correct numeric form and month abbreviation. IE: ["01", "Jan"]
	*
	* @param {Integer} i 1 - 12. The month abbreviation number you want to return
	*/
	getMonAbr : function(i) {
	
		var months = [["00","Nowt"],["01", "Jan"],["02", "Feb"],["03", "Mar"],["04", "Apr"],["05", "May"],["06", "Jun"],["07", "Jul"],["08", "Aug"],["09", "Sep"],["10", "Oct"],["11", "Nov"],["12", "Dec"]];
		
		if (i > months.length || i < 0) return months[0];
		
		return months[i];
	}, 
	
	/**
	* Returns the current selected month option as an integer
	*
	* @param {Array} theOptions An array of all the HTML options you want to check.
	* @param {Integer} theOptionsLength The length of theOptions array
	*/
	getSelectedOption : function (theOptions, theOptionsLength) {
		
		// Loop through all month options 
		for (var i=0;i < theOptionsLength;i++) {

			// Cache option
			thisEl = theOptions[i];
			
			// If it's selected return it's number in array
			if (thisEl.selected) {
				return i;
			}
		
		}
	}
}

