var news_array = new Array();
var time_news = 2000;
var news_auto_enable = 1;

function debug(data) {
    jQuery('#box-debug').append('<p>'+data+'</p>');
    if(window.console) {
        console.info(data);
    }
}

function removeAccents(s){
    s = (typeof(s) == 'string' ? s : '');
    return s.toLowerCase()
    .replace(/[àáâãäå]/g, "a")
    .replace(/æ/g, "ae")
    .replace(/ç/g, "c")
    .replace(/[èéêë]/g, "e")
    .replace(/[ìíîï]/g, "i")
    .replace(/ñ/g, "n")                         
    .replace(/[òóôõö]/g, "o")
    .replace(/œ/g, "oe")
    .replace(/[ùúûü]/g, "u")
    .replace(/[ýÿ]/g, "y")
    .replace(/\W/g, " ");
};

function tagcallback() {
    tel = jQuery('input[name=TELEPHONE]').val();
    if(tel != '') {
        if(enable_effiliation == 1) {
            img_effiliation = '<img src="http://track.effiliation.com';
            img_effiliation += '/servlet/effi.lead';
            img_effiliation += '?id=660005612&ref=CALLBACK_'+tel+'" />';
            jQuery('<div class="tag-effiliation">'+img_effiliation+'</div>').appendTo('body');
        }
        if(enable_google_analytics == 1) {
            pageTracker._trackPageview('/transformations/webcallback_submission');
        }
        if(enable_netbooster == 1) {
            var axel = Math.random()+'';
            var a = axel*10000000000000;
            img_netbooster = '<img src="http://t.bbtrack.net';
            img_netbooster += '/t.bb';
            img_netbooster += '?nt_event=co&nt_params='+tel+'&nt_entity=CF&nt_obj=C_&nt_ccy=EUR&nt_rev=0.0&timing='+a+'" />'
            jQuery('<div class="tag-netbooster">'+img_netbooster+'</div>').appendTo('body');
        }
        if(enable_zanox == 1) {
            img_zanox = '<img src="http://ad.zanox.com';
            img_zanox += '/ppl/';
            img_zanox += '?6575C587448742&mode=[[2]]&CID=[[CALL]]&OrderID=[['+tel+']]" />';
            jQuery('<div class="tag-zanox">'+img_zanox+'</div>').appendTo('body');
        }
    }
};

function news_auto() {
    if(news_auto_enable == 1) {
        current_index = jQuery('#news-list').data('current_index');
        news_array_count = jQuery(news_array).size()-1;
        if(current_index < news_array_count) {
            next = current_index + 1;
        } else {
            next = 1;
        }
        jQuery('#news-list').data('current_index', next);
        jQuery('#news-list .show').fadeOut('fast', function() {
            jQuery('#news-list .show').empty();
            jQuery('<p>'+news_array[next]+'</p>').appendTo('#news-list .show');
            jQuery('#news-list .show').fadeIn('slow', function() {
                news_timer_next = setTimeout('news_auto()', time_news);
            });
        });
    }
}

function astuces_auto(){
    jQuery('.box.infos li.active').removeClass('active').fadeOut('slow',function(){
        if (jQuery(this).next().length){
            jQuery(this).next().fadeIn().addClass('active');	
        }
        else{
            jQuery('.box.infos .first').fadeIn().addClass('active');	
        }
    });
}

