<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script type="text/javascript">


// common : tab functions
function tabCookie (hash){
    $.cookie('current_tab', hash);
}
function selectPreviousTab (){
  if($.cookie('current_tab') != null ){
      var hash = jQuery.cookie('current_tab')
      //console.log(hash)
      $("ul.nav li a[href='" + hash + "']").trigger("click");
  }
}

// common : title/acronym pref_label selection
function hide_pref_label_select(){
    $('label[for=id_pref_label]').css('visibility', 'hidden')
    $('select#id_pref_label').css('visibility', 'hidden')
}

function show_pref_label_select() {
    $('label[for=id_pref_label]').css('visibility', 'visible')
    $('select#id_pref_label').css('visibility', 'visible')
}


//(function($){
//$(document).ready(function(){
jQuery(document).ready(function($) {

  $.datepicker.regional['fr'] = {
    closeText: 'Fermer',
    prevText: '&#x3c;Préc',
    nextText: 'Suiv&#x3e;',
    currentText: 'Courant',
    monthNames: ['Janvier','Février','Mars','Avril','Mai','Juin',
    'Juillet','Août','Septembre','Octobre','Novembre','Décembre'],
    monthNamesShort: ['Jan','Fév','Mar','Avr','Mai','Jun',
    'Jul','Aoû','Sep','Oct','Nov','Déc'],
    dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
    dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
    dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
    weekHeader: 'Sm',
    dateFormat: 'dd/mm/yy',
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: false,
    yearSuffix: ''};
  $.datepicker.setDefaults($.datepicker.regional['fr']);

  $.timepicker.regional['fr'] = {
    timeOnlyTitle : 'Sélectionnez l’horaire',
    timeText : 'Horaire',
    hourText : 'Heure',
    minuteText : 'Minute',
    secondText : 'Seconde',
    currentText : 'Maintenant',
    closeText : 'Terminé',
    ampm : false
  };
  $.timepicker.setDefaults($.timepicker.regional['fr']);

    // messages
    $(".alert .close").click(function(){
        $(this).parent().fadeOut('slow');
    });


  // call cookie tab
  selectPreviousTab()


    // AdminDate widgets

    $('.vDateField').datepicker({dateFormat: 'yy-mm-dd'});
    $('.vTimeField').timepicker({ timeFormat: 'hh:mm', separator:' h ',  stepMinute: 15,  hour: 9, minute: 00});
    $('span.datetimeshortcuts').siblings('br').remove();

    //  $("a[rel=popover].top-icon").twipsy({offset: 20});


    $('ul.nav li a').bind('click', function (e) {
        //console.log($(this).attr('href'))
        tabCookie($(this).attr('href'))
    });


    // resize TinyMCE Editor
    $("table#id_description_tbl.mceLayout").width('617px');

    //exchange modalités
    //$("input[id$='mod_euro']").parents("div.control-group").css("max-width","700px");
    //$("select[id^='id_exchanges'][id$='-eway']").css("margin-right","20px").parents("div.control-group").chil"label")css("display","none")


    // adding edit link for FKeys
    // adapted from http://djangosnippets.org/snippets/431/

    empty_values = ['','---------']

    $("a.add-another").each(function(){
      if($.inArray($(this).prev().val(), empty_values)=='-1' &&       // si selection pas vide
          $(this).siblings('select').attr('multiple') != 'multiple'   // et qu'on est pas sur un select multiple
          ){
          $(this).after("&nbsp;<a class='changelink' href='#' title='Modifier cet objet'><img src='/static/admin/img/icon_changelink.gif'></a>");
          $(this).next().click(function(){
              if($(this).prev().prev().attr('value')=='')return false;
              var link = ($(this).prev().attr('href')+'../'+$(this).prev().prev().attr('value'));

              //window.location.href = link
              // popup version :
              var win = window.open(link + '?_popup=1', link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
              win.focus();
              return false;
            });
          }
    });


    // specific to organization admin
    // hide select pref_label if no acronym

    if($("input#id_acronym").length > 0 && $("input#id_acronym").val().length < 1){
    hide_pref_label_select()};

    $('input#id_acronym').bind('keyup',function(){
        if($(this).val().length > 0)
            show_pref_label_select()
        else
            hide_pref_label_select()
    });

    // http://djangosnippets.org/snippets/1492
    // Only for stacked inlines

    $('div.stacked-inline').each(function() {

        fs = $(this).find('fieldset')
        h3 = $(this).find('h3:first')

        // Don't collapse if fieldset contains errors
        if (fs.find('div').hasClass('errors'))
            fs.addClass('stacked_collapse');
        else
            fs.addClass('stacked_collapse collapsed');

        // Add toggle link
        h3.prepend('<a class="stacked_collapse-toggle" href="#">(' + gettext('Show') + ')</a> ');
        h3.find('a.stacked_collapse-toggle').bind("click", function(){
            fs = $(this).parent('h3').next('fieldset');
            if (!fs.hasClass('collapsed'))
            {
                fs.addClass('collapsed');
                $(this).html('(' + gettext('Show') + ')');
            }
            else
            {
                fs.removeClass('collapsed');
                $(this).html('(' + gettext('Hide') + ')');
            }
        }).removeAttr('href').css('cursor', 'pointer');
    });


    // PAS EXECUTES ???

  // decorate inline add buttons
    $('.add-row a').addClass('btn').addClass('btn-primary');
    $('.add-row a').prepend('<i class="icon-plus icon-white"></i> ');

    $("li.dropdown.open").removeClass('open');



});


  // complément au widget groupbox de coop-tag
  // doit être dans le namespace django.jQuery car il est loadé avant le custom jquery

(function($){



  if($('ul.groupbox-list').length > 0 && typeof django.jQuery.fn.columnize === 'function'){

    // en-tetes deroulants
    $('div.groupbox-header').click(function(e){
        $(this).next("ul.groupbox-list").slideToggle();
    });
    // cases en colonnes
    $('.groupbox ul').columnize({columns:3}).hide();
    // clic = mise à jour compteur en en-tete
    $('.groupbox-list input[type=checkbox]').bind('change',function(e){
        var parent_list = $(this).parents('ul');
        //console.log(parent_list);
        updateCounter(parent_list);
    });
    // si un secteur coché au démarrage, afficher la rubrique
    $('.groupbox-list').each(function(){
        var init = updateCounter($(this));
        if(init){
            $(this).show();
        }
    });
  }

})((typeof window.jQuery == 'undefined' && typeof window.django != 'undefined') ? django.jQuery : jQuery);



</script>