// Liens externes --------------------------------------------------------------

jQuery().ready(function() {
  if ($('body').attr('class')) {
    var langue = $('body').attr('class');
  } else {
    var langue = 'fr';
  }
  
  var h = document.getElementsByTagName('base')[0].href + langue + '/';

  $("a[href^='http:']").not("[href*='" + h + "']").attr('target','_blank');
  $('a[href*=.pdf]').attr('target','_blank');
});

// CSS Browser Selector --------------------------------------------------------

var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),is=function(t){
    return ua.indexOf(t) != -1;
  },
  h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;
}();

// Survol TR et Button pour IE6 ------------------------------------------------

if (!window.XMLHttpRequest){
  $(document).ready(function(){
    $("table tr").hover(
     function(){
      $(this).addClass("hover");
     },
     function(){
      $(this).removeClass("hover");
     }
    )
    $("button").hover(
     function(){
      $(this).addClass("hover");
     },
     function(){
      $(this).removeClass("hover");
     }
    )
  });
}

// transparence PNG ------------------------------------------------------------

var blank = new Image();
blank.src = 'images/elements/trans.gif';

$(document).ready(function() {
 var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
 if (badBrowser) {
   $('img[src$=.png]').each(function() {
     if (!this.complete) {
       this.onload = function() { fixPng(this) };
     } else {
       fixPng(this);
     }
   });
 }
});

