var Scroller = new Class({

	Implements: [Events, Options],

	options: {
		area: 250,
		velocity: 1,
		onChange: function(x, y){
		  
			this.element.scrollTo(x, y);
		},
		fps: 50,
		horizontal: false, //v3 modified
		vertical: true
	},

	initialize: function(element, options){ 
		this.setOptions(options);
		this.element = document.id(element);
		
		this.listener = ($type(this.element) != 'element') ? document.id(this.element.getDocument().body) : this.element;
		this.timer = null;
		this.bound = {
			attach: this.attach.bind(this),
			detach: this.detach.bind(this),
			getCoords: this.getCoords.bind(this)
		};
	},

	start: function(){
		this.listener.addEvents({
			mouseenter: this.bound.attach, //v3 modified
			mouseleave: this.bound.detach
		});
	},

	stop: function(){
		this.listener.removeEvents({
			mouseenter: this.bound.attach,
			mouseleave: this.bound.detach
		});
		this.detach();
		this.timer = $clear(this.timer);
	},

	attach: function(){
		this.listener.addEvent('mousemove', this.bound.getCoords);
	},

	detach: function(e){
		var node = this;
		this.detachTimer = (function() {
			node.listener.removeEvent('mousemove', node.bound.getCoords);
			node.timer = $clear(node.timer);
		}).delay(50);
	},

	getCoords: function(event){
	
		this.page = (this.listener.get('tag') == 'body') ? event.client : event.page;
		if (!this.timer) this.timer = this.scroll.periodical(Math.round(1000 / this.options.fps), this);
	},

	scroll: function(){
		
		var size = this.element.getSize(),
			scroll = this.element.getScroll(),
			pos = this.element.getOffsets(),
			scrollSize = this.element.getScrollSize(),
			change = {x: 0, y: 0};

		// v3 modified
		if (!this.options.horizontal) delete this.page.x;
		if (!this.options.vertical) delete this.page.y;

		for (var z in this.page){	
		
			if (this.page[z] < (this.options.area + pos[z]) ) {			
				change[z] = (this.page[z] - this.options.area - pos[z]) * this.options.velocity;
				
			}
			else if (this.page[z] + this.options.area > (size[z] + pos[z]) )
			{			
				change[z] = (this.page[z] - size[z] + this.options.area - pos[z]) * this.options.velocity;
			}
		}
		
		
		if (change.y || change.x) {
		
			this.fireEvent('change', [scroll.x + change.x, scroll.y + change.y]);
			
		}
	}

});


var Rotator = new Class({
    	Implements: [Events, Options],
    	options: {      		
        cur: 0,          
        timeout: 2000,
        method: 'visibility'
    	},
    
     initialize: function(list, options,events){
        		 this.setOptions(options);
        	 	 this.list=list;
        		 this.length=list.length;
             if (this.options.method=='visibility') {	
        		     this.list[0].setStyle('opacity', 1);
        		} else {
              //  this.list[0].setStyle('display', 'block');
            }
    			 	 this.f.delay(this.options.timeout, this);  
              	
  	},
  	start: function() {
              this.active=true;

    },
    stop: function() {
              this.active=false;
    },
    setpos: function(pos) {
            this.options.cur = pos;
            this.slide();            
    },
   getpos: function(pos) {             
           return   this.options.cur;            
    },
    f: function() {
        if (this.active) {
            this.options.cur++;
    	      this.slide();	
        } 
               
	      this.f.delay(this.options.timeout, this);
               
    },
     //РЎРѕР±С‹С‚РёСЏ
  
    slide: function() {         
        	for(i=0;i<this.length;i++) {
        
				      this.list.setStyle('opacity', 0).set('tween', {duration: 1000});
			       
			   
			     
			  
		    	}  			         
	        if (this.options.cur >=this.length  ) this.options.cur = 0;
	        if (this.options.cur < 0) this.options.cur = this.length-1;	
                
	        var myEffects = new Fx.Morph(this.list[this.options.cur], {duration: 2000, transition: Fx.Transitions.Sine.easeOut});
				myEffects.start({
					'opacity': 1
				});
	       
          this.public.slideAfter();         
    },
    
 public: { slideAfter: function() { 
    
    }   
 } 

})


