//jQuery's noConflict mode
jQuery.noConflict();
jQuery(document).ready(function(){


  jQuery('.tx-inmarchespublics-pi1 #marcheEC').change(marcheECChange);

    //-Gestion de l'ouverture automatique des blocs marchés
    jQuery('.tx-inmarchespublics-pi1 .bloc').each(function(){
      if(!jQuery(this).hasClass("blocSubmit")){
      if(!jQuery(this).hasClass("ouvert")){
        //on met la class
        jQuery(this).addClass('ferme');
        //on cache le contenu
        jQuery(this).children('.content').slideUp(0);
        //on remplit les outils
        if(jQuery(this).hasClass('obl'))
          jQuery(this).children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheBas_Rouge.gif" alt=""/> Ouvrir le bloc</span>');
        else
          jQuery(this).children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheBas_bleu.gif" alt=""/> Ouvrir le bloc</span>');
      }else{
        //on met la class
        jQuery(this).addClass('ouvert');
        //on remplit les outils
        if(jQuery(this).hasClass('obl'))
          jQuery(this).children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheHaut_rouge.gif" alt=""/> Fermer le bloc</span>');
        else
          jQuery(this).children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheHaut_bleu.gif" alt=""/> Fermer le bloc</span>');
      }
    }
  });

    //-Gestion du click sur les ouvertures/fermetures de bloc
    jQuery('.tx-inmarchespublics-pi1 .bloc .header .tool').css('cursor','pointer');
    jQuery('.tx-inmarchespublics-pi1 .bloc .header .tool').click(function(){
       var divContainer = jQuery(this).parent().parent();

    if(divContainer.hasClass('ouvert')){
        divContainer.removeClass('ouvert');
        divContainer.addClass('ferme');
        divContainer.children('.content').slideUp();
        if(divContainer.hasClass('obl'))
          divContainer.children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheBas_rouge.gif" alt=""/> Ouvrir le bloc</span>');
        else
          divContainer.children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheBas_bleu.gif" alt=""/> Ouvrir le bloc</span>');
    }else{
        divContainer.addClass('ouvert');
        divContainer.removeClass('ferme');
        divContainer.children('.content').slideDown();
        if(divContainer.hasClass('obl'))
          divContainer.children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheHaut_rouge.gif" alt=""/> Fermer le bloc</span>');
        else
          divContainer.children('.header').children('.tool').html('<span><img src="typo3conf/ext/in_marchespublics/pi1/img/flecheHaut_bleu.gif" alt=""/> Fermer le bloc</span>');
    }
  });

  //-Gestion des changements dans la saisie de marche
  jQuery('.tx-inmarchespublics-pi1 .bloc .content .needRefresh').change(refreshFields);
  jQuery('.tx-inmarchespublics-pi1 .bloc .content input.needRefresh').click(refreshFields);
  refreshFields();

  //-Gestion du click sur les boutons submit
  jQuery('.tx-inmarchespublics-pi1 #btnVerif').click(function(){
    jQuery('.tx-inmarchespublics-pi1 #modeBrouillon').val(2);
    jQuery('.tx-inmarchespublics-pi1 #formNewMarche').submit();
  })
  jQuery('.tx-inmarchespublics-pi1 #btnValid').click(function(){
    jQuery('.tx-inmarchespublics-pi1 #modeBrouillon').val(3);
    jQuery('.tx-inmarchespublics-pi1 #formNewMarche').submit();
  })

  //-Gestion des listes de brouillons
  jQuery('.tx-inmarchespublics-pi1 .showHideDocs').each(function(){
    if(jQuery(this).parent().children('.itemStatDocs').hasClass('ferme')){
      jQuery(this).parent().children('.itemStatDocs').removeClass('ferme');
      jQuery(this).parent().children('.itemStatDocs').addClass('ouvert');
      jQuery(this).parent().children('.itemStatDocs').slideDown();
    }else{
      jQuery(this).parent().children('.itemStatDocs').addClass('ferme');
      jQuery(this).parent().children('.itemStatDocs').removeClass('ouvert');
      jQuery(this).parent().children('.itemStatDocs').slideUp();
    }
  })
  jQuery('.tx-inmarchespublics-pi1 .showHideDocs').click(function(){
    if(jQuery(this).parent().children('.itemStatDocs').hasClass('ferme')){
      jQuery(this).parent().children('.itemStatDocs').removeClass('ferme');
      jQuery(this).parent().children('.itemStatDocs').addClass('ouvert');
      jQuery(this).parent().children('.itemStatDocs').slideDown();
      var str = jQuery(this).children('a').html();
      jQuery(this).children('a').html(str.replace('+','-'));
    }else{
      jQuery(this).parent().children('.itemStatDocs').addClass('ferme');
      jQuery(this).parent().children('.itemStatDocs').removeClass('ouvert');
      jQuery(this).parent().children('.itemStatDocs').slideUp();
      var str = jQuery(this).children('a').html();
      jQuery(this).children('a').html(str.replace('-','+'));
    }
  })
  jQuery('.tx-inmarchespublics-pi1 .delBtn').each(function(){
    var parent = jQuery(this).parent('a');
    parent.click(function(){
      if(!confirm('Etes-vous sûr ?'))
        return false;
    });
  });

  //-Bouton reset
  jQuery('.tx-inmarchespublics-pi1 .resetForm').click(function(){
    document.getElementById(jQuery(this).parents('form').attr('id')).reset();
    refreshFields();
  });


  //-Gestion du tout cocher/decocher
  jQuery('.tx-inmarchespublics-pi1 .checkbox').click(function(){
    var divId = jQuery(this).parents('div.item').attr('id');
    if(!jQuery(this).attr('checked'))
      jQuery('.tx-inmarchespublics-pi1 #'+divId+' .toutCocher').attr('checked',false);
  });
  jQuery('.tx-inmarchespublics-pi1 .toutCocher').click(function(){
    var divId = jQuery(this).parents('div.item').attr('id');
    var val = jQuery(this).attr('checked');
    jQuery('.tx-inmarchespublics-pi1 #'+divId+' .checkbox').each(function(){
      jQuery(this).attr('checked',val);
    });
  });
  jQuery('.tx-inmarchespublics-pi1 .type_m').change(function(){
    //on décoche tout
    for (var i=1;i<4;i++){
      jQuery('.tx-inmarchespublics-pi1 #classGroup'+i+' .checkbox').each(function(){
        jQuery(this).attr('checked',false);
      });
    }
  });

  jQuery('.tx-inmarchespublics-pi1 .groupDeCheck .checkbox').click(function(){
    descendCoche(this);
    if(jQuery(this).attr('checked'))
      remonteCoche(jQuery(this).parents('li'));
  });


});

