var max_attempts = 10;
(function($) {
    $.fn.jCadreClic = function(settings) {
        var nom_timeout = 'delai_' + Math.ceil(Math.random() * 10000);
        //clearTimeout(tab_timeout[nom_timeout]);
        verif_height(this, 0, settings, nom_timeout);
        /*setTimeout(function() {

        }, delai);*/
    };
})(jQuery);

function verif_height(object, attempts, settings, nom_timeout) {
    if (object.height() > 10) {
        clearTimeout(nom_timeout);
        object.jCadreClicExecute(settings);
    } else {
        if (attempts == max_attempts) {
            clearTimeout(nom_timeout);
        }
        attempts = attempts + 1;
        setTimeout(function() {
            verif_height(object, attempts, settings, nom_timeout);
        }, 100);
    }
}

(function($) {
    $.fn.jCadreClicExecute = function(settings) {
        settings = jQuery.extend({
            width_top_left      : 33,
            width_top_right     : 28,
            width_bottom_left   : 27,
            width_bottom_right  : 55,
            height_top_right    : 29,
            height_bottom_right : 31,
            height_top_left     : 26,
            height_bottom_left  : 26,
            padding_width       : 21,
            padding_height      : 19,
            calcul_width        : 0,
            calcul_height       : 0,
            top_left            : 'cadre_clic_top_left',
            top_right           : 'cadre_clic_top_right',
            top_fond            : 'cadre_clic_top_fond',
            bottom_left         : 'cadre_clic_bottom_left',
            bottom_right        : 'cadre_clic_bottom_right',
            bottom_fond         : 'cadre_clic_bottom_fond',
            right_fond          : 'cadre_clic_right_fond',
            left_fond           : 'cadre_clic_left_fond',
            layer1              : 'cadre_clic_layer',
            layer_top           : false,
            layer_bottom        : false,
            div                 : false,
            ie7fix              : false
        },settings);


        var width_top_left = settings.width_top_left;
        var width_top_right = settings.width_top_right;
        var width_bottom_left = settings.width_bottom_left;
        var width_bottom_right = settings.width_bottom_right;
        var height_top_right = settings.height_top_right;
        var height_bottom_right = settings.height_bottom_right;
        var height_top_left = settings.height_top_left;
        var height_bottom_left = settings.height_bottom_left;

        var padding_width = settings.padding_width;
        var padding_height = settings.padding_height;
        var calcul_width = settings.calcul_width;
        var calcul_height = settings.calcul_height;
        var nb_img = this.children('img').length;
        var img_width = this.children('img').width();
        var icon_video = false;


        if (!settings.div) {
            if (nb_img != 1) return;
            var class_img = this.children('img').attr('class');
            if (class_img == 'icon_video') icon_video = true;

            var height = this.children('img').height();
            var width = this.children('img').width();

            var is_ie = false;
            var is_7 = false;
            jQuery.each(jQuery.browser, function(i, val) {
                if (i == 'version') {
                    if (val == '7.0') is_7 = true;
                }
                else if (i == 'msie') {
                    is_ie = true;
                }
            });

            if (settings.ie7fix) {
                if (is_ie && is_7) {
                    height += 2;
                    width += 2;
                }
            }
            /*if (is_ie && is_7) {
                height += 4;
                width += 4;
            }*/

            /*if (width < 80) return;
            else if (height < 40) return;*/


            /*this.css({
                width   : width + 'px',
                height   : height + 'px'
            });*/
            this.width(width).height(height);

            var wrapper = this;
            var plus_height_right = 0;
            var plus_height_left = 0;
            var rel = this.attr('rel');
        } else {

            var rel = false;
            var float = this.css('float');
            this.wrap('<div class="wrapper_cadre" />');
            wrapper = this.parent('div.wrapper_cadre');

            wrapper.css({
                position    : 'relative',
                width       : 100,
                height      : 100
            });
            //return;


            var width = this.width();
            var height = this.height();
           /*if (intval(height) < 27) return;*/

            wrapper.width(width);
            wrapper.height(height);
            wrapper.css({
                position        : 'relative'
                /*width           : width,
                height          : height*/
            });

            if (float) wrapper.css({ float : float });
            if (float == 'right') {
                /*this.css({
                    'margin-left'  : '20px'
                });*/
                var new_width = intval(width);
                this.width(new_width - 20);
                /*this.css({
                    width       : (new_width - 20) + 'px'
                });*/
            }



            var width = intval(width);
            var height = intval(height);
            var plus_height_right = 24;
            var plus_height_left = 22;

        }

        wrapper.append('<div>&nbsp;</div>');
        wrapper.append('<div class="' + settings.top_left + '"></div>');
        wrapper.append('<div class="' + settings.top_right + '"></div>');
        wrapper.append('<div class="' + settings.bottom_left + '"></div>');
        wrapper.append('<div class="' + settings.bottom_right + '"></div>');

        wrapper.append('<div class="' + settings.top_fond + '"></div>');
        calcul_width = width - (width_top_left + width_top_right) + padding_width;

        /*wrapper.find('div.' + settings.top_fond).css({
            width   : calcul_width + 'px'
        });*/
        wrapper.find('div.' + settings.top_fond).width(calcul_width);

        wrapper.append('<div class="' + settings.bottom_fond + '"></div>');
        calcul_width = width - (width_bottom_left + width_bottom_right) + padding_width;
        /*wrapper.find('div.' + settings.bottom_fond).css({
            width   : calcul_width + 'px'
        });*/
        wrapper.find('div.' + settings.bottom_fond).width(calcul_width);

        wrapper.append('<div class="' + settings.right_fond + '"></div>');
        calcul_height = height - (height_top_right + height_bottom_right) + padding_height + plus_height_right;
        /*wrapper.find('div.' + settings.right_fond).css({
            height   : calcul_height + 'px'
        });*/
        wrapper.find('div.' + settings.right_fond).height(calcul_height);

        wrapper.append('<div class="' + settings.left_fond + '"></div>');
        calcul_height = height - (height_top_left + height_bottom_left) + padding_height + plus_height_left;
        /*wrapper.find('div.' + settings.left_fond).css({
            height   : calcul_height + 'px'
        });*/
        wrapper.find('div.' + settings.left_fond).height(calcul_height);

        if (rel) {
            this.append('<div class="layer_black"></div>');
            this.append('<div class="layer_texte"></div>');
            this.find('div.layer_texte').css({
                width       : (width - 30) + 'px'
            }).html($('#' + rel).html());
            //var layer_texte_height = this.find('div.layer_texte').css('height');
            var layer_texte_height = this.find('div.layer_texte').height();
            if (layer_texte_height) {
                this.find('div.layer_black').css({
                    width       : (width - 30) + 'px',
                    opacity     : 0.8,
                    height      : layer_texte_height
                });
            }
            /*this.append('<div class="layer_black"></div>');
            this.find('div.layer_black').css({
                width       : (width - 30) + 'px',
                opacity     : 0.8
            }).html($('#' + rel).html());*/
        }
        else if (settings.layer1) {
            this.append('<div class="' + settings.layer1 + '"></div>');
            calcul_height = height/2;
            //calcul_height = 50;
            this.find('div.' + settings.layer1).css({
                height      : calcul_height + 'px',
                width       : width + 'px',
                opacity     : 0.2
            });
        }

        if (icon_video) {
            this.append('<div class="icon_video_play"></div>');
        }

        if(settings.layer_top) {
            this.append('<div class="' + settings.layer_top + '"></div>');
            this.find('div.' + settings.layer_top).css({
                width       : img_width + 'px'
            });
        }

        if(settings.layer_bottom) {
            this.append('<div class="' + settings.layer_bottom + '"></div>');
            this.find('div.' + settings.layer_bottom).css({
                width       : img_width + 'px'
            });
        }


    };
})(jQuery);


function intval (mixed_var, base) {

    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;
    }
}
