var infos, cssObj, firstLi, target, stringref = 'e1', letImage = 2, image, espeed = 1000;

target = 'includes/eglou1.html';
vuTravail = "#text-contour";

var cookieName = 'premiere';

//message lors de la premi俊e visite
function checkCookie() {
    if (document.cookie.length > 0 && document.cookie.indexOf(cookieName + '=') != -1) {
    } 
    else {
   		$(document).one('mouseenter', function(){
   			$('#premiere').delay(500).fadeTo(500, 1);
           	for (i = 0; i<= 7; i++) {
     			$('#premiere').animate({'margin-left': +330  }, 500,  'easeOutCirc' ).animate({'margin-left': +300  }, 400,  'easeInQuad' );
     		}
			$('#premiere').fadeTo(1000, 0);
			});
        document.cookie = cookieName + "=1";
    }
}

//animation
function animTrans(e){
   $(e).fadeIn(1000);
};

function divEglou() {
	$('#text-contour').hide();
};
			
function divRea() {
	$('#glouglou').hide();
};

function ajaxLoad() {
	$('#' + stringref).html('<p><img src="images/ajax-loader.gif" /></p>');
	
	if (stringref == 'e1') $('#e1').html(image);

};

//ajax eglou
function ajaxEglou() {
	divEglou();
	$.ajax({
	    url: target,
	    success: function(htmlReturn) {
	    	var titre = $('#e-titre', htmlReturn).html();
	    	var desc = $('#e-texte', htmlReturn).html();
	    	if (letImage != 1) {
	    		image = $('.image', htmlReturn).html();
	    		ajaxLoad();
	    	}
	    	$('#glouglou #e-titre').html(titre);
	    	$('#glouglou #e-texte').html(desc);
	    }			
	});
	animTrans('#glouglou');
	return false;
};

//ajax rea
function ajaxRea() {
	divRea();
	$.ajax({
	    url: target,
	    success: function(htmlReturn) {
			var client = $('#client', htmlReturn).html();
			var date = $('#date', htmlReturn).html();
			var travail = $('#travail', htmlReturn).html();
			var url = $('#lien', htmlReturn).html();
			if (letImage != 1) {
	    		image = $('.image', htmlReturn).html();
	    		ajaxLoad();
	    	}
			$('#client .info-load').html(client);
			$('#date .info-load').html(date);
			$('#travail .info-load').html(travail);
			if (url == 0) {
				$('#lien .info-load').html('');
				$('#lien').hide();
			}
			else {
				$('#lien .info-load').html(url);
				$('#lien').show();
			}
			$('.info-titre').stop().fadeTo(0, 0.4).delay(espeed).fadeTo(0, 1);
			$('.info-load').stop().fadeTo(0,0).delay(espeed).fadeTo(200,1)
	    }
	});
	animTrans('#text-contour');
	return false;
};			

$(document).ready(function() {

checkCookie();
//initialisation du scroll
$.localScroll({
	target: '#visio',
	duration:1000,
	axis:'xy',
	easing:'easeOutExpo',
	onAfter:function(anchor, settings){
		if (letImage != 1) $('#' + stringref).html(image);
	}
});

	$('a[rel*=facebox]').facebox();

	ajaxEglou();
	$('#about').addClass('selected').find('li:first').addClass('get').next().find('a').addClass('active');
	$('#grid ul').find('li:first').not('.get').fadeTo(0, 0.7);
	 
// fonctions clic grid
	$('#grid ul li a').click(function() {
		target = $(this).attr("link");
		colorref = $(this).attr('href').substr(0,$(this).attr('href').length-1);
		urlAdd = $(this).attr("title");
		stringref = $(this).attr("href").split('#')[1];
		letImage = $(this).is('.loaded');
		
		//on ne recharge pas sur le lien actif
		if (! $(this).is('.active'))  {				
				if ( colorref == '#e') {
				    ajaxEglou();
				}
				else {
				    ajaxRea();
				}
		}
		
		$(this).addClass("selection");
	
		$('#grid ul li a').removeClass("active");
		$(this).addClass("active").addClass('loaded');
		
		if (colorref == '#e') {
			cssObj = '#444';
			firstLi = 'about';
		}
		else if (colorref == '#v') {
			cssObj = '#047391';
			firstLi = 'vitrine';
		}
		else if (colorref == '#d') {
			cssObj = '#047391';
			firstLi = 'dynamique';
		}
		else if (colorref == '#l') {
			cssObj = '#007900';
			firstLi = 'Logos';
		}
		else if (colorref == '#p') {
			cssObj = '#640000';
			firstLi = 'Print';
		}

		//grid
		$('.selection').removeAttr("style");
		if ( colorref == '#e') {
			$('.active').animate({ 'background-color' : '#ce0000'}, 400);
		}
		else {
			$('.active').animate({ 'background-color' : cssObj}, 400);
		}
		
		//bande top
		$('.color').animate({ 'background-color' : cssObj });
		
		// ne pas lancer le fondu dans la selection courante
		if ($('ul#' + firstLi ).is('.selected')) {
			$('#' + firstLi + ' li.first').fadeTo(0, 1);
		}
		else {
			//$('li.first:not').fadeTo(200, 0.7);
			$('#' + firstLi + ' li.first').fadeTo(200, 1);
		}
		
		$('#grid ul').removeClass("selected");
		$(this).parents("#grid ul").addClass("selected");
		
		//reecriture de l'url: 2 on coupe le nom de la div et l'index et on ajoute le titre
		window.location.hash = (document.location.href.split("index.html", "/") + urlAdd + '.html');
		
		return false;
	});
});
