(function() {
    
    var W = this, D = this.document;
	
    $(document).ready(function () {

        /**
         * @section     intro flash
         */
        flash_launch.init();
        
        /**
         * @section     carousel
         */
        carousel.init('.home #gammes.carousel', 400, 180);
        carousel.init('.home #products.carousel', 400, 240);
        var leftArrow = '<a href="#" class="leftArrow"><img src="/templates/tcl/img/home/picto/news-carousel-left-arrow.png" alt="Faire défiler vers la gauche" /></a>';
        var rightArrow = '<a href="#" class="rightArrow"><img src="/templates/tcl/img/home/picto/news-carousel-right-arrow.png" alt="Faire défiler vers la droite" /></a>';
        carousel.init('.home #news.carousel', 400, 269, leftArrow, rightArrow);
        
    });
    
    /**
     * @section     Flash
     */
    var flash_launch = {
        init:function(){			
			var so = new SWFObject("/templates/tcl/swf/visio.swf", "flashHome", "900", "320", "9");
			so.addParam("allowScriptAccess", "always");
			so.addParam("wmode", "transparent");

			so.addVariable("xml", "index.php");
			so.addVariable("option", "com_flashhome");
			so.addVariable("format", "xml");
			so.addVariable("view", "xml");
			so.addVariable("id", "1");

            so.write("introFlash");
        }
    };
    
    /**
     * @section     carousel
     */
    var carousel = {
        init: function (elmt, duration, distance, leftArrowHtml, rightArrowHtml) {
            var elmt = $(elmt);
            
            var leftArrow = '<a href="#" class="leftArrow"><img src="/templates/tcl/img/home/picto/arrow-left.png" alt="Faire défiler vers la gauche" /></a>';
            var rightArrow = '<a href="#" class="rightArrow"><img src="/templates/tcl/img/home/picto/arrow-right.png" alt="Faire défiler vers la droite" /></a>';
            
            if(leftArrowHtml) {leftArrow = leftArrowHtml;}
            if(rightArrowHtml) {rightArrow = rightArrowHtml;}
            
            elmt.find('.wrapper').before(leftArrow);
            elmt.find('.wrapper').after(rightArrow);
            
            if(!elmt.find('ul li:first').hasClass('first')) {elmt.find('ul li:first').addClass('first')}
            
            carousel.bindClick(elmt, duration, distance);
        },
        bindClick: function (elmt, duration, distance) {
            var elmt = $(elmt);
            elmt.find('.leftArrow').click(function () {
                if(!elmt.hasClass('move')) {
                    carousel.slide(elmt, 'left', duration, distance);
                }
                return false;
            });
            elmt.find('.rightArrow').click(function () {
                if(!elmt.hasClass('move')) {
                    carousel.slide(elmt, 'right', duration, distance);
                }
                return false;
            });
        },
        slide: function (elmt, direction, duration, distance) {
            var elmt = $(elmt);
            elmt.addClass('move');
            var generalMove = distance+"px";
            
            var signe = "+";
            if(direction == "right") {
                signe = "-";
            } else {
                var lastElmt = elmt.find('ul li:last');
                elmt.find('ul li:last').remove();
                elmt.find('ul').prepend(lastElmt);
                
                elmt.find('ul .first').removeClass('first').removeAttr('class');
                
                elmt.find('ul li:first').addClass('first');
                elmt.find('ul li').css('left', "-"+distance+"px");
                generalMove = "0px";
            }
            
            elmt.find('ul li:not(.first)').css('position', 'relative').animate({
                left: signe+generalMove
            }, duration);
            
            
            elmt.find('ul .first').css('position', 'relative').animate({
                left: signe+generalMove
            }, duration, function () {
                if(direction == "right") {
                    elmt.find('ul li').css('left', '0');
                    elmt.find('ul li').removeAttr('style');
                    
                    var firstElmt = elmt.find('ul li.first');
                    elmt.find('ul li.first').remove();
                    elmt.find('ul').append(firstElmt);
                    
                    elmt.find('ul li.first').removeAttr('class');
                    elmt.find('ul li:first').addClass('first');
                } else {
                    elmt.find('ul li').removeAttr('style');
                }
                elmt.removeClass('move');
            });
        }
    };
	
})();


