// CAROUSEL

var homefeed = {
	
	element_span : null,
	timer:null,
	currentIndex:0,
	
	init: function()
	{
		homefeed.element_span = $$('.feed_title');
		for(var i = 0; i < homefeed.element_span.length; i++)
		{
			if(i > 0)
			{
				homefeed.element_span[i].setStyle("display", "none");
			}
		}
		homefeed.timer = setInterval('homefeed.next()', 5000);
	},
	
	next: function()
	{
		var nextIndex = homefeed.nextIndex();
		homefeed.element_span[homefeed.currentIndex].setStyle("display", "none");
		homefeed.element_span[nextIndex].setStyle("display", "inline");
		homefeed.currentIndex = nextIndex;
	},
	
	nextIndex : function()
	{
		var nextIndex = (homefeed.currentIndex + 1 == homefeed.element_span.length)? 0 : homefeed.currentIndex + 1; 
		return nextIndex;
	}
};

var carousel = {
	
	buttonPrevId : "btn_carousel_left",
	buttonNextId : "btn_carousel_right",
	
	buttonPrev : null,
	buttonNext : null,
	
	images : [],
	imagesClass : 'carousel_image',
	
	currentIndex : 0,
	nextIndex : null,
	startAnimation : false,
	
	imageMorphTransition : 'expo:inOut',
	imageMorphDuration: 300,
	
	imagesBoxId : 'carousel_images',	
	imagesBox : null,
	
	timerInstance : null,
	timerDuration : 5000,
	timer: true,

	init: function()
	{
		
		// init buttons
		
		carousel.buttonPrev = $(carousel.buttonPrevId);
		carousel.buttonNext = $(carousel.buttonNextId);
		
		carousel.buttonPrev.addEvent("click", function(){
			if(carousel.startAnimation)
			{
				carousel.morph('prev', 0);	
			}
			return false;
		});
		
		carousel.buttonNext.addEvent("click", function(){
			if(carousel.startAnimation)
			{
				carousel.morph('next', 0);
			}
			return false;
		});
		
		// init elements
		
		carousel.images = $$('.'+carousel.imagesClass);
		carousel.imagesBox = $(carousel.imagesBoxId);
		
		carousel.disableButton();
		
		carousel.imagesBox.set('morph', {
			transition : carousel.imageMorphTransition,
			duration : carousel.imageMorphDuration,
			onComplete : function(){
				carousel.startAnimation = true;
				carousel.currentIndex = carousel.nextIndex;
				carousel.disableButton();
			}
		});
		
		if(carousel.images.length > 1)
		{
			carousel.startAnimation = true;
			
			if(carousel.timer)
			{
				carousel.timerInstance = setInterval('carousel.morph("next", 1)', carousel.timerDuration);
			}
			
		}	
		
	},
	
	morph: function(type, timed)
	{
		
		if(carousel.startAnimation)
		{
			
			carousel.startAnimation = false;
			
			// box height
			
			var imagesBoxHeight = carousel.imagesBox.getStyle('height').toInt();
			
			// indexes
			
			var currentIndex = carousel.currentIndex;
			var nextIndex = (type == "next")? currentIndex + 1 : currentIndex - 1;
			nextIndex = (nextIndex == carousel.images.length)?  0 : nextIndex;
			nextIndex = (nextIndex < 0)?  carousel.images.length - 1 : nextIndex;
			carousel.nextIndex = nextIndex;
			
			// set margin
			
			var imageHeight = imagesBoxHeight/carousel.images.length;
			var currentMargin = carousel.imagesBox.getStyle('top').toInt();
			currentMargin = (isNaN(currentMargin))? 0 : currentMargin;
			carousel.imagesBox.setStyle('top', currentMargin);
			
			var marginBox = (type == "next")? currentMargin - imageHeight : currentMargin + imageHeight;
			
			if(timed && (carousel.currentIndex == carousel.images.length - 1))
			{
				marginBox = 0
			}
			else
			{
				carousel.timerInstance = clearInterval(carousel.timerInstance);
				carousel.timerInstance = setInterval('carousel.morph("next", 1)', carousel.timerDuration);
			}
			
			carousel.imagesBox.morph({
					'top' : [currentMargin, marginBox]
				});
						
		}
	},
	
	disableButton : function()
	{
		var maxIndex = carousel.images.length - 1;
		switch(carousel.currentIndex)
		{
			case 0:
				carousel.buttonPrev.setStyle('display','none');
				carousel.buttonNext.setStyle('display','inline');
				break;
				
			case maxIndex:
				carousel.buttonPrev.setStyle('display','inline');
				carousel.buttonNext.setStyle('display','none');
				break;
				
			default:
				carousel.buttonPrev.setStyle('display','inline');
				carousel.buttonNext.setStyle('display','inline');
				break;
		}
	}
}

