$(document).ready(function() {
	$('#nav li').mouseover(function() {
		$(this).children('ul').css('left','auto');

		if ($(this).attr('id') == "m4" || $(this).attr('id') == "m2") {
			$('#m8').children('ul').css('left','-999em');
		}
	}).mouseout(function() {
		if ($(this).attr('id') != 'm8') {
			$(this).children('ul').css('left','-999em');
		}
	});
	
	$(function() {
		$('#ticker_wrapper').bind('mouseenter', function() {
			$('#ticker_wrapper').stop();
		});
		
		$('#ticker_wrapper').bind('mouseleave', function() {
			scrolling();
		});

		scrolling();
	});
	
	function scrolling() {
		
		scroll_time = 4000;
		
		ticker_left = Math.abs( cleanpx($('#ticker_wrapper').css('left')));

		if(ticker_left > 0) {
				
			remaining = $('#ticker_wrapper div:first').outerWidth() - ticker_left;
			speed = $('#ticker_wrapper div:first').outerWidth() / remaining;
			scroll_time /= speed;
			
		}
		
		$('#ticker_wrapper').animate({ 'left' : - $('#ticker_wrapper div:first').outerWidth() }, scroll_time  , 'linear', function () {
			
				$('#ticker_wrapper').append( $('#ticker_wrapper div:first') ); 
				$('#ticker_wrapper').css({ 'left' : '0' });
				scrolling();
		}); 
	}
	
	function cleanpx(px) {

		var i = (px).indexOf('px');
		
		var x = px.substring(0,i);
		//alert(x);
		return x;
	}

});
