/*
 * Nice Select - jQuery plugin for converting html select element to custom html
 * Examples and documentation at: 
 * Version: 0.4 (03/03/2009)
 * Copyright (c) 2009 Asela Jayarathne <asela@ultrasupernew.com>
 * Licensed under the MIT License: http://en.wikipedia.org/wiki/MIT_License
 * Requires: jQuery v1.3+
*/

(function($) {
  //
  // plugin definition
  //
  $.fn.niceselect = function(options) {
    debug(this);
    
    // build main options before element iteration
    var opts = $.extend({}, $.fn.niceselect.defaults, options);
    
    
    // iterate and reformat each matched element
    //return this.each(function() {
    this.each(function() {
      $this = $(this);
      
      // build element specific options
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      
      // nice select
      $this.find("select").hide();
      $this.find("input").hide();
      buildMarkup($this);
      
      
      // build markup
      function buildMarkup(wrapper){
        var selected = $this.find("select :selected").text();
        // var selected = $this.find("select option").text();
        
        // remove first option if asked
        var options = (o.removeFirst)? $this.find("select option:gt(0)") : $this.find("select option")
        
        var list = '<ul class="ns-list"">';
        options.each(function(el){ 
          list += '<li'+ ' class="'+ $(this).attr("selected") +'"' +'><a rel="' +$(this).val() +'" href="#">'+ $(this).text() +'</a></li>'; 
        });
        list += '</ul>';
        
        // assign label from options or 'option' tag with 'selected="selected"'
        var label    =  (o.togglerLabel != '')? o.togglerLabel : selected;
        
        var toggler  =  $('<a href="#" class="ns-toggler">'+ label +'</a>').
                        click(function(){ 
                                $(this).closest(".ns-wrap").find(".ns-dropdown").toggle();
                                $(this).closest(".ns-wrap").toggleClass("open");
                              });
                  
        var dropdown =  $('<div></div>').attr("class", "ns-dropdown").css({display: 'none'}).
                        append(o.prepend).
                        append(list).
                        append(o.append);
        
        
        $this.append($('<div></div>').attr("class","ns-wrap").
                      append(toggler).
                      append(dropdown));
      }
     });
    
    // do this, on select on an option
    $('ul.ns-list a').bind("click", function (e) {
      var wrapper = $(this).closest("div");
      $(this).closest("form").find("select :selected").val($(this).attr("rel"));
      wrapper.find("a.ns-toggler").text($(this).text());
      
      $(this).closest("form").submit();
      return false;
    });
    
    
    // close dropdown on click outside
    $('html').click(function() {
      $('div.ns-dropdown:visible').closest(".ns-wrap").removeClass("open");
      $('div.ns-dropdown:visible').hide();
    });

    $('div.ns-wrap').click(function(event){
         event.stopPropagation();
     });
 
   };
  
  
  
  //
  // private function for debugging
  //
  function debug($obj) {
    if (window.console && window.console.log)
    window.console.log('niceselect selection count: ' + $obj.size());
  };
  //
  // define and expose our format function
  //
  $.fn.niceselect.format = function(txt) {
    return '<strong>' + txt + '</strong>';
  };
  //
  // plugin defaults
  //
  $.fn.niceselect.defaults = {
    removeFirst: false, 
    togglerLabel: '', 
    append: '', 
    prepend: ''
  };
//
// end of closure
//
})(jQuery);