// $Id: views_savedsearches.js,v 1.1.2.3 2007/12/31 15:02:02 wimleers Exp $

var ViewsSavedSearches = ViewsSavedSearches || {};

ViewsSavedSearches.baseContext = function() {
  var $baseContext;
  
  if (undefined === $baseContext) {
    $baseContext = $("div.views-savedsearches-container");
  }
  return $baseContext;
};

ViewsSavedSearches.attachBindings = function(firstTime) {
  var viewName;
  var i;

  for (i = 0; i < Drupal.settings.views_savedsearches.view_names.length; i++) {
    viewName = Drupal.settings.views_savedsearches.view_names[i];

    if (firstTime) {
      // Move the Views Saved Searches container below the exposed filters.
      $('div#view-'+ viewName +'-views-savedsearches-container', ViewsSavedSearches.baseContext)
      .remove()
      .insertAfter('div.view-'+ viewName +' form');     
      
      ViewsSavedSearches.showHideList(viewName);
    }

    // Attach the bindings.
    ViewsSavedSearches.bindings(viewName);
  }
};

ViewsSavedSearches.bindings = function(view_name) {
  function ahahDeleteBeforeSubmit(formData, jqForm, options) {
    // Validate checkboxes: at least one must be checked.
    if ($('div#view-'+ view_name +'-views-savedsearches-container div.views-savedsearches-list-ahah form:first :checkbox[@checked]').length == 0) {
      alert("You must select at least one saved search to be deleted!");
      return false;
    }

    // The form is validated, it will be submitted. Now let's add the form
    // data of the views filters form, to be able to check if we should
    // display the save form.
    formData.push({ name: 'views_filters_form', value: $('form#views-filters').formSerialize() });

    return true;
  }

  function ahahSaveBeforeSubmit(formData, jqForm, options) {
    var $name = $('div#view-'+ view_name +'-views-savedsearches-container div.views-savedsearches-save-ahah form:first input#edit-name');
    
    // Validate the name field.
    if ($name.fieldValue()[0].length == 0 || $name.fieldValue()[0].length > 60 ) {
      alert("You must enter a name for this saved search (maximum 60 characters)!");
      return false;
    }

    // The form is validated, it will be submitted. Now let's add the form
    // data of the views filters form, to save it.

    // Remove the Hierarchical Select form build id and the form id, to
    // prevent them from ending up in the GET URL.
    $('#edit-hs-form-build-id, #edit-views-filters, #edit-views-filterblock').remove();

    // Prepare the hierarchical select form elements that are used as
    // exposed filters for a GET submit.
    $('form#views-filters, form#views-filterblock')
    .find('.hierarchical-select-wrapper')
    .trigger('prepare-GET-submit');

    formData.push({ name: 'views_filters_form', value: $('form#views-filters').formSerialize() });

    return true; 
  }
      
  // List (with Delete Saved Search form).
  var deleteOptions = {
    url: Drupal.settings.views_savedsearches.paths.basePath + Drupal.settings.views_savedsearches.paths.deletePath,
    beforeSubmit: ahahDeleteBeforeSubmit,
    target:   'div#view-'+ view_name +'-views-savedsearches-container',
    success:  function(response, status, view_name) { ViewsSavedSearches.attachBindings(false); ViewsSavedSearches.showHideList(view_name); }
   };
  $('div#view-'+ view_name +'-views-savedsearches-container div.views-savedsearches-list-ahah form:first', ViewsSavedSearches.baseContext)
  .ajaxForm(deleteOptions);
  
  // Save Search form.
  var saveOptions = {
    url: Drupal.settings.views_savedsearches.paths.basePath + Drupal.settings.views_savedsearches.paths.savePath,
    beforeSubmit: ahahSaveBeforeSubmit,
    target:   'div#view-'+ view_name +'-views-savedsearches-container',
    success:  function(response, status, view_name) { ViewsSavedSearches.attachBindings(false); ViewsSavedSearches.showHideList(view_name); }
  };
  $('div#view-'+ view_name +'-views-savedsearches-container div.views-savedsearches-save-ahah form:first', ViewsSavedSearches.baseContext)
  .ajaxForm(saveOptions);
};

ViewsSavedSearches.showHideList = function(viewName) {
  var showText;
  var hideText;
  var className;
  var showHide = Drupal.settings.views_savedsearches.showhide;
  var startHow = Drupal.settings.views_savedsearches.starthow;
  var $context;

  if (showHide != 'disabled') {
    switch (showHide) {
      case 'fancy':
        showText = 'show';
        hideText = 'hide';
        className = 'fancy';
        break;
      case 'simple':
        showText = '[+]';
        hideText = '[-]';
        className = 'simple';
        break;
    }

    $context = $('div#view-'+ viewName +'-views-savedsearches-container', ViewsSavedSearches.baseContext);
  
    if ( startHow != 'show' ) {
      // Hide all saved searches.
      $('div.views-savedsearches-list', $context).hide();
      $('div#delete_instructions', $context).hide();
      $('input#edit-delete', $context).hide();
      // Add a "show/hide" link after the "Saved searches" label.
      $('div.views-savedsearches-box', $context)
      .prepend('<a id="views-savedsearches-show-hide" class="'+ className +'" onSelectStart="return false;">'+ showText +'</a>');
    }
    else {
      // Show all saved searches.
      $('div.views-savedsearches-list', $context).show();
      $('div#delete_instructions', $context).show();
      $('input#edit-delete', $context).show();
      // Add a "show/hide" link after the "Saved searches" label.
      $('div.views-savedsearches-box', $context)
      .prepend('<a id="views-savedsearches-show-hide" class="'+ className +'" onSelectStart="return false;">'+ hideText +'</a>');
    }

  
    // Attach the bindings to toggle between hiding/showing.
    $('a#views-savedsearches-show-hide', $context).toggle(function() {
      $('div.views-savedsearches-list', $context).show();
      $('div#delete_instructions', $context).show();
      $('input#edit-delete', $context).show();
      $('a#views-savedsearches-show-hide', $context).text(hideText);
    }, function() {
      $('div.views-savedsearches-list', $context).hide();   
      $('div#delete_instructions', $context).hide();
      $('input#edit-delete', $context).hide();
      $('a#views-savedsearches-show-hide', $context).text(showText);
    });
  }
};

if (Drupal.jsEnabled) {
  $(document).ready(function(){
    var obj = ViewsSavedSearches.attachBindings(true);
  });
}

