/**
 * В© Conjecture corporation 2008
 *
 * jQuery customized (HTML) select box plugin 
 *
 * created by Ivan Klimenko
 * 
 */

(function($) {
    
      //public part
    $.fn.extend(
    {
    
          //Set ulistbox instance
        customselect : function(options) 
        {
            return this.each(function() {
                    
                      //Variables
                    var _object = this;
                    var _options = options;
                    
                    var _LastCheckedItem = '';
                    
                      //Functions
                    var showSelectList = function() {
                        
                        iTopOffset = $(_object).find('.calc_input_br').offset().top;
                        iLeftOffset = $(_object).find('.calc_input_bl').offset().left;
                        iElementHeight = $(_object).find('.calc_input_br').height();
                        
                        iListOffset = iTopOffset+iElementHeight;
                        
                        $('.calc_selecter'+_options.id).css('top', iListOffset).css('left', iLeftOffset);
                        
                        $('.calc_selecter'+_options.id).show();
                        
                        
                        $iScreenHeight = $(window).height();
                        
                        $iBottomScreenPos = $(window).scrollTop() + $iScreenHeight;
                        
                        $iSelectListHeight = $('.calc_selecter'+_options.id).height();
                        
                        if( (iListOffset + $iSelectListHeight) > $iBottomScreenPos ) {
                        
                          iListOffset = iListOffset - iElementHeight - $iSelectListHeight - 4;
                          
                          $('.calc_selecter'+_options.id).css('top', iListOffset).css('left', iLeftOffset);

                        }
                        

                    }//This is a last punched card of onInstanceMouseOver function


                    var hideOnBlur = function() {
                       
                        //sDisplay = $(_object).find('.calc_selecter'+_options.id).css('display');
                        sDisplay = $('.calc_selecter'+_options.id).css('display');
                        
                        if(sDisplay == 'block') {
                          $('.calc_selecter'+_options.id).hide();
                          //$(_object).find('.calc_selecter'+_options.id).hide();
                        }
                        
                    }//This is a last punched card of hideOnBlur function
                    
                    
                    var hideOthersSelectBoxes = function() {
                    
                        $('.calc_selecter').each( function(i) {
                        
                                                    if($(this).attr('class') != '.calc_selecter .calc_selecter'+_options.id) {
                                                      
                                                      if($(this).css('display') == 'block') {
                                                        $(this).hide();
                                                      }
                                                      
                                                    }
                                                    
                                                  }
                                                );
                                                
                    }//This is a last punched card of hideOthersSelectBoxes function 

                      //Events
                    var onInstanceMouseOver = function() {
                    
                        $(this).addClass('dark');
                        
                    }//This is a last punched card of onInstanceMouseOver function

                    
                    var onInstanceMouseOut = function() {

                        $(this).removeClass('dark');
                        
                    }//This is a last punched card of onInstanceMouseOut function

                    var onInputClick = function() {
                        
                        sDisplay = $('.calc_selecter'+_options.id).css('display');
                        //sDisplay = $(_object).find('.calc_selecter'+_options.id).css('display');
                        
                        hideOthersSelectBoxes();
                        
                        if(sDisplay == 'none') {
                          
                          showSelectList();  
                          
                        } else {
                          $('.calc_selecter'+_options.id).hide();
                          //$(_object).find('.calc_selecter'+_options.id).hide();
                        }
                        
                        $(_object).find('.input_calculator').focus();
                        
                        
                    }//This is a last punched card of onInputClick function

                    var onInputBlur = function() {
                         
                        setTimeout(hideOnBlur, 100); 
                       
                    }//This is a last punched card of onInstanceBlur function

                    var onButtonSelectClick = function() {
                        
                        sDisplay = $('.calc_selecter'+_options.id).css('display');
                        //sDisplay = $(_object).find('.calc_selecter'+_options.id).css('display');
                        
                        hideOthersSelectBoxes();
                        
                        if(sDisplay == 'none') {
                          showSelectList();
                        } else {
                          $('.calc_selecter'+_options.id).hide();
                          //$(_object).find('.calc_selecter'+_options.id).hide();
                        }
                        
                        $(_object).find('.input_calculator').focus();
                        
                    }//This is a last punched card of onButtonSelectClick function
                    

                    var onListElementMouseOver = function() {
                    
                        $(this).css('background', '#f1f4f8');
                        
                    }//This is a last punched card of onListElementMouseOver function

                    
                    var onListElementMouseOut = function() {

                        $(this).css('background', 'none');
                        
                    }//This is a last punched card of onListElementMouseOut function
                    
                    
                    var onListElementClick = function() {
                       
                        sElementName = $(this).text();
                        sElementIndex = $(this).attr('_selectedindex_');
                        
                        $(_object).find('option[selected = 1]').removeAttr('selected');
                        
                        $(_object).find('.input_calculator').attr('value', sElementName);
                        
                        $(_object).find('option[selectindex = '+sElementIndex+']').attr('selected', '1');
                        
                        $('.calc_selecter'+_options.id).hide();
                        //$(_object).find('.calc_selecter'+_options.id).hide();
                        
                        $(_object).trigger("OnItemChange");                        
                        
                    }//This is a last punched card of onListElementClick function
                    
                    
                      //Set instance
                    $(this).find(".select_form_element").attr('id', _options.id);  
                    $(this).find(".select_form_element").attr('name', _options.name);  
                    
                    $(this).find(".ins").hover(onInstanceMouseOver, onInstanceMouseOut);
                    
                    $(this).find(".input_calculator").click(onInputClick);
                    
                    //$(this).find(".input_calculator").blur(onInputBlur);
                    
                    $('body').click(onInputBlur);
                    $(_object).find('.inputter').click(function(e) { e.stopPropagation(); });
                    
                    $(this).find(".select_button").click(onButtonSelectClick);
                    
                    $(this).find(".element_spiska").hover(onListElementMouseOver, onListElementMouseOut);
                    $(this).find(".element_spiska").click(onListElementClick);                    
                                                                                 
                    
                    oSelectedItem = $(this).find('option[selected = 1]');
          
                    if(!oSelectedItem.attr('ratio') && !oSelectedItem.attr('field_sort') && !oSelectedItem.attr('field_value')) {
                      oSelectedItem = $(this).find('option:first');
                    } 
                    
                    //sFirstValue = $(this).find(".element_spiska:first").text();                    
                    
                    sFirstValue = oSelectedItem.text();                    
                    $(this).find(".input_calculator").attr('value', sFirstValue);
                    
                   $(_object).find('.calc_selecter'+_options.id).css('z-index', '1000000').appendTo('body'); 
                    
            });
        },
        
        
          //Get selected element of select instance
        getSelectedElement : function(options) {
        
          var aNames = new Array();
           
          oSelectedItem = $(this).find('option[selected = 1]');
          
          if(!oSelectedItem.attr('ratio') && !oSelectedItem.attr('field_sort') && !oSelectedItem.attr('field_value')) {
            oSelectedItem = $(this).find('option:first');
          } 
          
          return  oSelectedItem;
          
        },//This is a last punched card of getSelectedElement function
        

          //Select element of select instance
        selectElement : function(options) {
        
          sElementIndex = options.index;
                        
          $(this).find('option[selected = 1]').removeAttr('selected');
                        
          $(this).find('option[selectindex = '+sElementIndex+']').attr('selected', '1');
          
          sElementName = $(this).find('option[selectindex = '+sElementIndex+']').text();
          
          $(this).find('.input_calculator').attr('value', sElementName);
                        
        }//This is a last punched card of selectElement function
        
    });
   
})(jQuery);