/*
 * Lisearch - jQuery plugin 1.0
 *
 * Copyright (c) 2009 Claudio Cicali <claudio.cicali@gmail.com>
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Requirements and notes:
 *   - radio buttons have to be wrapped by the element(s) this plugin is called on
 *   - the list is created under the first input text just above or under the radio
 *     buttons container
 *   - if present, the submit button will be removed
 */
;(function($) {
 $.fn.lisearch = function() {
   return this.each(function() {

     var options = $(this);

     var mayClose = true;

     var form = options.parents("form");

     // Find the input field
     var input = options.prev().find('input[type=text]:first');
     if (input.length == 0)
       input = options.next().find('input[type=text]:first');
     if (input.length == 0)
       return;

     // Remove submit button
     form.find('input[type=submit]').remove();

     // Turn off autocomplete "feature" on the input text
     input.attr('autocomplete', 'off');

     // Hide radios
     options.hide();

     // Find the radios and build the option list
     var wrapper = $("<div class='lisearch-wrapper'/>");
     var ul = $("<ul class='lisearch'/>");
     var name = '';
     var hasChecked = false;
     options.find('input[type=radio]').each(function() {
       ul.append("<li" + (this.checked ? ' class="active" ' : '') + "><a href='#lisearch_" + this.getAttribute('value') + "'>" +  $(this).next('label').text() + "</a></li>");
       name = this.name;
       if (this.checked)
         hasChecked=true;
     });

     // Set first item active if there was no active radio
     if (!hasChecked && name != '')
       ul.find('li:first').addClass('active');

     // Add logic to the click, focus and blur events on each A in the list
     ul.find('a').click(function() {
       if ($.trim(input.val()) != '') {
         ul.find('li').removeClass('active');
         $(this).parent().addClass('active');
         // Submit the form (see submit event logic)
         form.trigger('submit');
       }
       return false;
     }).focus(function() {
       mayClose=false;
     }).blur(function() {
       mayClose=true;
       _closeWrapper();
     });

     // Submit logic
     form.submit(function() {
       // Find the active item in the list; if none, get the first (an active should always be present)
       var act = ul.find('li.active a');
       if (act.length == 0)
         act = ul.find('li:first a');
       // Create a phony hidden input to pass the form the selected item
       form.append("<input type='hidden' name='" + name + "' value='" + act.attr('href').match(/#lisearch_(.*)$/)[1] + "'>");
     });

     // Hide/Show logic
     input.focus(function() {
       mayClose=false;
       wrapper.show();
     }).blur(function() {
       mayClose=true;
       _closeWrapper();
     });

     // Append the ul and hide it 
     form.append(wrapper.append(ul).hide());

     // Finally, remove radio buttons
     options.remove();

     // Move the list wrapper under the input text box (requires (?) dimensions plugin)
     var offset = input.offset();
     wrapper.css({
       'position': 'absolute',
       'top': offset.top + input.outerHeight(),
       'left': offset.left,
       'width': input.width()
     });

     function _closeWrapper() {
       setTimeout(function() { 
       if (mayClose)
         wrapper.hide()
       }, 500);
     }
   });
 }
})(jQuery);

