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

function openSite(strLocationURL) {
  var siteWindow = null;
  siteWindow = window.open(strLocationURL);
}

function checkExternalLinks() {
  var objLinks = document.getElementsByTagName("a");
  for (i=0; i < objLinks.length; i++) {
    if (objLinks[i].href.length > 0) {
      if (objLinks[i].href.indexOf(document.domain) == -1 && objLinks[i].href.indexOf("javascript:") == -1 && objLinks[i].href.indexOf("mailto:") == -1) {
      objLinks[i].href = "javascript: openSite('" + objLinks[i].href + "');";
      }
    }
  }
}

try {
  window.addEventListener("load", checkExternalLinks, true);
}
catch(ex) {
  window.attachEvent("onload", checkExternalLinks);
}

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

jQuery().ready(function() {
  if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && typeof window['XMLHttpRequest'] != "object") {
    $('#topmenu li.active').find('a').each( function() {
      $(this).attr('writing-mode', 'tb-rl');
      $(this).css('background', 'none');
      $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/elements/menu_active.png",sizingMethod="scale")');
    });
    $('.blocEvents .bloc').each( function() {
      $(this).attr('writing-mode', 'tb-rl');
      $(this).css('background', 'none');
      $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/elements/fd_bloc_events.png",sizingMethod="scale")');
    });
    $('.blocEvents .bloc.acc').each( function() {
      $(this).attr('writing-mode', 'tb-rl');
      $(this).css('background', 'none');
      $(this).css( 'filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/elements/fd_bloc_events_acc.png",sizingMethod="scale")');
    })
  }
});

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

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

// Diaporama -------------------------------------------------------------------

jQuery().ready(function() {
  $("#diaporama").flashembed({ 
    src: 'flash/diaporama.swf',  
    version: [7, 0],  
    wmode: 'transparent',  
    quality: 'high'
  }, { 
      xmlPath: 'flash/xml/data.xml'  
  });
});

// Certifications --------------------------------------------------------------

jQuery().ready(function() {
  $('.blocSlider .slider').easySlider({
		loop: true,
		autoplayDuration: 0,
		autogeneratePagination: true,
		nextId: 'nextBtn',
		prevId: 'prevBtn',
		speed: 2000,
		orientation: 'horizontal'
  });
});


// Produits --------------------------------------------------------------------

jQuery().ready(function() {
	$("table.dimensions tr").find('td:last').css('border', 'none');
	$("table.dimensions tr").find('th:last').css('border', 'none');
})

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

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

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

jQuery().ready(function() {
	var validator = $("#formulaire form").validate({
		rules : {
			nom : {
				required : true,
				minlength : 2
			},
			prenom : {
				required : true,
				minlength : 2
			},
			societe : {
				required : true,
				minlength : 2
			},
			adresse : {
				required : true,
				minlength : 2
			},
			code_postal : {
				required : true,
				number : "#code_postal",
				minlength : 5
			},
			ville : {
				required : true
			},
			email : {
				required : true,
        email : "#email"
			},
			telephone : {
				required : true,
				number : "#telephone",
				minlength : 10
			},
			message : {
				required : true,
				minlength : 2
			}
		},
		messages : {
			nom : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			prenom : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			societe : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			adresse : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			code_postal : {
				required: "&nbsp;",
				number: jQuery.format("&nbsp;"),
				minlength: jQuery.format("&nbsp;")
			},
			ville : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			email : {
				required: "&nbsp;",
				email: jQuery.format("&nbsp;")
			},
			telephone : {
				required: "&nbsp;",
				number: jQuery.format("&nbsp;"),
				minlength: jQuery.format("&nbsp;")
			},
			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;
		}
	});
})

// Plan d'accès ----------------------------------------------------------------

jQuery().ready(function() {
  $("#map").delay(500).slideUp();
    $('.acces a').click(function(e) {
    e.preventDefault();
    $("#map").animate({height: 'toggle'},'fast');
    });
});
