/**
 * @author Profitroom
 */
 
 var scrollGallery = new Class({
    options:{
		galleryId:'scrollGallery',
		pagesId:'.scrollPages',
		galleryContId:'scrollGalleryContainer',
		scrollLeft:'scrollGalleryLf',
		scrollRight:'scrollGalleryRg',
		eventType:'click',
		periodical:5000,
		galleryOrientation:'horizontal',
		autoscroll:true
    },
    initialize:function(options){
		
			
		this.setOptions(options);
		var $this = this;
		this.gallery = $(this.options.galleryId);
		this.pages = $$(this.options.pagesId);
		this.galleryCont = $(this.options.galleryContId);
		this.scrollLeftCont = $(this.options.scrollLeft);
		this.scrollRightCont = $(this.options.scrollRight);
		
		this.galleryOrientation = this.options.galleryOrientation;	
		this.scrollDirection = 0;								//0=right, 1=left
		this.currentPage = 0;
		this.autoscroll = this.options.autoscroll;
		this.currentWidth = 0;
		
		if (this.galleryOrientation=='horizontal') 
		{
			this.fullWidth = this.pages.length * (this.pages[0].getScrollSize().x);
			this.scrollWidth = this.pages[0].getScrollSize().x;
			this.galleryCont.setStyle('width', this.fullWidth);
		}
		else if(this.galleryOrientation == 'vertical')
		{
			this.fullWidth = this.pages.length * (this.pages[0].getScrollSize().y);
			this.scrollWidth = this.pages[0].getScrollSize().y;
			this.galleryCont.setStyle('height', this.fullHeight);
		}
		
		this.currentWidth = this.scrollWidth*6;
		this.currentPage = 5;
			
		this.sg = new Fx.Scroll(this.options.galleryId,{
		    link:'cancel',
		    duration:1000
		});
		
		this.galleryCont.addEvent('mousewheel', function(event) {
			var ev = new Event(event);
			var orient = ev.wheel;
			ev.stop();		
			if(orient<0)
				this.scrollRight()
			else	
				this.scrollLeft()
		}.bind(this))
		
		if(this.scrollLeftCont && this.scrollRightCont)
		{
		    this.scrollLeftCont.addEvents({
				'click':function(){
				    $this.scrollLeft()
				}.bind(this),
				'mouseenter':function(e){
				    var ev = new Event(e);
				    ev.stopPropagation();
				    this.stopAutoScroll();
				}.bind(this)
			    })
			this.scrollRightCont.addEvents({
				'click':function(){
				    $this.scrollRight()
				}.bind(this),
				'mouseenter':function(e){
				    var ev = new Event(e);
				    ev.stopPropagation();
				    this.stopAutoScroll();
				}.bind(this)
		    });
		}
		
		if(this.autoscroll)
		{
		    this.startAutoScroll();
		    $(this.options.galleryId).addEvents({
			'mouseenter':function(e){
			    var ev = new Event(e);
			    ev.stopPropagation();
			    this.stopAutoScroll();
			}.bind(this),
			'mouseleave':function(e){
			    var ev = new Event(e);
			    ev.stopPropagation();
			    this.startAutoScroll();
			}.bind(this)
		    });
		}
		this.scrollGallery(0);
    },
    scrollGallery:function(value){
		if(this.galleryOrientation == 'horizontal')
			this.sg.start(value-(6*this.scrollWidth), 0);
		else
			this.sg.start(0,value-(6*this.scrollWidth));
	},
	scrollLeft:function(){
	if((this.currentWidth-this.scrollWidth)>=(this.scrollWidth*6) && (this.currentPage-1)>=0)
		{
		    this.scrollGallery((this.currentWidth-this.scrollWidth));
		    this.currentWidth-=this.scrollWidth;
		    this.currentPage--;
		    return ;
		}
    },
    scrollRight: function(){
		if ((this.currentWidth+this.scrollWidth)<=this.fullWidth && (this.currentPage+1)<this.pages.length) {
		    this.scrollGallery((this.currentWidth+this.scrollWidth));
		    this.currentWidth+=this.scrollWidth;
		    this.currentPage++;
		    return;
		}
    },
    autoScroll:function(){
		if((((this.currentWidth+this.scrollWidth) >= this.fullWidth) || (this.currentPage == this.pages.length-1)) && this.scrollDirection == 0)
		{
		    return this.scrollDirection = 1;
		}
		if((this.currentWidth == 0||this.currentPage ==0) && this.scrollDirection == 1)
		{
		    return this.scrollDirection = 0;
		}
		if(this.scrollDirection == 1)
		    this.scrollLeft();
		if (this.scrollDirection == 0)
		    this.scrollRight();
	    },
	    startAutoScroll:function(){
		var $this = this;
		$clear(this.timer);
		this.timer = (function(){
		    $this.autoScroll();
		}).periodical(this.periodical);
		this.scrollInProgress = true;
    },
    stopAutoScroll:function(){
		$clear(this.timer);
		this.scrollInProgress = false;
    }
});
scrollGallery.implement(new Options);


var offerImages = new Class({
	options:{
		mainId:'.paddingLeft',
		holderId:'.so_element',
		boxId:'.hoverAHome',
		link:'a.hoverPakiets'
	},
	initialize:function(options)
	{
		this.setOptions(options);
		this.offerHolders = $(this.options.mainId).getElements(this.options.holderId);
		this.boxes = this.offerHolders.getElements(this.options.boxId);
		this.offerActivators = this.offerHolders.getElements(this.options.link);
		
		this.offerActivators.each(function(el,i)
		{
			this.boxes[i].setStyle('opacity',0.8);
			this.topVal = this.boxes[i].getStyle('top');	            			
			el.index = i;
			el.addEvent('mouseenter',(function()
			{
				this.boxes[el.index].tween('top',0);
				
			}).bind(this));    
			
			el.addEvent('mouseleave',(function()
			{
				
				this.boxes[el.index].tween('top',this.topVal);
				
			}).bind(this));
			        			
			
		},this);
	}

});
offerImages.implement(new Options());

