/**
 * @author La Mire
 */

function preFader(classA) {
	$('#fader').addClass('fading');
	$('#fader #timer').stop().animate({width: 0},450, 'linear');
	var fadeDivLentgh = $('#fader > div').length;
	var count = 0;
	var currentDiv = $('#fader .current');
	var currentDivLength = $('div', currentDiv).length;
	$('#fader .current div').each(function(){
		var index = $(this).index();
		setTimeout(function(){
			$('div:eq(' + index + ')', currentDiv).animate({top: '-=20'},100, function(){
				$(this).animate({top: '+=400'},300, function(){
					count++;
					if (count == currentDivLength) {
						if (classA == 'next') {
							if (currentDiv.index() == fadeDivLentgh - 1) {
								var target = $('#fader > div:first-child');
							}
							else {
								var target = currentDiv.next('div');
							}
						}
						if (classA == 'prev') {
							if (currentDiv.index() == 0) {
								var target = $('#fader > div:last');
							}
							else {
								var target = currentDiv.prev('div');
							}
						}animDiv(currentDiv, target, fadeDivLentgh);
					}
				});
			});
		},150*index);
	});
}

function animDiv(currentDiv, target, fadeDivLentgh) {
	currentDiv.removeClass('current').fadeOut(300, function(){
		$('#fader #timer').animate({width: 940},8900, 'linear');
		target.addClass('current').find('div').animate({top: 400},0).parent().fadeIn(300, function(){
			$(this).find('div').each(function(){
				var index = $(this).index();
				setTimeout(function(){
					$('#fader .current div:eq(' + index + ')').animate({top: -20},100, function(){
						$(this).animate({top: 0},300, function(){
							$('#fader').removeClass('fading');
						});
					});
				},150*index);
			});
		});
	});
}

function faderAuto() {
	autoFader = setInterval(function(){
		var classA = 'next';
		preFader(classA);
	},10000);
}

function rotaActus(target) {
	var count = 0;
	if (target == 'next') {
		$('.actu').animate({top: '-=80'},500, function(){
			count++;
			if (count == $('.actu').length) {
				$('.actu').animate({top: '+=80'},0);
				$('.actu.current').appendTo('#actus').removeClass('current');
				$('.actu:eq(0)').addClass('current');
			}
		});
	}
	else {
		$('.actu:eq(3)').prependTo('#actus');
		$('.actu').animate({top: '-=80'},0).animate({top: '+=80'},500);
	}
}

function autoActus() {
	actuRota = setInterval(function(){
		var target = 'next';
		rotaActus(target);
	},4000);
}

