
Drupal.searchPagesView = function(base, settings) {
}

Drupal.searchPagesView.autoAttach = function() {  
  $('#search_pages_tab_form select.search-page-term-field').change(Drupal.searchPagesView.submitForm);
	$('#search_pages_tab_form select.search-page-additional-term-field').change(Drupal.searchPagesView.submitForm);
  $('#search_pages_tab_form select.search-page-term-field2').change(Drupal.searchPagesView.submitForm);
  $('#search_pages_tab_form').submit(Drupal.searchPagesView.submitForm);
}

Drupal.searchPagesView.submitForm = function() {
  $('#search_pages_tab_form input').attr('disabled', 'disabled');
  
  // Saving info about changed select filter if function was called from change event
  //$('#edit-gsa-query').val('');
  var tidValue = '';
  var tidValue2 = '';
	var additionalTidValue = new Array();
  var result = '';
  
  if ($(this).attr('name') != undefined && 
  	  !$(this).attr('name').match(/additional_term/) &&  
  	  $(this).attr('name').indexOf('term[') != -1 && $(this).attr('value').length > 0) {
    tidValue = $(this).attr('value');
  }
  if ($(this).attr('name') != undefined && $(this).attr('name').indexOf('term2[') != -1 
			&& $(this).attr('value').length > 0 && !$(this).attr('name').match(/additional_term/) ) {
    tidValue2 = $(this).attr('value');
  }
  
  if ($(this).attr('name') != undefined && $(this).attr('name').indexOf('additional_term[') != -1 && $(this).attr('value').length > 0) {
  	result = $(this).attr('name').match(/\[(.*?)\]\[(.*?)\]$/i);
		if (result) {
			additionalTidValue[result[1]] = $(this).attr('value');
		}
  }
	
  if (tidValue.length == 0) {
    // Finding the previous tid[] select field if one exists
    $('#search_pages_tab_form select.search-page-term-field').each(function() {
      if ($(this).attr('value') != 0) {
        tidValue = $(this).attr('value');
      }
    });     
  }
  if (tidValue2.length == 0) {
    // Finding the previous tid[] select field if one exists
    $('#search_pages_tab_form select.search-page-term-field2').each(function() {
      if ($(this).attr('value') != 0) {
        tidValue2 = $(this).attr('value');
      }
    });     
  }
	
	if ( Drupal.settings.searchPages.additionalTags ) {
		for ( $i = 0; $i < Drupal.settings.searchPages.additionalTags; $i++ ) {
			if ( additionalTidValue[$i] == undefined || additionalTidValue[$i] == '' ) {
				var $object = $("#search_pages_tab_form select[name^='additional_term[" + $i + "][']:last");
				if ( $object.length > 0 && $object.val() != '' ) {
					additionalTidValue[$i] = $object.val();
				}
			}
		}
	}
  
  // Creating query stirng   
  var args = '';
  $('#search_pages_tab_form input').each(function() {
    if($(this).attr('name').indexOf('gsa_query') == -1){
      if ($(this).attr('type') == 'text' && $(this).attr('value').length > 0) {
        if (args.length > 0) {
          args = args + '&';
        }
        args = args + $(this).attr('name') + '=' + $(this).attr('value');
      }
    }
  });
  $('#search_pages_tab_form select').each(function() {
    if ($(this).attr('name').indexOf('term[') == -1 && $(this).attr('name').indexOf('term2[') == -1  && $(this).attr('value').length > 0) {
      if (args.length > 0) {
        args = args + '&';
      }
      args = args + $(this).attr('name') + '=' + $(this).attr('value');
    }
  });  
  // Creating path string depending on settings
  if (Drupal.settings.searchPages.useVocabTermPath == 1) {
    if (tidValue != undefined && tidValue != '') {
      var path = tidValue;        
    } else {
      var tmp = window.location.href.split('?');
      var path = Drupal.settings.searchPages.tabPath;                  
    }    
  } else {
    if (tidValue != undefined && tidValue != '') {
      if (args.length > 0) {
        args = args + '&';
      }      
      args = args + 'tid=' + tidValue;
    }     
    var tmp = window.location.href.split('?');
    var path = Drupal.settings.searchPages.tabPath;                  
  }
  
  if (tidValue2 != undefined && tidValue2 != '') {
    if (args.length > 0) {
      args = args + '&';
    }      
    args = args + 'tid2=' + tidValue2;
  }
  
  if ( additionalTidValue != undefined && additionalTidValue.length > 0 ) {
		for ( $i = 0; $i < additionalTidValue.length; $i++) {
			if (additionalTidValue[$i] != undefined && additionalTidValue[$i] != '') {
	  	if (args.length > 0) {
		  		args = args + '&';
		  	}
		  	args = args + 'addtid' + $i + '=' + additionalTidValue[$i];
		  }
		}
  }
  
  var path = (args.length > 0) ? path + '?' + args : path;
  window.location.href = Drupal.settings.basePath + path;
  return false;
}

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.searchPagesView.autoAttach);
}
