shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

$(document).ready(function(){
	//sc1 = new hoverScroll("#scroller", "#scrollContainer");
	//return;
	var scroller = $("#scroller");
	var container = $("#scrollContainer")
	scroller.css({overflow:'hidden', position: 'relative', width: '100%'});
	container.css({position:'absolute', left: "0px"});
	var animate = true;
	var timer3dc = null;
	var count;
	var totalWidth;
	var maxHeight = 0;
	var widths;
	var elements;
	var contWidth;
	
	anim = function() {
		x = parseInt(container.css("left"));
		width = scroller.width();
		mX = scroller.mouseX;
		centerDist = mX - width / 2;
		porp = centerDist * 2 / width;
		if (porp < 0 && porp > -.25) porp = 0;
		if (porp > 0 && porp < .25) porp = 0;
		maxVel = 100;
		scroller.targetVel = maxVel * -porp;
		if (scroller.vel < scroller.targetVel) {
			if (scroller.vel < 0) scroller.acc = Math.min(5, scroller.targetVel - scroller.vel);
			else scroller.acc = Math.min(1, scroller.targetVel - scroller.vel);
		}
		else {
			if (scroller.vel > 0) scroller.acc = Math.max(-5, scroller.targetVel - scroller.vel);
			else scroller.acc = Math.max(-1, scroller.targetVel - scroller.vel);
		}
		scroller.vel += scroller.acc;
		scroller.vel = Math.max(-maxVel, Math.min(maxVel, scroller.vel));
		x += scroller.vel;
		if (x > 0) {
			x = 0;
			scroller.vel = 0;
		}
		if (x < width - totalWidth) {
			x = width - totalWidth;
			scroller.vel = 0;
		}
		
//		x = Math.min(0, Math.max(width - totalWidth, x));
//		animate = (scroller.targetVel != 0 && scroller.vel != 0);
		container[0].style.left = x + "px";
		if (Math.abs(scroller.vel) < .1) animate = false;
		if (animate) timer3dc = setTimeout(function() {anim();}, 33);
		else timer3dc = null;
		
		if (count < 4) {
			scroller.targetX = -totalWidth / 2 + contWidth / 2;
			container[0].style.left = scroller.targetX + "px";
			animate = false;
		}
	}
	
	initScroller = function(doShuffle) {
		elements = new Array();
		widths = new Array();
		count = 0;
		totalWidth = 0;
		maxHeight = 0;
		$(".scrollElement").each(function(){
			widths[count] = parseInt($(this).css('width'));//$(this).width();
			widths[count] += parseInt($(this).css('padding-left'));
			widths[count] += parseInt($(this).css('padding-right'));
			widths[count] += parseInt($(this).css('margin-left'));
			widths[count] += parseInt($(this).css('margin-right'));
			totalWidth += widths[count] + 4;

			height = parseInt($(this).css('height'));//$(this).height();
			height += parseInt($(this).css('padding-top'));
			height += parseInt($(this).css('padding-bottom'));
			height += parseInt($(this).css('margin-top'));
			height += parseInt($(this).css('margin-bottom'));
			if (height > maxHeight) maxHeight = height;
			//alert(widths[count]);
			if (doShuffle)
				elements[count] = $(this).remove();
			else
				elements[count] = $(this);
			count++;
		});
		//alert(count);
		if (doShuffle) {
			elements = shuffle(elements);
			for (i = 0; i < count; i++) {
				container.append(elements[i]);
			}
		}
		scroller.css({height: maxHeight + 'px'});
		container.css({width: totalWidth + "px"});

		contWidth = scroller.width();
		scroller.targetX = -totalWidth / 2 + contWidth / 2;
		container[0].style.left = scroller.targetX + "px";
		scroller.targetVel = 0;
		scroller.vel = 0;
		scroller.acc = 1;
		scroller.mouseX = scroller.width() / 2;
//		anim();
	}
	
	
	initScroller(false);
	scroller.bind("mouseenter", function(e){
		if (count < 4) return;
		scroller.inside = true;
		scroller.mouseX = e.pageX - scroller.offset().left;
		animate = true;
		if (timer3dc == null) anim();
	}).bind("mouseleave", function(e){
		if (count < 4) return;
		scroller.inside = false;
		scroller.targetVel = 0;
		scroller.mouseX = scroller.width() / 2;
	}).mousemove(function(e) {
		if (count < 4) return;
		scroller.mouseX = e.pageX - scroller.offset().left;
		animate = true;
		if (timer3dc == null) anim();
	});
	$(window).resize(function(e) {
		if (count < 4) return;
		scroller.targetVel = 0;
		scroller.mouseX = scroller.width() / 2;	
		animate = true;
		if (timer3dc == null) anim();
	});
});


