/**
 * Search filter Javascript functions used for the
 * EuroAlumni web application
 * (c) 2001 - 2009 Euro-Alumni e.V.
 **/

function FilterOptions() {

	//The user search string to use
	this.userSearchString = "";

  this.showMoreFilterOptions = function ( filterName ) {

    if ( (filterName == null) || (moreLists[filterName] == null) ) {
        return;
    }

    //Get the reference to the main filter options element and clean it
    var moreFilterOptions = document.getElementById( "moreFilterOptions" );
    var moreFilterOptionsCloseDiv = document.getElementById( "moreFilterOptionsCloseDiv" );
    while ( moreFilterOptions.hasChildNodes() ) {
        	  moreFilterOptions.removeChild( moreFilterOptions.firstChild );
    }
    moreFilterOptions.appendChild( moreFilterOptionsCloseDiv );

    //Deselect the other options
    this.removeSelection();
    
    //Create the initial box
    var listBox = new this.newMoreListBox();

    //Go through the complete list for this filter name
    var moreList = moreLists[filterName];
    for ( var t=0; t<moreList.length; t=t+1 ) {

      //Create the list element and append it
    	var item = moreList[t];
      var li = document.createElement( "li" );
      var link = document.createElement( "a" );
      li.appendChild( link );
      link.href = "?searchstring=("+this.userSearchString+") AND "+filterName+":\""+item.name+"\"";
      var linkText = document.createTextNode( item.name+" ("+item.count+")" );
      link.appendChild( linkText );
      listBox.getElementsByTagName( "ul" )[0].appendChild( li );

      //Use a new box every 10 records
      if ( ((t+1) % 10 == 0) && (t > 0) ) {

    	  moreFilterOptions.appendChild( listBox );
        listBox = new this.newMoreListBox();
      } 
    }

    //Final appending
    moreFilterOptions.appendChild( listBox );
    
    //Show the filter options element
    moreFilterOptions.style.display = "block";

    //Highlight the correct selection
    var selectionDivSelected = document.getElementById( "SearchFilterDiv_"+filterName );
    selectionDivSelected.className='searchFilterBox searchFilterBoxSelected';

    //Define the correct height for the selection div
    var searchFilterConnector;
    var innerDivs = selectionDivSelected.getElementsByTagName( "div" );
    for ( var t=0; t<innerDivs.length; t=t+1 ) {
        if ( innerDivs[t].className == "searchFilterConnector" ) {
        	searchFilterConnector = innerDivs[t];
        }
    }
    var searchFilterConnectorDivHeight = getObjectPosition( moreFilterOptions ).y - getObjectPosition( searchFilterConnector ).y + 1;
    searchFilterConnector.style.height = searchFilterConnectorDivHeight+"px";
  }


  /**
   * Closes the more options panel and removes the selection
   **/
  this.closeMoreOptions = function() {

    var moreFilterOptions = document.getElementById( "moreFilterOptions" );
    moreFilterOptions.style.display = "none";
    this.removeSelection();
    return false;
  }

  /**
   * Deselects all other options
   **/
  this.removeSelection = function() {

    //Deselect the other options
    for( var otherFilterName in moreLists ) {
        var selectionDiv = document.getElementById( "SearchFilterDiv_"+otherFilterName );
        if ( (selectionDiv != null) && 
              selectionDiv.className && 
             (selectionDiv.className == "searchFilterBox searchFilterBoxSelected") ) {
          selectionDiv.className = "searchFilterBox";
        }
    }
  }

  
  this.newMoreListBox = function() {

    var moreListDiv = document.createElement( "div" );
    moreListDiv.className = "searchFilterMoreListBox";

    var ul = document.createElement( "ul" );
    moreListDiv.appendChild( ul );

    return moreListDiv;
  }
}