﻿// Function
// ############################################################
function scrollY(){
    scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ){
        //Netscape compliant
        scrOfY = window.pageYOffset;
    }
    else if( document.body && ( document.body.scrollTop ) ){
        //DOM compliant
        scrOfY = document.body.scrollTop;
    }
    else if( document.documentElement && ( document.documentElement.scrollTop ) ){
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

$(function(){
    ascenseur.init('.widget_archive h3','.widget_archive ul');
	/* fluid scroll */
	$('a[href^=#]').click(function() {
		cible=$(this).attr('href');
		if($(cible).length>=1){
            hauteur=$(cible).offset().top;
		}
		else{
            hauteur=$("a[name="+cible.substr(1,cible.length-1)+"]").offset().top;
		}
		$('html,body').animate({scrollTop: hauteur}, 700);
		return false;
	});
	/* FIN SCROLL */
});

// ############################################################
/******************************
        Make some fun
 ******************************/
//$("#flash").hide().slideDown()

$("#newsletter-nosend").hide().slideDown()
$("newsletter-send").hide().slideDown()

$("#contact-nosend").hide().slideDown(500)
$("#contact-send").hide().slideDown(500)

ascenseur = {
    // Initialisation des comportements
    /////////////////
    init : function(btn,acacher){			
        $(acacher).hide();
        $(btn).css('cursor','pointer');
        $(btn).click(function(){
            $(acacher).slideToggle(300);
            return false;
        });
    }
}

/******************************
        Autoempty
 ******************************/
$("input.autoEmpty").each(function(){
    var defaultText = $(this).val();
    $(this).focus(function(){
        if($(this).val()==defaultText){
            $(this).val("");
        }
    });
    $(this).blur(function(){
        if($(this).val()==""){
            $(this).val(defaultText);
        }
    });
});

/******************************
        Elevator
 ******************************/
$(function(){ 
    $('.sticky').each(function(){
        var parent = $(this).parent();
        var dTop = $(this).offset().top;
        var elem = $(this);
        $(window).scroll(function(){
            parent.css('position','relative');
            elem.css('position','relative');
            if(scrollY()>dTop){
                elem.stop().animate({top:scrollY()-parent.offset().top+20},500);
            }
            else{
                elem.stop().animate({top:dTop-parent.offset().top},500)
            }
        });
        if(scrollY()>dTop){
            elem.stop().animate({top:scrollY()-parent.offset().top+20},500);
        }
    });  
});

/******************************
        Carrousel
 ******************************/
var carrousel = {

    nbSlide:0,
    nbCurrent:1,
    elemCurrent:null,
    elem:null,
    timer:null,

    init:function(elem){
        this.nbSlide=elem.find(".slide").length;
       
        // Créer la pagination
        elem.append('<div class="navigation"></div>');
        for(var i=1;i<=this.nbSlide;i++){
            elem.find(".navigation").append("<span>"+i+"</span>");
        }
        elem.find(".navigation span").click(function(){
            carrousel.gotoSlide($(this).text());
        })
        
        // Initialisation du carrousel
        this.elem=elem;
        elem.find(".slide").hide();
        elem.find(".slide:first").show();
        this.elemCurrent=elem.find(".slide:first");
        
        this.elem.find(".navigation").css("opacity",0.6);
        this.elem.find(".navigation span:first").addClass("active");
        
        // Créer le timer
        carrousel.play();
        
        // Stop quand on passe dessus
        elem.mouseover(carrousel.stop);
        elem.mouseout(carrousel.play);
    },
    
    gotoSlide:function(num){
        if(num==this.nbCurrent){return false;}
        this.elemCurrent.find(".visu").fadeOut();
        this.elem.find("#slide"+num).show();
        this.elem.find("#slide"+num+".visu").hide().fadeIn();
        
        var titleHeight=this.elemCurrent.find(".title").height();
        this.elemCurrent.find(".title").animate({"bottom":-titleHeight},500);
        this.elem.find("#slide"+num+" .title").css("bottom",-titleHeight).animate({"bottom":0},500);
        this.elem.find(".navigation span").removeClass("active");
        this.elem.find(".navigation span:eq("+(num-1)+")").addClass("active");
        this.nbCurrent=num;
        this.elemCurrent=this.elem.find("#slide"+num);
    },
    
    next:function(){
        var num=this.nbCurrent+1;
        if(num>this.nbSlide){num=1;}
        this.gotoSlide(num);
    },
    
    prev:function(){
        var num=this.nbCurrent-1;
        if(num<1){num=this.nbSlide;}
        this.gotoSlide(num);
    },
    
    stop:function(){
        window.clearInterval(carrousel.timer);
    },

    play:function(){
        window.clearInterval(carrousel.timer);
        carrousel.timer=window.setInterval("carrousel.next()",10000);
    }
}

$(function(){
    carrousel.init($("#carrousel"));
});

