var span1Table = ["D&eacute;veloppeur","Programmeur","Cr&eacute;ateur","Constructeur","Concepteur"];
var span2Table = ["Site Internet","Site Web","Webdesign","Charte graphique"];
var mot1;
var mot2;
var mot3;
var mot4;
var nbAnim1;
var nbAnim2;
var nbTemp;
var isLoading;

//Analytics (a tester)
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-3966601-5']);
_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);
})();


$(function(){
	//Animation header
	
	$('#animation #partie1 span:first').css("opacity", "0");
	$('#animation #partie3 span:first').css("opacity", "0");
	
	nbAnim1=0;
	nbAnim2=0;
	mot1=span1Table[nbAnim1];
	mot2=span1Table[1];
	mot3=span2Table[nbAnim2];
	mot4=span2Table[1];
	
	$('#animation #partie1 span:first').html(mot1);
	$('#animation #partie1 span:last').html(mot2);
	$('#animation #partie3 span:first').html(mot3);
	$('#animation #partie4 span:last').html(mot4);
	goDown();

	createEffectFadeTooltip();

	//Ajout des textes oranges
	$('.bloc h2').each(add_orange);
	$('h3').each(add_orange);
	
	//Ajout des tooltips
	$("#aptitudes a[title]").tooltip({effect: 'bouncy'}); //Les aptitudes
	$(".bloc h2 a[title]").tooltip({effect: 'bouncy'}); //Les titre de blocs
	$(".inactif[title]").tooltip({effect: 'bouncy'}); //Le bouton submit  inactif

	//Gestion de l'affichage du bloc contact	
	$('#contact input,#contact textarea').focus(inInput).blur(outInput);
	$('#contact input').keyup(showHideInputLabel);
	$('#contact textarea').keyup(showHideTextareaLabel);
	showHideSubmit();
	$('#contact input,#contact textarea').keyup(showHideSubmit);
	
	//DŽplacement du body pour la navigation
	$('#navigation a').click(moveBodyTo);
	
	
	//Rajouter des réalisations
	isLoading=false;
	$('#nextRealisations').click(loadNextRealisations);
	
	//Animation logo
	$('#logo').hover(
		function(){
			$(this).animate({top:"0"},200)
		},
		function(){
			$(this).animate({top:"-10"},200)
		}
	);
});

//Animation header
function goDown () {

	if(Math.floor(Math.random()*2)==0){
		var span1=$('#animation #partie1 span:first');
		var span2=$('#animation #partie1 span:last');
		
		span1.delay(1200).animate(
			{top:"13",opacity:1}, 
			500
		)
		span2.delay(1200).animate(
			{top:"13",opacity:0}, 
			500,
			function() {
    			span1.css("top", "0px").css("opacity", "0");
    			span2.css("top", "0px").css("opacity", "1");
    			mot2=mot1;
    			nbTemp=nbAnim1;
    			while(nbAnim1==nbTemp){
    				nbAnim1=Math.floor(Math.random()*span1Table.length);
    			}
    			mot1=span1Table[nbAnim1];
    			span1.html(mot1);
				span2.html(mot2);
    			goDown();
  			}
		)
	}
	else {
		
		var span3=$('#animation #partie3 span:first');
		var span4=$('#animation #partie3 span:last');
				
		span3.delay(1200).animate(
			{top:"13",opacity:1}, 
			500
		)
		span4.delay(1200).animate(
			{top:"13",opacity:0}, 
			500,
			function() {
    			span3.css("top", "0px").css("opacity", "0");
    			span4.css("top", "0px").css("opacity", "1");
    			mot4=mot3;
    			nbTemp=nbAnim2;
    			while(nbAnim2==nbTemp){
    				nbAnim2=Math.floor(Math.random()*span2Table.length);
    			}
    			mot3=span2Table[nbAnim2];
    			span3.html(mot3);
				span4.html(mot4);
    			goDown();
  			}
		)
	}
		
	
	
}



//Ajout des textes oranges

function add_orange(){
	$this=$(this);
	
	var reg=new RegExp("^(<a.*>)?([a-zA-Z0-9_!?:;,\350\351\133\136\138\151\.\-]*) (.*)(</a>)?$");
	$this.html($this.html().replace(reg,'$1$2<span>$3</span>$4'));
}

//Ajout des tooltips

function createEffectFadeTooltip() {
	// create custom tooltip effect for jQuery Tooltip
	$.tools.tooltip.addEffect(
		"bouncy",

		// opening animation
		function(done) {
			this.getTip().fadeIn('100');
		},
	
		// closing animation
		function(done) {
			this.getTip().fadeOut('100');
		}
	);
}


//Gestion de l'affichage du bloc contact	

function inInput(){
	$this=$(this);
	$this.addClass('focus').removeClass('blur');
}

function outInput(){
	$this=$(this);
	$this.addClass('blur').removeClass('focus');
}

function showHideInputLabel(){
	$this=$(this);
	if($this.val()!='')
		$this.next('label').fadeOut();
	else
		$this.next('label').fadeIn();
}

function showHideTextareaLabel(){
	$this=$(this);
	if($this.val()!='')
		$this.prev('label').fadeOut();
	else
		$this.prev('label').fadeIn();
}


function showHideSubmit(){
	if($('#name').val()=='' || $('#mail').val()=='' || $('#message').val()==''  ){
		$(".inactif").show();
		$('#submit').fadeOut();
	}
	else {
		$('#submit').fadeIn(500,function () {
            $(".inactif").hide();
		});
	}
}

//DŽplacement du body pour la navigation

function moveBodyTo(){
    $this=$(this);
    var offset=$('#'+$this.attr('rel')).offset();
    $('html,body').animate({scrollTop:offset.top},500);
    return false;
}


//Rajouter des réalisations
function loadNextRealisations(){
	if(isLoading==true)
		return false;
	
	var $this=$(this);
	var indexRealisation=$this.attr('class');
	var newIndexRealisation=String(parseInt(indexRealisation)+4);
	$.ajax({
		url:$this.attr('href')+'&indexRealisations='+indexRealisation,
		cache:false,
		success:function(response){
			isLoading=false;
			$this.removeClass(indexRealisation);
			$this.addClass(newIndexRealisation);
			$this.before(response);
			if($('.newRealisations:last').html()==""){
				$('#nextRealisations').slideUp();
			}
			else {
				$('.newRealisations:last').hide().slideDown(200);
				$('.newRealisations:last h3').each(add_orange);
			}
		},
		error:function(XMLHttpRequest,textStatus,errorThrows){
			$this.before(errorThrows);
		}
	});
	
	return false;
}
