/**
 * @author dla
 */

/**
 * check radio button when list-menu is selected on admin/node/content page
 * pretty usability
 */
Drupal.checkRadioButton = function () {
    $('#node-admin-filter select').click(function() {
      var reg = new RegExp("(edit-)", "g");
      var id = $(this).attr('id')
    id = id.replace(reg,'');
    $('#node-admin-filter :radio').each(function(){
      $(this).removeAttr('checked');
      if ($(this).val() == id) $(this).attr('checked', 'checked');
    });
    });
};

/**
 * Set a link between radio selector and fieldsets
 *
 * @param radiosetName the name of the radio set (without the [key] tag)
 * @param options Object defining links between radio values and fieldsetnames {"radio_value_0":"fieldset_name_0","radio_value_1":"fieldset_name_1",... }
 * @param defaultValue the value of the radio to be selected by default (set to null if not needed)
 */
Drupal.linkFieldsetWithRadioButton = function( radiosetName, options, defaultValue){
  //Adds the event hook
  $('.form-radio[name="'+radiosetName+'[key]"]').click(function(){
    var actual = $(this).val();
    $.each(options,function(i,val){
      if(i==actual){
        Drupal.openFieldset($('.'+val));
      }else{
        Drupal.closeFieldset($('.'+val));
      }
    });
  });
  //Initial view
  //If there is an element checked
  if($('.form-radio[name="'+radiosetName+'[key]"]:checked').length){
	  $('.form-radio[name="'+radiosetName+'[key]"]').each(function(){
	    var actual = $(this).val();
	    var checked = this.checked;
	    $.each(options,function(i,val){
			$('.'+val).removeClass('collapsible');
			if(i==actual){
				if(checked){
		        	Drupal.openFieldset($('.'+val));
		      	}else{
		        	Drupal.closeFieldset($('.'+val));
		        }
			}
	    });
	  });
  }else{	//No element is checked
  	$('.form-radio[name="'+radiosetName+'[key]"]').each(function(){
	    var actual = $(this).val();
	    this.checked = defaultValue==actual;
	    var checked = this.checked;
	    $.each(options,function(i,val){
			$('.'+val).removeClass('collapsible');
			if(i==actual){
				if(checked){
		        	Drupal.openFieldset($('.'+val));
		      	}else{
		        	Drupal.closeFieldset($('.'+val));
		        }
			}
	    });
	  });
  }
}

/**
 * Closes a fieldset
 *
 * @param fieldset dom object or jQuery object of the fieldset
 */
Drupal.closeFieldset = function(fieldset) {
  if (!$(fieldset).is('.collapsed')) {
    var content = $('> div', fieldset).slideUp('medium', function() {
      $(this.parentNode).addClass('collapsed');
      this.parentNode.animating = false;
    });
  }
}

/**
 * Opens a fieldset
 *
 * @param fieldset dom object or jQuery object of the fieldset
 */
Drupal.openFieldset = function(fieldset) {
  if ($(fieldset).is('.collapsed')) {
    var content = $('> div', fieldset).hide();
    $(fieldset).removeClass('collapsed');
    content.slideDown({
      duration: 300,
      complete: function() {
        // Make sure we open to height auto
        $(this).css('height', 'auto');
        Drupal.collapseScrollIntoView(this.parentNode);
        this.parentNode.animating = false;
      },
      step: function() {
         // Scroll the fieldset into view
        Drupal.collapseScrollIntoView(this.parentNode);
      }
    });
    if (typeof Drupal.textareaAttach != 'undefined') {
      // Initialize resizable textareas that are now revealed
      Drupal.textareaAttach(null, fieldset);
    }
  }
}

 /**
  * Vide le champ recherche sur un clic
  *
  * @param fieldset dom object or jQuery object of the fieldset
  */
 Drupal.resetInputSearch = function() {
	 $('#edit-search-theme-form-keys').focus(function(e) {
		 $('#edit-search-theme-form-keys').attr('value', '');
	 });
 }
 
$(document).ready(function() {
  Drupal.checkRadioButton();
  Drupal.resetInputSearch();
  Drupal.linkFieldsetWithRadioButton('field_home_news',
    {
      0:'group-rubrique-de-news',
      1:'group-home-news'
    },
    0
  );
});

