var speed = 0;
var mouseX = 0;
var OverCheck = 0;
var Direction = 1;

function round ( val, precision ) {
	return parseFloat(parseFloat(val).toFixed(precision));
}

function replace(search, replace, subject){
	var ra = replace instanceof Array,
		sa = subject instanceof Array,
		l = (search = [].concat(search)).length,
		replace = [].concat(replace),
		i = (subject = [].concat(subject)).length;
	while(j = 0, i--)
		while(subject[i] = subject[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
	return sa ? subject : subject[0];
}

function scroller() {	var offset = $("#spec").offset();
	var blokLeft = offset.left;
	var blokRight = offset.left+$("#spec").width();
	if(mouseX <= (blokRight-250) || mouseX >= (blokLeft+250)) speed = 0;
	if(mouseX > (blokRight-250) && OverCheck == 1){
		speed = round((mouseX-(blokRight-250))/50);
		Direction = 0;
	}
	if(mouseX < (blokLeft+250) && OverCheck == 1){
		speed = round(((blokLeft+250)-mouseX)/50);
		Direction = 1;
	}
	$("#spec table:first-child").animate({"marginLeft": (Direction==1?"+":"-")+"=" + speed + "px"}, 10, "swing", function(){
		var curimg = $("#spec table:first-child");
		var lastimg = $("#spec table:last-child");
		var leftMargin = replace("px", "", curimg.css('margin-left'));
		if ((leftMargin >= 16 && Direction == 1) || (leftMargin <= -(10+curimg.width()) && Direction == 0))
		{
			curimg.css('margin-left','15px');
			if(Direction == 1){
				$("#spec").remove("table:last-child");
				lastimg.css('margin-left','-'+(9+curimg.width())+'px');
				lastimg.prependTo("#spec");
			}
			else{
				$("#spec").remove("table:first-child");
				curimg.appendTo("#spec");
			}
		}
		scroller();
	});
}

$(document).ready(function() {	$("#spec").mousemove(function(e){
		mouseX = e.pageX;
	});
	$("#spec").mouseover(function(){
		OverCheck = 1;
	});
	$("#spec").mouseout(function(){
		OverCheck = 0;
	});
	scroller();
});
