/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

Type.registerNamespace('eBags.Applications.Web.ConsumerWebSite.Javascript.Extender');

eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior = function(element) {
	eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior.initializeBase(this, [element]);

	// properties
	this._popupContainerId = null;
	this._popupWrapperId = null;
	this._closeLinkId = null;
	this._extendedContainerId = null;
	this._extendedContainerExtraPx = 0;
	this._duration = null;
	this._fps = null;
	this._showPopup = false;

	// variables
	this._popupElement = null;
	this._wrapperElement = null;
	this._wrapperHeight = null;
	this._popupHeight = null;
	this._closeLink = null;
	this._extendedContainer = null;
	this._clickHandler = null;
	this._popupClickHandler = null;
	this._closeClickHandler = null;
	this._bodyClickHandler = null;
	this._hideAnimationEndHandler = null;
	this._showAnimationEndHandler = null;
	this._popupVisible = false;
	this._showAnimation = null;
	this._hideAnimation = null;
	this._scrollPosition = null;
	this._supressShowEvent = false;
	this._extendedShowAnimation = null;
	this._extendedHideAnimation = null;
	this._useExtendedAnimation = false;
}

eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior.prototype = {
	initialize: function() {
		eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior.callBaseMethod(this, 'initialize');

		// get associated elements
		var elem = this.get_element();
		this._popupElement = $get(this._popupContainerId);
		this._wrapperElement = $get(this._popupWrapperId);
		this._closeLink = $get(this._closeLinkId);
		this._extendedContainer = $get(this._extendedContainerId);

		// set extended contianer flag
		if (this._extendedContainer) {
			this._useExtendedAnimation = true;
		}

		// set popup and wrapper dims
		this._setDimensions();

		// create animations
		this._showAnimation = this._createAnimation(true);
		this._hideAnimation = this._createAnimation(false);

		// create delegates
		this._clickHandler = Function.createDelegate(this, this._onClick);
		this._popupClickHandler = Function.createDelegate(this, this._onPopupClick);
		this._closeClickHandler = Function.createDelegate(this, this._onCloseClick);
		this._bodyClickHandler = Function.createDelegate(this, this._onBodyClick);
		this._showAnimationEndHandler = Function.createDelegate(this, this._onShowAnimationEnd);
		this._hideAnimationEndHandler = Function.createDelegate(this, this._onHideAnimationEnd);

		// attach events
		$addHandler(elem, 'click', this._clickHandler);
		$addHandler(this._popupElement, 'click', this._popupClickHandler);
		$addHandler(this._closeLink, 'click', this._closeClickHandler);
		$addHandler(document.body, 'click', this._bodyClickHandler);
		this._showAnimation.get_events().addHandler('ended', this._showAnimationEndHandler);
		this._hideAnimation.get_events().addHandler('ended', this._hideAnimationEndHandler);

		if (this._showPopup) {
			// need to make sure the use can see the drop down
			if (window.scrollTo) {
				var b = Sys.UI.DomElement.getBounds(this._wrapperElement);
				window.scrollTo(b.x, 0);

				// make sure the PageRequestManager does not re-scroll 
				var prm = Sys.WebForms.PageRequestManager.getInstance();
				this._scrollPosition = prm._scrollPosition;
				prm._scrollPosition = null;
			}

			this._supressShowEvent = true;
			this.showPopup();
		}
	},

	dispose: function() {
		// clean up event handlers
		if (this._bodyClickHandler) {
			$removeHandler(document.body, 'click', this._bodyClickHandler);
			this._bodyClickHandler = null;
		}
		if (this._closeClickHandler) {
			$removeHandler(this._closeLink, 'click', this._closeClickHandler);
			this._closeClickHandler = null;
		}
		if (this._popupClickHandler) {
			$removeHandler(this._popupElement, 'click', this._popupClickHandler);
			this._popupClickHandler = null;
		}
		if (this._clickHandler) {
			$removeHandler(this.get_element(), 'click', this._clickHandler);
			this._clickHandler = null;
		}
		if (this._showAnimationEndHandler) {
			this._showAnimation.get_events().removeHandler('ended', this._showAnimationEndHandler);
			this._showAnimationEndHandler = null;
		}
		if (this._hideAnimationEndHandler) {
			this._hideAnimation.get_events().removeHandler('ended', this._hideAnimationEndHandler);
			this._hideAnimationEndHandler = null;
		}

		eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior.callBaseMethod(this, 'dispose');
	},

	_setDimensions: function() {
		var popupHeight, extendedBoxHeight;

		// set height and top for extended container
		if (this._extendedContainer) {
			extendedBoxHeight = Sys.UI.DomElement.getBounds(this._extendedContainer).height;
		} else {
			extendedBoxHeight = 0;
		}

		// set height and top for wrapper and main container
		this._popupHeight = Sys.UI.DomElement.getBounds(this._popupElement).height;
		this._wrapperHeight = (this._popupHeight + extendedBoxHeight);
		//this._wrapperElement.style.height = this._wrapperHeight + 'px';
		this._popupElement.style.top = (this._popupHeight * -1) + 'px';

		if (this._extendedContainer) {
			this._extendedContainer.style.top = (this._popupHeight - (extendedBoxHeight + this._extendedContainerExtraPx)) + 'px';
			this._extendedContainer.style.left = '0px';
		}
	},

	_createAnimation: function(bCreateShow) {
		var animation;
		var mainAnimation = this._createMoveAnimation(bCreateShow, this._popupElement, this._popupHeight);

		if (this._useExtendedAnimation) {
			var sequence = new AjaxControlToolkit.Animation.SequenceAnimation();
			var extAnimation = this._createMoveAnimation(bCreateShow, this._extendedContainer,
				Sys.UI.DomElement.getBounds(this._extendedContainer).height);// + this._extendedContainerExtraPx);
			if (bCreateShow) {
				sequence.add(mainAnimation);
				sequence.add(extAnimation);
			} else {
				sequence.add(extAnimation);
				sequence.add(mainAnimation);
			}
			animation = sequence;
		} else {
			animation = mainAnimation;
		}

		return animation;
	},

	_createMoveAnimation: function(bCreateShow, element, height) {
		var vertical = height;

		if (!bCreateShow) {
			vertical = vertical * -1;
		}

		return new AjaxControlToolkit.Animation.MoveAnimation(element, this._duration, this._fps, 0, vertical, true, 'px');
	},

	_onClick: function(e) {
		if (!this._popupVisible) {
			this.showPopup();
		}
		if (e) {
			e.stopPropagation();
		}
	},

	_onPopupClick: function(e) {
		e.stopPropagation();
	},

	_onCloseClick: function(e) {
		if (this._popupVisible) {
			this.hidePopup();
		}
		if (e) {
			e.stopPropagation();
		}
	},

	_onBodyClick: function() {
		if (this._popupVisible) {
			this.hidePopup();
		}
	},

	_onShowAnimationEnd: function() {
		this._popupVisible = true;
		if (!this._supressShowEvent) {
			this._raiseShow();
		}
		this._supressShowEvent = false;
	},

	_onHideAnimationEnd: function() {
		this._setWrapperHeight();
		this._popupVisible = false;
		if (this._scrollPosition) {
			if (window.scrollTo) {
				window.scrollTo(this._scrollPosition.x, this._scrollPosition.y);
			}
		}
		this._scrollPosition = null;
	},

	_setWrapperHeight: function() {
		if (this._popupVisible) {
			// set to zero
			this._wrapperElement.style.height = '0';
		} else {
			// set big enough to show popup element
			this._wrapperElement.style.height = this._wrapperHeight + 'px';
		}
	},

	_raiseShow: function() {
		var handlers = this.get_events().getHandler('show');
		if (handlers) {
			handlers(this, Sys.EventArgs.Empty);
		}
	},

	add_show: function(handler) {
		this.get_events().addHandler('show', handler);
	},
	remove_show: function(handler) {
		this.get_events().removeHandler('show', handler);
	},

	showPopup: function() {
		this._setWrapperHeight();
		this._showAnimation.play();
	},

	hidePopup: function() {
		this._hideAnimation.play();
	},

	get_PopupContainerId: function() {
		return this._popupContainerId;
	},
	set_PopupContainerId: function(value) {
		this._popupContainerId = value;
	},

	get_PopupWrapperId: function() {
		return this._popupWrapperId;
	},
	set_PopupWrapperId: function(value) {
		this._popupWrapperId = value;
	},

	get_CloseLinkId: function() {
		return this._closeLinkId;
	},
	set_CloseLinkId: function(value) {
		this._closeLinkId = value;
	},

	get_ExtendedContainerId: function() {
		return this._extendedContainerId;
	},
	set_ExtendedContainerId: function(value) {
		this._extendedContainerId = value;
	},

	get_ExtendedContainerExtraPx: function() {
		return this._extendedContainerExtraPx;
	},
	set_ExtendedContainerExtraPx: function(value) {
		this._extendedContainerExtraPx = value;
	},

	get_Duration: function() {
		return this._duration;
	},
	set_Duration: function(value) {
		this._duration = value;
	},

	get_Fps: function() {
		return this._fps;
	},
	set_Fps: function(value) {
		this._fps = value;
	},

	get_ShowPopup: function() {
		return this._showPopup;
	},
	set_ShowPopup: function(value) {
		this._showPopup = value;
	}
}

eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior.registerClass('eBags.Applications.Web.ConsumerWebSite.Javascript.Extender.DropDownAnimationBehavior', AjaxControlToolkit.BehaviorBase);

if (typeof(Sys) !== 'undefined'){ Sys.Application.notifyScriptLoaded(); }
