/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);
(function($){var cache=[];$.preLoadImages=function(){var args_len=arguments.length;for (var i=args_len;i--;){var cacheImage=document.createElement('img');cacheImage.src=arguments[i];cache.push(cacheImage);}}})(jQuery);


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()
{
	$('#header_container a.sub').parent().hoverIntent({
		over: function() {$(this).addClass('hover');},
     	timeout: 500, 
     	out: function() {$(this).removeClass('hover');}
	});

	$("#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;
	});
});
