// 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')";
}

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


// Nouveautés ------------------------------------------------------------------

jQuery().ready(function() {
  $("#blocNouveautes .slider").easySlider({
		loop: true,
		orientation: 'horizontal',
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
  	controlsBefore:	'<p class="controls">',
  	controlsAfter: '</p>',
		nextId: 'nextBtnNouveautes',
		prevId: 'prevBtnNouveautes',
		pauseable: true
  });  
})

// Notre Gamme -----------------------------------------------------------------

jQuery().ready(function() {
  $("#blocGamme .slider").easySlider({
		loop: true,
		orientation: 'horizontal',
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
  	controlsBefore:	'<p class="controls">',
  	controlsAfter: '</p>',
		nextId: 'nextBtnGamme',
		prevId: 'prevBtnGamme',
		pauseable: true
  });  
});

// Sur page produits

// Produits dans la Sous Gamme  ------------------------------------------------

jQuery().ready(function() {
  $(".blocSousGamme .slider").easySlider({
		loop: true,
		orientation: 'horizontal',
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
  	controlsBefore:	'<p class="controls">',
  	controlsAfter: '</p>',
		nextId: 'nextBtnSousGamme',
		prevId: 'prevBtnSousGamme',
		pauseable: true
  });  
})

// Slider Produits -------------------------------------------------------------

jQuery().ready(function() {
  $(".blocProduits .slider").easySlider({
		loop: true,
		orientation: 'vertical',
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
  	controlsBefore:	'<p class="controls">',
  	controlsAfter: '</p>',
		nextId: 'nextBtnProduit',
		prevId: 'prevBtnProduit',
		pauseable: true
  });  
});

// Slider Téléchargement -------------------------------------------------------------

jQuery().ready(function() {
  $(".blocTelechargement .slider").easySlider({
		loop: true,
		orientation: 'horizontal',
		autoplayDuration: 5000,
		autogeneratePagination: true,
		restartDuration: 1000,
  	controlsBefore:	'<p class="controls">',
  	controlsAfter: '</p>',
		nextId: 'nextBtnTelechargement',
		prevId: 'prevBtnTelechargement',
		pauseable: true
  });  
});

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

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

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

jQuery().ready(function() {
	var validator = $("#formulaire form").validate({
		rules : {
			nom : {
				required : true,
				minlength : 2
			},
			secteur : {
				required : true
			},			
			adresse : {
				required : true,
				minlength : 2
			},
			code_postal : {
				required : true,
				minlength : 4
			},
			ville : {
				required : true
			},
			pays : {
				required : true
			},
			email : {
				required : true,
				email : "#email"
			},
			telephone : {
				required : true,
				minlength : 10
			},
			captcha : {
				required : true,
				equalTo : "#sessioncaptcha"
			},			
			message : {
				required : true,
				minlength : 2
			}
		},
		messages : {
			nom : {
				required: "&nbsp;",
        minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			secteur : {
			required: "&nbsp;"
			},
			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")
			},
			pays : {
				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")
			},
			captcha : {
				required: "&nbsp;",
				equalTo: jQuery.format("Incorrect")
			},
			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;
		}
	});
});
