var header={};
header.fn={};
header.cst={};

;(function($){
	header.cst.timerAjax = false;

	header.fn.ajaxRequest = function(jLink){
		var sHeaderAjaxUrl="\common_ajax.php?action=menu-tab";
		$.ajax({
			url: sHeaderAjaxUrl,
			cache: true,
			data: "id="+jLinkID[1],
			dataType: "html",
			success:function(data){ 
				jLink.find(".header-tab-content").html($(data).html()).show();
				jLink.data("loaded",true);
				//hide menu on last link leave
				jLink.find('a:last').bind('blur',function(){
					jLink.find("div.header-tab-content").hide();
				}); 
			}
		});
	}

	var navHover = false;

	header.fn.showHeaderTab = function(jLink) {
		if(jLink.find("div.header-tab-content").length == 0 || !jLink.data("loaded")) {
			jLink.find(".header-tab-content").remove();
			jLink.append('\
				<div class="header-tab-content" style="display:none;">\
					<div class="loading"><span class="loader">Chargement...</span></div>\
					<div style="position:relative;">\
						<div class="col-left"></div>\
						<div class="col-right"></div>\
					</div>\
				</div>\
			');
			navHover=true;

			jLinkID = jLink.find("a.tab-link").attr("id").split("cat-");
			if (!navHover) { 
				header.fn.ajaxRequest(jLink,jLinkID);
			} else {
				jLink.find("div.header-tab-content").show();
				header.cst.timerAjax = setTimeout("header.fn.ajaxRequest(jLink,jLinkID);",200);
			}
		}
		else jLink.find("div.header-tab-content").show();	
	}

	header.fn.showHeaderTabs = function(){
		$input=$('select:visible');
		var $tabs=$('#header .nav .tab').data("loaded",false),
				$nav=$('#header .nav'),
				jFocusElt = 'button,a,input,textarea,select',
				$escapeMenuLink = $nav.prevUntil('#header').find(jFocusElt).last(),
				timer;

		if ( $nav.height() > 70 ) $nav.find(">li:last-child").hide(); // Remove Tabs

		if(!window.orientation){
			$tabs.bind("mouseenter",function() {
				jLink = $(this);
				if(!jLink.hasClass("home") && !jLink.hasClass("disabled") ) {
					if (!navHover) {
						timer = setTimeout("header.fn.showHeaderTab(jLink);",500);
					} else {
						timer = 1;
						header.fn.showHeaderTab(jLink,true);
					}
				}
			});
			// keyboard navigation (rules for all tabs)
			$tabs.find("a").bind("focus",function() {
				jLink = $(this).parent("li");
				$tabs.find("div.header-tab-content").hide(); //close all tabs
				if(!jLink.hasClass("home") && !jLink.hasClass("disabled") ) { //show current tab
					header.fn.showHeaderTab(jLink,true);
				}
			});
			// keyboard navigation (rules for first tabs)
			$escapeMenuLink.bind('focus', function() {
				$tabs.find("div.header-tab-content").hide();
			});
			$('#header .nav .tab').bind("mouseleave",function(){ 
				clearTimeout(timer);
				clearTimeout(header.cst.timerAjax);
				jLink = $(this);
				jLink.parent().find("div.header-tab-content").hide();
			});
			$tabs.find("a").bind("focus",function() {
			});
		} else {
			$tabs.find('.tab-link').unbind('click');
			$tabs.find('.tab-link').bind("click",function(e) {
				if(!$(this).parent().hasClass('services')){
					e.preventDefault();
				}
				jLink = $(this).parent();
				jLink.parent().find("div.header-tab-content").css("display","none");
			});
		};
		$nav.bind("mouseleave",function(){ navHover = false; });
	}
})(jQuery);

jQuery(function(){
	header.fn.showHeaderTabs();
})

