/*
   This file is part of WheelSpiner jQuery plugin for jQuery

   Copyrights (C) 2009 David Esperalta <http://www.davidesperalta.com>

   WheelSpiner is free software: you can redistribute it and/or
   modify it under the terms of the GNU General Public License as 
   published by the Free Software Foundation, either version 3 of 
   the License, or (at your option) any later version.

   WheelSpiner is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with WheelSpiner. If not, see <http://www.gnu.org/licenses/>.

*/

/*
   ------------------------------------
   WheelSpiner jQuery plugin for jQuery
   ------------------------------------
  
   Please, use the "example.html" and source code to more information
   about how use this plugin. Thanks very much for your interest.
   
   Plugin tested in Firefox 2, Opera 9, IExplorer 7 and Safari 3
   
   This plugin assume the existence of MouseWheel jQuery plugin. This 
   not is an optional plugin, MouseWheel jQuery plugin must be require
   in order of use WheelSpiner plugin.

   Example of usage:

   // First sample
   var options = {
     interval : 10,
     minValue : 10,
     maxValue : 100,
     focusAfter : true,
     selectAfter : true,
     stopPropagation : true
   };
   $('#sample1').AttachWheelSpiner(options);

   ---

   For information and download visit my weblog:
   
   http://www.bitacora.davidesperalta.com/
    
*/

(function($){

  // Attach a WheelSpiner into an object
  $.fn.AttachWheelSpiner = function(options){

    // Merge default options
    var opts = $.extend({}, $.fn.AttachWheelSpiner.defaults, options);

    // Ok, iterate objects
    return this.each(function(){

      // Merge final options
      var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;

      //add a cursor to control
      if(o.cursor!==''){
        $(this).css('cursor',o.cursor);
      }
        
      // Bind the mouse wheel event
      $(this).mousewheel(function(event, delta){

        // Set auxiliar variables
        var increment = (delta > 0);
        var value = parseInt($(this).val());
        var index = 0;//variable for selectedIndex

        // WTF with Opera?
        if($.browser.opera){
          if((delta < 0)){
            increment = true;
          }else{
            increment = false;
          }
        }
        if($(this)[0].tagName=='SELECT'){
          index = $(this)[0].selectedIndex;//set the current selected index
              
          if(increment){
            if(index>0){
              --index;
            }
          }else{
            if(index < $(this)[0].length){
              ++index;
            }
          }
          $(this)[0].selectedIndex = index;
          
        }else{
          // Test if value is an integer or not
          if(isNaN(value)){
            value = o.minValue;
          }
  
          // Increment or decrement, is the question
          if(increment){
            value += o.interval;
          }else{
            value -= o.interval;
          }
  
          // Prevent values gretter that max value
          if((o.maxValue != -1) && (value > o.maxValue)){
            value = o.maxValue;
          }
  
          // Prevent values less that min value
          if(value < o.minValue){
            value = o.minValue;
          }
  
          // Set the value
          $(this).val(value);
        }

        // Set focus?
        if(o.focusAfter){
          $(this).focus();
        }

        // Select?
        if(o.selectAfter){
          $(this).select();
        }

        $(this).trigger('onchange');
        
        // Propagation?
        if(o.stopPropagation){
          event.preventDefault();
        }

      });
    });

    return true;
  };

  // Detach a WheelSpiner into an object
  $.fn.DetachWheelSpiner = function(){
    $(this).unmousewheel();
    return true;
  };

  // WheelSpiner default options
  $.fn.AttachWheelSpiner.defaults = {
    interval : 1,
    minValue : 0,
    maxValue : -1,
    focusAfter : true,
    selectAfter : true,
    stopPropagation : true,
    cursor: 'row-resize'
  };

})(jQuery);
