/// <reference path="jQuery.js" />


function wwEditable(column, callback, options) {
    var _I = this;
    var _Ctl = column;            
    if (typeof(column)=="string")
        _Ctl = jQuery("#" + column)[0];    
             	    
    // options
    this.callback = callback;
    this.extraData = null;   
    this.textMode = "text";  
    this.updatedColor = null;
    
    // operationals
    this.origHtml = null;    
    this.origText = null;
    this.enteredText = null;            
    this.jcol = jQuery(_Ctl);     
    this.jedit = null;
    _Ctl.wwEditable = true;        

    if (options)
        jQuery.extend(this,options);
                   
    this.edit = function()
    {        
        if (callback)
          _I.callback = callback;
                   
          
        // *** Check if we're already editing and exit
        var jctl = jQuery("#__editColumn");
        if (jctl.length > 0)
            return;
                                
        _I.origHtml = _I.jcol.html();
        _I.origText = _I.jcol.text();
        if (_I.textMode == "text")       
            _I.jcol.html("<" +"form action='javascript:void(0);'><input id='__editColumn' value='" + _I.jcol.text() + "' style='width:" + _I.jcol.css("width") + "' /></form>");
        else
            _I.jcol.html("<" + "form action='javascript:void(0);'><textarea id='__editColumn' style='width:" + _I.jcol.css("width") + "'>" +  _I.jcol.text() + "</textarea></form>");
        
        // *** Select and focus the new input control
        var jctl = jQuery("#__editColumn");
        _I.jedit = jctl;        
        setTimeout( function() { jctl[0].focus() },50);

        jctl.keydown(_I._keyDownHandler);
        _I._bindBlur();
    }
    this.update = function() {           
        if (_I.jedit == null)
          return; 
        
        _I.jedit.remove();    
        _I.jedit = null;          
              
        if (_I.origHtml.toLowerCase().substr(0,2) == "<a") {
            _I.jcol.html(_I.origHtml);            
            var c = _I.jcol.find("a");
            _I.jcol.find("a").text(_I.enteredText);
        }
        else
            _I.jcol.text(_I.enteredText);        
                
        if (_I.updatedColor)
           _I.jcol.prepend("<div style='float: right;background:" + _I.updatedColor + ";height: 5px; width: 5px;'></div>");
                        
        _I.origHtml = _I.jcol.html();
    }
    this.abort = function()
    {    
        _I.jedit.remove();        
        _I.jedit == null;
        _I.jcol.html(_I.origHtml);                    
    }
    this.nextColumn = function(jcols)
    {
          if (!jcols)
            jcols = _I.jcol.find("~td");
            
          if (jcols.length>0)
          {
              for(var x=0;x<jcols.length;x++) {
                var jitem = jQuery(jcols[x]);
                if (jitem[0].wwEditable) {
                    jitem.trigger("click");
                    break;
                }
              }
          }
          else{                 
            var row = _I.jcol.parent().find("+tr");                    
            if (row.length > 0)
               _I.nextColumn(row.find(">td"));  
         }
    }
    this._updateHandler=function(event)
    {                                    
        _I.enteredText = _I.jedit.val();     
        _I._bindBlur(true);
        var res = null;        
        if (_I.callback) {                               
           res = _I.callback(_I.enteredText,_I)
           if (res==false) _I.abort(); 
           if (res) _I.update();           
        }                 
        _I._bindBlur();                          
    }
    this._bindBlur = function(unbind)
    {
        if (unbind)
            _I.jedit.unbind("blur",_I._updateHandler);
        else
            _I.jedit.bind("blur",_I._updateHandler);
    }
    this._keyDownHandler = function(event)
    {                                     
        if (event.keyCode == 27)
            {_I.abort(); return;}
        if (event.keyCode == 13){ 
            if (_I.textMode!="multiline"){
                _I._updateHandler();
                _I.nextColumn();
                return false;
            } 
        }                      
        if (event.keyCode == 9 && event.shiftKey) {
            _I._updateHandler();                 
             var prev = _I.jcol[0].previousSibling;
             if (prev)
                   jQuery(prev).trigger("click");              
             return false;             
        }
        if (event.keyCode == 9) {            
            _I._updateHandler();
            _I.nextColumn()
            return false;                                                                                                       
        }    
    }
}

// Hook up as jQuery plug-in
// static object
jQuery.editable = 
{
    makeEditable:  function(selector, callback, options)
    {            
        var s = jQuery(selector);
        if (s.length < 1)
           return;
        
        s.each( function(index) {          
            var jitem = jQuery(this);        
            this.wwEditable = true;
            jitem.click(function(event) {new wwEditable(this,callback,options).edit(); return true; } );                    
        } );    
        return s;
    },
    makeTableEditable: function(selector,callback,options)
    {
        var s = jQuery(selector);
        if (s.length < 1)
           return;    
        this.makeEditable(s.find("td"),callback,options);    
        return s;
    }
}

// jQuery selector function extensions
jQuery.fn.makeEditable = function(callback,options)
{
    return jQuery.editable.makeEditable(this,callback,options);
}
jQuery.fn.makeTableEditable = function(callback,options)
{
   return jQuery.editable.makeTableEditable(this,callback,options);
}
