$(document).ready(function() {

    var liste_tracking_ga_ok = [];
    //Initialisation Google Analytics
    var _gaq = _gaq || [];

    Qtab.init();
    $('.tab-calendar, .tab-secteur, .tab-connaitre').click(function() {
        Qtab.show($(this).attr('class').replace('tab-', ''));
        return false;
    });

    $('#header_left-partenaire').css({
        'margin-top' : (($('#header_right-partenaire').height() - $('#header_left-partenaire img').height()) / 2)+'px'
    });

    $('.affiche-partenaires').each(function() {
        if($(this).find('div').html() == '') {
            $(this).hide();
        }
    });

    // lancement  du calendrier
    chargeCalendrier('', '');


    /* CARTOUCHE PRODUIT */
   //$('.horizontal_scroller').SetScroller();

    $('div#zone-centrale #content #content-date #date').innerfade({
        animationtype: 'slide',
        speed: 750,
        timeout: 4000
    });

    jQuery("#input-text-search").autocomplete(BASE_URL+"ajax/conference/recherche_autocompletion.php", {
        width: 650,
        extraParams: {type_recherche: 'full_text'},
        selectFirst: false,
        max: 50,
        cacheLength: 1,
        minChars: 2
    }).result(function(event, item) {
        jQuery('#input-text-search').val(item[0]);
        document.location.href = item[1];
    });

    /* --- DEROULEMENT DU MENU AVEC FLECHE ROUGE AVEC EFFET JQUERY --- */

    $('#barre-rouge .header-arrow').click(function() {
        var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
        if ($('#menu').attr('class') == 'menu-fade') {
            $('#header-arrow-bottom').css({
                'display' : 'none'
            });
            if (ie7) {
                $('.main-save-date-dedie').css({'marginTop' : '0'});
            }
            $('#header-arrow-top').css({
                'display' : 'block'
            });
            $('#menu').slideDown('800', function() {
                $('#menu').removeClass('menu-fade');
                $('#quicktab-container').css('marginTop', '44px');
            });

        } else {
            $('#quicktab-container').css('marginTop', '0px');
            $('#header-arrow-top').css({
                'display' : 'none'
            });
            if (ie7) {
                $('.main-save-date-dedie').css({'marginTop' : '10px'});
            }
            $('.qtab').slideUp('800', function () {
                $('#menu').slideUp('800', function() {
                    $('#menu').addClass('menu-fade');
                    $('#header-arrow-bottom').css({
                        'display' : 'block'
                    });
                });
            });
            $('#button-connaitre').replaceWith('<img id="button-connaitre" src="static/images/ec/commun-gabarit/header_menu_connaitre.gif" alt="Nous connaître" border="0" />');
            $('#button-secteur').replaceWith('<img id="button-secteur" src="static/images/ec/commun-gabarit/header_menu_secteurs.gif" alt="Nos secteurs" border="0" />');
        }
    });
    /*
     *
     * diaporama actualite home page
     */

    $('#diapo-actu-home').cycle({
        fx:    'fade',
        timeout: 0,
        next : '.navigation-slider #next',
        prev : '.navigation-slider #prev'
    });

    $('ul.slide-actu').cycle({
        fx:    'fade',
        timeout: 6000,
        next : 'img#next_slide_actu',
        prev : 'img#prev_slide_actu'
    });
    $('ul#slider-partenaire').cycle({
        fx:    'fade',
        timeout: 6000,
        next : '',
        prev : ''
    });


    slider_auto = true;

    $(document).ready(function() {
        $('#diapo-actu-home').each(function() {
            setInterval(function() {
                if (slider_auto) {
                    $('#diapo-actu-home').cycle('next');
                    changebanniereDiaporama('next');
                }
            }, 10000);
        });
    });

    $('.navigation-slider .fleches img').click(function() {
        slider_auto = false;
    });
    $('#diapo-actu-home #content').mouseover(function() {
        slider_auto = false;
    });
    $('#diapo-actu-home #content').mouseout(function() {
        slider_auto = true;
    });


/* ********************************************** */

    /* --- HEADER DEDIE AVEC LOGOS ET BANDE EXTENSIBLE --- */

    $('#zone-header-dedie #vous-invitent').each(function() {
        var position = $(this).position();
        var reste = parseInt($('#header').width()) - position.left - parseInt($(this).width()) - 150;
        $('#barre-rouge-dedie').css({
            'width' : reste +'px'
        });
    });
    /* --- HEADER DEDIE AVEC LOGOS ET BANDE EXTENSIBLE --- */


    $('.produit-infos').mouseover(function() {
        var prod_id = $(this).attr('prod_id');
        $('#apercu_produit_'+prod_id).show();
    }).mouseout(function() {
        var prod_id = $(this).attr('prod_id');
        $('#apercu_produit_'+prod_id).hide();
    });

    // partie programme save the date
    var compteur = 1;
    $('.drapeau-compteur').each(function() {
        $(this).html(compteur);
        compteur++;
    });

});


