$(document).ready(function() {
	selectedCompany = '';
	var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	
	// Vérification si une compagnie est sélectionnée
	function checkIfCompanySelected() {
		if ($('.btn_company').hasClass('selected')) {
			selectedCompany = $('#top_companies .selected').attr('rel');
			$('#submenu_' + selectedCompany).css('display', 'block').stop().animate( { height: '36px', queue: true } );
		}
	}
	
	checkIfCompanySelected();
	
	
	// On fade tous les sous-menus
	$('#submenu_about, #submenu_news, #submenu_career, #submenu_contact, div[id*="submenu_category"]').animate( { opacity:'0', queue: true }, function() { $(this).css('display', 'none'); } );
	
	
	// Rollover des images du header
	$('div.hover > a > img').hover(function(){
		var name = $(this).attr('src').replace('.png','_hover.png');
		$(this).attr('src',name);											
	}, function(){
		var name = $(this).attr('src').replace('_hover.png','.png');
		$(this).attr('src',name);
	});
	
	
	$('.submenu_company_title').each(function(index) {
		if ($(this).attr('id') == 'products' || $(this).attr('id') == 'category') {
			$('a', this).css('width', '105px');
		} else if ($(this).attr('id') == 'dealer_locator') {
			$('a', this).css('width', '145px');
		} else {
			$('a', this).css('width', '162px');
		}
	});
	
	
	// Gestion des couleurs des rollover des sous-menus des compagnies
	var submenuColors = new Array('#003F83', '#0F582E', '#EA3226', '#57585B', '#57585B');
	
	$('#btnCompanyCategoryManac .btn_ssubmenu_company').each(function(index) {
		if (index <= submenuColors.length - 1) {
			$('#btnCompanyCategoryManac .btn_ssubmenu_company:eq(' + index + ') .bullet, #btnCompanyCategoryManac .btn_ssubmenu_company:eq(' + index + ') .arrow').css('background-color', submenuColors[index]);
		}
	});
	
	
	// Animation des sous-menus des compagnies
	$('.btn_company, .compagnies_submenu').hover(function() {
		id = $(this).attr('rel').substr(0, 1).toUpperCase() + $(this).attr('rel').substr(1);
		
		$('#submenu_' + selectedCompany).css('display', 'block').stop().animate( { height: '0px', queue: true }, function() { $(this).css('display', 'none'); } );
		$('#submenu_' + id).css('display', 'block').stop().animate( { height: '36px', queue: true } );
	}, function() {
		$('#submenu_' + selectedCompany).css('display', 'block').stop().animate( { height: '36px', queue: true } );
		
		if (id != selectedCompany) {
			$('#submenu_' + id).stop().animate( { height: '0px', queue: true }, function() { $(this).css('display', 'none'); } );
		}
	});
	
	
	// Clic sur les boutons compagnies
	$('.btn_company').click(function() { document.location.href = $('#submenu_' + $(this).attr('rel')).find('a').attr('href'); });
	
	
	// Click sur les div des sous-menus
	//$('.submenu_companies .btn').click(function() { document.location.href = $(this).find('a').attr('href'); });
	
	
	// Sélection de l'état selected du nom de la compagnie
	$('.compagnies_submenu').hover(function() {
		$('.btn_company #logo_' + $(this).attr('rel')).parent('div').addClass('selected');
	}, function() {
		if (selectedCompany.toLowerCase() != $(this).attr('rel')) {
			$('.btn_company #logo_' + $(this).attr('rel')).parent('div').removeClass('selected');
		}
	});
	
	
	// Affichage des boutons du sous-menu des compagnies
	$('div[id*="btnCompany"]').hover(function() {
		$('#submenu_category' + $(this).attr('rel')).clearQueue();
		$('#submenu_category' + $(this).attr('rel')).css('display', 'block').stop().animate( { opacity:'1', queue: true } );
	}, function() {
		$('#submenu_category' + $(this).attr('rel')).clearQueue();
		$('#submenu_category' + $(this).attr('rel')).stop().animate( { opacity:'0', queue: true }, function() { $(this).css('display', 'none'); } );
	});
	
	
	// Affichage des sous-menus du sous-menu des compagnies
	$('.submenu_companies .btn_ssubmenu_company').hover(function() { $('.ssubmenu_companies', this).css('display', 'block'); }, function() { $('.ssubmenu_companies', this).css('display', 'none'); });
	
	
	// Affichage des sous-menus des sous-menus du sous-menu des compagnies
	$('.ssubmenu_companies .btn_sssubmenu_company').hover(function() { $('.submenu_img', this).css('display', 'block'); }, function() { $('.submenu_img', this).css('display', 'none'); });
	$('.submenu_companies .btn_ssubmenu_company').hover(function() { $('.submenu_img', this).css('display', 'block'); }, function() { $('.submenu_img', this).css('display', 'none'); });
	
	
	// Création du dernier sous-menu contenant l'image que l'on affiche seulement au rollover du sous-sous-menu
	$('.btn_sssubmenu_company, .btn_ssubmenu_company').hover(function() {
		if (!IE7) {
			if ($(this).html().indexOf('submenu_img') == -1 && $(this).attr('rel').length != 0) {
				$(this).prepend('<div class="arrow" style="background-color: ' + $('.bullet', this).css('background-color') + '"></div><div class="submenu_img" style="border-right-color: ' + $('.bullet', this).css('background-color') + '"><div align="center"><img src="img/submenu/' + $(this).attr('rel') + '" /></div></div>');
			}
		}
	});
	
	// Sous-menu des liens rouges
	$('#btnAbout, #submenu_about, #btnNews, #submenu_news, #btnContact, #submenu_contact, #btnCareer, #submenu_career').hover(function() {
		$('#submenu_' + $(this).attr('rel')).css('display', 'block');
		$('#submenu_' + $(this).attr('rel')).css('display', 'block').stop().animate( { opacity:'1', queue: true } );
	}, function() {
		$('#submenu_' + $(this).attr('rel')).css('display', 'block');
		$('#submenu_' + $(this).attr('rel')).stop().animate( { opacity:'0', queue: true }, function() { $(this).css('display', 'none'); } );
	});
	
	// Rollover des icônes sociaux
	//$('#index_reseaux img, #footer_reseaux img').fadeTo(0, 0.5);
	
	$('#index_reseaux img, #footer_reseaux img').mouseover(function() {
		$('#index_reseaux img').clearQueue();
		
		if ($(this).attr('id') == 'img_twitter') {
			$('#img_facebook, #img_rss').stop().fadeTo(500, 0.5);
		} else if ($(this).attr('id') == 'img_facebook') {
			$('#img_twitter, #img_rss').stop().fadeTo(500, 0.5);
		} else {
			$('#img_twitter, #img_facebook').stop().fadeTo(500, 0.5);
		}
	});
	
	$('#index_reseaux img, #footer_reseaux img').mouseout(function() {
		$('#index_reseaux img').clearQueue();
		
		if ($(this).attr('id') == 'img_twitter') {
			$('#img_facebook, #img_rss').stop().fadeTo(500, 1);
		} else if ($(this).attr('id') == 'img_facebook') {
			$('#img_twitter, #img_rss').stop().fadeTo(500, 11);
		} else {
			$('#img_twitter, #img_facebook').stop().fadeTo(500, 1);
		}
	});
});
