/**
 * Gestion d'un caroussel avec JQuery
 *
 * Permet de gérer un systeme de caroussel en 3D
 *
 * @author bbroussolle
 * @since 2009-11-23
 * @requires jquery
 * @copyrights SQLI 2009
 */
(function($) {

    $.fn.caroussel = function(params) {
        var master_box = $(this);

        var returns = new Array();

        master_box.each(function() {
            master_obj = $(this);
            var obj = {
                constructor: function() {
                    this.saveParams(this.defaults);
                    this.saveParams(params);
                    this.convertHTML();
                    master_obj = master_obj.blockImage({ osT: this.params.osT, osL: this.params.osL, W: this.params.W, H: this.params.H, iH: this.params.iH, iW: this.params.iW, cp: this.params.cp, p: this.params.p, s: this.params.s })
                    this.clickL();
                    this.clickR();
                    return obj;
                },
                defaults: { btnL: $('#gauche'), btnR: $('#droite'), osT: 0, osL: 0, W: 200, H: 100, iH: 110, iW: 110, cp: 300, p: 2, s: 1500 },
                params: {},
                saveParams: function(newParams) { this.params = $.extend(this.params, newParams); },
                clickL: function() {
                    var e = this;
                    this.params.btnL.click(function() {
                        $(this).unbind('click');
                        master_obj.carouselMove('gauche', e.clickL());
                        return false
                    });
                },
                clickR: function() {
                    var e = this;
                    this.params.btnR.click(function() {
                        $(this).unbind('click');
                        master_obj.carouselMove('droite', e.clickR());
                        return false
                    });
                },
                convertHTML: function() {
                    var listElement = master_obj.children('ul').children('li').clone();
                    master_obj.html('');

                    listElement.each(function() {
                        var child = $(this).children()
                        var html = null;
                        switch (child[0].tagName) {
                            case 'A':
                                html = child.html();
                                html = $(html);
                                if (navigator.appName == 'Microsoft Internet Explorer')
                                {
                                    // IE6, 7 , 8 : On remplace l'extension .PNG par .GIF
                                    var oldSrc = html.attr('src');
                                    var newSrc = oldSrc;
                                    if (oldSrc.substring(oldSrc.length - 3, oldSrc.length).toUpperCase() == 'PNG') {
                                        newSrc = oldSrc.substring(0, oldSrc.length - 3) + "gif";
                                    }
                                    html.attr('src', newSrc);                                    
                                }
                                html.click(function() {
                                    $.cookie("caroussel", html.attr('src'), {});
                                    document.location.href = child.attr('href');
                                });
                                break;
                            case 'IMG':
                                html = $(this).html();
                                html = $(html);
                                break;
                        }
                        var image = html;
                        image.css({ position: 'absolute' });
                        master_obj.append(image);
                    });
                }
            };
            obj.constructor();
            returns.push(master_obj);
        });

        return returns;
    };
})(jQuery);


