// Fonction de création d'un slide
function createSlide(texte) {
	var masque = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '999',
			'top': '0',
			'left': '0',
			'width': $('body').width()+'px',
			'height': $('body').height()+'px',
			'background-color': 'gray',
			'opacity': '0.5',
			'filter': 'alpha(opacity=50)',
			'cursor': 'pointer'
		},
		click: function() {
			slide.remove();
			masque.remove();
		}
	});
	$('body').append(masque);
	var slide = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '1000',
			'padding': '10px',
			'background-color': '#FFFFFF',
			'text-align': 'center',
			'font-weight': 'bold'
		}
	});
	slide.append($('<div>', { html: texte }));
	slide.append($('<img>', {
		'class': 'png_bg',
		id: 'slide_close',
		src: 'images/fermer.png',
		alt: 'Fermer',
		css: {
			'cursor': 'pointer'
		},
		click: function() {
			slide.remove();
			masque.remove();
		}
	}));
	$('#slide_close').live('mouseover', function() {
		$(this).attr('src','images/fermer_hover.png');
	});
	$('#slide_close').live('mouseout', function() {
		$(this).attr('src','images/fermer.png');
	});
	if ($('div img', slide).size() > 0) {
		slide.css('display', 'none');
		$('body').append(slide);
		var loader = $('<div>', {
			html: '<img src="images/ajax-loader.gif" alt="" /> Chargement...',
			css: {
				'position': 'absolute',
				'z-index': '1000',
				'padding': '10px',
				'width': '200px',
				'height': '20px',
				'left': ($(window).width() - 200) / 2 + $(window).scrollLeft()+'px',
				'top': ($(window).height() - 20) / 2 + $(window).scrollTop()+'px',
				'background-color': 'white',
				'text-align': 'center',
				'font-weight': 'bold'
			}
		});
		$('body').append(loader);
		loader.corner();
		$('div img', slide).load(function() {
			loader.remove();
			slide.css({
				'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
				'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px',
				'display': 'block'
			});
		});
	} else {
		$('body').append(slide);
		slide.css({
			'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
			'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
		});
	}
	slide.corner();
	$(window).resize(function() {
		masque.css({
			'width': $('body').width()+'px',
			'height': $('body').height()+'px'
		});
		slide.css({
			'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
			'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
		});
	});
}

