(function($) {
    $.fn.jDiaporama = function(settings) {
        settings = jQuery.extend({
            start                   : 1,
            delai                   : 3000,
            auto                    : true,
            speed                   : 'slow',
            controls                : false,
            div_control             : false,
            div_slider              : 'div_slider',
            transition              : 'move',
            controls_position       : 'bottom',
            controls_height         : 40,
            control_btn_space       : 10,
            control_btn             : 'control_diaporama_bouton',
            control_btn_on          : 'control_diaporama_bouton_on',
            arrows                  : true,
            prev                    : 'div.arrow_left',
            next                    : 'div.arrow_right',
            direction               : 'horizontal',
            item_par_ligne          : 1,
            navigation_visible      : false,
            jDiaporama_next_item    : 'jDiaporama_next_item',   //Class de l'element next item
            jDiaporama_prev_item    : 'jDiaporama_prev_item',   //Class de l'element prev item
            calcul_height           : false                     //Auto calcul de la hauteur
        },settings);

        var start = settings.start;
        var delai = settings.delai;
        var nbDiapos = this.children('div').length;

        if (settings.item_par_ligne != nbDiapos) nbDiapos = Math.ceil(nbDiapos/settings.item_par_ligne);

        var diapos = new Array();
        var boutons = new Array();
        var tab_timeout = new Array();
        var nom_timeout = 'delai_' + Math.ceil(Math.random() * 10000);
        var after_id = '_' + Math.ceil(Math.random() * 10000);

        var hauteur_max = 0;
        var pos = $(this).donne_position();
        var is_timeout = false;
        var conteneur = this;
        var width_conteneur = $(this).width();
        var height_conteneur = $(this).height();

        var last_diapo = '';
        var in_motion = false;
        var page_max_visible = false;
        var page_min_visible = false;
        var nb_pages = false;

        var max_height_item = 0;
        var items_height = new Array();

        //$(this).hide();

        //si le settings.start est trop haut, on le repositionne
        if (start > nbDiapos) start = 1;
        var diapo_en_cours = start;

        if (settings.direction == 'horizontal') {
            this.children('div').each(function (i) {
                //on voit quelle div est la plus haute pour définir la hauteur max du diaporama
                if ($(this).height() > hauteur_max) hauteur_max = $(this).height();
                var controle_start = i + 1;
                if (i+1 == nbDiapos){
                    last_diapo = $(this);
                }
                diapos[controle_start] = $(this);

                switch (settings.transition) {
                case 'move' :
                    var new_margin = i * width_conteneur;
                    $(this).css({width : width_conteneur + 'px', position : 'absolute', marginLeft: new_margin + 'px'});
                    break;
                case 'fade' :
                    $(this).hide();
                    break;
                }
            });
        } else if (settings.direction == 'vertical' && settings.calcul_height) {
            //max_height_item
            this.children('div').each(function (i) {
                items_height[(i+1)] = $(this).height();
                if ($(this).height() > max_height_item)
                    max_height_item = $(this).height();
            });
            this.children('div').each(function (i) {
                $(this).height(max_height_item);
            });
            $(this).height(max_height_item);
            height_conteneur = max_height_item;
        }

        /*else if (settings.direction == 'vertical' && settings.calcul_height) {
            this.children('div').each(function (i) {
                items_height[(i+1)] = $(this).height();
            });
        }*/

        $(this).fadeIn('slow');

        $(this).wrapInner('<div id="' + settings.div_slider + '"></div>');
        $('#' + settings.div_slider).css({position : 'relative', height : hauteur_max});

        //parametrage des fleches
        if (settings.arrows) {
            if (settings.transition == 'move') {
                $(settings.prev).click(function(){
                    if (!in_motion) {
                        if (is_timeout) {
                            clearTimeout(tab_timeout[nom_timeout]);
                            is_timeout = false;
                        }
                        $(this).fadeOut();
                        diapo_precedente();
                        in_motion = true;
                    }
                });
                $(settings.next).click(function(){
                    if (!in_motion) {
                        if (is_timeout) {
                            clearTimeout(tab_timeout[nom_timeout]);
                            is_timeout = false;
                        }
                        $(this).fadeOut();
                        diapo_suivante();
                        in_motion = true;
                    }
                });
            }
        }

        function diapo_suivante() {
            if (nbDiapos <= 1) return false;
            var diapo_a_montrer = diapo_en_cours + 1;
            if (diapo_a_montrer > nbDiapos) diapo_a_montrer = 1;
            active_diapo(diapo_a_montrer, true);
            if (is_timeout)
                tab_timeout[nom_timeout] = setTimeout(function() { diapo_suivante(); }, delai);
        }

        function diapo_precedente() {
            if (nbDiapos <= 1) return false;
            var diapo_a_montrer = diapo_en_cours - 1;
            if (diapo_a_montrer < 1) diapo_a_montrer = nbDiapos;
            active_diapo(diapo_a_montrer, true);
            if (is_timeout)
                tab_timeout[nom_timeout] = setTimeout(function() { diapo_precedente(); }, delai);
        }

        function active_diapo(id_diapo, anim) {
            //

            var cur_diapo = diapos[diapo_en_cours];
            var diapo = diapos[id_diapo];

            switch (settings.transition) {
            case 'move' :
                switch (settings.direction) {
                case 'horizontal' :

                    var new_position = -((id_diapo - 1) * width_conteneur);
                    $('#' + settings.div_slider).animate({
                        'left': new_position + 'px'},
                        'slow',
                        function(){
                            in_motion = false;
                            if (settings.arrows) {
                                if (id_diapo == 1) {
                                    if (nbDiapos > 1) {
                                        $(settings.next).fadeIn('fast');
                                    } else
                                        $(settings.next).fadeOut('fase');
                                    $(settings.prev).fadeOut('fase');
                                } else if (id_diapo == nbDiapos) {
                                    $(settings.prev).fadeIn('fast');
                                    $(settings.next).fadeOut('fase');
                                } else {
                                    $(settings.prev).fadeIn('fast');
                                    $(settings.next).fadeIn('fast');
                                }
                            }
                    });

                    break;

                case 'vertical' :

                    var new_position = -((id_diapo - 1) * height_conteneur);


                    $(settings.div_slider).animate({
                        //height : items_height[id_diapo],
                        'top': new_position + 'px'},
                        'slow',
                        function(){
                            in_motion = false;
                            if (settings.arrows) {
                                if (id_diapo == 1) {
                                    if (nbDiapos > 1) {
                                        $(settings.next).fadeIn('fast');
                                    } else
                                        $(settings.next).fadeOut('fase');
                                    $(settings.prev).fadeOut('fase');
                                } else if (id_diapo == nbDiapos) {
                                    $(settings.prev).fadeIn('fast');
                                    $(settings.next).fadeOut('fase');
                                } else {
                                    $(settings.prev).fadeIn('fast');
                                    $(settings.next).fadeIn('fast');
                                }
                            }
                            if (settings.calcul_height) {
                                //$(settings.div_slider).animate({height : items_height[id_diapo]});
                                $(settings.div_slider).height(items_height[id_diapo]);
                            }
                    });

                    break;
                }


                break;
            case 'fade' :
                cur_diapo.hide();
                diapo.fadeIn(settings.speed);
                break;
            }

            if(settings.controls && settings.control_btn_on) {
                var cur_bouton = boutons[diapo_en_cours];
                var bouton = boutons[id_diapo];
                cur_bouton.removeClass(settings.control_btn_on);
                cur_bouton.addClass(settings.control_btn);
                bouton.removeClass(settings.control_btn);
                bouton.addClass(settings.control_btn_on);
            }
            diapo_en_cours = id_diapo;
        }

        function init_controls() {
            var div_conteneur = create_controls_div();
        }

        function create_controls_div() {
            if (nbDiapos < 2) {
                $('#' + settings.div_control).html('');
                return;
            }
            var html = '';
            for (i = 1; i <= nbDiapos; i++) {
                html = html + '<div id="btn_' + i + after_id + '">' + i + '</div>';
                nb_pages = i;
            }

            if (settings.div_control) {
                $('#' + settings.div_control).html(html);
                var identifiant_control = '#' + settings.div_control;
            } else {
                var html = '<div class="control_diaporama"></div>';
                var identifiant_control = '.control_diaporama';
            }
            var affiche_fleche_suivant = false;
            $(identifiant_control).children('div').each(function (i) {
                if (i) {
                    if (settings.control_btn_space) {
                        $(this).before('<div style="float:left; height:1px; width:' + settings.control_btn_space + 'px;"></div>');
                    }
                }
                if (settings.navigation_visible && (i + 1 > settings.navigation_visible)) {
                    $(this).hide();
                    affiche_fleche_suivant = true;
                    page_max_visible = settings.navigation_visible;
                    page_min_visible = 1;
                }
                //$(this).css({ float: 'left'});
                $(this).addClass(settings.control_btn)
                .click(function(){
                    if (diapo_en_cours == (i + 1)) return false;
                    if (is_timeout) {
                        clearTimeout(tab_timeout[nom_timeout]);
                        is_timeout = false;
                    }
                    active_diapo(i + 1);
                });
                boutons[i+1] = $(this);
            });
            if (affiche_fleche_suivant) {
                $(identifiant_control).prepend('<div id="prev' + after_id + '" class="' + settings.jDiaporama_prev_item + '"></div>&nbsp;&nbsp;&nbsp;');
                $(identifiant_control).append('<div id="next' + after_id + '" class="' + settings.jDiaporama_next_item + '"></div>');
                $('#prev' + after_id).removeClass(settings.jDiaporama_prev_item);
                $('#prev' + after_id).click(function(){
                    var new_id = diapo_en_cours - 1;
                    var nom_id_btn = 'btn_' + new_id + after_id;
                    $('#' + nom_id_btn).click();

                    if (new_id < page_min_visible) {
                        $('#' + nom_id_btn).show();
                        page_min_visible = new_id;

                        $('#btn_' + page_max_visible + after_id).hide();
                        page_max_visible = page_max_visible - 1;
                    }
                    if (new_id < nb_pages) {
                        $('#next' + after_id).addClass(settings.jDiaporama_next_item);
                    }
                    if (new_id == 1) {
                        $(this).removeClass(settings.jDiaporama_prev_item);
                    }
                });
                $('#next' + after_id).click(function(){
                    var new_id = diapo_en_cours + 1;
                    var nom_id_btn = 'btn_' + new_id + after_id;
                    $('#' + nom_id_btn).click();

                    if (new_id > page_max_visible) {
                        $('#' + nom_id_btn).show();
                        page_max_visible = new_id;

                        $('#btn_' + page_min_visible + after_id).hide();
                        page_min_visible = page_min_visible + 1;
                    }
                    if (new_id > 1) {
                        $('#prev' + after_id).addClass(settings.jDiaporama_prev_item);
                    }
                    if (new_id == nb_pages) {
                        $(this).removeClass(settings.jDiaporama_next_item);
                    }

                });
            }
        }

        if (settings.auto) {
            tab_timeout[nom_timeout] = setTimeout(function() { diapo_suivante(); }, delai);
            is_timeout = true;
        }
        if (settings.controls)
            init_controls();
        active_diapo(diapo_en_cours, false);

    };
    jQuery.fn.extend({
        donne_position : function() {
            obj = $(this).get(0);
            var curleft = obj.offsetLeft || 0;
            var curtop = obj.offsetTop || 0;
            while (obj = obj.offsetParent) {
                     curleft += obj.offsetLeft;
                     curtop += obj.offsetTop;
            }
            return {x:curleft,y:curtop};
        }
    });
})(jQuery);

function intval (mixed_var, base) {
    var tmp;

    var type = typeof( mixed_var );

    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

