/**
 * Media Gallery & Brand
 */

var BarillaGallery = new Class(
		{
			Implements : [ Options, Events ],

			/**
			 * offSet: block width into a slider, default 100 metaAttr: name of
			 * slide gallery (append to 'scrollbar_' and 'knob_'), default
			 * 'gallery'
			 */
			options : {
				offSet : 100,
				metaAttr : 'gallery',
				autoSize : false
			},

			initialize : function(element, options) {

				this.setOptions(options);
				this.element = document.id(element.id);
				this.numImage = this.element.getChildren().length;
				var scrollsize = 0;
				this.element.getElements('div.cntImg').each(function(item) {
					scrollsize += item.getSize().x + (2*item.getStyle('margin-right').toInt());
				});
				this.scrollbarSize = (this.options.autoSize ? scrollsize-this.element.getParent('div.cntGallery').getSize().x
						: this.options.offSet * this.numImage);
				this.setSlyder(element);
				
				console.log("MG numImage: " + this.numImage);
				console.log("MG offSet: " + this.options.offSet);
				console.log("MG FAKE scroll: " + (this.numImage * this.options.offSet));
				console.log("MG scroll: " + this.scrollbarSize);
				console.log("--------------------------------------------------------");
			},

			setSlyder : function(element) {
				var that = this;
				var gallerySlider = new Slider(
						'scrollbar_' + that.options.metaAttr,
						'knob_' + that.options.metaAttr, {
							range : [ 0, that.scrollbarSize ],
							snap : false,
							steps : Math.round(that.scrollbarSize
									/ that.numImage),
							offset : 0,
							wheel : true,
							onChange : function(move) {
								move = Math.abs(move);
								if (this.isDragging) {
									that.element.setStyle('margin-left', -1
											* move);
								} else {
									that.element.get('tween').start(
											'margin-left', -1 * move);
								}
							}
						});

				this.element.getParent().addEvent('mousewheel',
						function(event) {
							gallerySlider.scrolledElement(event);
						});
			}

		});