jQuery(document).ready(function () {
    jQuery('a.newslink').click(function(event) {
        event.preventDefault();
        jQuery('#vignette-newsletter').click();
    });
	


    jQuery('#box-mainmenu a').mouseenter(function () {
        rel = jQuery(this).attr('rel');
        locked = jQuery(this).parent().hasClass('locked');
        jQuery(this).find('img').attr('src','/skin/frontend/custom/prixtel/images/global/buttons/home-active.png');
        if(locked == false && !jQuery('#'+rel).is(':visible')) {
            jQuery('#box-expandedmenu .box-expand').hide();
            jQuery('#box-mainmenu li').removeClass('active');
            jQuery('#box-mainmenu li').removeClass('active_serviceclient');
            parent_li = jQuery(this).parent();
            jQuery('#box-expandedmenu').slideDown(function() {
                jQuery('#'+rel).fadeIn();
                if(rel == 'expand-service-client') {
                    parent_li.addClass('active_serviceclient');
                }
                parent_li.addClass('active');
            }
            );
        }
    });
	
    jQuery('#box-mainmenu a').mouseleave(function () {
        jQuery(this).find('img').attr('src','/skin/frontend/custom/prixtel/images/global/buttons/home-inactive.png');
    });
	
    jQuery('#box-expandedmenu').mouseleave(function () {
        jQuery('#box-mainmenu li').removeClass('active');
        jQuery('#box-mainmenu li').removeClass('active_serviceclient');
        jQuery('#box-mainmenu li.locked').addClass('active');
        rel = jQuery('#box-mainmenu li.locked a').attr('rel');
        if(rel == 'expand-service-client') {
            jQuery('#box-mainmenu li.locked').addClass('active_serviceclient');
        }
        jQuery('#box-expandedmenu').fadeOut();
    });

    bk = jQuery('#links-pushcontent .active').css('background-image');
    if(bk) {
        jQuery('#links-pushcontent .active').css('background-image', bk.replace('-off', '-on'));
        jQuery('#links-pushcontent .active').find('a img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-fermer.png');
    }
    jQuery('#links-pushcontent .link-pushcontent').mouseenter(function () {
        //jQuery('#links-pushcontent .link-pushcontent').removeClass('active');
        bk = jQuery(this).css('background-image');
        jQuery(this).css('background-image', bk.replace('-off', '-on'));
        jQuery('#links-pushcontent .link-pushcontent p').hide();
        jQuery(this).find('p').show();
    //jQuery(this).addClass('active');
    });
    jQuery('#links-pushcontent .link-pushcontent').mouseleave(function () {
        jQuery('#links-pushcontent .link-pushcontent p').hide();
        href = jQuery(this).find('a').attr('href');
        if(!jQuery(href).is(':visible')) {
            bk = jQuery(this).css('background-image');
            jQuery(this).css('background-image', bk.replace('-on', '-off'));
        }
    });
    jQuery('#links-pushcontent .link-pushcontent a, .switch-pushcontent').not('.pdf_link, .other_link').click(function () {
        href = jQuery(this).attr('href');
        rel = jQuery(this).attr('rel');
        if(rel) {
            var mySplitResult = rel.split('|');
            rel = mySplitResult[0];
        }

        jQuery('#links-pushcontent .link-pushcontent').find('a img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus.png');

        jQuery('#links-pushcontent .link-pushcontent').each(function() {
            bk = jQuery(this).css('background-image');
            jQuery(this).css('background-image', bk.replace('-on', '-off'));
        });

        if(!jQuery(href).is(':visible')) {
            jQuery('#links-pushcontent .link-pushcontent').removeClass('active');
            jQuery('#links-pushcontent #link-pushcontent-'+rel).addClass('active');
            bk = jQuery('#links-pushcontent #link-pushcontent-'+rel).css('background-image');
            jQuery('#links-pushcontent #link-pushcontent-'+rel).css('background-image', bk.replace('-off', '-on'));
            jQuery('#links-pushcontent #link-pushcontent-'+rel).find('a img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-fermer.png');
            jQuery('.part-pushcontent').slideUp();
            jQuery(href).slideDown();
        } else {
            jQuery('#links-pushcontent .link-pushcontent').removeClass('active');
            jQuery(href).slideUp();
        }

        if(rel) {
            if(mySplitResult[1]) {
                link_changer(mySplitResult[1]);
            }
        }
        return false;
    });

    function link_changer(id) {
        href = jQuery('#'+id).attr('href');
        if(!jQuery(href).is(':visible')) {
            if (! jQuery('#'+id).hasClass('pdf_link')){
                jQuery('#part-pushcontent-changer .savoirplus a').find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-off.png');
                jQuery('#part-pushcontent-changer .box-changer').hide();
            }
            jQuery('#'+id).find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-on.png');
            jQuery(href).show();
        } else {
            jQuery('#'+id).find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-off.png');
            jQuery(href).hide();
        }
    }
    jQuery('#part-pushcontent-changer a').not('.pdf_link, .other_link').click(function () {
        id = jQuery(this).attr('id');
        href = jQuery(this).attr('href');
        if(id) {
            link_changer(id);
        } else {
            link_changer(href);
        }
        return false;
    });

    jQuery('#part-pushcontent-choisir a').not('.pdf_link, .other_link').click(function () {
        href = jQuery(this).attr('href');

        if(!jQuery(href).is(':visible')) {
            if (! jQuery(this).hasClass('pdf_link')){
                jQuery('#part-pushcontent-choisir .savoirplus a').find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-off.png');
                jQuery('#part-pushcontent-choisir .box-choisir').hide();
            }
            jQuery(this).find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-on.png');
            jQuery(href).show();
        } else {
            jQuery(this).find('img').attr('src', '/skin/frontend/custom/prixtel/images/global/buttons/btn-savoirplus-off.png');
            jQuery(href).hide();
        }
        return false;
    });

    u = 1;
    jQuery('#news-list .hide p').each(function(index) {
        news_array[u] = jQuery(this).html();
        u++;
    });
    jQuery('#news-list').data('current_index', 1);
    news_timer_next = setTimeout('news_auto()', time_news);
    setInterval("astuces_auto()",3000);
	
    jQuery('#box-news #news-expand p a, #news-letter .show a').click(function () {
        if(!jQuery('#box-news #news-expand .hide').is(':visible')) {
            //clearTimeout(news_timer_next);
            news_auto_enable = 0;
            jQuery('#box-news .column .show').fadeOut(function() {
                jQuery('#box-news .column .hide').fadeIn();
            });
        } else {
            jQuery('#box-news .column .hide').fadeOut(function() {
                jQuery('#box-news .column .show').fadeIn();
            });
            news_auto_enable = 1;
            news_timer_next = setTimeout('news_auto()', time_news);
        }
        return false;
    });

    jQuery('#box-teasing .box').mouseenter(function () {
        jQuery('#box-teasing .box').removeClass('active');
        jQuery('#box-teasing .box p').hide();
        jQuery(this).find('p').show();
        jQuery(this).addClass('active');
    });
    jQuery('#box-teasing .box').mouseleave(function () {
        jQuery('#box-teasing .box').removeClass('active');
        jQuery('#box-teasing .box p').hide();
    });

    empty_telephone = 0;
    jQuery('#box-questionform input[name=TELEPHONE]').focus(function() {
        if(empty_telephone == 0) {
            jQuery('#box-questionform input[name=TELEPHONE]').attr('value', '');
            empty_telephone = 1;
        }
    });

    jQuery('#box-questionform form').submit(function () {
        tagcallback();
        return checkwcb(this, 'champ');
    });

    jQuery('#box-selectiontelephones #selection-telephones').scrollable({
        size: 1,
        clickable: false,
        easing: "linear",
        speed: 400,
        prevPage: ".prev",
        nextPage: ".next",
        items: ".selection-telephones-display",
        item: ".selection-cell-group"
    });

    jQuery('#box-logos #selection-logos').scrollable({
        size: 1,
        clickable: false,
        easing: "linear",
        speed: 400,
        prevPage: ".prev",
        nextPage: ".next",
        items: ".selection-logos-display",
        item: ".selection-cell-group"
    });

    jQuery('.selection-cell').click(function () {
        href = jQuery(this).find('.info h3 a').attr('href');
        if(href) {
            window.location.href = href;
        }
    });

    //Ajout fonction infobule
    jQuery("#imgtip1").tooltip({
        tip : '.divtip1',
        tipClass:'divtip1'
    });
    jQuery("#imgtip2").tooltip({
        tip : '.divtip2',
        tipClass:'divtip2'
    });
    jQuery("#imgtip3").tooltip({
        tip : '.divtip3',
        tipClass:'divtip3'
    });
    jQuery("#imgtip4").tooltip({
        tip : '.divtip4',
        tipClass:'divtip4'
    });
    jQuery("#imgtip5").tooltip({
        tip : '.divtip5',
        tipClass:'divtip5'
    });
    jQuery("#imgtip6").tooltip({
        tip : '.divtip6',
        tipClass:'divtip6'
    });
    jQuery("#imgtip7").tooltip({
        tip : '.divtip7',
        tipClass:'divtip7'
    });
    
    jQuery('#links-pushcontent a, #part-pushcontent-changer a, #part-pushcontent-choisir a, #news-letter a').click(function(){
        _gaq.push(['_trackEvent', 'encart-presentation-prixtel', 'click', '/' + jQuery(this).attr('href').replace('#','') ]);
    });
    
    jQuery('#selection-plus').click(function(){
        openPopupDesimlockage();   
        return false;
    });
    
    jQuery('a.open-cetel').click(function(){
        if(jQuery('#bandeau-cetel-ouvert').is(':hidden')) {
            jQuery('#bandeau-cetel-ouvert').slideDown();
            jQuery('a.open-cetel img').attr('src', '/skin/frontend/custom/prixtel/images/pages/telephones/refermer.png');
        } else {
            jQuery('#bandeau-cetel-ouvert').hide();
            jQuery('a.open-cetel img').attr('src', '/skin/frontend/custom/prixtel/images/pages/telephones/en-savoir-plus.png');
        };        
    });
    
    jQuery('.btn-info-bulle').hover(function(){
        jQuery(this).siblings('.info-bulle').fadeIn('fast');
    },function(){
        jQuery(this).siblings('.info-bulle').fadeOut('fast');
    });
});

function openPopupDesimlockage() {

    jQuery('#popup-deblocage').bPopup({
        content:'iframe', 
        contentContainer:'#popup-deblocage-content',
        loadUrl:'http://www.deblocage-facile.com/order-iframe.php?affid=55',
        follow: [true,true,true]
    });  
}