$(function() {
    $('a#voir_actu').fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'enableEscapeButton': true,
        'hideOnContentClick': false,
        'showNavArrows': false,
        'cyclic': false,
        'padding': 30,
        'frameWidth': 525,
        'frameHeight' : 500
    });

    $('a.voir_audio_attache').fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'enableEscapeButton': true,
        'hideOnContentClick': false,
        'showNavArrows': false,
        'cyclic': false,
        'padding': 30,
        'frameWidth': 370,
        'frameHeight' : 270
    });

    $('a.afficheMedia').fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'enableEscapeButton': true,
        'hideOnContentClick': false,
        'showNavArrows': false,
        'cyclic': false,
        'padding': 30,
        'frameWidth': 520,
        'frameHeight' : 450
    });
});


// fonction changement de jour programme save the date
function changeJourProgramme(jour) {
    $('.journee').each(function() {
        $(this).css({'display' : 'none'});
    });
    $('#journee_'+jour).fadeIn();
    $('.date-programme').each(function() {
        $(this).css({'font-weight' : 'normal'});
    });
    $('#date_journee_'+jour).css({'font-weight' : 'bold'});
}


// fonction pour faire dérouler dans les secteurs
function afficheSecteur(monTd, cat_id) {
     var monTr = $(monTd).parent();
     var position = $(monTd).parent().index();
     if ($(monTd).attr('class') == 'tab-sous-secteur-ouvert') {
         $('.tab-secteur-ouvert').remove();
         $(monTd).removeClass();

     } else {
         $('.tab-sous-secteur-ouvert').each (function() {
             $(this).removeClass();
             $('.tab-secteur-ouvert').remove();
         });
         $.ajax({
             type: "POST",
             dataType : 'json',
             async : 'true',
             url: BASE_URL+'ajax/conference/changeTabSecteur.php',
             data: "todo=changeSecteur&id="+cat_id,
             success: function(data){
                 if (data) {
                         $(monTr).after(data['liste_conference']);
                         $('.tab-secteur-ouvert').fadeIn('150');
                 }
                 $(monTd).addClass('tab-sous-secteur-ouvert');
             }
         });
     }
}


function changeCssPrint() {
    var str_print ='';
    if ($('#fancy_outer').css('display') == 'block') {
        $('#head_css_print').attr('href', BASE_URL+'static/css/ec/print-intervenant.css');
//        str_print = '@import url("'+BASE_URL+'static/css/ec/print-intervenant.css");';
    } else {
        $('#head_css_print').attr('href', BASE_URL+'static/css/ec/print-normal.css');
//        str_print = '@import url("'+BASE_URL+'static/css/ec/print-normal.css");';
    }
//    $('#head_css_print').html(str_print);
    window.print();
}


// bouton Addthis

var addthis_config = {
     services_compact: 'email, facebook, twitter, viadeo, linkedin, mailto, more',
     ui_language: "fr",
     ui_click: true
}



function chargeCalendrier(annee) {
    if (annee == '') {
        annee = new Date();
        annee = annee.getFullYear();
    }
    $('#spinner').show();
    jQuery.ajax({
        type: "POST",
        dataType : 'html',
        url: BASE_URL+"ajax/conference/afficheCalendrier.php",
        data: 'todo=calendar&annee='+annee,
        success: function(msg) {
            if (msg) {
                $('#spinner').hide();
                $('#quicktab-calendar .zone #content-calendar').html(msg);
            }
        }
    });
}


/**
 * Quick tabs
 */

