jQuery.fn.extend({
	findPos : function() {
	obj = jQuery(this).get(0);
		var curleft = obj.offsetLeft || 0;
		var curtop = obj.offsetTop || 0;
		return {x:curleft,y:curtop};
	}
});

var timer;
var timerSubMenu;

$(document).ready( function(){
	var width = $('.menu li.active').width();
	var pos = $('.menu li.active').findPos();

	
	if ( $(".hover").hasClass("") )
	{
		$(".hover").css('width', width).css('margin-left', pos.x - 45 + 'px');
		
		$('.item').hover(function(){
			var item = $(this);
			var width = $(item).width();
			var pos = $(item).findPos();
						
			timer = setTimeout(function(){
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 45) + 'px'
				}, 300 , function(){
					$(item).children('.sub-menu').fadeIn(500);
				});
			}, 200);	
		}, function(){
			clearTimeout(timer);
			$(this).children('.sub-menu').fadeOut(500);
		});

		$('.header').mouseleave(function(){
			var width = $('.menu li.active').width();
			var pos = $('.menu li.active').findPos();
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 45) + 'px'
				}, 300 );
		});
	}
	
	if ( $(".hover").hasClass("en") )
	{
		$(".hover").css('width', width).css('margin-left', pos.x - 69 + 'px');
		
		$('.item').hover(function(){
			var item = $(this);
			var width = $(item).width();
			var pos = $(item).findPos();
						
			timer = setTimeout(function(){
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 69) + 'px'
				}, 300 , function(){
					$(item).children('.sub-menu').fadeIn(500);
				});
			}, 200);	
		}, function(){
			clearTimeout(timer);
			$(this).children('.sub-menu').fadeOut(500);
		});

		$('.header').mouseleave(function(){
			var width = $('.menu li.active').width();
			var pos = $('.menu li.active').findPos();
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 69) + 'px'
				}, 300 );
		});
	}
	
	if ( $(".hover").hasClass("ita") )
	{
		$(".hover").css('width', width).css('margin-left', pos.x - 55 + 'px');
		
		$('.item').hover(function(){
			var item = $(this);
			var width = $(item).width();
			var pos = $(item).findPos();
						
			timer = setTimeout(function(){
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 55) + 'px'
				}, 300 , function(){
					$(item).children('.sub-menu').fadeIn(500);
				});
			}, 200);	
		}, function(){
			clearTimeout(timer);
			$(this).children('.sub-menu').fadeOut(500);
		});

		$('.header').mouseleave(function(){
			var width = $('.menu li.active').width();
			var pos = $('.menu li.active').findPos();
				$(".hover").animate({ 
					width: width ,
					marginLeft: parseInt(pos.x - 55) + 'px'
				}, 300 );
		});
	}
})