function fixPng(png) {
 var src = png.src;
 if (!png.style.width) { png.style.width = $(png).width(); }
 if (!png.style.height) { png.style.height = $(png).height(); }
 png.onload = function() { };
 png.src = blank.src;
 png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

// Choix langue ----------------------------------------------------------------

jQuery().ready(function() {
	var currentLangText = $.trim($('#blocLangue .langue a').text());
	$('#blocLangue .select ul li a').each(function() {
		if ($(this).text() == currentLangText) {
			$(this).parent().hide();
			return false;
		}
	});
	$('#blocLangue .langue a').bind('click', function(e) {
		e.stopPropagation();
		e.preventDefault();
		$("#blocLangue .select").animate({height: 'toggle'},'fast');
	});
	$(document).bind('click', function(e) {
		$("#blocLangue .select").slideUp('fast');
	});
	
	// Pour la démo --------------------------------------
	/*
	$('#blocLangue .select a').bind('click', function(e) {
		e.preventDefault();
		$.cookie('choix_langue', { expires: -1 });
    var pays = $(this).attr('class');
		$('body').removeClass();
    $('body').addClass(pays);
		if (pays == 'de') {
      $.cookie('choix_langue', 'de');
    } else if (pays == 'en') {
      $.cookie('choix_langue', 'en');
    } else if (pays == 'it') {
      $.cookie('choix_langue', 'it');
    } else {
      $.cookie('choix_langue', 'fr');
    }
    window.location.reload();
	});
	
  if ($.cookie('choix_langue') == 'de') {
    $('body').addClass('de');
   } else if ($.cookie('choix_langue') == 'en') {
    $('body').addClass('en');
   } else if ($.cookie('choix_langue') == 'it') {
    $('body').addClass('it');
   }
   $.cookie('choix_langue', { expires: -1 });
   */
   // --------------------------------------------------
   
});

// Menu principal --------------------------------------------------------------

jQuery().ready(function() {
	$("#topmenu ul ul").css('display', 'none');
	$("#topmenu ul li").bind('mouseenter mouseleave', function() {
		$(this).find("ul").animate({height: "toggle"},"fast");
	});
});

// Téléchargements -------------------------------------------------------------

jQuery().ready(function() {
  $('.blocTelechargements .slider').easySlider({
    loop: true,
    orientation: 'horizontal',
    autogeneratePagination: true,
    restartDuration: 1000,
    controlsBefore:	'<p id="controls">',
    controlsAfter: '</p>',
    nextId: 'nextBtn',
    prevId: 'prevBtn',
    prevText: 'Précédent',
    nextText: 'Suivant',
    pauseable: true
  });
});

// Produits associés -----------------------------------------------------------

jQuery().ready(function() {
  $('.blocProduits .slider').easySlider({
    loop: true,
    orientation: 'vertical',
    autoplayDuration: 5000,
    autogeneratePagination: true,
    restartDuration: 1000,
    controlsBefore:	'<p id="controls2">',
    controlsAfter: '</p>',
    nextId: 'nextBtn2',
    prevId: 'prevBtn2',
    prevText: 'Précédent',
    nextText: 'Suivant',
    pauseable: true
  });
});

// Vidéo -----------------------------------------------------------------------

jQuery().ready(function() {
  jQuery(".blocVideo .voir a").fancybox({
    'hideOnContentClick': false,
    frameWidth : 340,
    frameHeight : 260
  });
});

// Actualités ------------------------------------------------------------------

jQuery().ready(function() {
  $(".blocActus .slider").easySlider({
    loop: true,
    orientation: 'horizontal',
    autoplayDuration: 5000,
    autogeneratePagination: true,
    restartDuration: 1000,
    controlsBefore:	'<p id="controls">',
    controlsAfter: '</p>',
    nextId: 'nextBtn',
    prevId: 'prevBtn',
    prevText: 'Précédent',
    nextText: 'Suivant',
    pauseable: true
  });  
})

// Flash -----------------------------------------------------------------------

jQuery().ready(function() {
  if ($('body').attr('class')) {
    var langue = $('body').attr('class');
  } else {
    var langue = 'fr';
  }
  
  $("#blocFlash").flashembed({ 
    src: 'flash/accueil_'+langue+'.swf',  
    version: [9, 0],  
    wmode: 'transparent',  
    quality: 'high',
    width: 980,
    height: 300
    }, {
      xmlPath: 'flash/xml/rub_flash.php?lang='+langue  
  });
	$("#monde").flashembed({ 
    src: 'flash/distributeurs_'+langue+'.swf',  
    version: [9, 0],  
    wmode: 'transparent',  
    quality: 'high',
    width: 680,
    height: 450
  });
	$("#acces").flashembed({ 
    src: 'flash/localisation.swf',  
    version: [9, 0],  
    wmode: 'transparent',  
    quality: 'high',
    width: 680,
    height: 450
  });
});

jQuery().ready(function() {
  $(".blocContacts #flash").flashembed({ 
    src: '../flash/contacts.swf',  
    version: [9, 0],  
    wmode: 'transparent',  
    quality: 'high',
    width: 460,
    height: 200
  });
});

// Plan du site ----------------------------------------------------------------

jQuery().ready(function() {
	$("#plan").treeview({
		collapsed: false,
		animated: "medium",
		control: "#blocPlan",
		persist: "location"
	});
})

// Documentations  ----------------------------------------------------------------

jQuery().ready(function() {
	$("#tema_docs").change(function(){
		$("#form_docs").submit();
	});
})

// Formulaire ------------------------------------------------------------------

jQuery().ready(function() {
	var validator = $("#formulaire form").validate({
		rules : {
			nom : {
				required : true,
				minlength : 2
			},
			adresse : {
				required : true,
				minlength : 2
			},
			code_postal : {
				required : true,
				minlength : 5
			},
			ville : {
				required : true
			},
			email : {
				required : true,
        email : "#email"
			},
			telephone : {
				required : true,
				minlength : 10
			},
			message : {
				required : true,
				minlength : 2
			}
		},
		messages : {
			nom : {
				required: "&nbsp;",
        minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			adresse : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			code_postal : {
				required: "&nbsp;",
				number: jQuery.format("Veuillez saisir un code postal valide"),
				minlength: jQuery.format("Veuillez saisir un code postal valide")
			},
			ville : {
				required: "&nbsp;",
				minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			email : {
				required: "&nbsp;",
				email: jQuery.format("Veuillez saisir un email valide")
			},
			telephone : {
				required: "&nbsp;",
				minlength: jQuery.format("Veuillez saisir un numéro de téléphone valide")
			},
			message : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			}
		},
		errorPlacement : function(error, element) {
			error.appendTo( element.parent() );
		},
		success : function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	$("#nom").focus(function() {
		var firstname = $("#nom").val();
		var lastname = $("#prenom").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});
})
