﻿/* 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();

window.addEvent('domready', function() {

    /* CUFON */
    if (typeof Cufon == 'function') {
        //Cufon.replace('.newsList h2');
        Cufon.replace('h2.title');
        Cufon.replace('.formatting h3');
        //Cufon.replace('.formatting h4');
        //Cufon.replace('.year a');
    };


    /* MENU */
    if ($('topMenu')) {
        $$('#topMenu ul').each(function(item) {
            item.setStyle('z-index', 999);
        });
        var myMenu = new MenuMatic({
            id: 'topMenu',
            opacity: 98
        });
    }



    /* SMOOTH SCROLL */
    // Smooth Scroll sur toutes les ancres

    var cap_smoothScroll = new Fx.SmoothScroll({
        duration: 800,
        transition: Fx.Transitions.Pow.easeOut
    });




    /* ACCORDÉONS */
    // boite dans la sidebar

    var cap_accordion = {
        launch: function(toggles, content, displayElement) {

            // initialisation de l'accordéon
            if (displayElement) {
                if (toggles && content) {
                    initAccordion(toggles, content, displayElement);
                }
            } else {
                if (toggles && content) {
                    initAccordion(toggles, content);
                }
            }

            function initAccordion(toggles, content, displayElement) {
                if (!displayElement) displayElement = 0;
                var AccordionObject = new Accordion(toggles, content, {
                    display: displayElement
                });
            }

            // ouverture d'un élément au passage de la souris, avec un délais
            toggles.addEvents({
                'mouseenter': function() {
                    this.setProperty('status', 'wait');
                    function openAccordion() {
                        if (this.getProperty('status') == 'wait') {
                            this.fireEvent('click');
                            this.removeProperty('status');
                        }
                    };
                    openAccordion.bind(this).delay(1500);
                },
                'mouseleave': function() {
                    this.removeProperty('status');
                }
            });

        }
    }

    cap_accordion.launch($$('.togglers'), $$('.elements'), -1);




    /* LIENS EXTERNES */
    // améliore l'ergonomie et la visibilité des liens pointant vers l'extérieur du site

    var cap_externalLinks = { // Mise à jour : 15/01/2009

        init: function(container, noInproveCssClass) {
            if (container && noInproveCssClass) cap_externalLinks.improve(container, noInproveCssClass);
        },

        improve: function(container, noInproveCssClass) {

            // on récupere les liens de la page (dans le div #container)
            container.each(function(item, index) {

                // on teste si le domaine du site se trouve pas dans le href, et que l'adresse commence bien par http:// ou https://
                if (!item.href.contains(cap_siteURL) && (item.href.match("^http://") || item.href.match("^https://"))) {

                    // si le lien n'a pas de propriété 'target', on en ajoute un (les liens s'ouvriront dans une nouvelle fenêtre)
                    if (!item.target) {
                        item.setProperty('target', '_blank');
                    }

                    // si le lien n'a pas de propriété 'title', on en ajoute un
                    if (!item.title && !item.hasClass('boxed')) {
                        item.setProperty('title', 'lien vers : ' + item.href);
                    }

                    // si le liens n'est pas sur une image et qu'il na pas de CssClass interdisant le traitement, on ajoute la classe CSS
                    if (!item.getElement("img") && !item.hasClass(noInproveCssClass)) {
                        // on ajoute la classe CSS
                        item.addClass('externalLink');
                        //item.adopt(item, 'top');
                    }
                }
            })
        }
    };

    cap_externalLinks.init($$('#zoneMain a'), 'noExternalLink');


    /* ARTICLES TOOLS BOX */
    // Attache les fonctionalités correspondantes à la barre d'outils dans un article (imprimer, partager...)

    var cap_attachArticlesTools = {

        print: function(el) {
            if (el) el.addEvent('click', function() { window.print() });
        },

        addThis: function(el) {
            if (el) {
                // initialisation des paramètres
                addthis_pub = 'aqem';
                addthis_language = "fr";
                ui_click: true;

                // on attache le lien + la CssClass 'noInproveCssClass' de la fonction 'cap_externalLinks'
                el.setProperty('href', 'http://www.addthis.com/bookmark.php?v=250&amp;pub=thesquarefoot');
                el.addClass('noExternalLink');

                // on charge le .js requis
                Asset.javascript('http://s7.addthis.com/js/250/addthis_widget.js?pub=thesquarefoot');

                // on attache les événements
                el.addEvents({
                    'click': function() {
                        return addthis_sendto();
                    },
                    'mouseover': function() {
                        //return addthis_open(this, '', '[URL]', '[TITLE]');
                    },
                    'mouseout': function() {
                        //addthis_close();
                    }
                });

                // on affiche l'élément
                el.setStyle('display', 'inline');

                // on affiche les séparateurs
                $$('.addthis_separator').each(function(item) {
                    item.setStyle('display', 'inline');
                });

            }
        }
    }

    cap_attachArticlesTools.print($$('.toolBox .print'))
    cap_attachArticlesTools.addThis($$('.toolBox .addthis_button_expanded'))

});


/*  GOOGLE ANALYTICS  */
// Injection du script Google Analytics
// Asynchronous Tracking 

function cap_googleAnalytics() {
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-12186819-3']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script');
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        ga.setAttribute('async', 'true');
        document.documentElement.firstChild.appendChild(ga);
    })();

};
if (cap_CMS_Environment == "production") cap_googleAnalytics();
