
var TScroller = new Class({
	Implements: [Options, Events],
	options: {
		ItemCount:0,		// numarul de produse recomandate
		ItemDimension:175,	// latimea/inaltimea unui block de produs
		Constrain:154,		// constrainul (orizontal/vertical)
		ItemsPerScroll:6,	// numarul de produse pe scroll
		DisplayedItems:6,	// numarul de produse afisate
		ScrollDimension:1050,	// latimea/inaltimea scrollului
		MinMargin:0,		// scroll min
		MaxMargin:0,		// scroll max
		UpButton:null,		// butonul UP
		DownButton:null,	// butonul DOWN
		ScrollSpeed:2000,	//viteza de scroll (ms)
		ScrollContainer:null,// containerul produselor
		IsScrolling:false,	// daca scrollul este in desfasurare
		UseMouseWheel:true,	// functie scroll pentru mousewheel
		MouseItemsPerScroll:3, //numarul de produse miscate la scroll din mouseweheel
		IsMouseScroll:false,//flag pentru initierea scrollului din mousewheel
		ActionPreformed:false,// utilizatorul a dat scroll
		Timer:false,		// functia de animatie periodica
		PageInterval:3000,	// timerul pentru viteza de schimbare a imaginilor (ms)
		FadeSpeed:700,		// viteza pentru fade (ms),
		Orientation:'vertical' // orientare
	},
	CurrentPos:0,		// pozitia curenta a produselor
	Margin:'top',
	Dimension:'width',
	TmpXY:'left',
	initialize : function(element,options){
		var Context = this;
		this.setOptions(options)
		var Items = element.getElements('.product');
		Items.each(function(item){
			Context.options.ItemCount++;
		})
		if (element.hasClass('horizontal')){
			Context.options.Orientation = 'horizontal';
			Context.Margin = 'left';
			Context.Dimension = 'height';
			Context.TmpXY = 'top';
			element.getElement('.product-list').setStyle('width',Context.options.ItemDimension*Context.options.ItemCount)
		}
		// calculeaza inaltimea containerului
		Context.options.ScrollDimension =Context.options.ItemDimension*Context.options.ItemsPerScroll;

		// calculeaza pozitia minima admisa
		Context.options.MinMargin = -(Context.options.ItemCount*Context.options.ItemDimension)+(Context.options.DisplayedItems*Context.options.ItemDimension);
		element.getElements('.scroll').each(function(el){
			el.ControlElement = $(el.rel);
			Context.options.ScrollContainer = el.ControlElement;
			el.ControlElement.set('tween', {
				duration: Context.options.ScrollSpeed,
				onInit: function(){
					// animatia pentru scroll este initiat
					Context.options.IsScrolling = true;
				},
				onStart: function(){
					// animatia pentru scroll este initiat
					Context.options.IsScrolling = true;
				},
				onComplete: function(){
					// animatia pentru scroll s-a terminat
					Context.options.IsScrolling = false;
				}
			});
			// UP BUTTON	
			if (el.hasClass('up') || el.hasClass('left')){
				Context.options.UpButton = el;
				// din default butonul UP este inactiv
				el.addClass('disabled');
				el.addEvent('click',function(e){
					Context.options.ActionPreformed = true;
					Context.AutoAnimate();
					Context.ScrollUp(this);
			});
			// DOWN BUTTON
			}else if(el.hasClass('down') || el.hasClass('right')){
				if (Context.options.MinMargin == 0) el.addClass('disabled');
				Context.options.DownButton = el;
				el.addEvent('click',function(e){
					Context.options.ActionPreformed = true;
					Context.AutoAnimate();
					Context.ScrollDown(this);
				});
			}
		})
		if (Context.options.ItemCount<=Context.options.DisplayedItems){
			Context.options.DownButton.dispose();
			Context.options.UpButton.dispose();
			return;
		}

		// MOUSE WHEEL
		if (Context.options.UseMouseWheel)
		Context.options.ScrollContainer.addEvent('mousewheel',function(e){
			e.stop();
			if (!Context.options.IsScrolling)
				Context.options.IsMouseScroll = true;
			// scroll down
			if (e.wheel < 0) {
				Context.options.DownButton.fireEvent('click');
			// scroll up
			}else if(e.wheel>0) {
				Context.options.UpButton.fireEvent('click');
			}
		});

		Context.options.Timer = Context.AutoAnimate.periodical(Context.options.PageInterval,this);
		Context.options.Temp = Context.options.ScrollContainer.clone(true,true).inject(Context.options.ScrollContainer,'before');
		Context.options.ScrollContainer.setStyle('display','none');
		Context.options.IsScrolling = false;
		Context.options.Temp.addEvent('mouseover',function(e){
			Context.options.ActionPreformed = true;
			Context.AutoAnimate();
		}); 
	},
	AutoAnimate:function(){
		var Context = this;
		if (Context.options.ActionPreformed && Context.options.Timer) {
			$clear(Context.options.Timer);
			Context.options.ScrollContainer.setStyle('margin-'+Context.Margin, Context.options.Temp.getStyle('margin-'+Context.Margin));
			Context.options.ScrollContainer.setStyle('display', 'block');
			Context.options.Temp.destroy();
			Context.options.Timer = null;
			return false;
		}
		else if (Context.options.Timer) {
			if (Context.CurrentPos == Context.options.MinMargin) {
				Context.options.UpButton.addClass('disabled');
				Context.options.DownButton.removeClass('disabled');
				Context.CurrentPos = Context.options.ScrollDimension;
			}
			Context.ScrollDown(Context.options.DownButton);
		}
	},
	ScrollUp:function(el){
		var Context = this;
		if (Context.options.IsScrolling) {
			return false;
		}// daca animatia nu s-a terminat, nu se initializeaza un alt scroll
		// daca scrollul este la limita maxima
		if (Context.CurrentPos == Context.options.MaxMargin) {
			if (!el.hasClass('disabled'))
				el.addClass('disabled');
			return false;
		}

		// pozitia la care se opreste scrollul
		if (!Context.options.IsMouseScroll)
			var Stop = Context.CurrentPos+Context.options.ScrollDimension<=Context.options.MaxMargin?Context.CurrentPos+Context.options.ScrollDimension:Context.options.MaxMargin;
		else
			var Stop = (Context.CurrentPos+(Context.options.MouseItemsPerScroll*Context.options.ItemDimension)<=Context.options.MaxMargin)?Context.CurrentPos+(Context.options.MouseItemsPerScroll*Context.options.ItemDimension):Context.options.MaxMargin;
		// se reactiveaza butonul DOWN daca este cazul
		if (Context.options.MinMargin!=Stop && Context.options.DownButton.hasClass('disabled')){
			Context.options.DownButton.removeClass('disabled');
		}

		// animatia pentru scroll
		el.ControlElement.tween('margin-'+Context.Margin,Context.CurrentPos+'px',Stop+'px');
		// daca am atins maxima pentru scroll
		if (Stop==Context.options.MaxMargin) el.addClass('disabled')
		Context.CurrentPos = Stop; // se reactualizeaza pozitia curenta
		Context.options.IsMouseScroll = false;
	},
	ScrollDown:function(el){
		var Context = this;
		if (Context.options.IsScrolling) return false; // daca animatia nu s-a terminat, nu se initializeaza un alt scroll
		// daca scrollul este la limita minima
		//$('debug').set('text',Context.options.MinMargin);
		if (Context.CurrentPos == Context.options.MinMargin) {
			if (!el.hasClass('disabled'))
				el.addClass('disabled');
			return false;
		}
		// pozitia la care se opreste scrollul
		if (!Context.options.IsMouseScroll) {
			var Stop = (Context.CurrentPos - Context.options.ScrollDimension >= Context.options.MinMargin) ? Context.CurrentPos - Context.options.ScrollDimension : Context.options.MinMargin;
		}else 
			var Stop = (Context.CurrentPos - (Context.options.MouseItemsPerScroll * Context.options.ItemDimension) >= Context.options.MinMargin) ? Context.CurrentPos - (Context.options.MouseItemsPerScroll * Context.options.ItemDimension) : Context.options.MinMargin;
		// se reactiveaza butonul UP daca este cazul
		if (Context.options.MaxMargin!=Stop && Context.options.UpButton.hasClass('disabled')){
			Context.options.UpButton.removeClass('disabled');
		}
		// daca am atins minima pentru scroll
		if (Stop==Context.options.MinMargin) el.addClass('disabled')
		// animatia pentru scroll
		if (!Context.options.ActionPreformed) {
			var tmp = Context.options.Temp.clone().inject(Context.options.Temp,'after');
			tmp.set('tween',{
				duration:Context.options.FadeSpeed,
				onComplete:function(){
					tmp.destroy();
				}
			})
			tmp.setStyle('position','absolute')
			var tmpPos = Stop==0?Context.options.MinMargin:Context.CurrentPos;
			//tmp.setStyle('background','#FFCC00');
			tmp.setStyle('position','absolute');
			tmp.setStyle('margin-'+Context.Margin,0)
			tmp.setStyle(Context.Margin,tmpPos)
			tmp.setStyle(Context.TmpXY,0)
			tmp.setStyle(Context.Dimension,Context.options.Constrain);
			tmp.fade('out',{duration:Context.options.FadeSpeed});
			
			Context.options.Temp.setStyle('opacity','0');
			Context.options.Temp.setStyle('margin-'+Context.Margin,Stop);
			Context.options.Temp.fade('in');
		} else {
			el.ControlElement.tween('margin-'+Context.Margin, Context.CurrentPos + 'px', Stop + 'px');
		}
		Context.CurrentPos = Stop; // se reactualizeaza pozitia curenta
		Context.options.IsMouseScroll = false;
	}
});
