var LibraryMediator = {

	aBanners      : [],

	oButtonRotate : null,

	oAnimationController : null,

	iSelectedBannerIndex : 0,
	bBusy                : false,

	CLASS_NAME_BANNER_ANIMATED  : 'library-banner-animated',
	CLASS_NAME_BANNER_INVISIBLE : 'invisible',

	init : function() {

		 var
			oThis = this, 
			aElementBannerContainers = Common.Dom.getElementsByClassName(
			document.getElementById('library-banners'),
			'library-banner'
			);

		for(var i = 0; i < aElementBannerContainers.length; i++) {

			this.aBanners.push(
				{
					oElementContainer : aElementBannerContainers[i],
					oElementContent   : Common.Dom.getElementsByClassName(aElementBannerContainers[i], 'library-banner-content')[0]
				}
				);

		}

		this.oButtonRotate = new ActionButton(
			document.getElementById('banner-rotator'),
			[
				{
					fHandler : function() {

            			oThis.rotateBanner();

						return true;

					}
				}
			]
			);

		this.oAnimationController = new AnimationController(30);

	},

	setBusy : function() {

		this.bBusy = true;

	},

	setUnBusy : function() {

		this.bBusy = false;

	},

	isBusy : function() {

		return this.bBusy;

	},

	rotateBanner : function() {

		if(this.isBusy()) {
			return;
		}

		this.setBusy();

		var iNewBannerIndex = this.iSelectedBannerIndex + 1 == this.aBanners.length? 0 : this.iSelectedBannerIndex + 1;

		Common.Class.add(
			this.aBanners[iNewBannerIndex].oElementContainer,
			this.CLASS_NAME_BANNER_ANIMATED
			);

		Common.Class.remove(
			this.aBanners[iNewBannerIndex].oElementContainer,
			this.CLASS_NAME_BANNER_INVISIBLE
			);

		var iNewBannerTopOffset = this.aBanners[iNewBannerIndex].oElementContainer.offsetHeight;

		this.aBanners[iNewBannerIndex].oElementContainer.style.top = -iNewBannerTopOffset + 'px';

		var oThis = this;

		Animator.fadeOut(this.aBanners[this.iSelectedBannerIndex].oElementContent);

		this.oAnimationController.addAnimation(
			new AnimationEquation(
				this.aBanners[iNewBannerIndex].oElementContainer,
				{
					aProperties  : ['top'],
					aValuesStart : [-iNewBannerTopOffset],
					aEquations   : [AnimationEquation.Quartic.easeOut],
					aValuesEnd   : [0],
					iFramesCount : 30,
					fStopCallbackFunction : function() {

						Common.Class.add(
							oThis.aBanners[oThis.iSelectedBannerIndex].oElementContainer,
							oThis.CLASS_NAME_BANNER_INVISIBLE
							);

						Common.Class.remove(
							oThis.aBanners[iNewBannerIndex].oElementContainer,
							oThis.CLASS_NAME_BANNER_ANIMATED
							);

						Common.Class.remove(
							oThis.aBanners[oThis.iSelectedBannerIndex].oElementContent,
							oThis.CLASS_NAME_BANNER_INVISIBLE
							);

						Common.Dom.setOpacity(
							oThis.aBanners[oThis.iSelectedBannerIndex].oElementContent,
							1
							);

						oThis.iSelectedBannerIndex = iNewBannerIndex;

						oThis.setUnBusy();
						
					}
				}
				)
			);

		this.oAnimationController.start();

	}

};