﻿if (typeof(MR) == "undefined"){ MR={}; }

MR.HorizontalScroller={
	pageId:0,
	productSlider:null,
	sliderVal:0,
	animationId:false,
	viewportWidth:984,
	contentWidth:984,
	contentLength:1,
	contentLengthColumn2:0,
	contentLengthColumn3:0,
	contentLengthColumn300px:0,
	arrowScrollAmount:0.24,
	isSliding:false,
	lastX:0.32,
	isMouseDown:false,
	dif:0,
	overlap:0,
	visibleColumns:4,
	offsetImageWidth:137,
	offsetImageWidthColumn2:0,
	offsetImageWidthColumn3:0,
	offsetImageWidthColumn300px:0,
	sliderOffset:291,
	offsetContentWidth:-984,
	clicked:false,
	startIndex:0,
	isIpScroll:false,
	hasIpDragged:false,

	init:function(setupArgs) {
		$('pb-productsliderhandleimage').style.left = $('pb-productsliderhandle').style.left;

		if(typeof(setupArgs.imageOverlap) != 'undefined') this.overlap = setupArgs.imageOverlap;
		if(typeof(setupArgs.sliderCentering) != 'undefined') this.sliderOffset = setupArgs.sliderCentering;
		if(typeof(setupArgs.arrowScrollAmount)!='undefined') this.arrowScrollAmount = setupArgs.arrowScrollAmount;
		if(typeof(setupArgs.visibleColumns)!='undefined') this.visibleColumns = setupArgs.visibleColumns;

		$('pb-productslidertrack').style.visibility = "visible";
		$('scrollboxcontainer').style.overflow = "hidden";

		this.viewportWidth = $('scrollboxcontainer').getWidth()

		this.contentLength = $$('#columns .column').length;
		this.contentLengthColumn2 = $$('#columns .columnx2').length;
		this.contentLengthColumn3 = $$('#columns .columnx3').length;
		this.contentLengthColumn300px = $$('#columns .column300px').length;
		//this.contentLengthColumnQuarter = $$('#columns .quarter').length;

		var extraSingleColumns = 0;

		if (this.contentLength > 0) {
			this.offsetImageWidth = $$('#columns .column')[0].getWidth();
		}

		if (this.contentLengthColumn2 > 0) {
			extraSingleColumns += this.contentLengthColumn2*2;
			this.offsetImageWidthColumn2 = $$('#columns .columnx2')[0].getWidth();
		}

		if (this.contentLengthColumn3 > 0) {
			extraSingleColumns += this.contentLengthColumn3*3;
			this.offsetImageWidthColumn3 = $$('#columns .columnx3')[0].getWidth();
		}

		if (this.contentLengthColumn300px > 0) {
			this.offsetImageWidthColumn300px = $$('#columns .column300px')[0].getWidth();
		}

		if (this.contentLengthColumnQuarter > 0) {
			this.offsetImageWidthColumnQuarter = $$('#columns .quarter')[0].getWidth();
		}

		this.contentWidth = this.offsetImageWidth*this.contentLength;
		this.contentWidth += this.offsetImageWidthColumn2*this.contentLengthColumn2;
		this.contentWidth += this.offsetImageWidthColumn3*this.contentLengthColumn3;
		this.contentWidth += this.offsetImageWidthColumn300px*this.contentLengthColumn300px;
		//this.contentWidth += this.offsetImageWidthColumnQuarter*this.contentLengthColumnQuarter;
//alert(this.contentWidth);
		this.offsetContentWidth = -1*(this.contentWidth-this.viewportWidth);

		if (this.arrowScrollAmount == "auto") {
			this.arrowScrollAmount = 1/((this.contentLength+extraSingleColumns)-this.visibleColumns);
		}

		this.productSlider = new Control.Slider('pb-productsliderhandle', 'pb-productslidertrack', {
			axis: 'horizontal'
		});

		if (this.contentWidth <= this.viewportWidth) {
			this.productSlider.setDisabled();
			$('pb-productslidertrack').hide();
			$('pb-leftarrow').hide();
			$('pb-rightarrow').hide();
		}

		this.productSlider.options.onChange = function(value) {
			$('pb-productsliderhandleimage').style.left = $('pb-productsliderhandle').style.left;

			if (MR.HorizontalScroller.isThrow && !MR.HorizontalScroller.isSliding) {
				MR.HorizontalScroller.isSliding = true;
				MR.HorizontalScroller.isThrow = false;
	
				var mod = value+MR.HorizontalScroller.throwMod;
				if(mod<0)mod=0;
				if(mod>1)mod=1;
				MR.HorizontalScroller.animateSlide(mod);
			}
			else if (!MR.HorizontalScroller.isSliding && value) {
				MR.HorizontalScroller.isSliding = true;
				MR.HorizontalScroller.animateSlide(value);
			}

			if (value < 0 || value == 0) {
				MR.HorizontalScroller.animateSlide(0);
			}
		};

		this.productSlider.options.onSlide = function(value) {
			$('pb-productsliderhandleimage').style.left = $('pb-productsliderhandle').style.left;

			if (value && !MR.HorizontalScroller.isSliding) {
				MR.HorizontalScroller.isSliding = true;
				MR.HorizontalScroller.isThrow = false;

				if (MR.HorizontalScroller.isMouseDown) {
					MR.HorizontalScroller.dif = value-MR.HorizontalScroller.lastX;
					MR.HorizontalScroller.lastX = value;
				}

				var w = MR.HorizontalScroller.offsetContentWidth;
				$('columns').style.left = w*value+"px";
	
				this.sliderVal=value;
				MR.HorizontalScroller.lastX = value;
				MR.HorizontalScroller.isSliding = false;
			}

			Element.setStyle($('scrollboxcontainer'), {overflow:"hidden"});
		};

		Event.observe('pb-productslidertrack', 'mousedown', function(e) {
			var o = e.offsetX || e.layerX;
			if (Event.element(e).id == 'pb-productslidertrack' && o < 100) {
				MR.HorizontalScroller.animateSlide(0);
			}
		});

		Event.observe('pb-leftarrow', 'mousedown', function() {
			MR.HorizontalScroller.left();
		});

		Event.observe('pb-rightarrow', 'mousedown', function() {
			MR.HorizontalScroller.right();
		});

		Event.observe('pb-productsliderhandle', 'mousedown',function() {
			MR.HorizontalScroller.isMouseDown = true;
			$('pb-productsliderhandle').style.zIndex = 15;
		});

		Event.observe('pb-productsliderhandle', 'mouseup', function() {
			MR.HorizontalScroller.isMouseDown = false;
		});
	},

	animateSlide:function(toX) {
		if (toX > 1) toX = 1;
		if (toX < 0) toX = 0;

		MR.HorizontalScroller.sliderVal = toX;
		window.clearInterval(MR.HorizontalScroller.animationId);
		var w = MR.HorizontalScroller.offsetContentWidth;
		var stopPoint = w*toX;

		var sliderStopPoint = (Math.round(MR.HorizontalScroller.viewportWidth-MR.HorizontalScroller.sliderOffset)*toX);

		MR.HorizontalScroller.isSliding = true;
	
		MR.HorizontalScroller.animationId = window.setInterval(function() {
			var sliderPos = parseInt($('columns').getStyle('left')) || 0;
			var handlePos = parseInt($('pb-productsliderhandle').getStyle('left')) || 0;
			var x = MR.HorizontalScroller.calculateDecel(sliderPos, stopPoint);
			var sx = MR.HorizontalScroller.calculateDecel(handlePos, sliderStopPoint);
			$('columns').style.left = x+"px";
			$('pb-productsliderhandle').style.left = sx+"px";
			$('pb-productsliderhandleimage').style.left=sx+"px";
			
			if (x == stopPoint) {
				window.clearInterval(MR.HorizontalScroller.animationId);
				MR.HorizontalScroller.isSliding = false;
			}
		}, 30);

		// document.location.href = "#"+toX;
	},

	jump:function(jump) {
		var n = this.contentLength-4;
		var j = 1/n;

		MR.HorizontalScroller.animateSlide(j*jump);
	},

	left:function() {
		var pos = MR.HorizontalScroller.sliderVal-MR.HorizontalScroller.arrowScrollAmount;

		if (pos.toString().indexOf("e") != -1) {
			pos = 0;
		}

		MR.HorizontalScroller.animateSlide(pos);
	},
	
	right:function() {
		MR.HorizontalScroller.animateSlide(MR.HorizontalScroller.sliderVal+MR.HorizontalScroller.arrowScrollAmount);
	},

	calculateDecel:function(from, to) {
		var n=from-Math.floor((from-to)*.4);
		if (Math.abs(from-to)<4) return to;
		else return n;
	}
};