/**
*   Плагин jquery.kiwi_select.js 
*   
*   @author Isaev Roman ( romis.bbx@gmail.com )
*   @firma BlackBox ( http://www.blackbox.ru/ )
*   @modufy_date 16.08.10
*   @version 1.0 beta
*   
*/

(function($) { 
    
	// constructor ( jel - JqueryELement )
	function Kiwi_select(jel, ops) 
  {          
      var self = this;
      var text = '';
      var optionStr = ''; // список оптионов
      var width = 0;
      var opts_el = 0;
      var select = 0;
      						
      jel.init = function()
  		{            
          select = jel;
          text = select.find("option:selected").text();
          var start_index = select.find("option:selected").index();
          width = select.width();
          optionStr = select.html();          
          optionStr = optionStr.replace(/option/ig,"span");          
        	var newSel = '<div class="kw_select"><div class="input">' + text + '</div><div class="options">' + optionStr  + '</div></div>';        		
        	select.after(newSel);
        	select.hide();
        	jel = $("div.kw_select");
        	
        	jel.find("div.input").width(width - 6);
        	
        	jel.find("span").hover(function(){
              $(this).addClass("hover");
          },function(){
              $(this).removeClass("hover");          
          });
          
          opts_el = jel.find("div.options span");
          
          var i = 0;
          var counter = Math.ceil(opts_el.length / ops.cols);
          while( i < opts_el.length ) {				
            i += counter;
            if (i > opts_el.length) {
              opts_el.slice(i - counter).wrapAll("<td></td>");
            } else {
              opts_el.slice(i - counter, i).wrapAll("<td></td>");
            }    			
      		};
      		
      		jel.find("div.options td").wrapAll('<table><tr></tr></table>');          
          jel.find("div.options").width( jel.find("div.options table").width() + 20 );
          
          jel.find("div.options span").eq(start_index).addClass("select"); 
          
          opts_el.click(function()
          {
            var text = $(this).text();            
            jel.find("div.input").html(text);
            
            var val = $(this).attr("value");
            select.find('option[value="' + val + '"]').attr('selected', 'selected');
            
            opts_el.removeClass("select"); 
            $(this).addClass("select");
            
            jel.find("div.options").hide();
          });
          
          jel.find("div.input")
            .click(function()
            {
                var el = jel.find("div.options");
                if ( el.is(':hidden') ){
                    el.show();
                    el.width( el.find("table").width() + 20 );
                } else {
                    el.hide();
                }            
            })
            .hover(function(){
                $(this).addClass("hover");
            },function(){
                $(this).removeClass("hover");          
            })
            .mousedown(function(){
                $(this).addClass("click");
            })
            .mouseup(function(){
                $(this).removeClass("click");
            }); 
            
          $(document).keypress(function (e) {
            if (e.keyCode == 27){
                jel.find("div.options").hide();
            }
          });     
  		
          
  		}
  		  		      
  		// methods
  		$.extend(this, {
  		
    			getOptions: function() {
            return ops;	
    			}            
                                        
  		}); 
		
  		jel.init(); 
						          		
	}

  // jQuery plugin implementation
  $.fn.kiwi_select = function(ops)
  {  
  		var el = this.eq(0).data('kiwi_select');
  		if (el) { return el; }
      
      // default settings
      var ops = $.extend({
          cols: 1 // количество колонок
      },ops);
          
  		this.each(function() {			
  			el = new Kiwi_select($(this), ops);
  			$(this).data('kiwi_select', el);	
  		});
  		
  		return this;    
  };

})(jQuery);