var Qtab = {
    id: 'quicktab-container',
    prefix: 'quicktab-',
    $ctn: null,
    duration: 400,

    init: function() {
        // Nothing to do here
        this.$ctn = $('#'+this.id);
    },

    show: function(id) {
        $id = $('#'+ this.prefix + id);

        // Clic sur l'element affiche actuel : on le cache
        if (this.$ctn.find('#'+ this.prefix + id + ':visible').length) {
            this.$ctn.find('.qtab:visible').slideUp(this.duration);
            $('#tabs .on').removeClass('on');
            if(id!="calendar") {
                if (id=="secteur"){
                    $('#button-secteur').replaceWith('<img id="button-secteur" src="static/images/ec/commun-gabarit/header_menu_secteurs.gif" alt="Nos secteurs" border="0" />');
                }
                if (id=="connaitre"){
                    $('#button-connaitre').replaceWith('<img id="button-connaitre" src="static/images/ec/commun-gabarit/header_menu_connaitre.gif" alt="Nous connaître" border="0" />');
                }
            } else {
                $('#button-calendar').replaceWith('<img id="button-calendar" src="static/images/ec/commun-gabarit/fond-calendrier.gif" alt="Calendrier" border="0" />');
            }
            $('.zone').css({'width'  : '976px'});
        } else {
            // Element actuellement affiche
            this.$ctn.find('.qtab:visible').not('#'+ this.prefix + id).slideUp(this.duration);
            $('#tabs .on').removeClass('on');
            // Affichage de l'element demande
            if ($id.length) {
                $id.slideDown(this.duration);
                $('#tab-'+id).addClass('on');

                trackingGoogleAnalytics(window.location.href, 'menu', id);

            }
            if(id!="calendar") {
                if (id=="secteur"){
                    $('#button-secteur').replaceWith('<img id="button-secteur" src="static/images/ec/commun-gabarit/header_menu_secteurs_on.gif" alt="Nos secteurs" border="0" />');
                    $('#button-connaitre').replaceWith('<img id="button-connaitre" src="static/images/ec/commun-gabarit/header_menu_connaitre.gif" alt="Nous connaître" border="0" />');
                    $('#button-calendar').replaceWith('<img id="button-calendar" src="static/images/ec/commun-gabarit/fond-calendrier.gif" alt="Calendrier" border="0" />');
                }
                if (id=="connaitre"){
                    $('#button-connaitre').replaceWith('<img id="button-connaitre" src="static/images/ec/commun-gabarit/header_menu_connaitre_on.gif" alt="Nous connaître" border="0" />');
                    $('#button-secteur').replaceWith('<img id="button-secteur" src="static/images/ec/commun-gabarit/header_menu_secteurs.gif" alt="Nos secteurs" border="0" />');
                    $('#button-calendar').replaceWith('<img id="button-calendar" src="static/images/ec/commun-gabarit/fond-calendrier.gif" alt="Calendrier" border="0" />');
                }
                $('.zone').css({'width'  : '976px'});
            } else {
                $('#button-connaitre').replaceWith('<img id="button-connaitre" src="static/images/ec/commun-gabarit/header_menu_connaitre.gif" alt="Nous connaître" border="0" />');
                $('#button-secteur').replaceWith('<img id="button-secteur" src="static/images/ec/commun-gabarit/header_menu_secteurs.gif" alt="Nos secteurs" border="0" />');
                $('#button-calendar').replaceWith('<img id="button-calendar" src="static/images/ec/commun-gabarit/fond-calendrier_on.gif" alt="Calendrier" border="0" />');
                $('.zone').css({'width'  : 'auto'});
            }

        }
        return false;
    }
};


function in_array(p_val, tab) {
    for (var i = 0; i < tab.length; i++) {
        if (tab[i] == p_val) {
            rowid = i;
            return true;
        }
    }
    return false;
}

/* Tracking Google Analytics lors de l'affichage d'un élément en hidden au chargement de la page (onglet, élément que l'on scroll, ...) */
function trackingGoogleAnalytics(url, categorie, action, label) {

    if (label == undefined) {
        label = action;
    }
    var liste_tracking_ga_ok = liste_tracking_ga_ok || [];
    url = url.replace(BASE_URL, '');
    url = url.replace('.html', '');
    var tracking_url = url + '/' + categorie + '-' + action + '.html'; // bizy_categorie/bizy_produit/onglet-programme.html

    if (in_array(tracking_url, liste_tracking_ga_ok)) {
        return;
    }
    liste_tracking_ga_ok.push(tracking_url);
    // Tracking de la page "virtuelle"
    _gaq.push(['_trackPageview', tracking_url]);

    // Tracking de l'évenement
    _gaq.push(['_trackEvent', categorie, action, label, '']);
}

/* Tracking Google Analytics lors du click sur un lien menant vers un fichier PDF par ex
 * dir : nom du dossier pour le tri dans analytics (ex : docs_secu)
 * name : nom "virtuel" du fichier tracké
 * */
function trackingGoogleAnalyticsFile(dir, categorie, name, label) {
    var tracking_url = '/' + dir + '/' + name;

    // Tracking de la page "virtuelle"
    _gaq.push(['_trackPageview', tracking_url]);

    // Tracking de l'évenement
    _gaq.push(['_trackEvent', categorie, dir + '/' + name, label, '']);
}



