/**
 * @author tara
 */

/*
 * General-purpose date filter manager; for month, day, and year filters
 */
adobe.marketplace.DateFilterManager = Class.create({
	initialize: function(monthSelect, daySelect, yearSelect) {
		this.monthSelect = monthSelect;
		this.daySelect = daySelect;
		this.yearSelect = yearSelect;
		if ((this.monthSelect) && (this.yearSelect)) {
			Event.observe(this.monthSelect, 'change', function() {
				var mDaysInMonth = this.getDaysInMonth(this.getMonthValue(), this.getYearValue());
				this.updateDaySelect(mDaysInMonth);
			}.bind(this));
			Event.observe(this.yearSelect, 'change', function() {
				var yDaysInMonth = this.getDaysInMonth(this.getMonthValue(), this.getYearValue());
				this.updateDaySelect(yDaysInMonth);
			}.bind(this));
		}
	},
	/**
	 * Gets or sets the value of the month select
	 */
	getMonthValue: function() {
		if (this.monthSelect.getValue() != '') {
			return parseInt(this.monthSelect.options[this.monthSelect.selectedIndex].value)
		} else {
			return 1;
		}
	},
	/**
	 * Gets or sets the value of the year select
	 */
	getYearValue: function() {
		if (this.yearSelect.getValue() != '') {
			return parseInt(this.yearSelect.options[this.yearSelect.selectedIndex].value)
		} else {
			var d = new Date();
			return d.getFullYear();
		}
	},
	/**
	 * Gets the number of days in the specified month
	 */
	getDaysInMonth: function(monthValue, yearValue) {
		var daysInMonth;
		switch (monthValue) {
			case 2:
				if (this.isLeapYear(yearValue)) {
					daysInMonth = 29;
				} else {
					daysInMonth = 28;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				daysInMonth = 30;
				break;
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
			default:
				daysInMonth = 31;
				break;
		}
		return daysInMonth;
	},
	/**
	 * Calculates whether a particular year is a leap year
	 */
	isLeapYear: function(yearValue) {
		if (((yearValue % 4) == 0) && ((yearValue % 100) != 0) || ((yearValue % 400) == 0)) {
			return (true);
		} else {
			return (false);
		}
	},
	/**
	 * Updates the day select to include the number of days in the selected month
	 */
	updateDaySelect: function(daysInMonth) {
		var currentDaysInMonth = this.daySelect.options.length - 1;
		if (currentDaysInMonth < daysInMonth) {
			// add options
			var daysToAdd = daysInMonth - currentDaysInMonth;

			$R(1,daysToAdd).each(function(index) {
				var dayOptionToAdd = $(document.createElement('option'));
				var dayOptionToAddValue = document.createAttribute('value');
				dayOptionToAddValue.value = currentDaysInMonth + index;
				dayOptionToAdd.setAttributeNode(dayOptionToAddValue);
				dayOptionToAdd.innerHTML = currentDaysInMonth + index;
				this.daySelect.insert({'bottom': dayOptionToAdd});
			}.bind(this));
		} else if (currentDaysInMonth > daysInMonth) {
			// remove options
			var daysToRemove = currentDaysInMonth - daysInMonth;

			$R(0, daysToRemove - 1).each(function(index) {
				var dayOptionToRemove = $(this.daySelect.options[currentDaysInMonth - index]);
				dayOptionToRemove.remove();
			}.bind(this));
		}
	}
});

/*
 * Publisher reports date filter manager; for month and year filters
 */
adobe.marketplace.ReportsDateFilterManager = Class.create({
	initialize: function(nowMonth, nowYear) {
		// current month and year
		this.nowMonth = nowMonth;
		this.nowYear = nowYear;
		// month and year select elements
		this.monthSelect = $('offeringsFilterMonth');
		this.yearSelect = $('offeringsFilterYear');
		// indices of selected month and year options
		this.monthSelectedIndex = this.monthSelect.selectedIndex;
		this.yearSelectedIndex = this.yearSelect.selectedIndex;
		// repopulate month select if needed
		this.setMonthSelectOptions();
		// add event listener for year pulldown change
		if ((this.monthSelect) && (this.yearSelect)) {
			Event.observe(this.yearSelect, 'change', function() {
				this.setMonthSelectOptions();
			}.bind(this));
		}
	},
	/**
	* Checks and maintains or resets the options in the month select
	*/
	setMonthSelectOptions: function() {
		// determine final month option
		var endMonth = 12;
		this.yearSelect.select('option').each(function(option) {
			if ((option.selected == true) && (option.value == this.nowYear)) {
				endMonth = this.nowMonth - 1;
			}
		}.bind(this));
		// if current end month equals new end month, stop -- no need to repopulate select
		var currentEndMonth = this.monthSelect.options[this.monthSelect.length - 1].value;
		if (currentEndMonth == endMonth) {
			return;
		}
		// otherwise, first clear out month select
		this.monthSelect.innerHTML = '';
		// then re-add options to month select
		var optionElement = document.createElement('option');
		optionElement.value = '';
		optionElement.innerHTML = 'Month';
		this.monthSelect.appendChild(optionElement);
		var monthsArray = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
		$R(1, endMonth).each(function(month) {
			var optionElement = document.createElement('option');
			optionElement.value = month;
			optionElement.innerHTML = monthsArray[month - 1];
			this.monthSelect.appendChild(optionElement);
		}.bind(this));
		// if previously selected month is available in new year's list, re-select it
		// otherwise, select Jan
		this.monthSelect.select('option').each(function(option) {
			if (option.index == this.monthSelectedIndex) {
				this.monthSelect.selectedIndex = option.index;
			}
		}.bind(this));
		this.monthSelectedIndex = this.monthSelect.selectedIndex;
	}
});