var speed = 0;
var animate_timeout = '';

var $window_width = '';
var $fss_wrapper = '';
var $fss_mover = '';
var $fss_mover_width = 0;
var $fss_mover_margin = 0;

var $fss_stop = 0;

$(window).load(function() {
	var base = document.getElementsByTagName('base');
    /* Add images to slider */
	$.ajax({
		type: 'GET',
		url: $(base).attr('href')+'swf/products.html',
		success: function(data) {
			$('#fss-mover').html(data);
			$('#fss-mover').find('img:last').load(function() {
				init();
				$(window).bind('resize', function() {
					init();
				});
			});
		}
	});
});

function init() {
	$window_width = $(window).width();

	$fss_wrapper = $('#fss-wrapper');
	$fss_mover = $('#fss-mover');
	$fss_mover_width = 0;
	
	/* Set the width of the mover */
	$fss_mover.find('img.image').each(function() {
		$fss_mover_width += $(this).width(); 
	});
	$fss_mover.css({
		width: $fss_mover_width + 'px'
	});
	
	$fss_stop = $window_width - $fss_mover_width;
	
	/* Check if the wrapper should be centered */
	if ($fss_mover_width < $window_width) {
		$fss_mover.css('margin', '0 auto');
	}
	
	if ($fss_stop > 0)
		return false;
	
	/* Activate the mouse actions */
	$fss_wrapper.mouseenter(function() {
		//alert('enter');
		animate();
	}).mouseleave(function() {
		clearTimeout(animate_timeout);
	}).mousemove(function(e) {
		speed = e.pageX / $window_width;
		if (speed < 0.5) {
			speed = speed + -0.5;
		} else {
			speed = speed - 0.5;
		}
	});
};

function animate() {
	$fss_mover_margin = - speed * 20 + $fss_mover_margin;
	if ($fss_mover_margin > 0) {
		$fss_mover_margin = 0;
	} else if ($fss_mover_margin <= $fss_stop) {
		$fss_mover_margin = $fss_stop;
	}
	
	$fss_mover.css({
		marginLeft: $fss_mover_margin + 'px'
	});
	
	animate_timeout = setTimeout('animate()', 10);
}
