var cycleimg = null;


var isPhone = navigator.userAgent.match(/iPhone/i);

// Anim accueil ==============================================
	// si pas flash
	
	function nextZ(obj) {
		z = parseInt(parseInt($(obj).css('zIndex'))+1);
		return z;
	}
	function showWhich(current, next, speed) {
		$(next).hide();
		$(next).css('zIndex', nextZ(current));
		
		if (!isPhone){
			$(next).stop().fadeIn(speed, function(){ 
				if ($(current).parents("li.selected").length == 1) {
					$(current).removeClass("current").hide(); 
					$(next).addClass("current"); 
				}
			});
		} else {
			$(next).show()
			if ($(current).parents("li.selected").length == 1) {
				$(current).removeClass("current").hide(); 
				$(next).addClass("current"); 
			}
		}
	}
	function goShow(speed, pause) {
		if ($("#accordion li.selected img.show").length > 1) {
			theCurrent = $("#accordion li.selected img.current");
		
			if ($(theCurrent).next().length == 0) {
				theNext = $("#accordion li.selected img.show:first");
			} else { 
				theNext = $(theCurrent).next();
			}
			
			showWhich(theCurrent, theNext, speed);	
			cycleimg = setTimeout("goShow("+speed+","+pause+")", pause);
		}
	}

	function highestZ(obj) {
		z = 1;
		if (parseInt($(obj).css("zIndex")) > 1) {
			z = $(obj).css("zIndex");
			
		}		
		$(obj).siblings('.show').each(function(i, e){
			if (parseInt($(e).css("zIndex")) > z) {
				z = parseInt($(e).css("zIndex"));
			}
		})
		return parseInt(z)+1;
	}
	
	function resetFirst() {
		$("#accordion li:not(.selected)").each(function(){
			
			first = $("img.show:first", this);
			current = $(".current", this);
			
			if (!$(first).hasClass('current')) {
							
				$(first).css('zIndex', highestZ(first)).show().addClass('current');
				$(current).removeClass('current');
			}
		});
	}


	function slideshowClic(obj){
		next = $($(obj).attr('href')+ "_slide") ;
		href = $(obj).attr('href') + "_slide";
				
		if (!$(next).hasClass('selected')) {
	
			clearTimeout(cycleimg);
			
			$('#navaccueil li a.selected').removeClass('selected');
			$('.' + href.replace('#', '') + ' a').addClass('selected');
			
			$('#accordion li.selected').animate(
				{width: "29px", opacity: 0.99999}, 
				{ 	
					duration: 500,
					queue: false,
					step: function(now){
						prevWidth = $(this).width();
						nextWidth = 755 - prevWidth;
						$(next).width(nextWidth);
					},
					complete: function() {
						$(next).width(726);
						$(this).width(29).css('opacity', 1);
						
						$(this).removeClass('selected');
						$(next).addClass('selected');
						resetFirst();
						$(".accordionHandle a, #navaccueil a").bind('click', function(){ $(".accordionHandle a, #navaccueil a").unbind(); slideshowClic(this);  });
						cycleimg = setTimeout("goShow(1000, 3600)", 3600);
					}
					
				}
			);
		} else {
			$(".accordionHandle a, #navaccueil a").bind('click', function(){ $(".accordionHandle a, #navaccueil a").unbind(); slideshowClic(this);  });
			return false;
		}
	}

	function initAccordion() {
		$("#accordion").each(function(){
			// Load
			$(this).addClass("accordion").show();
			lochash = location.hash + "_slide";

			if (lochash != '_slide') {
				$('li.selected').removeClass('selected');
				$(lochash).addClass('selected');
				$('.'+lochash.replace('#', '') + ' a').addClass('selected');
			} else {
				$('li:first', this).addClass('selected')
			}
			
			
			$('li', this).each(function(){
				$("img.show", this).each(function(i, e){
					$(e).css("zIndex", ($(e).siblings().length+1)-i);
				});
			})				
		});
		
		
		// Clickevent
		$(".accordionHandle a, #navaccueil a").bind('click', function(){ 
			$(".accordionHandle a, #navaccueil a").unbind();
			slideshowClic(this); 
		});	
		
		if (cycleimg == null) {
			cycleimg = setTimeout("goShow(1000, 3600)", 3600);
		}
	};
	
		
