$(document).ready(function() {
    // Mise en place du slider pour les réalisations
    rea_slider();

    // Pour pouvoir scroller de catégorie en catégorie via le menu
    $('.menu .presentation').click(function() { $.scrollTo(0, 1300); });
    $('.menu .skills').click(function() { $.scrollTo('#skills', 1300); });
    $('.menu .realisations').click(function() { $.scrollTo('#realisations', 1300); });
    $('.menu .contact').click(function() { $.scrollTo('#contact', 1300); });
	
	// Sous IE, on place la bulle en dehors du container
	if($.browser.msie){
		$('#bulle').remove();
		$('<div id="bulle"></div>').insertBefore('#container');
	}
	
    // Bulle "Contact Me"
	$('#bulle').click(function(){ $.scrollTo('#contact', 1300); });
	
	// Lien "Top" dans le footer
	$('#footer-last-links div').click(function(){ $.scrollTo(0,1300); });

    // Pour afficher l'adresse mail
	$('span.mail').each(function(){
		var addr = $(this).text().replace('/!\\Vous', 'yukilas').replace(' devez ', '@').replace('utiliser', 'naeka').replace(' le ', '.').replace('javascript', 'fr');
		$(this).html('<a href="mailto:' + addr + '">' + addr + '</a>');
	});

	// Lien sur le header
	$("#header-yukilas h1").click(function(){ location.reload(); });
	
    // Pour déclencher le script d'envois de mail
    $('#contact_form_submit').click(function() {
        mail();
    });

    // Pour afficher une bulle d'information (tipsy)
    $('#contact-bonus img').tipsy();

    // Correction d'un petit bug de la textarea
    $('#contact_form textarea').focus(function() {
        if (!$(this).hasClass('initialized')) {
            $(this).attr('value', '');
            $(this).addClass('initialized');
        }
    });
	
	// Changement de la description en fonction du tag sur lequel on a cliqué
	$('#php').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#python').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#mysql').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#xhtml').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#css').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#django').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#inte').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#jquery').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#js').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#xml').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#unix').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	$('#wordpress').click(function(){ changeSkill('skills-'+$(this).attr('id')); });
	
});

function changeSkill(newSkill){
	var height = $('#skills-description').css('height');
	if(height != "110px"){
		$('#skills-description').animate({
				height: "110px"
			}, 500, function(){
				$('#skills-description div').each(function(){
					$(this).css('display','none');
				});
				$('#'+newSkill).fadeIn('slow');
			}
		);
	}
	else{
		$('#skills-description div').each(function(){
			$(this).css('display','none');
		});
		$('#'+newSkill).fadeIn('slow');
	}
}


function mail() {
	$('#contact-loader').html('<img src="static/img/loader.gif" alt="" />');

    var name = $('#contact form input[type=text]').first().attr('value');
    var mail = $('#contact form input[type=text]').last().attr('value');
    var content = $('#contact form textarea').attr('value');

    $.post("actions/mail.php", { name: name, mail: mail, content: content },
        function success(data) {
            if (data == "success") {
                $('#success-contact').fadeIn(300).delay(10000).fadeOut(500);
            }
            else if (data == "spam") {
                $('#error-contact-spam').fadeIn(300).delay(10000).fadeOut(500);
            }
			else if(data == "missing"){
				$('#error-contact-missing').fadeIn(300).delay(10000).fadeOut(500);
			}
            else {
                $('#error-contact').fadeIn(300).delay(10000).fadeOut(500);
            }
			
			$('#contact-loader').html('');
        });
}   

/* Realisations slider */
function rea_slider() {
    $(".main_image .desc").show(); //Show Banner
    $(".main_image .block").animate({ opacity: 0.85 }, 1); //Set Opacity

    $(".image_thumb ul li:first").addClass('active');  //Add the active class (highlights the very first list item by default)
    $(".image_thumb ul li").click(function() {
        //Set Variables
        var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
        var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
        var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
        var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"

        if ($(this).is(".active")) {  //If the list item is active/selected, then...
            return false; // Don't click through - Prevents repetitive animations on active/selected list-item
        } else { //If not active then...
            //Animate the Description
            $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250, function() { //Pull the block down (negative bottom margin of its own height)
                $(".main_image .block").html(imgDesc).animate({ opacity: 0.85, marginBottom: "0" }, 250); //swap the html of the block, then pull the block container back up and set opacity
                $(".main_image img").attr({ src: imgTitle, alt: imgAlt }); //Switch the main image (URL + alt tag)
            });
        }
        //Show active list-item
        $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
        $(this).addClass('active');  //Add class of 'active' on the selected list
        return false;

    }).hover(function() { //Hover effects on list-item
        $(this).addClass('hover'); //Add class "hover" on hover
    }, function() {
        $(this).removeClass('hover'); //Remove class "hover" on hover out
    });
}