﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("AsiaAustraliaTech");

/// <summary>
/// This component is responsible for entire row hover selection when the rows are grouped
///</summary>
AsiaAustraliaTech.GPNavigationBehavior = function(element) {

	AsiaAustraliaTech.GPNavigationBehavior.initializeBase(this, [element]);

	//fields    
	this._prevYearButton = null;
	this._nextYearButton = null;
	this._yearDropdown = null;
	this._prevGPButton = null;
	this._nextGPButton = null;
	this._gpDropdown = null;
	this._timerCookie = null;
	this._timerInterval = 0;

	//delegates
	this._onDataReceived$delegate = Function.createDelegate(this, this._onDataReceived);
	this._onRequestFailed$delegate = Function.createDelegate(this, this._onRequestFailed);
	this._onPrevYearClick$delegate = Function.createDelegate(this, this._onPrevYearClick);
	this._onNextYearClick$delegate = Function.createDelegate(this, this._onNextYearClick);
	this._onPrevGPClick$delegate = Function.createDelegate(this, this._onPrevGPClick);
	this._onNextGPClick$delegate = Function.createDelegate(this, this._onNextGPClick);
}

AsiaAustraliaTech.GPNavigationBehavior.prototype = {
	initialize: function () {
		AsiaAustraliaTech.GPNavigationBehavior.callBaseMethod(this, 'initialize');

		//get data
		if (!this.get_data()) {
			PageMethods.GetGrandsPrix(this._onDataReceived$delegate, this._onRequestFailed$delegate);
		} else {
			this._initControls();
		}
	},

	dispose: function () {
		AsiaAustraliaTech.GPNavigationBehavior.callBaseMethod(this, 'dispose');
	},

	//properties
	get_data: function () {
		return window.$grandsPrixData;
	},
	set_data: function (value) {
		window.$grandsPrixData = value;
	},

	get_yearDropdown: function () {
		return this._yearDropdown;
	},

	get_prevYearButton: function () {
		return this._prevYearButton;
	},

	get_nextYearButton: function () {
		return this._nextYearButton;
	},

	get_gpDropdown: function () {
		return this._gpDropdown;
	},

	get_prevGPButton: function () {
		return this._prevGPButton;
	},

	get_nextGPButton: function () {
		return this._nextGPButton;
	},

	get_timerInterval: function () {
		return this._timerInterval;
	},
	set_timerInterval: function (value) {
		this._timerInterval = value;
	},

	//private methods
	_initControls: function () {
		//initialize year control
		this._prevYearButton = $(this.get_element()).find('div.leftButton').find('a')[0];
		$addHandler(this._prevYearButton, 'click', this._onPrevYearClick$delegate);
		this._nextYearButton = $(this.get_element()).find('div.rightButton').find('a')[0];
		$addHandler(this._nextYearButton, 'click', this._onNextYearClick$delegate);
		this._yearDropdown = $(this.get_element()).find('div.middle').find('ul:first')[0];

		//initialize grand prix control
		this._prevGPButton = $(this.get_element()).find('div.prevButton').find('a')[0];
		$addHandler(this._prevGPButton, 'click', this._onPrevGPClick$delegate);
		this._nextGPButton = $(this.get_element()).find('div.nextButton').find('a')[0];
		$addHandler(this._nextGPButton, 'click', this._onNextGPClick$delegate);
		this._gpDropdown = $('div.gpSelect').find('ul:first')[0];
	},

	_getSelectedGrandPrix: function () {
		var selectedYear = $(this.get_yearDropdown()).children('li').children('input[type=hidden]').val();
		var selectedCode = $(this.get_gpDropdown()).children('li').children('input[type=hidden]').val();
		return $.grep(this.get_data(), function (item) { return item.Y == selectedYear && item.C == selectedCode })[0];
	},

	_hasPrevGrandPrix: function () {
		var selectedGP = this._getSelectedGrandPrix();
		var index = Array.indexOf(this.get_data(), selectedGP);
		return index > 0;
	},

	_hasNextGrandPrix: function () {
		var selectedGP = this._getSelectedGrandPrix();
		var index = Array.indexOf(this.get_data(), selectedGP);
		return index < this.get_data().length - 1;
	},

	_rebuildGPList: function () {
		var ddl = this.get_gpDropdown();
		var selectedYear = $(this.get_yearDropdown()).children('li').children('input[type=hidden]').val();
		var selectedCode = $(ddl).children('li').children('input[type=hidden]').val();
		var selectedYearGrandsPrix = $.grep(this.get_data(), function (item) { return item.Y == selectedYear; }).sort(createComparer('R'));
		$(ddl).find('ul:first').html("");
		$(selectedYearGrandsPrix).each(function (index, gp) {
			var option = $('<li onclick="_ddl_onchange(this);"></li>');
			$('<span></span>').html(gp.N).appendTo(option);
			$('<input type="hidden" />').val(gp.C).appendTo(option);
			$(ddl).find('ul:first').append(option);
			if (index == 0 || gp.C == selectedCode) {
				$(ddl).children('li').children('input[type=hidden]').val(gp.C);
				$(ddl).children('li').children('span').html(gp.N);
			}
		});
	},

	_moveDropdown: function (ddl, next) {
		var data = _ddl_getData(ddl);
		if (!next) {
			if (data.selectedIndex < data.options.size() - 1) {
				_ddl_onchange(data.options[data.selectedIndex + 1], true);
				return true;
			}
			return false;
		}
		if (next) {
			if (data.selectedIndex > 0) {
				_ddl_onchange(data.options[data.selectedIndex - 1], true);
				return true;
			}
			return false;
		}
	},

	_moveYear: function (ddl, next) {
		this._moveDropdown(ddl, next);
		var data = _ddl_getData(ddl);
		return next
		? data.selectedIndex > 0
		: data.selectedIndex < data.options.length - 1;
	},

	_disableButton: function (button) {
		$(button).addClass('disabled');
		var parentTable = $(button).closest('table.custom-button');
		if (parentTable) {
			parentTable.addClass('disabled');
		}
		$(button).attr('disabled', 'disabled');
	},

	_enableButton: function (button) {
		$(button).removeClass('disabled');
		var parentTable = $(button).closest('table.custom-button');
		if (parentTable) {
			parentTable.removeClass('disabled');
		}
		$(button).removeAttr('disabled');
	},

	_processGPButtons: function () {
		if (this._hasPrevGrandPrix()) {
			this._enableButton(this.get_prevGPButton());
		} else {
			this._disableButton(this.get_prevGPButton());
		}
		if (this._hasNextGrandPrix()) {
			this._enableButton(this.get_nextGPButton());
		} else {
			this._disableButton(this.get_nextGPButton());
		}
	},

	_prevYear: function () {
		if (this._moveYear(this.get_yearDropdown(), false)) {
			this._enableButton(this.get_nextYearButton());
		} else {
			this._disableButton(this.get_prevYearButton());
		}
		this._rebuildGPList();
	},

	_nextYear: function () {
		if (this._moveYear(this.get_yearDropdown(), true)) {
			this._enableButton(this.get_prevYearButton());
		} else {
			this._disableButton(this.get_nextYearButton());
		}
		this._rebuildGPList();
	},

	_scheduleChange: function (ddl) {
		this._timerCookie = window.setTimeout(function () { _ddl_triggerChange($(ddl)[0]); }, this.get_timerInterval());
	},

	_clearTimer: function () {
		window.clearTimeout(this._timerCookie);
	},

	_shouldProcessClick: function (evt) {
		if (evt && evt.target) {
			var button = $(evt.target);
			if (button.parents('table:first').hasClass('disabled')) {
				return false;
			}
		}
		return !Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();
	},

	_cancelEvent: function (evt) {
		if (evt) {
			cancelEvent(evt);
		}
	},


	//event handlers
	_onDataReceived: function (data) {
		this.set_data(data);
		this._initControls();
	},

	_onRequestFailed: function (error) {
		var message = 'Error when retrieving Grands Prix data from the server.\n';
		if (error &&
			error.get_message) {
			message += error.get_message();
		}
		alert(message);
	},

	_onPrevYearClick: function (evt) {
		if (this._shouldProcessClick(evt)) {
			this._clearTimer();
			this._prevYear();
			this._scheduleChange(this.get_gpDropdown());
		}
		this._cancelEvent(evt);
	},

	_onNextYearClick: function (evt) {
		if (this._shouldProcessClick(evt)) {
			this._clearTimer();
			this._nextYear();
			this._scheduleChange(this.get_gpDropdown());
		}
		this._cancelEvent(evt);
	},

	_onPrevGPClick: function (evt) {
		if (this._shouldProcessClick(evt)) {
			this._clearTimer();
			if (!this._moveDropdown(this.get_gpDropdown(), true)) {
				if (this._hasPrevGrandPrix()) {
					this._prevYear();
					var ddl = this.get_gpDropdown();
					var data = _ddl_getData(ddl);
					_ddl_onchange(data.options[data.options.length - 1], true);
				}
			}
			this._processGPButtons();
			this._scheduleChange(this.get_gpDropdown());
		}
		this._cancelEvent(evt);
	},

	_onNextGPClick: function (evt) {
		if (this._shouldProcessClick(evt)) {
			this._clearTimer();
			if (!this._moveDropdown(this.get_gpDropdown(), false)) {
				if (this._hasNextGrandPrix()) {
					this._nextYear();
					var ddl = this.get_gpDropdown();
					var data = _ddl_getData(ddl);
					_ddl_onchange(data.options[0], true);
				}
			}
			this._processGPButtons();
			this._scheduleChange(this.get_gpDropdown());
		}
		this._cancelEvent(evt);
	}

}

AsiaAustraliaTech.GPNavigationBehavior.registerClass('AsiaAustraliaTech.GPNavigationBehavior', Sys.UI.Behavior);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

