var scrollImgBlock = new Class({
	//implements
	Implements: [Options],
	
	//options
	options: {
		scrollWrapper: false,
		counter: false,
		elems: 0,
		elemsLeg: 0,
		elemsLength: 0,
		current: 0,
		scroller: false,
		fade: true,
		autostart: true
	},
	
	initialize: function(options) {
		this.setOptions(options);
		if($(this.options.scrollWrapper) != null) {
			this.options.scroller = new Fx.Scroll($(this.options.scrollWrapper).getElement('div'),
					{
					duration: 1000,
					wait: false,
					transition: Fx.Transitions.Quad.easeInOut
			});
			this.options.counter = $('counter');
			this.options.elems = this.options.scrollWrapper.getElement('ul').getElements('li');
			if( this.options.scrollWrapper.getElement('.imgLegend') != null ) this.options.elemsLeg = this.options.scrollWrapper.getElement('.imgLegend').getElements('li');
			this.options.elemsLength = this.options.elems.length;
			this.options.current = 0;
			if( this.options.counter != null ) this.options.counter.set('text',this.options.current+1 +'/'+ this.options.elemsLength);			
			if(this.options.fade) {
				this.options.elems.each(function(elem) {
					if( this.options.elemsLength > 1 ) elem.fade(0);							 
				}.bindWithEvent(this));
				this.options.elems[0].fade(1);
				
				if( this.options.scrollWrapper.getElement('.imgLegend') != null ) {
					this.options.elemsLeg.each(function(elem) {
						elem.fade(0);							 
					});
					this.options.elemsLeg[0].fade(1);	
				}
				
							
			}
			this.crop();
		
			
		
		}

		// LEFT AND RIGHT SCROLL 
		if( this.options.scrollWrapper.getElement('a.next') != null ) {
			this.options.scrollWrapper.getElement('a.next').addEvent('click', function(){ 
				this.scrollRight();
				this.options.counter.set('text',this.options.current+1 +'/'+ this.options.elemsLength);
				return false;
			}.bindWithEvent(this));	
		}
		if( this.options.scrollWrapper.getElement('a.prev') != null ) {
			this.options.scrollWrapper.getElement('a.prev').addEvent('click', function(){ 
				this.scrollLeft();
				this.options.counter.set('text',this.options.current+1 +'/'+ this.options.elemsLength);				
				return false;
			}.bindWithEvent(this));	
		}
		
		if( this.options.autostart ) {
			var periodical = function() {
				if( this.options.elemsLength > 1 ) this.scrollRightFade();
			}.bind(this);
			var fperiodical = setInterval( periodical, 3000 );
			$(this.options.scrollWrapper).addEvent( 'mouseenter', function() {
					clearInterval( fperiodical );
			});
			$(this.options.scrollWrapper).addEvent( 'mouseleave', function() {
					fperiodical = setInterval( periodical, 3000 )
			});
		}
		
	},

	scrollLeft: function(){
		if(this.options.fade) {
			this.scrollLeftFade();
		}
		else {
			if(this.options.current <= 0) {
				this.options.current = this.options.elemsLength-1;
				this.options.scroller.toElement(this.options.elems[this.options.current]);
			}
			else {
				this.options.current--;
				this.options.scroller.toElement(this.options.elems[this.options.current]);
			}
		}

	},
	
	scrollRight: function(e){
		if(this.options.fade) {
			this.scrollRightFade();
		}
		else {
			if(this.options.current >= this.options.elemsLength-1) {
				this.options.current = 0;
				this.options.scroller.toElement(this.options.elems[this.options.current]);
			}
			else {
				this.options.current++;
				this.options.scroller.toElement(this.options.elems[this.options.current]);
			}
		}
		this.options.counter.set('text',this.options.current+1 +'/'+ this.options.elemsLength);
	},
	
	scrollLeftFade: function(){
		if(this.options.current <= 0) { 
			this.options.elems[this.options.current].fade(0);
			this.options.elemsLeg[this.options.current].fade(0);				
			
			this.options.current = this.options.elemsLength-1;
			this.options.elems[this.options.current].fade(1);
			this.options.elemsLeg[this.options.current].fade(1);				
			this.options.scroller.toElement(this.options.elems[this.options.current]);
		}
		else {
			this.options.elems[this.options.current].fade(0);
			this.options.elemsLeg[this.options.current].fade(0);				
			
			this.options.current--;
			this.options.elems[this.options.current].fade(1);
			this.options.elemsLeg[this.options.current].fade(1);				
			this.options.scroller.toElement(this.options.elems[this.options.current]);
		}
		//this.options.counter.set('text',this.options.current-1 +'/'+ this.options.elemsLength);
	},
	
	scrollRightFade: function(){
		if(this.options.current >= this.options.elemsLength-1) { 
			this.options.elems[this.options.current].fade(0);
			this.options.elemsLeg[this.options.current].fade(0);
			this.options.current = 0;
			this.options.elems[this.options.current].fade(1);
			this.options.elemsLeg[this.options.current].fade(1);
			this.options.scroller.toElement(this.options.elems[this.options.current]);
		}
		else {
			this.options.elems[this.options.current].fade(0);
			this.options.elemsLeg[this.options.current].fade(0);
			
			this.options.current++;
			this.options.elems[this.options.current].fade(1);
			this.options.elemsLeg[this.options.current].fade(1);
			this.options.scroller.toElement(this.options.elems[this.options.current]);
		}
		this.options.counter.set('text',this.options.current+1 +'/'+ this.options.elemsLength);
	},
	
	crop: function() {
		var size = 0;
		var imgElems = this.options.scrollWrapper.getElement('ul').getElements('img');
		var imgPhotoGraphers = this.options.scrollWrapper.getElement('ul').getElements('p');
		if(imgElems != null) { var i = 0;
			imgElems.each(function(elem) {
				size = elem.getSize();
				
				if(size.x < 230){
					var margin = 115-(size.x/2)
					elem.setStyle('margin-left',margin);
					//imgPhotoGraphers[i].setStyle('right', -115+(size.x/2)+15);
				}				
				if(size.y < 180){
					var margin = 90-(size.y/2)
					elem.setStyle('margin-top',margin);
					//imgPhotoGraphers[i].setStyle('margin',0);					
				}
				if(size.x > 230) elem.setStyle('width',230);
				if(size.y > 180) elem.setStyle('height',180);
				i++;
			});
		}
		
	}
	
});
			
