var iScroller = new Class({
	options:{
		elementWidth: 254,
		elementPadd: 20,
		step: 1
	},
	
	initialize: function(options){		
		this.setOptions(options);	
		this.initScroller();
	},
	
	initScroller: function(){	
		this.scroll_blocks = $('scroll_line').getElements('div.img_block');
		
		this.stopScroll = false;
		
		this.count = this.scroll_blocks.length;

		switch (screen.width){
			case 1024:				
				this.scrollWidth = 274;
				
				break;
			case 1280:
				this.options.elementPadd = 94;
				this.scrollWidth = 348;
				$('scroll_container').setStyle('width', '950px');
				
				this.scroll_blocks.each(function(block,i){					
					if (this.count-1 != i){
						block.setStyle('margin-right', '94px');
					}
				}.bind(this));				
				break;
			case 1680:
				this.scrollWidth = 274;
				break;
		}		
		
		var lng = (this.count*this.options.elementWidth)+((this.count-1)*this.options.elementPadd);	
		this.scrollLine = $('scroll_line');
		this.scrollLine.setStyle('width',lng.toInt() + 'px');
		this.counter = 0;
		this.limit = Math.round(this.count / (this.options.step));
				
		this.effect = new Fx.Morph('scroll_line', {duration: 1300, transition: 'back:out', onComplete : function(){
			this.scroll = true;			
		}.bind(this)});
		
		this.scrollLine.setStyle('left', 0);

		this.scroll = true;
		this.leftUnactive = true;
		
		$('right_nav').addEvent('click', this.scrollToLeft.bind(this));		
		$('left_nav').addEvent('click', this.scrollToRight.bind(this));
		
		if (this.count <= 3)
		{			
			this.setButtonsToUnactive();
		}
		
		this.addInteractive();

	},
	
	scrollToLeft: function(e){
		if ((this.counter!=this.limit.toInt() - 3) && !this.stopScroll){
			
			this.effect.cancel();			
			this.counter++;	
			
			if (this.leftUnactive){					
				$('left_nav').setStyle('background', 'url(/images/left_nav.png) no-repeat left top');
				this.leftUnactive = false;
			}

			this.effect.start({'left': ((-this.scrollWidth*this.options.step)*this.counter)});
		}	

		if (this.counter == (this.limit - 3)){
			$('right_nav').setStyle('background', 'url(/images/right_nav_unactive.png) no-repeat left top');
			this.rightUnactive = true;
		}
		
		return false;
	},
	
	scrollToRight: function(e){		
		//e.stop();		
		if (this.counter!=0 && !this.stopScroll){
			this.counter--;	
			
			this.effect.cancel();
			
			if (this.rightUnactive){				
				$('right_nav').setStyle('background', 'url(/images/right_nav.png) no-repeat left top');
				this.rightUnactive = false;
			}

			this.effect.start({'left': ((-this.scrollWidth*this.options.step)*this.counter)});	

			if (this.counter==0){
				$('left_nav').setStyle('background', 'url(/images/left_nav_unactive.png) no-repeat left top');
				this.leftUnactive = true;
			}			
		}		
		return false;
	},
	
	setButtonsToUnactive: function(){
		$('left_nav').setStyle('background', 'url(/images/left_nav_unactive.png) no-repeat left top');
		this.leftUnactive = true;
		$('right_nav').setStyle('background', 'url(/images/right_nav_unactive.png) no-repeat left top');
		this.rightUnactive = true;
		this.stopScroll = true;
	},
	
	addInteractive: function(){		
		$('scroll_line').getElements('div.img_block').each(function(block){

			var img_block = block.getElement('div.main');
			var shadow = block.getElement('div.shadow');
			
			img_block.set('morph', {duration: 500, transition: 'back:out'});
			shadow.set('morph', {duration: 500, transition: 'back:out'});
			
			block.getElement('div.main').addEvents({
				'mouseenter': function(){					
					
					img_block.get('morph').cancel();
					shadow.get('morph').cancel();
					
					img_block.morph({'margin-top': -5});
					shadow.morph({'margin-top': 5});
					
					if (!Browser.Engine.trident){
						//shadow.morph({'opacity': 0.6});
					}					
				},				
				'mouseleave': function(){					
					img_block.get('morph').cancel();
					shadow.get('morph').cancel();
					
					img_block.morph({'margin-top': 0});				
					shadow.morph({'margin-top': 0});

					if (!Browser.Engine.trident){
						//shadow.tween('opacity', 1);
					}					
				}
			});
		});	
	}
});

iScroller.implement(new Options, new Events);
