﻿if (typeof(MR) == "undefined"){ MR={}; }

MR.NewsTicker={
	pageId:0,
	productSlider:null,
	sliderVal:0,
	animationId:false,
	viewportWidth:984,
	contentWidth:10,
	contentLength:1,
	arrowScrollAmount:0.5,
	isSliding:false,
	lastX:0.32,
	isMouseDown:false,
	dif:0,
	overlap:0,
	visibleColumns:4,
	offsetImageWidth:137,
	sliderOffset:291,
	offsetContentWidth:-984,
	clicked:false,
	startIndex:0,
	isIpScroll:false,
	hasIpDragged:false,

	init:function(setupArgs) {
		this.viewportWidth = $('newslist').getWidth();
	
		
		this.contentLength = $$('#newsitems li').length;
	
		for (i=0; i<this.contentLength; i++) {
			this.contentWidth += $$('#newsitems li')[i].getWidth();
		}
		
		this.contentWidth -= 30;
				
		this.offsetContentWidth = -1*(this.contentWidth-this.viewportWidth);

		Event.observe('nt-leftarrow', 'mousedown', function() {
			MR.NewsTicker.left();
		});
	
		Event.observe('nt-rightarrow', 'mousedown', function() {
			MR.NewsTicker.right();
		});
	},

	animateSlide:function(toX) {
		if (toX>1) toX = 1;
		if (toX<0) toX = 0;

		//eraseCookie("scrollbox"+this.pageId);
		//createCookie("scrollbox"+this.pageId, toX, 1);

		MR.NewsTicker.sliderVal = toX;
		window.clearInterval(MR.NewsTicker.animationId);
		var w = MR.NewsTicker.offsetContentWidth;
		var stopPoint = w*toX;

		var sliderStopPoint = (Math.round(MR.NewsTicker.viewportWidth-MR.NewsTicker.sliderOffset)*toX);

		MR.NewsTicker.isSliding = true;
	
		MR.NewsTicker.animationId = window.setInterval(function() {
			var sliderPos = parseInt($('newsitems').getStyle('left')) || 0;
			var x = MR.NewsTicker.calculateDecel(sliderPos, stopPoint);
			$('newsitems').style.left = x+"px";
			
			if (x == stopPoint) {
				window.clearInterval(MR.NewsTicker.animationId);
			}
		}, 30);
	},


	left:function() {
		MR.NewsTicker.animateSlide(MR.NewsTicker.sliderVal-MR.NewsTicker.arrowScrollAmount);
	},
	
	right:function() {
		MR.NewsTicker.animateSlide(MR.NewsTicker.sliderVal+MR.NewsTicker.arrowScrollAmount);
	},

	calculateDecel:function(from, to) {
		var n=from-Math.floor((from-to)*.4);
		if (Math.abs(from-to)<4) return to;
		else return n;
	}
};