/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
$(window).load(function() {$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)};

// MENU SCRIPTED
	$('#main li, #main-red li').hoverIntent({
		over: function () {
			$(this).find('ul:first').stop().height('auto').animate({"opacity": "toggle"}, 250);
		},
		timeout: 500,
		out: function () {
			$(this).find('ul:first').stop().animate({"opacity": "toggle"}, 60);
		}
	});

});


$(document).ready(function() {
	$('a.photo').fancyzoom();
	$('#img_slider').nivoSlider({
        effect:"random",
        animSpeed:1000,
        pauseTime:7000,
        startSlide:0,
        directionNav:true,
        directionNavHide:true,
        controlNav:true,
        controlNavThumbs:false,
        controlNavThumbsFromRel:false,
        keyboardNav:true,
        pauseOnHover:true,
        manualAdvance:false
    });
	
	$('#adresse').click(function() {
		if(	$('#opening').height() == 112){					 
			$('#opening').animate({height: 272});
			$('#img_plus_minus').attr('src','images/minus.png');
		}else{
			$('#opening').animate({height: 112});
			$('#img_plus_minus').attr('src','images/plus.png');
		}
	});

	//remove js-disabled class
	$("#viewer").removeClass("js-disabled");
	//create new container for images
	$("<div>").attr("id", "container_scroller").css({
		position: "absolute"
	}).width($(".wrapper").length * 170).height(170).appendTo("div#viewer");
	//add images to container
	$(".wrapper").each(function () {
		$(this).appendTo("div#container_scroller");
	});
	//work out duration of anim based on number of images (1 second for each image)
	var duration = $(".wrapper").length * 3000;
	//store speed for later (distance / time)
	var speed = (parseInt($("div#container_scroller").width()) + parseInt($("div#viewer").width())) / duration;
	//set direction
	var direction = "rtl";
	//set initial position and class based on direction
	(direction == "rtl") ? $("div#container_scroller").css("left", $("div#viewer").width()).addClass("rtl") : $("div#container_scroller").css("left", 0 - $("div#container_scroller").width()).addClass("ltr");
	//animator function
	var animator = function (el, time, dir) {
			//which direction to scroll
			if (dir == "rtl") {
				//add direction class
				el.removeClass("ltr").addClass("rtl");
				//animate the el
				el.animate({
					left: "-" + el.width() + "px"
				}, time, "linear", function () {
					//reset container position
					$(this).css({
						left: $("div#imageScroller").width(),
						right: ""
					});
					//restart animation
					animator($(this), duration, "rtl");
					//hide controls if visible
					($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null;
				});
			} else {
				//add direction class
				el.removeClass("rtl").addClass("ltr");
				//animate the el
				el.animate({
					left: $("div#viewer").width() + "px"
				}, time, "linear", function () {
					//reset container position
					$(this).css({
						left: 0 - $("div#container_scroller").width()
					});
					//restart animation
					animator($(this), duration, "ltr");
					//hide controls if visible
					($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null;
				});
			}
		}
		//start anim
		animator($("div#container_scroller"), duration, direction);
	//pause on mouseover
	$("a.wrapper").live("mouseover", function () {
		//stop anim
		$("div#container_scroller").stop(true);
		//show controls
		($("div#controls").length == 0) ? $("<div>").attr("id", "controls").appendTo("div#outerContainer").css({
			opacity: 0.7
		}).slideDown("slow") : null;
		($("a#rtl").length == 0) ? $("<a>").attr({
			id: "rtl",
			href: "#",
			title: "rtl"
		}).appendTo("#controls") : null;
		($("a#ltr").length == 0) ? $("<a>").attr({
			id: "ltr",
			href: "#",
			title: "ltr"
		}).appendTo("#controls") : null;
		//variable to hold trigger element
		var title = $(this).attr("title");
		//add p if doesn't exist, update it if it does
		($("p#title").length == 0) ? $("<p>").attr("id", "title").text(title).appendTo("div#controls") : $("p#title").text(title);
	});
	//restart on mouseout
	$("a.wrapper").live("mouseout", function (e) {
		//hide controls if not hovering on them
		(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $("div#controls").slideUp("slow").remove() : null;
		//work out total travel distance
		var totalDistance = parseInt($("div#container_scroller").width()) + parseInt($("div#viewer").width());
		//work out distance left to travel
		var distanceLeft = ($("div#container_scroller").hasClass("ltr")) ? totalDistance - (parseInt($("div#container_scroller").css("left")) + parseInt($("div#container_scroller").width())) : totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container_scroller").css("left"))));
		//new duration is distance left / speed)
		var newDuration = distanceLeft / speed;
		//restart anim
		animator($("div#container_scroller"), newDuration, $("div#container_scroller").attr("class"));
	});
	//handler for ltr button
	$("#ltr").live("click", function () {
		//stop anim
		$("div#container_scroller").stop(true);
		//swap class names
		$("div#container_scroller").removeClass("rtl").addClass("ltr");
		//work out total travel distance
		var totalDistance = parseInt($("div#container_scroller").width()) + parseInt($("div#viewer").width());
		//work out remaining distance
		var distanceLeft = totalDistance - (parseInt($("div#container_scroller").css("left")) + parseInt($("div#container_scroller").width()));
		//new duration is distance left / speed)
		var newDuration = distanceLeft / speed;
		//restart anim
		animator($("div#container_scroller"), newDuration, "ltr");
	});
	//handler for rtl button
	$("#rtl").live("click", function () {
		//stop anim
		$("div#container_scroller").stop(true);
		//swap class names
		$("div#container_scroller").removeClass("ltr").addClass("rtl");
		//work out total travel distance
		var totalDistance = parseInt($("div#container_scroller").width()) + parseInt($("div#viewer").width());
		//work out remaining distance
		var distanceLeft = totalDistance - (parseInt($("div#viewer").width()) - (parseInt($("div#container_scroller").css("left"))));
		//new duration is distance left / speed)
		var newDuration = distanceLeft / speed;
		//restart anim
		animator($("div#container_scroller"), newDuration, "rtl");
	});	
	
});
// GOOGLE ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27205480-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);
})();
