var ThumbSlider = Class.create({
	log: function(what) {
		if (!Object.isUndefined(site)) { site.log('ThumbSlider: '+what); }
	},

	getElementsByClassName: function(classname) {
		return this.containerElement.select('.'+classname);
	},

	getElementByClassName: function(classname,firstonly) {
		var elements = this.getElementsByClassName(classname);
		if (elements[0]===undefined) {
			this.log('Unable to select at least one element with class "'+classname+'" in #'+this.containerElement.id);
			return false;
		}
		return elements[0];
	},

	initialize: function(containerElementId,options) {

		// Options
		this.options = Object.extend({
			'contentClassName':		'thumbslidercontent',
			'windowClassName':		'thumbsliderwindow',
			'leftClassName':		'sliderbutton_left',
			'rightClassName':		'sliderbutton_right',
			'itemClassName':		'thumbslideritem'
		}, options || {});

		this.log('Initializing..');

		// Element shortcuts
		this.containerElement = $(containerElementId);
		if (!this.containerElement) {
			this.log('Unable to find element #'+containerElementId);
			return false;
		}

		// Shortcuts (required!)
		if (!(this.contentElement = this.getElementByClassName(this.options.contentClassName))) { return false; }
		if (!(this.windowElement = this.getElementByClassName(this.options.windowClassName))) { return false; }
		if (!(this.leftElement = this.getElementByClassName(this.options.leftClassName))) { return false; }
		if (!(this.rightElement = this.getElementByClassName(this.options.rightClassName))) { return false; }

		this.setup();

		// Attach events
		this.attach();

		// Update buttons
		this.updateButtons();

		this.log('Initialized.');

	},

	setEnabledClass: function(element,enabled) {
		if (enabled) {
			element.removeClassName('disabled').addClassName('enabled');
		} else {
			element.removeClassName('enabled').addClassName('disabled');
		}
	},

	setup: function() {

		// Calculate window width
		this.windowWidth = this.windowElement.getWidth();

		// Calculate total width of items
		this.itemsWidth = 0;
		var lastWidth = 0;
		this.getElementsByClassName(this.options.itemClassName).each((function(element){
			lastWidth = element.getWidth();
			this.itemsWidth += lastWidth;
			this.log('Adding element with width '+lastWidth);
		}).bind(this));

		// Slider
		this.sliderPosition = 0;
		this.sliderPageWidth = this.windowWidth;
		this.sliderPositionMax = (Math.ceil(this.itemsWidth-this.windowWidth));
		this.sliderEffect = undefined;
		this.log('itemsWidth='+this.itemsWidth+' sliderPageWidth='+this.sliderPageWidth+' sliderPositionMax='+this.sliderPositionMax);

		// Update height of left/right elements
		var height = this.windowElement.getHeight();
		this.leftElement.style.height = height+'px';
		this.rightElement.style.height = height+'px';
		this.log('Button elements height updated to '+height+' pixels');

	},

	attach: function() {
		this.log('Attaching..');
		this.containerElement.observe('click',this.onClickAggregator.bindAsEventListener(this));
	},

	move: function(position) {
		if (undefined!==this.sliderEffect) { this.sliderEffect.cancel(); }
		this.sliderPosition = position;
		if (this.sliderPosition<0) { this.sliderPosition=0; }
		if (this.sliderPosition>this.sliderPositionMax) { this.sliderPosition=this.sliderPositionMax; }
		this.log('Moving to position '+this.sliderPosition);
		this.sliderEffect = new Effect.Move(this.contentElement,{mode: 'absolute',x: 0-this.sliderPosition,y: 0,duration: 1});
		this.updateButtons();
	},

	updateButtons: function() {
		this.setEnabledClass(this.leftElement, this.sliderPosition>0);
		this.setEnabledClass(this.rightElement, this.sliderPosition<this.sliderPositionMax);
	},

	onClickAggregator: function(event) {
		var element = Event.element(event);
		if (element.hasClassName('disabled')) { return; }

		if (element == this.leftElement) {
			this.log('Left clicked');
			this.move(this.sliderPosition-this.sliderPageWidth);
			Event.stop(event);
		}
		else if (element == this.rightElement) {
			this.log('Right clicked');
			this.move(this.sliderPosition+this.sliderPageWidth);
			Event.stop(event);
		}
	}

});