$(document).ready(function() {
	// Arrondi du menu client
	$('#menu_client').corner('top');
	
	////////////////////////////
	// Activation des métiers //
	//         (Début)        //
	////////////////////////////
	
	// Retrait des formulaires existants
	$('#metiers').parent().remove();
	
	// Création DOM du menu des métiers
	$('#moteur_recherche').parent().before($('<div>', {
		id: 'metiers',
		'class': 'div_banniere png_bg',
		css: {
			'background': "url('images/fond_form.png') no-repeat top left"
		}
	}));
	$('#metiers').append($('<div>', {
		text: 'Mon métier',
		css: {
			'float': 'left',
			'width': '110px',
			'height': '25px',
			'padding-left': '30px',
			'color': '#BBBBBB',
			'cursor': 'pointer'
		},
		click: function() {showMetiers()}
	}));
	$('#metiers').append($('<img>', {
		src: 'images/derouler.jpg',
		alt: '',
		css: {
			'position': 'relative',
			'top': '2px',
			'left': '9px',
			'cursor': 'pointer'
		},
		click: function() {showMetiers()}
	}));
	$('#metiers img').mouseover(
		function() {
			$(this).attr('src','images/derouler_hover.jpg');
		}
	);
	$('#metiers img').mouseout(
		function() {
			$(this).attr('src','images/derouler.jpg');
		}
	);
	$('#banniere').css({
		'position': 'relative',
		'z-index': '100'
	});
	$('#metiers').append($('<div>', {
		id: 'liste_metiers',
		css: {
			'position': 'absolute',
			'z-index': '99',
			'top': '25px',
			'left': '3px',
			'width': '200px',
			'background-color': '#FFFFFF',
			'border': '1px solid #E6502E',
			'display': 'none'
		}
	}));
	
	// Fonction d'affichage de la liste des métiers
	function showMetiers() {
		if ($('#liste_metiers').css('display') == 'none') {
			$('#liste_metiers').slideDown('slow');
		} else {
			$('#liste_metiers').slideUp('slow');
		}
	}
	
	// Vérification de la présence de métiers
	$.ajax({
		url: 'ajax/metiers.php',
		success: function(data) {
			if (data != '') {
				$('#liste_metiers').append(data);
			}
		}
	});
	
	////////////////////////////
	// Activation des métiers //
	//          (Fin)         //
	////////////////////////////
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//               (Début)             //
	///////////////////////////////////////
	
	// Suppression du texte par défaut au focus
	$('#recherche').focusin(function() {
		if ($(this).val() == 'Rechercher...') {
			$(this).val('');
			$(this).css('color', '#000000');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus
	$('#recherche').focusout(function() {
		if ($(this).val() == '') {
			$(this).val('Rechercher...');
			$(this).css('color', '#BBBBBB');
		}
	});
	
	// Désactivation de l'autocomplete du champ de recherche
	$('#recherche').css('color', '#BBBBBB');
	$('#recherche').attr('autocomplete', 'off');
	
	function validRech() {
		$('#form_recherche').submit();
		return;
	}
	
	$('#recherche').autocomplete({
		minLength: 3,
		position: {
			my: "left top",
			at: "left bottom"
		},
		source: "ajax/moteur.php",
		focus: function() {
			// interdire les valeurs insérées au focus
			return false;
		},
		select: function(event, ui) {
			if(ui.item) {
				if(ui.item.id == 0) {
					$('#recherche').val('');
				}
				else {
					location.href = ui.item.href;
				}
			}
			else {
				validRech();
			}
			return;
		}
	});
	
	$('#recherche').bind('paste', function() {
		$(this).keydown();
	});
	
	$.ui.autocomplete.prototype._renderItem = function( ul, item ) {
		$( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a href=\"" + item.href + "\">" + item.label +  "</a>" )
			.appendTo( ul );
		//$('#recherche').css('paddingBottom','6px');
		return;
	}
	
	$.ui.autocomplete.prototype._renderMenu = function(ul, items) {
		var self = this;
		var nbv = 0;
		var nbv_submit = 0;
		$.each( items, function( index, item ) {
			if(item.id >= 0) {					
				self._renderItem( ul, item );
			}
			if(item.id == -1) {
				nbv = item.value;
			}
			if(item.id < -1) {
				nbv_submit = item.value;
			}
		});
		if(nbv > 10) {
			return $( "<li style=\"background: #9c9d9f;\"></li>" )
				.append( "<a style=\"text-align: center; color: #ffffff; font-size: 10px;\" onclick=\"validRech();\" href=\"\">Plus de résultats ("+nbv_submit+")</a>" )
				.appendTo( ul );
		}
	};
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//                (Fin)              //
	///////////////////////////////////////
	
	$('#page').show();
	
	// Activation du menu déroulant pour les familles
	$('.derouler_famille').click(function() {
		var ul_display = $(this).parent().parent().children('ul').css('display');
		$('#menu_familles li').children('ul').slideUp('slow');
		$('#menu_familles li').find('img').attr('src', 'images/plus.jpg');
		if (ul_display == 'none') {
			$(this).parent().parent().children('ul').slideDown('slow');
			$(this).attr('src', 'images/moins.jpg');
		}
	});
	
	// Arrondi des actualités
	$('.titre_actu').corner();
	
	// Arrondi du footer
	$('#pied').corner('bottom');
	
	// Activation du lien externe DSoft
	$('#dsoft a').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	///////////////////////////////////////////////////////////
	// Activation des flèches de navigation pour les marques //
	//                        (Début)                        //
	///////////////////////////////////////////////////////////
	
	// Création DOM des flèches
	function createArrows() {
		$('#scroll').before($('<img>', {
			'class': 'fleche',
			src: 'images/fleche_gauche.jpg',
			alt: 'Gauche',
			css: {'float': 'left'},
			click: function() {
				$('#scroll_content').animate({scrollLeft: '-='+$('#scroll').width()}, 'slow');
			}
		}));
		$('#scroll').before($('<img>', {
			'class': 'fleche',
			src: 'images/fleche_droite.jpg',
			alt: 'Droite',
			css: {'float': 'right'},
			click: function() {
				$('#scroll_content').animate({scrollLeft: '+='+$('#scroll').width()}, 'slow');
			}
		}));
		$('.fleche').css({
			'padding-top': '15px',
			'width': '20px',
			'height': '20px',
			'cursor': 'pointer'
		});
		return true;
	}
	
	var arrowsEnabled = false;
	// Pour IE, Firefox & Opera
	if ($('#scroll_content table').width() > $('#scroll').width()) {
		arrowsEnabled = createArrows();
		if ($('#player').size() > 0) {
			$('#player').mousedown(function(event) {
				event.preventDefault();
			});
		}
		if ($('.player').size() > 0) {
			$('.player').mousedown(function(event) {
				event.preventDefault();
			});
		}
	}
	// Pour Safari & Chrome
	if (!arrowsEnabled) {
		var tableWidth = 0;
		$('#scroll_content img').load(function() {
			tableWidth += $(this).width();
			if (!arrowsEnabled && tableWidth > $('#scroll').width()) {
				arrowsEnabled = createArrows();
			}
		});
	}
	
	///////////////////////////////////////////////////////////
	// Activation des flèches de navigation pour les marques //
	//                         (Fin)                         //
	///////////////////////////////////////////////////////////
	
	// Gestion du slide d'informations
	if ($('#message_distrimat').size() > 0) {
		var message = $('#message_distrimat').html();
		$('#message_distrimat').remove();
		createSlide(message);
	}
});
