$(document).ready(function() {
    /****************************************************************/
    // ===== Pour le menu
    /****************************************************************/

    /** enleve les border des premiers et derniers items du menu **/
    $("ul#menu > li:first-child").addClass('firstNone');
    $("ul#menu > li:last-child").addClass('lastNone');
    /** gere les etats hover avec bordures arrondies **/
    $("ul#menu > li:first-child").hover(function(){$(this).addClass('first');},function(){$(this).removeClass('first');});
    $("ul#menu > li:last-child").hover(function(){$(this).addClass('last');},function(){$(this).removeClass('last');});
    /** gere les etats actifs avec bordures arrondies **/
    /*$("ul#menu > li:first-child").click(function(){$(this).addClass('firstActive');});
    $("ul#menu > li:last-child").click(function(){$(this).addClass('lastActive');});*/
    $("ul#menu > li.currentpage:first-child").addClass('firstActive');
    $("ul#menu > li.currentpage:last-child").addClass('lastActive');

    /** calcul des padding des items pour alignement sur la totalité de la largeur du menu **/
    var totalLiWidth = 0;
    var totalPadding = 0;
    var totalUlWidth = 960;
    var ff = $.browser.mozilla ? true : false;
    var is_MAC = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
    $("ul#menu > li").each(function(){totalLiWidth += $(this).width();});
    totalPadding = (totalUlWidth - totalLiWidth)/7;
    if(totalPadding < 2){
        var paddingLeft = 27 + Math.round(totalPadding);
        $("ul#menu > li > a >span").each(function(){$(this).css({'padding-left':paddingLeft});});
        /** pour bug de ff sous mac **/
        if(is_MAC == true && ff == true){
            $("ul#menu > li.lastNone > a > span").css({paddingRight:'27px'});
        }else{
            $("ul#menu > li.lastNone > a > span").css({paddingRight:'28px'});
        }
    }else{
        totalPadding = Math.round(totalPadding);
        var padding = 27 + (totalPadding/2);
        $("ul#menu li a span").each(function(){$(this).css({paddingRight:parseInt(padding)+'px',paddingLeft:parseInt(padding)+'px'});});
        totalLiWidth = 0;
        $("ul#menu > li").each(function(){totalLiWidth += $(this).width();});
        totalPadding = 0;
        totalPadding = parseInt((totalUlWidth - totalLiWidth)/2);
        /** pour bug de ff sous mac **/
        if(is_MAC == true && ff == true){
            $("ul#menu > li.lastNone > a > span").css({paddingRight:parseInt(padding)+totalPadding-1,paddingLeft:parseInt(padding)+totalPadding-1});
        }else{
            $("ul#menu > li.lastNone > a > span").css({paddingRight:parseInt(padding)+totalPadding,paddingLeft:parseInt(padding)+totalPadding});
        }
    }

    /****************************************************************/
    // ===== Pour le sous-menu des nouvelles
    /****************************************************************/

    /** enleve les border des premiers et derniers items du menu **/
    $("ul#ongNav > li:first-child").addClass('firstSmenu');
    $("ul#ongNav > li:last-child").addClass('lastSmenu');
    /** ajoute la fleche **/
    $("ul#ongNav > li > span > a").hover(function(){$(this).parent().parent().addClass('flecheBas');$(this).parent().addClass('roundLhover');},function(){$(this).parent().parent().removeClass('flecheBas');$(this).parent().removeClass('roundLhover');});

    /****************************************************************/
    // ===== Pour la navigation par onglets
    /****************************************************************/
    $('#contentSlide').tabs();


    /****************************************************************/
    // ===== Pour les slides
    /****************************************************************/
    var currentPosition = 0;
    var currentPosition1 = 0;
    var currentPosition2 = 0;
    var currentPosition3 = 0;
    var currentPosition4 = 0;
    var currentPosition5 = 0;
    var slideWidth = 538;
      /** Les slides **/
      var slides = $('.slide_show');
      var slides2 = $('div#news_1 div.contentTextSlide');
      var slides3 = $('div#news_2 div.contentTextSlide');
      var slides4 = $('div#news_3 div.contentTextSlide');
      var slides5 = $('div#news_4 div.contentTextSlide');
      var slides6 = $('div#news_5 div.contentTextSlide');

      /** Width pour le wrapper en fonction des slides **/
      var numberOfSlides = slides.length;
      var numberOfSlides2 = slides2.length;
      var numberOfSlides3 = slides3.length;
      var numberOfSlides4 = slides4.length;
      var numberOfSlides5 = slides5.length;
      var numberOfSlides6 = slides6.length;

      // Remove scrollbar in JS
      $('.slidesContainer').css('overflow','hidden');
      $('.slidesContainerTop').css('overflow','hidden');

      // Wrap all .slides with #slideInner div
      // Float left to display horizontally, readjust .slides width
      slides.wrapAll('<div id="slideInner"></div>').css({'float' : 'left','width' : slideWidth});
      slides2.wrapAll('<div id="slideInner1"></div>').css({'float' : 'left','width' : slideWidth});
      slides3.wrapAll('<div id="slideInner2"></div>').css({'float' : 'left','width' : slideWidth});
      slides4.wrapAll('<div id="slideInner3"></div>').css({'float' : 'left','width' : slideWidth});
      slides5.wrapAll('<div id="slideInner4"></div>').css({'float' : 'left','width' : slideWidth});
      slides6.wrapAll('<div id="slideInner5"></div>').css({'float' : 'left','width' : slideWidth});

      // Set #slideInner width equal to total width of all slides
      $('#slideInner').css('width', slideWidth * numberOfSlides);
      $('#slideInner1').css('width', slideWidth * numberOfSlides2);
      $('#slideInner2').css('width', slideWidth * numberOfSlides3);
      $('#slideInner3').css('width', slideWidth * numberOfSlides4);
      $('#slideInner4').css('width', slideWidth * numberOfSlides5);
      $('#slideInner5').css('width', slideWidth * numberOfSlides6);

     /** controls and slide bas **/
      manageControls(currentPosition); // Hide left arrow control on first load
      $('.control').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1; // Determine new position
        manageControls(currentPosition); // Hide / show controls
        $('#slideInner').animate({'marginLeft' : slideWidth*(-currentPosition)});  // Move slideInner using margin-left
      });

      function manageControls(position){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() } // Hide left arrow if position is first slide
        if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }  // Hide right arrow if position is last slide
      }

      /** controls and slide 1 **/
      manageControls1(currentPosition1); // Hide left arrow control on first load
      $('.control_1').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition1 = ($(this).attr('id')=='rightControl_news_1') ? currentPosition1+1 : currentPosition1-1; // Determine new position
        manageControls1(currentPosition1); // Hide / show controls
        $('#slideInner1').animate({'marginLeft' : slideWidth*(-currentPosition1)});  // Move slideInner using margin-left
      });

      function manageControls1(position1){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position1==0){ $('#leftControl_news_1').hide() } else{ $('#leftControl_news_1').show() } // Hide left arrow if position is first slide
        if(position1==numberOfSlides2-1){ $('#rightControl_news_1').hide() } else{ $('#rightControl_news_1').show() }  // Hide right arrow if position is last slide
      }

      /** controls and slide 2 **/
      manageControls2(currentPosition2); // Hide left arrow control on first load
      $('.control_2').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition2 = ($(this).attr('id')=='rightControl_news_2') ? currentPosition2+1 : currentPosition2-1; // Determine new position
        manageControls2(currentPosition2); // Hide / show controls
        $('#slideInner2').animate({'marginLeft' : slideWidth*(-currentPosition2)});  // Move slideInner using margin-left
      });

      function manageControls2(position2){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position2==0){ $('#leftControl_news_2').hide() } else{ $('#leftControl_news_2').show() } // Hide left arrow if position is first slide
        if(position2==numberOfSlides3-1){ $('#rightControl_news_2').hide() } else{ $('#rightControl_news_2').show() }  // Hide right arrow if position is last slide
      }

      /** controls and slide 3 **/
      manageControls3(currentPosition3); // Hide left arrow control on first load
      $('.control_3').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition3 = ($(this).attr('id')=='rightControl_news_3') ? currentPosition3+1 : currentPosition3-1; // Determine new position
        manageControls3(currentPosition3); // Hide / show controls
        $('#slideInner3').animate({'marginLeft' : slideWidth*(-currentPosition3)});  // Move slideInner using margin-left
      });

      function manageControls3(position3){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position3==0){ $('#leftControl_news_3').hide() } else{ $('#leftControl_news_3').show() } // Hide left arrow if position is first slide
        if(position3==numberOfSlides4-1){ $('#rightControl_news_3').hide() } else{ $('#rightControl_news_3').show() }  // Hide right arrow if position is last slide
      }

      /** controls and slide 4 **/
      manageControls4(currentPosition4); // Hide left arrow control on first load
      $('.control_4').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition4 = ($(this).attr('id')=='rightControl_news_4') ? currentPosition4+1 : currentPosition4-1; // Determine new position
        manageControls4(currentPosition4); // Hide / show controls
        $('#slideInner4').animate({'marginLeft' : slideWidth*(-currentPosition4)});  // Move slideInner using margin-left
      });

      function manageControls4(position4){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position4==0){ $('#leftControl_news_4').hide() } else{ $('#leftControl_news_4').show() } // Hide left arrow if position is first slide
        if(position4==numberOfSlides5-1){ $('#rightControl_news_4').hide() } else{ $('#rightControl_news_4').show() }  // Hide right arrow if position is last slide
      }

      /** controls and slide 5 **/
      manageControls5(currentPosition5); // Hide left arrow control on first load
      $('.control_5').bind('click', function(){ // Create event listeners for .controls clicks
        currentPosition5 = ($(this).attr('id')=='rightControl_news_5') ? currentPosition5+1 : currentPosition5-1; // Determine new position
        manageControls5(currentPosition5); // Hide / show controls
        $('#slideInner5').animate({'marginLeft' : slideWidth*(-currentPosition5)});  // Move slideInner using margin-left
      });

      function manageControls5(position5){ // manageControls: Hides and Shows controls depending on currentPosition
        if(position5==0){ $('#leftControl_news_5').hide() } else{ $('#leftControl_news_5').show() } // Hide left arrow if position is first slide
        if(position5==numberOfSlides6-1){ $('#rightControl_news_5').hide() } else{ $('#rightControl_news_5').show() }  // Hide right arrow if position is last slide
      }

    /****************************************************************/
    // ===== Pour le formulaire de newsletter
    /****************************************************************/
    /** Control du mode de transmission, de la langue et de la publication **/

    if($('div.newsletter').length != '0'){

        $('#cntnt01moduleform_2').submit(function(){
            var error='';           
		if (currLang == 'en') {
		    if($('#cntnt01_72:checked').size()==false && $('#cntnt01_73:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of transmission\n';}else{error +='- Veuillez faire au moins un choix de transmission\n';}}
		    if($('#cntnt01_75:checked').size()==false && $('#cntnt01_76:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of language\n';}else{error +='- Veuillez faire au moins un choix de langue\n';}}
		    if($('#cntnt01_78:checked').size()==false && $('#cntnt01_79:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of publication\n';}else{error +='- Veuillez faire au moins un choix de publication\n';}}
		} else {
		    if($('#cntnt01_31:checked').size()==false && $('#cntnt01_32:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of transmission\n';}else{error +='- Veuillez faire au moins un choix de transmission\n';}}
		    if($('#cntnt01_34:checked').size()==false && $('#cntnt01_35:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of language\n';}else{error +='- Veuillez faire au moins un choix de langue\n';}}
		    if($('#cntnt01_37:checked').size()==false && $('#cntnt01_38:checked').size()==false){if(currLang == 'fr'){error +='- Please write at least one choice of publication\n';}else{error +='- Veuillez faire au moins un choix de publication\n';}}
		}
            if(error != ''){alert('Attention\n\n'+error);return false;}
        });
    } 
    /****************************************************************/
    // ===== Pour bug des categories dans les petites annonces
    /****************************************************************/
    var currLang = $('li#lang a').attr('lang');
    if(currLang == 'en'){
        /*$('div#introInterne ul.menuInterneCat li:nth-child(3)').css('display','none');
        $('div#introInterne ul.menuInterneCat li:nth-child(4)').css('display','none');

        $('h3.interneFirst ul.annoncesMenu li:nth-child(2)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(2) a').attr('name','');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(3)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(3) a').attr('name','');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(4)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(4) a').attr('name','');

        $('h4 ul.annoncesMenu li:nth-child(1)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(1) a').attr('name','');
        $('h4 ul.annoncesMenu li:nth-child(3)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(3) a').attr('name','');
        $('h4 ul.annoncesMenu li:nth-child(4)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(4) a').attr('name','');*/
    }else{
        /*$('div#introInterne ul.menuInterneCat li:nth-child(1)').css('display','none');
        $('div#introInterne ul.menuInterneCat li:nth-child(2)').css('display','none');

        $('h3.interneFirst ul.annoncesMenu li:nth-child(1)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(1) a').attr('name','');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(2)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(2) a').attr('name','');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(4)').css('display','none');
        $('h3.interneFirst ul.annoncesMenu li:nth-child(4) a').attr('name','');

        $('h4 ul.annoncesMenu li:nth-child(1)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(1) a').attr('name','');
        $('h4 ul.annoncesMenu li:nth-child(2)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(2) a').attr('name','');
        $('h4 ul.annoncesMenu li:nth-child(3)').css('display','none');
        $('h4 ul.annoncesMenu li:nth-child(3) a').attr('name','');*/
    }


    /****************************************************************/
    // ===== Pour le formulaire de publication de petites annonces
    /****************************************************************/
    $('input#check_m66aa3cgblog_content').hide();//input wysiwyg
    if($.browser.msie != true){$('form#m66aa3moduleform_2 label:last').hide();} //label wysiwyg sous ff
    /** Pour la version anglaise **/
    if(currLang == 'fr'){
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_1').prev().text('Practice/company:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_2').prev().text('Last name:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_3').prev().text('First name:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_5').prev().text('Office address:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_6').prev().text('City:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_8').prev().text('Postal code:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_9').prev().text('Office phone number:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_10').prev().text('Office fax number:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_11').prev().text('E-mail:');
        $('div.annoncesSubmit input#m66aa3cgblog_customfield_12').prev().text("What is your mother's first name ?");
        $('div.annoncesSubmit input:checkbox[name=m66aa3cgblog_customfield_13]').prev().text("I want to post my ad in French:");
        $('div.annoncesSubmit input:checkbox[name=m66aa3cgblog_customfield_14]').prev().text("I want to post my ad in English:");
    }else{
        
        
    }
    /** si premier élément de liste de seconde partie vide**/
    if($('div#radio div.contentText:first-child ul li').length == '0'){
        $('div#radio div.contentText:first-child').hide();
    }


    /****************************************************************/
    // ===== Pour les liens mailto des evenements
    /****************************************************************/
   /** page accueil **/
   $('div#evenements ul li div.art a').attr('target', function(){
        if($(this).attr('target') == '_blank'){$(this).addClass('target');}
    });

   /** page interne **/
   $('div#descInterne ul.evenementsInterne li div.art a').attr('target', function(){
        if($(this).attr('target') == '_blank'){$(this).addClass('target');}
    });
   $('div#descInterne_2 ul.evenementsInterne li div.art a').attr('target', function(){
        if($(this).attr('target') == '_blank'){$(this).addClass('target');}
    });

    /** cacher les 3 premiers item de la seconde liste des evenements (bug filtre) **/
	if($('a.prevEvent').length == '0'){
		$('div#descInterne_2 ul.evenementsInterne li:nth-child(1)').hide();
		$('div#descInterne_2 ul.evenementsInterne li:nth-child(2)').hide();
		$('div#descInterne_2 ul.evenementsInterne li:nth-child(3)').hide();
		$('div#descInterne_2 ul.evenementsInterne li:nth-child(4)').css('border-top','none');
	}
    

    /****************************************************************/
    // ===== Pour les nouvelles de Radio Canada
    /****************************************************************/
    /** fix bug filtre 2e liste **/
    $('div#radio ul.radioCanadaInterne li:nth-child(1)').hide();
    $('div#radio ul.radioCanadaInterne li:nth-child(2)').hide();
    $('div#radio ul.radioCanadaInterne li:nth-child(3)').css('border-top','none');

    /****************************************************************/
    // ===== Pour le liens du bottin des produits
    /****************************************************************/
    if($('input#page').val()== 'products'){
        if($('div#header ul#perm li#lang a').text()=='English'){
            $('div#header ul#perm li#lang a').attr('href','http://studioblakkat.com/infoclip/product/en');
        }else{
            $('div#header ul#perm li#lang a').attr('href','http://studioblakkat.com/infoclip/product/fr');
        }        
    }

    /****************************************************************/
    // ===== Pour le fil d'ariane dans les pages d'article
    /****************************************************************/
    if($('div#fil_ariane span.articleTitle').length != '0'){$('div#fil_ariane span.lastitem').hide();}


    /****************************************************************/
    // ===== Bug CSS sous ie7
    /****************************************************************/
    $('div#container div#content div#contentLeft div#descInterne div.contentText ul li:first-child').css({'border-top':'none','padding':'0px','margin':'0px'});
    $('div#container div#content div#contentLeft div#radio div.contentText ul li:first-child').css({'border-top':'none','padding':'0px','margin':'0px'});
    $('div#container div#content div#contentLeft div#annonces div.contentText ul li:first-child').css({'border-top':'none','padding':'0px','margin':'0px'});
});