$(document).ready(function(){
	if ($(window).width() <= 980) {
		window.location='mobile.html';
	}
	
	var windowWidth = $(window).width();
	var current = $('#header .current');
	
	$('#line').css({width: current.width(), left: current.position().left});
	
	$('#header a#logo, #nav a').hover(function(){
		$('#line').stop().animate({width: $(this).width(), left: $(this).position().left},300);
	});
	$('#header').mouseleave(function(){
		$('#line').stop().animate({width: current.width(), left: current.position().left},300);
	});
	
	
	$('.projet h3 a').hover(function(){
		$('> span', this).stop().animate({width: 290},300);
	}, function(){
		$('> span', this).stop().animate({width: 26},300);
	});
	
	$(document).bind('mousemove', function(e){
		if (!$('#fader').hasClass('fading')) {
			var mouseX = e.pageX;
			$('#fader div.current .bg_1').css({left: -(mouseX - windowWidth / 2) / 70});
			$('#fader div.current .bg_2').css({left: -(mouseX - windowWidth / 2) / 50});
			$('#fader div.current .bg_3').css({left: -(mouseX - windowWidth / 2) / 30});
		}
	});
	
	$('#fader > div').hide()
	$('#fader > div:first-child').show();
	$('#fader > a').click(function(){
		if (!$('#fader').hasClass('fading')) {
			clearInterval(autoFader);
			var classA = $(this).attr('class');
			preFader(classA);
			setTimeout(function(){
				faderAuto();
			},500);
		}
	});
	
	$('#fader #timer').animate({width: 940},10000, 'linear');
	faderAuto();
	
	$('#fader div a').hover(function(){
		$('span', this).stop().animate({width: 80},300);
	}, function(){
		$('span', this).stop().animate({width: 0},300);
	});
	
	$.get('actus.xml', function(data){
		var nbrActu = $(data).find('actu').length
		var counter = 0;
		$(data).find('actu').each(function(){
			counter++;
			var $actu = $(this);
			var date = $actu.find('date').text();
			var titre = $actu.find('titre').text();
			var detail = $actu.find('detail').text();
			var lien = $actu.find('lien').text();
			var shortUrl = $actu.find('shortUrl').text();
			if (lien == '') {
				var actu = '<div class="actu"><span class="date">'+ date +': </span><span class="titre">' + titre + ' </span>' + detail + '</div>'
			}
			else {
				var actu = '<div class="actu"><a href="' + lien + '" target="_blank"><span class="date">'+ date +': </span><span class="titre">' + titre + ' </span>' + detail + '</a><a href="http://twitter.com/home?status=La mire et @hcotwitt vous recommande cet article : ' + shortUrl + '" class="twitter" title="Twitt this" target="_blank">Twitt this!</a><a href="http://www.facebook.com/sharer.php?u=' + shortUrl + '" class="facebook" title="Share this" target="_blank">Share this!</a></div>'
			}
			$('#actus').append(actu);
			if (counter == nbrActu) {
				$('.actu:eq(0)').addClass('current');
				$('.actu').each(function(){
					$(this).css({marginTop: (80 - $(this).height()) / 2, marginBottom: (80 - $(this).height()) / 2});
				});
			}
		});
	});
	
	$('#actus .next, #actus .prev').click(function(){
		var target = $(this).attr('class');
		rotaActus(target);
	});
	
	$('#actus').hover(function(){
		clearInterval(actuRota);
		$('.next, .prev', this).stop(true, true).fadeIn();
	}, function(){
		autoActus();
		$('.next, .prev', this).stop(true, true).fadeOut();
	});
	
	autoActus();
	
	$('#panel_form, #panel_map, #panel_map iframe').css({width: $(window).width(), height: $(window).height() / 2});
	$('#panel_form').css({top: - $(window).height() / 2 - 40});
	$('#panel_map').css({top: $(window).height()});
	
	$(window).resize(function(){
		$('#panel_form, #panel_map, #panel_map iframe').css({width: $(window).width(), height: $(window).height() / 2});
		$('#panel_form').css({top: - $(window).height() / 2 - 40});
		$('#panel_map').css({top: $(window).height()});
	});
	$('.contact').click(function(){
		$('#panel_form').animate({top: 0});
		$('#panel_map').animate({top: $(window).height() / 2});
	});
	
	$('#panel_form .close').live('click', function(){
		$('#panel_form').animate({top: - $(window).height() / 2 - 40},500);
		$('#panel_map').animate({top: $(window).height()});
	});
	
	$('#anchors').css({left: -$('#anchors').width()});
	
	$('#anchors .open').click(function(){
		if ($(this).hasClass('close')) {
			$(this).removeClass('close');
			$(this).parent().stop().animate({left: -$('#anchors').width()},300);
		}
		else {
			$(this).addClass('close');
			$(this).parent().stop().animate({left: 0},300);
		}
	});
	$('#anchors li a').click(function(){
		var targetId = $(this).attr('href');
		var targetPos = $(targetId).position().top;
		$('html, body').stop().animate({scrollTop: targetPos + 100},800);
		return false;
	});
	
	$('.coming').append('<span>Très bientôt</span>');
	$('.coming').hover(function(){
		$('span', this).stop(true, true).fadeIn(300);
	}, function(){
		$('span', this).stop(true, true).fadeOut(300);
	});
	$('.coming').click(function(){
		return false;
	});
	
	$('.folio:odd').addClass('odd');
	
	$('#agence li:last-child').animate({top: -200},10000, 'linear');
	setInterval(function(){
		$('#agence li:last-child img').fadeOut(3000, function(){
			$(this).parent().css({top: 0}).prependTo('#agence ul').find('img').show();
		});
		$('#agence li:eq(1)').animate({top: -200},10000, 'linear');
	},7000);
	
});
