/*

  SearchableSelect - Plugin for JQuery (tested for 1.3.2)
  Author: Robin Orheden (www.redemption.se) - 2009.04.08
  
  Url: www.redemption.se/other-means-of-selection/

  ******************************************************
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
  
  Copyright (c) 2009 Robin Orheden

*/

  $(document).ready(function()
  {
    /* Find all friendly UL's */
    $('ul.SearchableSelect').each(function()
    {
      /* Hide list element */
      $(this).css("display", "none");
    
      /* Create new ID for the select, use existent Id of UL but add the _Select prefix */
      var ElementId = $(this).attr("id") + "_Select";
      
      /* Create new Select-element */
      $(this).after("<select id='" + ElementId + "' class='SearchableSelect'></select>");
      
      /* Loop-through list and add children to select */
      $(this).find("li").each(function()
      {
        /* Setup values */
        var Anchor = $(this).find("a").eq(0), Value = (Anchor.length > 0 ? $(Anchor).attr("href") : '');
        var SelectedElement = ($(this).hasClass("SelectedItem") ? ' selected' : '');
        var Title = $(Anchor).attr("title");

        /* Add child to select */
        $("#"+ElementId).append("<option value='"+Value+"'"+SelectedElement+" title='"+Title+"'>"+$(this).html()+"</option>");
      });
    });
    
    /* Hook selects */
    $("select.SearchableSelect").change(function()
    {
      if(this.value.length > 0) window.location.href = this.value;
    });
  });