if (typeof(AC) == "undefined") { AC = {}; }

AC.navigateur = {
	productSlider: null,
	sliderVal: 0,
	animationId: false,
	viewportWidth: 946,
	contentWidth: 946,
	categories: [  // init with offsets, this can be overridden at init
		{id: 'corsaires_cat1', offset: 0}, 
		{id: 'corsaires_cat2', offset: 0.32}
	],
	isSliding: false,
	lastX: 0.32,
	isMouseDown: false,
	dif: 0,
	overlap: 0,
	offsetImageWidth: 127,
	sliderOffset: 291,
	offsetContentWidth: -946,
	clicked: false,
	startIndex: 0,
	arrowScrollAmount: 0.24,
	init: function(setupArgs) {
		if (typeof(setupArgs.categories) != 'undefined') this.categories = setupArgs.categories;
		if (typeof(setupArgs.imageOverlap) != 'undefined') this.overlap = setupArgs.imageOverlap;
		if (typeof(setupArgs.sliderCentering) != 'undefined') this.sliderOffset = setupArgs.sliderCentering;
		if (typeof(setupArgs.initialCategory) != 'undefined') this.startIndex = setupArgs.initialCategory;
		if (typeof(setupArgs.arrowScrollAmount) != 'undefined') this.arrowScrollAmount = setupArgs.arrowScrollAmount;
		
		$('corsaires_slidertrack').style.visibility = "visible";
		$('corsaires_navcontainer').style.overflow = "hidden";
		
		this.viewportWidth = $('corsaires_navcontainer').getWidth();
		this.offsetImageWidth = $$('#corsaires_slider .corsaires_image')[0].getWidth()-this.overlap;
		this.contentWidth = this.offsetImageWidth * $$('#corsaires_slider .corsaires_image').length;
		this.offsetContentWidth = -1 * (this.contentWidth - this.viewportWidth);
		
		this.productSlider = new Control.Slider('corsaires_sliderhandle', 'corsaires_slidertrack', {
				axis:'horizontal' 
		});
		
		AC.navigateur.animateSlide(this.categories[this.startIndex].offset); // initial slide
		
		// Slider callbacks
		this.productSlider.options.onChange = function(value) {
			$('corsaires_sliderhandleimage').style.left = $('corsaires_sliderhandle').style.left;
			
		 if (AC.navigateur.isThrow && ! AC.navigateur.isSliding) {
				AC.navigateur.isSliding = true;
				AC.navigateur.isThrow = false;
				
				var mod = value + AC.navigateur.throwMod;
				if (mod < 0) mod = 0;
				if (mod > 1) mod = 1;
				AC.navigateur.animateSlide(mod);
			}
			else if (! AC.navigateur.isSliding && value){
				AC.navigateur.isSliding = true;
				AC.navigateur.animateSlide(value);
			}	
		};

		this.productSlider.options.onSlide = function(value) {		 
			$('corsaires_sliderhandleimage').style.left = $('corsaires_sliderhandle').style.left;
		if (value && ! AC.navigateur.isSliding) {
					AC.navigateur.isSliding = true;
				 	AC.navigateur.isThrow = false;
					if (AC.navigateur.isMouseDown) {
						AC.navigateur.dif = value - AC.navigateur.lastX;
						AC.navigateur.lastX = value;	
						
						// check for throw, the dif thresholds will affect how easily the throw happens
						if(AC.navigateur.dif > 0.05) {
							AC.navigateur.isThrow = true;
							AC.navigateur.throwMod = 0.2;
						}
						else if (AC.navigateur.dif < -0.04) {
							AC.navigateur.isThrow = true;
							AC.navigateur.throwMod = -0.2;
						}
					}
			
					var w = AC.navigateur.offsetContentWidth;
					$('corsaires_slider').style.left = w * value + "px";
					
					this.sliderVal = value;
					AC.navigateur.lastX = value;
					AC.navigateur.colorCats();		
					AC.navigateur.isSliding = false; // reset
			}
			
			Element.setStyle($('corsaires_navcontainer'), { overflow: "hidden"} );
		};
		
		Event.observe('corsaires_slidertrack', 'mousedown', function(e) {
			var o = e.offsetX || e.layerX;
			if (Event.element(e).id =='corsaires_slidertrack' && o < 100) AC.navigateur.animateSlide(0);
		});
		Event.observe('corsaires_leftarrow', 'mousedown', function() {
			AC.navigateur.left();
		});
		Event.observe('corsaires_rightarrow', 'mousedown', function() {
			AC.navigateur.right();
		});
		
		Event.observe('corsaires_sliderhandle', 'mousedown', function() {
			AC.navigateur.isMouseDown = true;
			$('corsaires_sliderhandle').style.zIndex = "5";
		});
		
		Event.observe('corsaires_sliderhandle', 'mouseup', function() {
			AC.navigateur.isMouseDown = false;
		});
		
		AC.navigateur.categories.each(function(c) {
			Event.observe($(c.id), 'mouseup', function(e) {
				AC.navigateur.animateSlide(c.offset);
			});
		});			
	},
	
	animateSlide: function(toX) {
		// make sure toX is sane:
		if (toX > 1) toX = 1;
		if (toX < 0) toX = 0;
		AC.navigateur.sliderVal = toX;
		window.clearInterval(AC.navigateur.animationId); // kill any running animation
		var w = AC.navigateur.offsetContentWidth;
		var stopPoint = w * toX;
		//stopPoint = Math.round(stopPoint / AC.navigateur.offsetImageWidth) * AC.navigateur.offsetImageWidth;
		var sliderStopPoint = (Math.round(AC.navigateur.viewportWidth - AC.navigateur.sliderOffset) * toX);
		
		AC.navigateur.isSliding = true;
 {
			AC.navigateur.animationId = window.setInterval(function() {
				var sliderPos = parseInt($('corsaires_slider').getStyle('left')) || 0;
				var handlePos = parseInt($('corsaires_sliderhandle').getStyle('left')) || 0;
				var x = AC.navigateur.calculateDecel(sliderPos, stopPoint);
				var sx = AC.navigateur.calculateDecel(handlePos, sliderStopPoint); 
				$('corsaires_slider').style.left = x + "px";
				$('corsaires_sliderhandle').style.left = sx + "px";
				$('corsaires_sliderhandleimage').style.left = sx + "px";		
				AC.navigateur.colorCats();
			
				if (x == stopPoint) {
					window.clearInterval(AC.navigateur.animationId);
					AC.navigateur.isSliding = false;			
				}
			}, 30);
		}
	},
	
	colorCats: function() {
		var sliderX = parseInt($('corsaires_sliderhandle').getStyle('left')) + (($('corsaires_sliderhandle').getWidth()-20)/2);
		AC.navigateur.categories.each(function(c) {
			var left = parseInt($(c.id).getStyle('left')) 
		 	var clr = Math.ceil((Math.min(sliderX,left) / Math.max(sliderX,left))*10);
		 	$(c.id).className = 'corsaires_catclass'+clr;
		});
	},
	
	left: function() {
		AC.navigateur.animateSlide(AC.navigateur.sliderVal - AC.navigateur.arrowScrollAmount);
	},
	
	right: function() {
		AC.navigateur.animateSlide(AC.navigateur.sliderVal + AC.navigateur.arrowScrollAmount);
	},
	
	calculateDecel: function(from, to) {
		var n = from - Math.floor( (from - to) * .4);
		if (Math.abs(from - to) < 4) return to;
		else return n;
	}
};