var store = {
	
	elements_link_less 	: null,
	elements_link_plus 	: null,
	elements_quantity 	: null,
	element_link_chart	: null,
	
	current_id			: null,
	total				: 0,
	
	chart				: {},
	
	init : function()
	{
		
		store.elements_link_less = $$('.img_less');
		store.elements_link_plus = $$('.img_plus');
		store.elements_quantity = $$('.quantity');
		store.element_link_chart = $('btn_chart');
		
		store.element_link_chart.addEvent('click', function(){
			$('user_chart_form').submit();
			return false;
		});
		
		for(var i = 0; i < store.elements_link_less.length; i++)
		{
			store.elements_link_less[i].addEvent("click", function(){
				store.less(this.id);
				return false;
			});
			
			store.elements_link_plus[i].addEvent("click", function(){
				store.add(this.id);
				return false;
			});
			
			store.elements_quantity[i].addEvent("change", function(){
				var the_value = parseInt(this.value);
				the_value = (the_value < 0)? 0 : the_value;
				this.value = (isNaN(the_value))? 0 : the_value;
				store.recalculatePrice();
			});
			
			// reset inputs
			store.elements_quantity[i].value = 0;	
			$('total_price_value').set({'html': "0 €"});		
		}
		
	},
	
	add: function(the_id)
	{
		var id = parseInt(the_id.replace("btn_plus_", ""));
		var quantity = Math.abs(parseInt($('quantity_'+id).value));
		var value = Math.abs($('price_'+id).value);
		quantity = (isNaN(quantity))? 0 : quantity + 1;
		store.current_id = id;
		$('quantity_'+id).value = quantity;
		store.calculatePrice("add", value);
		
	},
	
	less: function(the_id)
	{
		var id = parseInt(the_id.replace("btn_less_", ""));
		var quantity = Math.abs(parseInt($('quantity_'+id).value));
		var value = Math.abs($('price_'+id).value);
		quantity = (isNaN(quantity))? 0 : quantity - 1;
		store.current_id = id;
		
		if(quantity >= 0)
		{
			store.calculatePrice("less", value);
		}
		quantity = (quantity < 0)? 0 : quantity;
		$('quantity_'+id).value = quantity;
	},
	
	calculatePrice: function(operation, value)
	{
		this.total = (operation == "add")? this.total + value : this.total - value;
		store.updateChart();
		store.setTotal(store.roundNumber(this.total, 2));
	},
	
	recalculatePrice: function()
	{
		var quantity;
		var price;
		var id;
		var total = 0;
		for (var i=0; i < store.elements_quantity.length; i++) 
		{
			quantity = store.elements_quantity[i].value;
			id = parseInt(store.elements_quantity[i].id.replace("quantity_", ""));
			total += $('price_'+id).value * quantity;
		};
		this.total = total;
		store.updateChart();
		store.setTotal(store.roundNumber(this.total, 2));
	},
	
	updateChart: function()
	{
		var quantity;
		var id;
		var str_chart = "";
		for (var i=0; i < store.elements_quantity.length; i++) 
		{
			quantity = store.elements_quantity[i].value;
			if(quantity > 0)
			{
				id = parseInt(store.elements_quantity[i].id.replace("quantity_", ""));
				store.chart[id] = quantity;
				str_chart += "{" + id +":"+ quantity +"}";	
			}
		}
		$('UserChartChart').value = str_chart;
	},
	
	roundNumber: function(num, dec) 
	{
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	},
	
	setTotal: function(value)
	{
		$('total_price_value').set({'html': value + " €"});
	}
	
}

window.addEvent("domready", function(){
	
	// home
	if(document.getElementById("carousel"))
	{
		// carousel.init(); not used
		// new slideshow
		theSlideshow = new SlideShow('carousel_images', {
			delay: 6500,
			transition: 'fade',
			duration: 1000,
			autoplay: true
		});
		homefeed.init();
	}
	
	// shop
	if(document.getElementById("products_list"))
	{
		store.init();
	}
	
});


