/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev: 1 $
 * $Author: cs $
 * $Date: 2010-12-27 17:07:22 +0100 (Mon, 27 Dec 2010) $
 *
 */
document.observe('dom:loaded', function() {

	initFeaturedProductTT();	
	//initReflections('.article-carousel .container ul img');
	var pcarousel = new UI.Carousel('featured-products');
	var artCarousel = new UI.Carousel('article-carousel');
});

/**
 * Initialize featured product tooltips
 * 
 * @example initFeaturedProductTT()
 */
function initFeaturedProductTT() {
	
	$$('#featured-products li').each(function(tt) {
		
		var txt = tt.down('span');
		if(txt !== undefined) {
			new Tip(tt, txt);
			txt.hide();
		}
	});
}

/**
 * Init reflections for images in a given selector
 * 
 * @param {String} selector
 * @example initReflections('img')
 */
function initReflections(selector) {

	$$(selector).each(function(r) {
		Reflection.add(r, { height: 0.3, opacity: 0.5 });
	});
}

/**
 * Create a slider function
 * 
 * @param {Object} slider
 * @param {Object} controls_slider
 * @param {Object} controls
 */
var Clicksports_Slider = Class.create({

	/**
	 * @constructor
	 */	
	initialize: function(slider, controls_slider, controls) {
		
		this.slider = slider;
		this.controls_slider = controls_slider;
		this.controls = controls;
		this.scrolledByControl = false;

		this.oldIndex = 0;

		this.controls_slider.elementSize = this.getControlsElementSize();
		this.setControlsElementSize();

		// Set click listener to control slider
		this.initListeners();

		// Set default direction
		this.scrollDirection = 'right';

		this.slider.observe('scroll:started', function(event) {

			if(this.slider.scrollDirection == 'left') {
				
				var scrollOffset = -1;
				if (!this.scrolledByControl) {
					this.controls.previous();
					this.scrolledByControl = false;
				}
			} else {
				var scrollOffset = 1;
				this.scrollDirection = 'right';
				if (!this.scrolledByControl) {
					this.controls.next();
					this.scrolledByControl = false;
				}
			}

			if(!this.scrolledByControl) {
				var sindex = (this.slider.currentIndex() + scrollOffset);
				this.controls_slider.scrollTo(sindex);
			}
        }.bind(this));
	},
	
	/**
	 * Get the li with maximal width and return it as max width
	 */
	getControlsElementSize: function() {

		this.lm = parseInt(this.controls_slider.elements.first().getStyle('margin-left').replace('px', ''));
		this.rm = parseInt(this.controls_slider.elements.first().getStyle('margin-right').replace('px', ''));
		
		this.elmsize = [];
		var counter = 0;
		
		this.controls_slider.elements.each(function(li) {
			
			var width = 0;
			width += li.getWidth();
			
			this.elmsize[counter] = width;
			counter++;
		}, this);
		
		// Define array sorting for integers and sort the sizes according to it
		var numsort = function(a, b) { return a-b; };
		
		this.elmsize.sort(numsort);

		this.elmWidth = this.elmsize.last();
		this.elmFullWidth = this.elmWidth + this.lm + this.rm;

		// Try to calculate the next value where we have even options
		var container_size = this.slider.container.up('div').getWidth();
		var maxElms = Math.floor(container_size / this.elmFullWidth);
		
		// Try to max out the non used space
		this.roundSize = (container_size/maxElms);
		
		return this.roundSize;
	},
	
	/**
	 * Set the calculated size for all lis
	 */
	setControlsElementSize: function() {
		
		var width = this.roundSize - this.lm - this.rm;
		this.controls_slider.elements.invoke('setStyle', { width: width + 'px' });
	},
	
	/**
	 * Initialize needed click listeners
	 */
	initListeners: function() {

		this.controls_slider.elements.each(function(li) {

			li.down('a').observe('click', function(e) {

				this.scrolledByControl = true;
				var elm = e.findElement('li');
				var index = this.controls_slider.elements.indexOf(elm);
				this.slider.scrollTo(index);
				this.controls_slider.scrollTo(index);
				this.scrolledByControl = false;
			}.bind(this));
		}, this);
	}
});

