/**
 * Class for controlling the scroll of a DIV with overflow: hidden
 *
 * @requires	Scriptaculous (http://script.aculo.us)
 * @requires	Prototype (http://www.prototypejs.org)
 *
 * @author		Valentin VALCIU, Lunatech
 * @version		$Id: scroller.js 49 2010-03-01 15:01:51Z axiac $
 */

Scroller = Class.create({
	initialize: function(box, inner, elBackward, elForward, stepX, stepY) {
		var scroller = this;

		this.box = $(box);
		this.handle = $(inner);
		this.elBackward = $(elBackward);
		this.elForward = $(elForward);
		this.stepX = stepX;
		this.stepY = stepY;
		this.currentOffsetLeft = 0;
		this.currentOffsetTop = 0;

		this.eventBackwardScrollOnClick = this.ScrollBackward.bindAsEventListener(this);
		this.eventForwardScrollOnClick = this.ScrollForward.bindAsEventListener(this);

		this.elBackward.observe("click", this.eventBackwardScrollOnClick);
		this.elForward.observe("click", this.eventForwardScrollOnClick);

		this.updateArrows();

		this.initialized = true;
	},
	ScrollBackward: function(event){
//		if (Event.isLeftClick(event)) {
			offsetLeft = this.currentOffsetLeft;
			offsetTop = this.currentOffsetTop;

			if (this.stepX > 0 && (! offsetLeft || offsetLeft >= 0))
				return this.updateArrows();
			if (this.stepY > 0 && (! offsetTop || offsetTop >= 0))
				return this.updateArrows();

			if (this.stepX > 0) this.currentOffsetLeft += Math.min(this.stepX, Math.abs(- this.currentOffsetLeft));
			if (this.stepY > 0) this.currentOffsetTop += Math.min(this.stepY, Math.abs(- this.currentOffsetTop));

			new Effect.Move(this.handle, { x: this.currentOffsetLeft - offsetLeft, y: this.currentOffsetTop - offsetTop, transition: Effect.Transitions.sinoidal, queue: 'end'});
			Event.stop(event);
			return this.updateArrows();
//		}
	},
	ScrollForward: function(event){
//		if (Event.isLeftClick(event)) {
			offsetLeft = this.currentOffsetLeft;
			offsetTop = this.currentOffsetTop;
			boxWidth = this.getObjStyleProperty(this.box, 'width');
			boxHeight = this.getObjStyleProperty(this.box, 'height');
			innerWidth = this.getObjStyleProperty(this.handle, 'padding-left') + this.getObjStyleProperty(this.handle, 'width') + this.getObjStyleProperty(this.handle, 'padding-right');
			innerHeight = this.getObjStyleProperty(this.handle, 'padding-top') + this.getObjStyleProperty(this.handle, 'height') + this.getObjStyleProperty(this.handle, 'padding-bottom');

			if (this.stepX > 0 && (boxWidth >= this.currentOffsetLeft + innerWidth))
				return this.updateArrows();
			if (this.stepY > 0 && (boxHeight >= this.currentOffsetTop + innerHeight))
				return this.updateArrows();
			if (this.stepX > 0) this.currentOffsetLeft -= Math.min(this.stepX, Math.abs(this.currentOffsetLeft + innerWidth - boxWidth));
			if (this.stepY > 0) this.currentOffsetTop -= Math.min(this.stepY, Math.abs(this.currentOffsetTop + innerHeight - boxHeight));

			new Effect.Move(this.handle, { x: this.currentOffsetLeft - offsetLeft, y: this.currentOffsetTop - offsetTop, transition: Effect.Transitions.sinoidal, queue: 'end'});
			Event.stop(event);
			return this.updateArrows();
//		}
	},
	getObjStyleProperty: function(obj, property){
		value = obj.getStyle(property);
		if (! value)
			return 0;
		return parseInt(value.replace('px', ''));
	},
	updateArrows: function() {
		offsetLeft = this.currentOffsetLeft;
		offsetTop = this.currentOffsetTop;
		boxWidth = this.getObjStyleProperty(this.box, 'width');
		boxHeight = this.getObjStyleProperty(this.box, 'height');
		innerWidth = this.getObjStyleProperty(this.handle, 'padding-left') + this.getObjStyleProperty(this.handle, 'width') + this.getObjStyleProperty(this.handle, 'padding-right');
		innerHeight = this.getObjStyleProperty(this.handle, 'padding-top') + this.getObjStyleProperty(this.handle, 'height') + this.getObjStyleProperty(this.handle, 'padding-bottom');

//		window.status = offsetLeft +','+ offsetTop +' : '+ boxWidth +','+ boxHeight;
		this.elBackward.style.display = (offsetLeft >= 0 && offsetTop >= 0) ? 'none' : 'block';
		this.elForward.style.display = (boxWidth >= this.currentOffsetLeft + innerWidth && boxHeight >= this.currentOffsetTop + innerHeight) ? 'none' : 'block';
		return true;
	}
});

