/*
* GalleryController
* ----------------------------------------
* @last_modified: 2007-10-29
* @author: Filatov Dmitry <alpha@design.ru>
*/

function GalleryController(
	oParams
	) {

	GalleryController.baseConstructor.call(this);

	var oParams = oParams || Params;

	this.aItems = [];

	this.oElementArrowPrev = oParams.oElementArrowPrev;
	this.oElementArrowNext = oParams.oElementArrowNext;
	
	this.bBusy = false;

	this.init();

};

GalleryController.EVENT_TYPE_ON_BEFORE_SHOW = 'onbeforeshow';
GalleryController.EVENT_TYPE_ON_BEFORE_HIDe = 'onbeforehide';
GalleryController.EVENT_TYPE_ON_HIDE_ALL    = 'onhideall';

GalleryController.CLASS_NAME_ARROW_DISABLED = 'arrow-disabled';

GalleryController.inheritFrom(
	Observable,
	{

		init : function() {

			if(this.oElementArrowPrev && this.oElementArrowNext) {

				var oThis = this;

				Common.Event.add(
					this.oElementArrowPrev,
					'click',
					function(oEvent) {

						Common.Event.cancel(oEvent);

						oThis.prev();

					}
					);

				Common.Event.add(
					this.oElementArrowNext,
					'click',
					function(oEvent) {

						Common.Event.cancel(oEvent);

						oThis.next();

					}
					);

			}

		},

		addItem : function(oItem) {

			var oThis = this;

			oItem.setController(this);
			oItem.attachObserver(
				GalleryItem.EVENT_TYPE_ON_BEFORE_SHOW,
				function() {

					oThis.notify(GalleryController.EVENT_TYPE_ON_BEFORE_SHOW, { oItem : oItem });

					var iIndexOfShowedElement = oThis.indexOfShowedElement();

					if(iIndexOfShowedElement > -1) {
						oThis.aItems[iIndexOfShowedElement].hide();
					}

				}
				);

			oItem.attachObserver(
				GalleryItem.EVENT_TYPE_ON_BEFORE_HIDE,
				function() {

					oThis.notify(GalleryController.EVENT_TYPE_ON_BEFORE_HIDE, { oItem : oItem });

				}
				);


			this.aItems.push(oItem);

			if(this.aItems.length == 1) {

				oItem.hilight();
				oItem.show();

			}

			this.updateArrows();

		},

		prev : function() {

			var iIndexOfShowedElement = this.indexOfShowedElement();

		    if(iIndexOfShowedElement > 0) {

				this.aItems[iIndexOfShowedElement - 1].hilight();
				this.aItems[iIndexOfShowedElement - 1].show();

			}

			this.updateArrows();

		},

		next : function() {

			var iIndexOfShowedElement = this.indexOfShowedElement();

		    if(iIndexOfShowedElement < this.aItems.length - 1) {

				this.aItems[iIndexOfShowedElement + 1].hilight();
				this.aItems[iIndexOfShowedElement + 1].show();

			}

			this.updateArrows();

		},

		updateArrows : function() {

			var iIndexOfShowedElement = this.indexOfShowedElement();

			if(iIndexOfShowedElement == 0) {
				Common.Class.add(this.oElementArrowPrev, GalleryController.CLASS_NAME_ARROW_DISABLED);
			}
			else {
				Common.Class.remove(this.oElementArrowPrev, GalleryController.CLASS_NAME_ARROW_DISABLED);
			}

			if(iIndexOfShowedElement == this.aItems.length - 1) {
				Common.Class.add(this.oElementArrowNext, GalleryController.CLASS_NAME_ARROW_DISABLED);
			}
			else {
				Common.Class.remove(this.oElementArrowNext, GalleryController.CLASS_NAME_ARROW_DISABLED);
			}

		},

		isBusy : function() {

			return this.bBusy;

		},

		indexOfShowedElement : function() {

			for(var i = 0; i < this.aItems.length; i++) {
				if(this.aItems[i].isShowed()) {
					return i;
				}
			}

			return 0;

		},

		isAllowedShow : function(oItem) {

			return true;

		},

		hideAllShowed : function() {

			for(var i = 0; i < this.aItems.length; i++) {
				this.aItems[i].hide();
			}

			this.notify(GalleryController.EVENT_TYPE_ON_HIDE_ALL);

		}

	}
	);