var _pause = false;

function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		
		if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){
		
		$(this).removeAttr("title").mouseover(function(){
					my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				var border_top = $(window).scrollTop(); 
				var border_right = $(window).width();
				var left_pos;
				var top_pos;
				var offset = 20;
				if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
					left_pos = kmouse.pageX+offset;
					} else{
					left_pos = border_right-my_tooltip.width()-offset;
					}
					
				if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
					top_pos = border_top +offset;
					} else{
					top_pos = kmouse.pageY-my_tooltip.height()-offset;
					}	
				
				
				my_tooltip.css({left:left_pos, top:top_pos});
		}).mouseout(function(){
				my_tooltip.css({left:"-9999px"});				  
		});
		
		}
		
		
		
	});
}

$(document).ready(function()
{
	$("#scroll_left").hide();
	simple_tooltip("a","tooltip");
	$(".hideit").fadeIn(1000);
	$(".alloys").fadeIn(1000);
	$(".tyres").fadeIn(2000);
	$(".access").fadeIn(3000);
	var _i = setInterval(function()
	{
		var _container = $('#scroll_content');
		var _items = $('#scroll_items');
	
		if (_pause == false && _items.queue('fx').length == 0)
		{
			var _pos = _items.css('marginLeft').split('px');
			_pos = (_pos[0] == parseInt(_pos[0])) ? _pos[0] : 0;
			var _length = _items.find('td').length-1;

			if ((_pos * -1) < (_length * 1000))
			{
				  _items.animate({marginLeft:'-=1000',easing:'swing'}, 700);
			  	  $("#scroll_left").fadeIn(700);
					if ((_pos * -1 + 1000) == (_length * 1000)) {$("#scroll_right").fadeOut(700);}
			}
			else
			{
				  _items.animate({marginLeft:'0',easing:'swing'}, 700);
			  	  $("#scroll_left").fadeOut(700);
			  	  $("#scroll_right").fadeIn(700);
			}
		}
	},5000);
	
	
	$('#scroll_right').click(function ()
	{
		var _container = $('#scroll_content');
		var _items = $('#scroll_items');
	
		if (_items.queue('fx').length == 0)
		{
			var _pos = _items.css('marginLeft').split('px');
			_pos = (_pos[0] == parseInt(_pos[0])) ? _pos[0] : 0;
			var _length = _items.find('td').length-1;
			
			if ((_pos * -1) < (_length * 1000))
			{
				_pause = true;
				_items.animate({marginLeft:'-=1000',easing:'swing'}, 700, function(){_pause=false;});
 		  	    $("#scroll_left").fadeIn(700);
				if ((_pos * -1 + 1000) == (_length * 1000)) {$("#scroll_right").fadeOut(700);}
			}
		}
	
		return false;
	});
	
	$('#scroll_left').click(function ()
	{
		var _items = $('#scroll_items');
		if (_items.queue('fx').length == 0)
		{
			var _pos = _items.css('marginLeft').split('px');
			if (_pos[0] < 0)
			{
				_pause = true;
				$('#scroll_items').animate({marginLeft:'+=1000',easing:'swing'}, 700, function(){_pause=false;});
 		  	    $("#scroll_right").fadeIn(700);
				if (_pos[0] == -1000) {$("#scroll_left").fadeOut(700);}
			}
		}
		return false;
	});
	
	$('#scroll_items').mouseover(function()
	{
		_pause = true;
	}).mouseout(function()
	{
		_pause = false;
	});

});