function marcheECChange(){
  var typeM;
  if(jQuery(this).val()=='-1' || jQuery(this).val()==''){
    jQuery('.tx-inmarchespublics-pi1 #objet').val('');
    jQuery('.tx-inmarchespublics-pi1 #duree').val('');
    typeM = '-1';
  }else{
    jQuery('.tx-inmarchespublics-pi1 #objet').val(tabMarche[jQuery(this).val()]['objet']);
    jQuery('.tx-inmarchespublics-pi1 #duree').val(tabMarche[jQuery(this).val()]['duree']);
    typeM = tabMarche[jQuery(this).val()]['type_m'];
  }


  jQuery.each(jQuery('.tx-inmarchespublics-pi1 #listeTypeM input'),function(){
    if(jQuery(this).val()==typeM)
      jQuery(this).attr('checked',true);
    else
      jQuery(this).attr('checked',false);
  });
}

function descendCoche(box){
  var coche = jQuery(box).attr('checked');

  //on parcourt les enfants pour leur appliquer le même traitement
  var id =jQuery(box).parents('li').attr('id');
  jQuery('.tx-inmarchespublics-pi1 #'+id+' ul li .checkbox').attr('checked',coche);
}

function remonteCoche(box){
  var coche = jQuery(box).attr('checked');

  //on parcourt les parents pour leur appliquer le même traitement
  jQuery(box).parents('li').children('label').children('.checkbox').attr('checked',true);
  //remonte(jQuery(box).parents('li'));
}

function videClassGroup(i){
  jQuery('.tx-inmarchespublics-pi1 #classGroup'+i+' .checkbox').each(function(){
    jQuery(this).attr('checked',false);
  });
}

function refreshFields(){
  if(jQuery('.tx-inmarchespublics-pi1 #adjudicateur').val()=="4")
    jQuery('.tx-inmarchespublics-pi1 #divAdjudicateur').show();
  else
    jQuery('.tx-inmarchespublics-pi1 #divAdjudicateur').hide();

  for (var i=1;i<4;i++){
    if(jQuery('.tx-inmarchespublics-pi1 #type_m'+i).attr('checked')){
      jQuery('.tx-inmarchespublics-pi1 #classGroup'+i).show();
    }else{
      if(!(jQuery('.tx-inmarchespublics-pi1 #classGroup'+i).hasClass('alwaysShown'))){
        jQuery('.tx-inmarchespublics-pi1 #classGroup'+i).hide();
        videClassGroup(i);
      }
    }
    if(i==1){
      if(jQuery('.tx-inmarchespublics-pi1 #type_m'+i).attr('checked'))
        jQuery('.tx-inmarchespublics-pi1 #itemTypeMF').show();
      else{
        jQuery('.tx-inmarchespublics-pi1 #itemTypeMF').hide();
        jQuery('.tx-inmarchespublics-pi1 #itemTypeMF #type_mf').val(-1);
      }
    }
  }


  if(jQuery('.tx-inmarchespublics-pi1 #type_a1').attr('checked') || jQuery('.tx-inmarchespublics-pi1 #type_a').val()==1)
    jQuery('.tx-inmarchespublics-pi1 #divAccordCadre').show();
  else
    jQuery('.tx-inmarchespublics-pi1 #divAccordCadre').hide();


  if((jQuery('.tx-inmarchespublics-pi1 #variantes').attr('type')=='checkbox' && jQuery('.tx-inmarchespublics-pi1 #variantes').attr('checked')) || (jQuery('.tx-inmarchespublics-pi1 #variantes').attr('type')!='checkbox' && jQuery('.tx-inmarchespublics-pi1 #variantes').val()==1))
    jQuery('.tx-inmarchespublics-pi1 #divVariantesDescription').show();
  else
    jQuery('.tx-inmarchespublics-pi1 #divVariantesDescription').hide();

  if((jQuery('.tx-inmarchespublics-pi1 #lot_division').attr('type')=='checkbox' && jQuery('.tx-inmarchespublics-pi1 #lot_division').attr('checked')) || (jQuery('.tx-inmarchespublics-pi1 #lot_division').attr('type')!='checkbox' && jQuery('.tx-inmarchespublics-pi1 #lot_division').val()==1))
    jQuery('.tx-inmarchespublics-pi1 #divDivisionLot').show();
  else
    jQuery('.tx-inmarchespublics-pi1 #divDivisionLot').hide();


  if(parseInt(jQuery('.tx-inmarchespublics-pi1 #appreciation').val())>=2)
    jQuery('.tx-inmarchespublics-pi1 #divAppeciation').show();
  else
    jQuery('.tx-inmarchespublics-pi1 #divAppeciation').hide();


}
