$.fn.diapo00 = function(options) {
    var defaults = {
        delay: 3,
        animationSpeed: "normal"
    };
    
    var options = $.extend(defaults, options);
    var obj = $(this);
    
    if ($(obj).find("li").length > 1) {
        
        var inter = setInterval(function() { nextElt(options) }, (options.delay * 1000));
       
        var sens = "right";
        var pause = false;
        $(obj).find("li").hide();
        $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
        
        function nextElt(options) {
            $(obj).find("li.active").fadeOut(options.animationSpeed);
            if (!$(obj).find("li.active").is(":last-child")) {
                $(obj).find("li.active").next().addClass("active").prev().removeClass("active");
                $(obj).find("li.active").fadeIn(options.animationSpeed);
            }
            else {
                $(obj).find("li:first-child").addClass("active").fadeIn(options.animationSpeed);
                $(obj).find("li:last-child").removeClass("active");
            }
        }

        function prevElt(options) {
            $(obj).find("li.active").fadeOut(options.animationSpeed);
            if (!$(obj).find("li.active").is(":first-child")) {
                $(obj).find("li.active").prev().addClass("active").next().removeClass("active");
                $(obj).find("li.active").fadeIn(options.animationSpeed);
            }
            else {
                $(obj).find("li:last-child").addClass("active").fadeIn(options.animationSpeed);
                $(obj).find("li:first-child").removeClass("active");
            }
        }
    }
    return this;
};

$(document).ready(function() {
    
    // Open video links in new windows
    $("a", "span.plugin_picture").attr('target', '_blank');
    // Open press releases links in new windows
    $("a", ".plugin_file").attr('target', '_blank');
    // Put a special icon for sound files
    if($('a[href$=".mp3"]', 'p.plugin_file').length){
        var src = $('a[href$=".mp3"]', 'p.plugin_file').parent('p.plugin_file').next('img').attr('src');
        var splitted_url = src.split('/');
        splitted_url[splitted_url.length-1] = "mp3_icon.png";
        $('a[href$=".mp3"]', 'p.plugin_file').parent('p.plugin_file').next('img').attr('src', splitted_url.join('/'));
    }
    
    
    var slider = $('#diapoHome').bxSlider({
        mode : 'fade',
        controls : false,
        auto : true,
        onAfterSlide : function(currentSlideNumber, totalSlideQty, currentSlideHtmlObject){
            var img_id = $('img', currentSlideHtmlObject).attr('id');
            var img_url = $('#'+img_id+'_url').attr('url');
            $('#read_more_area').attr('href', img_url);
        }
    });
    
    $("#pause_area").click(function(){
        if($(this).hasClass('stop')){
            $(this).removeClass('stop');
            slider.stopShow()
        }
        else{
            $(this).addClass('stop');
            slider.goToNextSlide();
            slider.startShow()
        }
    });
    
    $("#method_area").click(function(){
        if($("#pause_area").hasClass('stop')){
            $("#pause_area").removeClass('stop');
        }
        slider.goToSlide(1)
    });
    
    $("#mission_area").click(function(){
        if($("#pause_area").hasClass('stop')){
            $("#pause_area").removeClass('stop');
        }
        slider.goToSlide(0)
    });
    
    $("#technology_area").click(function(){
        if($("#pause_area").hasClass('stop')){
            $("#pause_area").removeClass('stop');
        }
        slider.goToSlide(2)
    });
    
    $("div.bx-window").css('height', '403px');
    
    $('#accordion00').accordion({
        autoHeight: false,
        header: 'dt',
        collapsible: true,
        active: false
    });
    $(".openPopup").fancybox({
        'titlePosition' : 'inside',
        'transitionIn'  : 'none',
        'transitionOut' : 'none'
    });
    $("#more").click(function(){
        $(this).hide();
        $(this).next('div#more_content').slideDown('slow');
        return false;
    });
    $("a.open_contact").click(function(){
        $("a#contact_link").click();
        return false;
    });
    // Contact form validation
    $("form#contact_form").validationEngine('attach');
    
}); 

