$(window).load(function(){

	function scrollTo(el, f){
		try {
			var offset = $(el).offset().top;
			var tag = self.navigator.userAgent.toLowerCase().match("safari") ? "html, body" : "html";
			$(tag).scrollTop() === offset ? $(f) : $(tag).animate({ scrollTop: offset }, 1000, f);
		} catch(e) {}
		return false;
	}

  
  $("#menu a, #top-container a").hover(function(){
			$("span", this).fadeIn("fast");
		}, function(){
      $("span", this).fadeOut("fast");
	});


	$("#menu a:not(.noscroll)").click(function(){
		var a = $(this);
		if (a.attr("href") && a.attr("rel")) {
			if ($("#" + a.attr("rel")).hasClass("box") && $("#" + a.attr("rel")).length) {
					scrollTo("#" + a.attr("rel"));
			} else {
				$.ajax({
					url: a.attr("href"),
					beforeSend: function(){
						$("#main").prepend("<p id='preload'><img src='img/design/preload2.gif' alt='' /></p>");
						$("#preload").fadeIn("fast");
					},
					success: function(re){
						$("div.container:first", "#main").attr("id", a.attr("rel")).html($("<div>", {
							css: {
								display: "none"
							},
							html: re,
							id: "container-ajax"
							})
						);
			
	$("a.zoom").fancybox({
		'centerOnScroll'	:	true,
		'overlayOpacity'	:	0.7,
		'speedOut'			:	100, 
		'titleShow'			:	false,
		'showCloseButton'	:	false,
		'padding'			:	0,
		'hideOnContentClick': true,
	});
			

$("div.white-box div.image").mouseenter(function() {
$("div.hover", this).css({ visibility: "visible" }).fadeIn('fast');
}); 
$("div.image").mouseleave(function() {
$("div.hover", this).fadeOut("fast");
});


  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-16511779-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();



						$("#container-ajax").fadeIn(function(){
              $(this).children().unwrap();
						});
						$("#preload").fadeOut(function(){
							$(this).remove();
							scrollTo("#" + a.attr("rel"));
						});
					},
					error: function(){
						$("#preload").remove();
						alert("Erreur serveur, veuillez réessayer plus tard");
					}
				});
			}
		} else if (a.is("[href^=#]") && !a.attr("rel")) {
			scrollTo(a.attr("href"));
		}
		return false;
	});

// Script load images dans box

	$("div.box div.image ul a").live("click", function(){
		var a = $(this);
		if (!a.hasClass("active")) {
			$("img", a.parentsUntil("div.box").find("div.image p")).remove();

			var img = new Image();
			$(img).load(function () {
			  $("p", a.parentsUntil("div.box").find("div.image")).html(this).children().wrap("<span />");
				$(this).fadeIn(function(){
					$(this).unwrap();
				});
				$("a.active", a.parentsUntil("div.image")).removeClass("active").addClass("visited");
				a.addClass("active");
			})
			.error(function () {
				alert("Erreur lors du chargement d'image.");
			})
			.attr('src', a.attr("href"));
		}
		return false;
	});
	
	
	
// ici commence le script pour zoom sleeve	


	$("div.box div.imagesleeve ul a").live("click", function(){
		var a = $(this);
		if (!a.hasClass("active")) {
			$("img", a.parentsUntil("div.box").find("div.imagesleeve p")).remove();

			var img = new Image();
			$(img).load(function () {
			  $("p", a.parentsUntil("div.box").find("div.imagesleeve")).html(this).children().wrap("<span />");
				$(this).fadeIn(function(){
					$(this).unwrap();
				});
				$("a.active", a.parentsUntil("div.imagesleeve")).removeClass("active").addClass("visited");
				a.addClass("active");
			})
			.error(function () {
				alert("Erreur lors du chargement d'image.");
			})
			.attr('src', a.attr("href"));
		}
		return false;
	});
	
				$("div.imagesleeve p").mouseenter(function() {
				$("img.bigger", this).css({ visibility: "visible" }).fadeIn('fast');
				});
				$("div.imagesleeve p").mouseleave(function() {
				$("img.bigger", this).fadeOut("fast");
				});
	
// Formulaire	

	if ($.fn.validate && $("#contact form").length) {
		$("#contact form").validate({
			rules: {
			  name: "required",
			  email: {
					required: true,
					email: true
				},
				message: "required",
				comment: "required"
			},
		errorPlacement:	function(error, element) {
     	$("#ajax").remove();
        return false;
  		},
  		unhighlight: function(element, errorClass) {
        $(element).removeClass("error");
  		},
  		submitHandler: function(form){
				var f = $(form);
				$.ajax({
					url: f.attr("action"),
					data: f.serialize(),
					beforeSend: function(){
						$("#ajax", f).remove();
					},
					success: function(re){
						$(":text, textarea", f).val("");
						$("#submit", f).prepend($("<div>", {
							id: "ajax",
							html: re
							})
						);
						$("#ajax", f).fadeIn();
					},
					error: function(){
						alert("Erreur serveur, veuillez réessayer plus tard.");
					}
				});
				return false;
			}
		});
	};
		
		
//	if ($.fn.validate && $("#comments form").length) {
//		$("#comments form").validate({
//			rules: {
//			  author: "required",
//			  email: {
//					required: true,
//					email: true
//				},
//				message: "required",
//				comment: "required"
//			},
//		errorPlacement:	function(error, element) {
//    	$("#ajax").remove();
//       return false;
//  		},
//  		unhighlight: function(element, errorClass) {
//        $(element).removeClass("error");
//		}
//		});
//	};



});
