﻿/*

SOMMAIRE

- Déclaration / vérification des variables globales
- Onglets page d'accueil
- Smooth Scroll sur toutes les ancres
- SlideShow des logos
- Google Analytics

*/


////////////////////////////////////////////////
/////////////////// DomReady ///////////////////
////////////////////////////////////////////////


window.addEvent('domready', function() {

    /* VARIABLES GLOBALES */
    // URL du site, URL de la page courante

    var cap_siteURL = document.location.protocol + '//' + document.location.hostname;
    var cap_pageURL = document.location;
    if (typeof cap_CMS_SiteName == "undefined") cap_CMS_SiteName = "";
    if (typeof cap_CMS_DocumentName == "undefined") cap_CMS_DocumentName = "";
    if (typeof cap_CMS_Environment == "undefined") cap_CMS_Environment = "";
    if (typeof cap_SiteRootUrl == "undefined") cap_SiteRootUrl = "";
    if (typeof cap_CMS_AliasPath == "undefined") {
        cap_CMS_AliasPath = "";
    } else {
        if (cap_CMS_AliasPath.slice(0, 1) == "/") {
            cap_CMS_AliasPath = cap_CMS_AliasPath.slice(1);
        }
        cap_CMS_AliasPath = cap_CMS_AliasPath.toLowerCase()
    }

    var cap_globalVarAlert = {
        view: function() {
            alert(
                'hostname = ' + document.location.hostname + '\n' +
                'cap_siteURL = ' + cap_siteURL + '\n' +
                'cap_pageURL = ' + cap_pageURL + '\n' +
                'cap_CMS_SiteName = ' + cap_CMS_SiteName + '\n' +
                'cap_CMS_DocumentName = ' + cap_CMS_DocumentName + '\n' +
                'cap_CMS_Environment = ' + cap_CMS_Environment + '\n' +
                'cap_SiteRootUrl = ' + cap_SiteRootUrl + '\n' +
                'cap_CMS_AliasPath = ' + cap_CMS_AliasPath
            );
        }
    }
    //cap_globalVarAlert.view();



    /*  AUTOPROMO ACCUEIL (ONGLETS)  */
    // Gestion du contenu dans des onglets

    var cap_tabs = {

        set: function(tabs, panelAera, panels, delayValue) {

            if (tabs && panelAera && panels) {

                // INITIALISATION DES VARIABLES

                var panelAera = panelAera[0];

                // initialisation des variables pour le delais d'ouverture d'un panel avec 'mouseenter'
                if (!delayValue) delayValue = 600;
                var testDelay;

                // effet accordeon à l'ouverture des panels
                var openItemFx = new Fx.Morph(panelAera, { duration: 200, transition: Fx.Transitions.Linear });
                
                // initialisation des index des panels courant et précédent
                var activePanelIndex;
                var previousPanelIndex = 0;
                
                // initialisation du tableau contenant la hauteur de chaque panel
                var heightPanels = new Array;
                panels.each(function(item, index) {
                    heightPanels[index] = item.getDimensions()['height'];
                    if (item.hasClass('active')) {
                        activePanelIndex = index;
                        openItemFx.start({
                            'height': heightPanels[index]
                        });
                    }
                });

                // ACTIVATION D'UN ONGLET
                var showItem = function(el) {
                    openItemFx.cancel();
                    openItemFx.start({
                        'height': heightPanels[activePanelIndex]
                    });
                    el.addClass('active');
                };

                // DÉSACTIVATION D'UN ONGLET
                var hideItem = function(el) {
                    panels.each(function(item) {
                        el.removeClass('active');
                    });
                }

                // ACTIVATION D'UN ONGLET
                var activeTab = function(el) { el.addClass('active'); }
                
                // DÉSACTIVATION D'UN ONGLET
                var unactiveTab = function(el) { el.removeClass('active'); }

                // hover-unhover tab
                var hoverTab = function(el) { el.addClass('hover'); }
                var unhoverTab = function(el) { el.removeClass('hover'); }

                // SHOW TAB (Click)
                var showTab = function() {
                    if (this > 0) {
                        var index = this;
                    } else {
                        var index = 0;
                    }
                    // màj des indexs
                    previousPanelIndex = activePanelIndex;
                    activePanelIndex = index;
                    // annulation de delay
                    testDelay = false;
                    // mascage de tous les panels
                    panels.each(function(item) {
                        hideItem(item);
                    });
                    // affichage du bon panel
                    showItem(panels[index]);
                    // desactivation de tous les tags
                    tabs.each(function(item) {
                        unactiveTab(item);
                    });
                    // activation du bon tag
                    activeTab(tabs[index]);
                };

                // SHOW TAB (MouseEnter)
                var initShowTabWithDelay = function() {
                    if (this > 0) {
                        var index = this;
                    } else {
                        var index = 0;
                    }
                    // màj des indexs
                    previousPanelIndex = activePanelIndex;
                    activePanelIndex = index;
                    // initialisation du delais d'affichage
                    var showTabWithDelay = function() {
                        if (testDelay == panels[index]) {
                            // mascage de tous les panels
                            panels.each(function(item) {
                                hideItem(item);
                            });
                            // affichage du bon panel
                            showItem(panels[index]);
                            // desactivation de tous les tags
                            tabs.each(function(item) {
                                unactiveTab(item);
                            });
                            // activation du bon tag
                            activeTab(tabs[index]);
                        }
                    };
                    // initialisation du tab à afficher après délais
                    testDelay = panels[index];
                    // addichage du tab après un certain delais
                    showTabWithDelay.delay(delayValue);
                };
                var cancelShowTabWithDelay = function() {
                    // annulation de l'ouverture d'un tab
                    testDelay = null;
                };

                // ATTACH EVENTS
                tabs.each(function(item, index) {
                    item.addEvents({
                        'click': showTab.bind(index),
                        'mouseenter': initShowTabWithDelay.bind(index),
                        'mouseleave': cancelShowTabWithDelay.bind(index)
                    });
                });

            }
        }
    };

    cap_tabs.set($$('#autoPromo .tabs li'), $$('#autoPromo .panels'), $$('#autoPromo div.panel'), 600);



    /* SMOOTH SCROLL */
    // Smooth Scroll sur toutes les ancres
    
    var cap_smoothScroll = new Fx.SmoothScroll({ duration: 1000, transition: Fx.Transitions.Cubic.easeInOut });



    /* SLIDESHOW DE LOGOS */
    // Rotation des accréditations

    cap_slideshowLogo = {

        init: function(imagesList, slideshowElement) {
            if (imagesList && slideshowElement && slideshowElement != '') {
                cap_slideshowLogo.prepareHtml(imagesList, slideshowElement);
                cap_slideshowLogo.createSlideshow(slideshowElement);
            }
        },

        prepareHtml: function(imagesList, slideshowElement) {
            var list = imagesList.get('html');
            imagesList.set('html', list + '<div id="' + slideshowElement + '"><img /></div>');
        },

        createSlideshow: function(slideshowElement) {

            var data = [];
            $$('.slideshow-list li img').each(function(item) {
                data = data.include(item.getProperty('src'));
            });

            var myShow = new Slideshow(slideshowElement, data, { loader: false, linked: false, overlap: true, resize: false, width: 168, height: 90, delay: 4000, duration: 300 });

        }
    };
    cap_slideshowLogo.init($$('.logoSlideshow'), 'my_show');



    /*  SQUEEZE BOX  */
    // Affichage d'un contenu dans un popup inline (ajax, from elements, iframe content, simgles images, youtube flash...)

    SqueezeBox.assign($$('a.boxed'), {
        size: { x: 750, y: 450 },
        sizeLoading: { x: 700, y: 400 },
        parse: 'rel',
        overlayOpacity: 0.2,
        ajaxOptions: {
            method: 'get' // we use GET for requesting plain HTML
        }
    });
    
    SqueezeBox.assign($$('a.certif'), {
        size: { x: 890, y: 450 },
        sizeLoading: { x: 700, y: 400 },
        parse: 'rel',
        overlayOpacity: 0.2,
        ajaxOptions: {
            method: 'get' // we use GET for requesting plain HTML
        }
    });


    /*  GOOGLE ANALYTICS  */
    // Injection du script Google Analytics

    var cap_googleAnalytics = {

        add: function(firstTracker, secondTracker) {

            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
                onload: function() {
                    firstTracker = _gat._getTracker("UA-442299-38");
                    firstTracker._initData();
                    firstTracker._trackPageview();
                    secondTracker = _gat._getTracker("UA-10183742-1");
                    secondTracker._initData();
                    secondTracker._trackPageview();

                    // pour tracker tous les liens pointant vers l'extérieur
                    if (firstTracker && secondTracker) {
                        $$('a[href^=http]').each(function(el) {
                            el.addEvent('click', function() {
                                var dd = '/outgoing/' + el.get('href').replace('http://', '');
                                firstTracker._trackPageview(dd);
                                secondTracker._trackPageview(dd);
                            } .bind(this));
                        });
                    }
                }
            });
        }
    };

    //ne pas ajouter google analytics sur les sites s'executant en local ou en stage
    if (cap_CMS_Environment != "stage" && cap_CMS_Environment != "localhost") cap_googleAnalytics.add();

});