Shadowbox.init({
	language: 'ru',
	adapter: 'mootools',
	skipSetup: true,
	players:  ['img']
});
window.addEvent('domready', function() {

	// включаем shadowbox
  var Shadowbox_options = {
    resizeDuration: 0.2,
    fadeDuration: 0.2,
    viewportPadding: 10,
    handleOversize: 'drag',
    overlayOpacity: 0.5,
    viewportPadding: 5,
    autoDimensions: true,
		animate: false
  };

	list = $$(document.links);
  list = list.filter(function(item){
  	//if (item.hasClass('no')) return false;
    return item.href.test(/\.(jpe?g|png|gif|swf)$/)
  });
  if (list && list.length) {
		Shadowbox.setup(list, Shadowbox_options);
  }

 	
	$$('.scrollable img').each(function(item) {

		item.set('morph', {duration: 150});
		item.addEvent('mouseenter', function(e) {
		


		}).addEvent('mouseleave', function(e) {
		

		});

	});
  if ($chk($('tbig'))) {
  	var  newslist = $$('#tbig > img ').setStyle('opacity', 0).set('tween', {duration: 200});
 
  	if (newslist.length>0) {
  		newslist.Rotator = new Rotator(newslist, {timeout: 4000});   
      	 
  		newslist.Rotator.start();		
  	} 
			
}
	//скроллер
	$$('.scrollable div').each(function(item) {
		var area = 200;
			
		item.myScroller = new Scroller(item, {
				area: area,
				velocity: 0.05
				
		});

		item.myScroller.start();
	});
      $$('.arrow').addEvent('click', function(e) {
      	e.preventDefault();
      })
	$$('.arrow').addEvent('mousedown', function(e) {
		var  deltay=0;
		var node =this.getParent().getParent().getParent().getElement('.scrollable div');
	         if (this.hasClass('arrow-up')) deltay = -6;
		     if (this.hasClass('arrow-down')) deltay = 6;
		     
	
		this.timer = (function() {
			var curCroll = node.getScroll();		
			node.scrollTo(0,curCroll.y+deltay);
		
		}).periodical(20);		
       	e.preventDefault(); 
	}).addEvent('mouseup', function(e) {
			
		this.timer = $clear(this.timer);
		
		e.preventDefault();
	});


	
	
 //JSON для продукции
 $$('.js-cat_item').each(function(item) {		
		item.addEvent('mouseenter', function(e) {
		
				var node = this;
		    	new Request({
				method: 'post',
				url: node.href,
				'link': 'ignore',
				data: {
					ajax: 1
				},
				onSuccess: function(response) {
					var index = node.get('rel');
					$('js-name'+index).setStyle('opacity', 0);
				//	$('js-image'+index).setStyle('opacity', 0);
					$('js-short'+index).setStyle('opacity', 0);
				
				    response = JSON.decode(response);
				    
				    $('js-name'+index).set('html', response.name);
				    $('js-image'+index).set('src', "/images/u/catalog/thumb_2"+response.image);
					$('js-short'+index).set('html', response.short);
				$('js-name'+index).set('morph', {duration: 100});
        	$('js-name'+index).morph({
						'opacity': 1						
					});
				//	 $('js-image'+index).morph({
				//		'opacity': 1						
				//	});
				$('js-short'+index).set('morph', {duration: 100});
					 $('js-short'+index).morph({
						'opacity': 1						
					});
			  }
			}).send();
			e.preventDefault(); 
			
				 
    	});
    	item.addEvent('click', function(e) {
		
			
			e.preventDefault(); 
			
				 
    	});
     


});
//менюшка 
if ($chk('#menu')) {

		$$('#menu .parent').addEvent('mouseenter', function(event) {
		
				event.preventDefault();
			   
				var myFx = new Fx.Tween($(this).getElement('ul'));
		
		
		
		
		$(this).getElement('ul').setStyle('visibility', 'visible');
			$(this).getElement('ul').morph({
			height: $(this).getElement('ul').verticalSize
				,opacity: 1
		});
		
			

		
		
		
			
		//	$(this).getElement('ul').fade('show');
		    //  	myFx.start('display', 'block');
				
     }).addEvent('mouseleave', function(event) {
		
				event.preventDefault();
		
				//var myFx = new Fx.Tween($(this).getElement('ul'));
				// 	myFx.set('tween', {duration: 'long'});
                //  $(this).getElement('ul').fade('hide'); //Fades "myElement" out.
                 $(this).getElement('ul').morph({
				height: 0
				,opacity: 0
			});
			//	myFx.start('display', 'none').delay(600);
		      
				
     });


}
 
 
});
