/**
 * IMoveControlSlider - Plugin di controllo 
 * 
 * alfredo.cerutti@intercom.it
 * 
 * 
 * RICHIEDE slider.js !!
 * 
 */

IMoveControlSliderOptions = Object.extend({
	fileBottomZoomOutImage: '/images/imove/control/slider_out.gif',
	fileBottomZoomInImage : '/images/imove/control/slider_in.gif',
	min:1,
	max: 100,
	fitViewZoomFactor: 1 // when appling fitView divide max height/width by this factor
});
	
var IMoveControlSlider = Class.create();

IMoveControlSlider.prototype = {

	initialize: function(conf){		 
		this.min = conf.min || IMoveControlSliderOptions.min;
		this.max = conf.max || IMoveControlSliderOptions.max;
		this.fitViewZoomFactor = conf.fitViewZoomFactor || IMoveControlSliderOptions.fitViewZoomFactor;
	},
	
	_setParent:function(parent) {	
		this.parent = parent;		
	},
	
	_appendControls: function() {
			
		$('imove_Image_div').insert({ top: Builder.node('div', {id: 'imove_control_slider'}) });
			
        $('imove_control_slider').insert({ top: 
			Builder.node('div', { id: 'imove_control_slider_up_div', href: '#' }, 
				Builder.node('a', { id: 'imove_control_slider_up', href: '#' }, 
					Builder.node('img', { src: IMoveControlSliderOptions.fileBottomZoomInImage})
				)
			)		
		});
		
		
		$('imove_control_slider').insert({ top: 
			Builder.node('div', { id: 'imove_control_slider_bar', className: 'imove_control_slider_bar'}, 
				Builder.node('div', { className: 'imove_control_slider_handle'})
		)}); 
		
		$('imove_control_slider').insert({ top: 
			Builder.node('div', { id: 'imove_control_slider_down_div', href: '#' }, 
				Builder.node('a', { id: 'imove_control_slider_down', href: '#' }, 
					Builder.node('img', { src: IMoveControlSliderOptions.fileBottomZoomOutImage})
				)
			)		
		});
		
		this._setupSliderControl();
	},
	
	_addEvents: function () {
		$('imove_control_slider_down').observe('click', (function(event) { event.stop(); this.jumpTo(this.oldValue-1); }).bind(this));
		$('imove_control_slider_up').observe('click', (function(event) { event.stop(); this.jumpTo(this.oldValue+1); }).bind(this));
	},
	
	move : function(direction) {
		var offset = this.parent.imove_image.positionedOffset(); //cumulativeOffset();
		
		if (direction == 'sud') {
			move = {
				x: 0,
				y: -this.parent.controlsStep
			};
			var dim = this.parent.imove_image.getDimensions();
			var residuo = (dim.height - this.parent.max_height) + offset[1];
			
			if (residuo  < this.parent.controlsStep) {
				if (residuo < 0) residuo = 0;	
				move.y  = -residuo;
			}			
		}
		if (direction == 'nord') {
			move = {
				x: 0,
				y: this.parent.controlsStep
			};			
			var dim = this.parent.imove_image.getDimensions();
			var residuo = (dim.height - this.parent.max_height) + offset[1];			
			if (residuo > this.parent.controlsStep) {				
				if (offset[1]<0) residuo = this.parent.controlsStep - residuo;
				move.y = -residuo;
			}
		}
		if (direction == 'est') {
			move = {
				x: -this.parent.controlsStep,
				y: 0
			};
			var dim = this.parent.imove_image.getDimensions();
			var residuo = (dim.width - this.parent.max_width) + offset[0];
			
			if (residuo  < this.parent.controlsStep) {
				if (residuo < 0) residuo = 0;	
				move.x  = -residuo; 
			}			
		}
		if (direction == 'ovest') {
			move = {
				x: this.parent.controlsStep,
				y: 0
			};
			var dim = this.parent.imove_image.getDimensions();
			var residuo = (dim.width - this.parent.max_width) + offset[0];

			if (Math.abs(offset[0]) < this.parent.controlsStep) {
				move.x = -offset[0];
			}
		}
		
		options = { queue: 'end', duration: this.parent.moveDuration, frequency: 3 };
		new Effect.Move(this.parent.imove_image, { x: move.x , y:move.y }, options);
	},
		   
    jumpTo : function(value){
        this.innerSlider.setValue(value);
	},
	
	_setupSliderControl : function(d) {
		var slider = $('imove_control_slider_bar');
		this.oldValue = this.min;
		 
		this.innerSlider = new Control.Slider(slider.down('.imove_control_slider_handle'), slider, {
			range: $R(this.min, this.max),		
			sliderValue: this.min,			
			onSlide: (this.onSlideFn).bind(this),			
			onChange: (this.onChangeFn).bind(this)
		});			
    },	
	
	onSlideFn: function(value) {		
		value = Math.round(value);
		
		if (value != this.oldValue) {
			if (value > this.oldValue) {
				this.zoom('in');
			} else {
				if (value == this.min) {					
					this.fitView(); // use fitView from plugin
				} else {
					this.zoom('out');	
				}				
			}					
		} else {
			//console.log('do nothing - zoom step the same as before');			
		}
		this.oldValue = value;
	},
	
	
	_getFitBase : function(value) {
		return Math.round(value / this.fitViewZoomFactor);
	},
	
	fitView: function() {	
		if (this.parent.max_height >= this.parent.max_width) {
			w = this._getFitBase(this.parent.max_width);
			this.parent.imove_image.setStyle({width : w+'px', height: ''}); // height will follow :-)	
		} else {
			h = this._getFitBase(this.parent.max_height);
			this.parent.imove_image.setStyle({height : h+'px', width: ''}); // width will follow :-)			
		}
		this.parent.centerImage();
		this.parent.hideCursors();					
	},	

	
	onChangeFn: function(value) {
		
		value = Math.round(value);
		//console.log('v', value, 'oldV', this.oldValue);
		
		if (value != this.oldValue) {
			if (value > this.oldValue) {
				this.zoomLoop('in', (value-this.oldValue));
			} else {
				if (value == this.min) {
					this.fitView(); // use fitView from plugin
				} else {
					this.zoomLoop('out', (this.oldValue-value) );	
				}				
			}					
		} else {
			//console.log('do nothing - zoom step the same as before');			
		}
		this.oldValue = value;
	},
	
	zoom: function(direction, factor) {
		this.parent.showCursors();
		
		if (direction == 'in') var z = 150;
		if (direction == 'out') var z = 50;		
		
		new Effect.Scale(this.parent.imove_image, z, {
			queue: 'end',
			scaleFrom: 100.0,
			scaleFromCenter: true,
			duration: this.parent.zoomDuration,
			afterUpdate: (function(){
				this.parent.centerImage();
			}).bind(this)
		});
		
		// zoomato troppo?
		var dim =  this.parent.imove_image.getDimensions();		
		if (this.parent.imove_image.height < this.parent.max_height && this.parent.imove_image.width < this.parent.max_width) {
			this.jumpTo(this.min);
		}		
	},		
	
	zoomLoop: function(direction, factor) {		
		for (var i = 0; i < factor; i++) {
			this.zoom(direction);
		}
	},	
	
	wheelFn: function(delta) {
		if (delta > 0) {
			this.jumpTo(this.oldValue+1);
		} else {
			this.jumpTo(this.oldValue-1);
		}
	},	
	
	reset : function(){
		this.jumpTo(this.min);
	}
	
	
}
