/************* TODO
- Auto select
- Pb pointer
*/
function getPosX(obj)
{
 return(obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getPosX(obj.offsetParent));
}


function getPosY(obj)
{
 return(obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getPosY(obj.offsetParent));
}

function compare_up(a, b)
{
 if(a[2]==b[2]) return 0;
 if(a[2]>b[2]) return -1;
 if(a[2]<b[2]) return 1;
}

function compare_down(a, b)
{
 if(a[2]==b[2]) return 0;
 if(a[2]>b[2]) return 1;
 if(a[2]<b[2]) return -1;
}


var _select_on = false ;
////////////////////////
//
// Class
//
///////////////////////

var AZK_TABLE_MULTI_SEL = 1 ;
var AZK_TABLE_KEY_MULTI_SEL = 2 ;

function AzkTableDisplayer(pid, prowheader, pmultisel)
{
 this.id = pid;

 this.displayer = null ;

 this.head_tbody = null ;
 this.body_tbody = null ;

//**s this.width = pw ;
//**s this.height = ph ;

 this.dragx = '0' ;

 this.cols = Array() ;

 this.key = 'id' ;

 this.multi_sel = pmultisel ;

 this.init = true ;

 this.is_ie = false ;
 if(document.all)
 {
  this.is_ie = true ;
 }

 this.datas = Array() ;
 this.vals_img = Array() ;

 this.auto_select = Array() ;

 //
 this.column_header_str = Array() ;
 this.body_str = Array() ;

 //S this.selection = new Array() ;
 this.selection = new AzkSelection(this.multi_sel) ;
 
 this.pre_selection = new Array() ;

 this.drag_col_area_w = 5 ;
}

AzkTableDisplayer.prototype.moveCurrentRowUp = function()
                            {
                             if(this.cur_row)
                             {
                              this.body_div.firstChild.firstChild.insertBefore(this.cur_row, this.cur_row.previousSibling) ;
                             }
                            }

AzkTableDisplayer.prototype.moveCurrentRowDown = function()
                            {
                             if(this.cur_row)
                             {
                              elem = this.cur_row.nextSibling ;

                              if(elem)
                              {
                               this.body_div.firstChild.firstChild.insertBefore(elem, this.cur_row) ;
                              }
                              else
                              {
                               elem = this.body_div.firstChild.firstChild.firstChild ;
                               this.body_div.firstChild.firstChild.insertBefore(this.cur_row, elem) ;
                              }
                             }
                            }


AzkTableDisplayer.prototype.setMultiSelection = function(pstate)
                            {
                             this.multi_sel = pstate ;
                            }

AzkTableDisplayer.prototype.setRowKey = function(pkey)
                            {
                             this.key = pkey ;
                            }

AzkTableDisplayer.prototype.setAutoSelect = function(pcol, pfct)
                            {
                             this.auto_select[pcol]=pfct
                            }

AzkTableDisplayer.prototype.setColImages = function(pcol, ptab)
                            {
                             this.vals_img[pcol] = ptab ;
                            }

AzkTableDisplayer.prototype.setColumns = function(pcols)
                            {
                             this.cols = pcols ;

                             for(key in pcols)
                             {
                              this.addColumn(key, pcols[key][0]) ;
                             }
                            }


AzkTableDisplayer.prototype.setSelection = function(pselection, pno_cb)
                            {
                             if(this.init)
                             {
                              this.pre_selection.push(pselection) ;
                             }
                             else
                             {
                              if(!pselection.splice)
                              {
                               pselection = new Array(pselection) ;
                              }

                              var row = null ;
                              for(sel in pselection)
                              {
                               row = this.getRowById(pselection[sel]) ;
                               
                               //trace(pselection[sel]+" "+row) ;
                               
                               if(row)
                               {
                                cb=null ;

                                if(pno_cb)
                                {
                                 cb = this.row_select_cb ;
                                 this.row_select_cb = 0 ;
                                }

                                this.selectRow(row) ;

                                if(pno_cb)
                                {
                                 this.row_select_cb = cb;
                                }
                               }                               
                              }
                              
                              if(row)
                              {
                               this.scrollToRow(row) ;
                              }
                             }
                            }

/***** CBs */

AzkTableDisplayer.prototype.setRowClassCB = function(pfct)
                            {
                             this.row_class_cb = pfct ;
                            }

AzkTableDisplayer.prototype.setOverRowCB = function(pcallback)
                            {
                             this.row_over_cb = pcallback ;
                            }

AzkTableDisplayer.prototype.setValueCB = function(pcb)
                            {
                             this.value_cb = pcb ;
                            }

AzkTableDisplayer.prototype.setRowSelectCB = function(pcallback)
                            {
                             this.row_select_cb = pcallback ;
                            }

AzkTableDisplayer.prototype.setRowDblClickCB = function(pcallback)
                            {
                             this.row_cbl_click_cb = pcallback ;
                            }


AzkTableDisplayer.prototype.setValue = function(pcol, val)
                            {
                             if(this.cur_row)
                             {
                              this._setValue(this.cur_row, pcol, val)
                             }
                            }

AzkTableDisplayer.prototype._setValue = function(row, pcol, val)
                            {
                             if(row)
                             {
                              rid = row.getAttribute('id') ;

                              col = this.getColById(row, pcol) ;

                              if(!this.datas[rid])
                              {
                               this.datas[rid] = new Array() ;
                              }

                              this.datas[rid][pcol]=val ;

                              if(this.row_class_cb)
                              {
                               var vclass = this.row_class_cb(this.datas[rid]) ;
                               
                               row.setAttribute('oldClassName', vclass) ;
                               row.className = vclass+' selected_row' ;
                              }

                              if(this.value_cb)
                              {
                               val = this.value_cb(pcol, val) ;
                              }

                              if(this.vals_img[pcol])
                              {
                               val = this.vals_img[key][val] ;
                              }

                              if(col)
                              {
                               col.firstChild.innerHTML = val ;
                              }
                             }
                            }


AzkTableDisplayer.prototype.setValues = function(pvals)
                            {
                             for(idx in pvals)
                             {
                              this.setValue(idx, pvals[idx]) ;
                             }
                            }


AzkTableDisplayer.prototype.getValue = function(pcol)
                            {
                             ret = null ;

                             if(this.cur_row)
                             {
                              rid = this.cur_row.getAttribute('id') ;
                              ret = this.datas[rid][pcol] ;
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype.getHeaderColById = function(pid)
                            {
                             ret = '' ;
                             for(vcol=0; vcol<this.head_tbody.firstChild.childNodes.length; vcol++)
                             {
                              cell = this.head_tbody.firstChild.childNodes[vcol] ;

                              if(cell.getAttribute('id') == pid)
                              {
                               ret = cell ;
                              }
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype.getColById = function(prow, pid)
                            {
                             ret=null ;

                             rows = new Array() ;

                             if(this.row_header)
                             {
                              rows.push(prow.header_row) ;
                             }

                             rows.push(prow) ;

                             for(row_idx in rows)
                             {
                              vcells = rows[row_idx].cells ;

                              cpt=0 ;
                              stop=false ;
                              while(vcells[cpt] && !stop)
                              {
                               stop=(vcells[cpt].getAttribute('id')==pid);

                               if(stop)
                               {
                                ret = vcells[cpt] ;
                               }

                               cpt++ ;
                              }
                             }

                             delete rows ;

                             return ret ;
                            }


AzkTableDisplayer.prototype.addColumn = function(pid, plabel)
                            {
                             cell = "<td class='c_"+pid+"' id='"+pid+"'><div class='cd_content'>"+plabel+"</div></td>" ;

                             if((this.row_header && this.not_first) || (!this.row_header))
                             {
                              this.column_header_str.push(cell) ;
                             }
                             else
                             {
                              this.header_str.push("<tr>"+cell+"</tr>") ;
                             }

                             this.not_first = true ;
                            }

AzkTableDisplayer.prototype.addRow = function(pvalues)
                            {
                             if(this.init)
                             {
                              for(col in this.auto_select)
                              {
                               if(pvalues[col])
                               {
                               if(this.auto_select[col](pvalues[col]))
                               {
                                this.pre_selection.push(pvalues[this.key]) ;
                               }
                               }
                              }
                             }

                             vid = pvalues[this.key]  ;

                             if(!this.displayer)
                             {
                              this.datas[vid]= pvalues ;
                             }

                             vclass = '' ;
                             if(this.row_class_cb)
                             {
                              vclass = this.row_class_cb(pvalues) ;
                             }

                             old_row = this.cur_row ;

                             this._addRow(vid, vclass) ;

                             for(key in pvalues)
                             {
                              val = pvalues[key] ;

                              if(this.cols[key])
                              {
                               if(this.value_cb)
                               {
                                val = this.value_cb(key, val) ;
                               }

                               if(this.vals_img[key])
                               {
                                if(this.vals_img[key][val])
                                {
                                 val = this.vals_img[key][val] ;
                                }
                                else
                                {
                                 val = '' ;
                                }
                               }

                               this.addCell(key, val) ;
                              }

                              if(this.displayer)
                              {
                               this.setValue(key, val) ;
                              }

                              delete val ;
                             }

                             if(this.displayer)
                             {
                              row = this.cur_row ;
                              this.cur_row = old_row ;
                              //**this.selectRow(row) ;
                             }
                             delete old_row ;
                            }

AzkTableDisplayer.prototype._addRow = function(pid, pclass)
                            {
                             if(!this.displayer)
                             {
                              if(this.cur_row)
                              {
                               this.body_str.push("</tr>") ;
                              }

                              vclass = this.row_class ;

                              if(pclass)
                              {
                               vclass = pclass ;
                              }

                              this.body_str.push("<tr id='"+pid+"' class='"+pclass+"'>") ;

                              this.cur_row = pid ;

                              this.col=0 ;
                             }
                             else
                             {
                              //old_row = this.cur_row ;
                              if(!this.body_div.firstChild.firstChild)
                              {
                               elem = document.createElement("TBody") ;
                               this.body_div.firstChild.appendChild(elem) ;

                               if(this.row_header_div)
                               {
                                elem = document.createElement("TBody") ;
                                this.row_header_div.firstChild.appendChild(elem) ;
                               }
                              }

                              crow = this._createRow(this.body_div.firstChild.firstChild) ;

                              crow.setAttribute('id',pid) ;

                              if(this.row_header_div)
                              {
                               row = this._createRow(this.row_header_div.firstChild.firstChild) ;
                               row.body_row = crow ;
                               crow.header_row = row ;
                              }

                              this.cur_row = crow ;
                             }
                            }

AzkTableDisplayer.prototype._createRow = function(pparent)
                            {
                             row = document.createElement("TR") ;

                             var vthis=this ;

                             pparent.appendChild(row) ;

                             return row ;
                            }

AzkTableDisplayer.prototype.deleteSelectedRows = function()
                            {
//S                             for(row_id in this.selection)
                             for(row_id in this.selection.items)
                             {
                              //S this.deleteRow(this.selection[row_id]) ;
                              this.deleteRow(this.selection.items[row_id]) ;
                              //S delete this.selection[row] ;
                              this.selection.remove(row_id) ;                              
                             }
                            }

AzkTableDisplayer.prototype.deleteRow = function(pid)
                            {
                             row = this.getRowById(pid) ;

                             if(row)
                             {
                              idx = row.rowIndex ;

                              this.body_div.firstChild.firstChild.deleteRow(idx) ;

                              delete this.datas[pid] ;

                              if(this.row_header_div)
                              {
                               this.row_header_div.firstChild.firstChild.deleteRow(idx) ;
                              }
                             }
                            }

AzkTableDisplayer.prototype.clearRows = function()
                            {
                             nbrows = this.body_div.firstChild.firstChild.rows.length ;

                             for(idx = nbrows; idx>0; idx--)
                             {
                              pid = this.body_div.firstChild.rows[idx-1].getAttribute('id') ;

                              this.body_div.firstChild.firstChild.deleteRow(idx-1) ;

                              delete this.datas[pid] ;

                              if(this.row_header_div)
                              {
                               this.row_header_div.firstChild.firstChild.deleteRow(idx-1) ;
                              }
                             }

                             //S delete this.selection ;
                             this.selection.unselectAll() ;
                            }


AzkTableDisplayer.prototype.getRowById = function(pid)
                            {
                             ret=null ;

                             rows = this.body_div.firstChild.firstChild.rows ;

                             cpt=0 ;
                             stop=false ;
                             while(rows[cpt] && !stop)
                             {
                              stop=(rows[cpt].getAttribute('id')==pid);

                              if(stop)
                              {
                               ret = rows[cpt] ;
                              }

                              cpt++ ;
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype.getSelectedId = function()
                            {
                             ret = null ;

                             if(this.cur_row)
                             {
                              ret = this.cur_row.getAttribute('id') ;
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype.getSelectedRowVals = function()
                            {
                             ret = null ;

                             if(this.cur_row)
                             {
                              ret = this.datas[this.cur_row.getAttribute('id')] ;
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype.getSelection = function()
                            {
                             return this.selection ;
                            }

AzkTableDisplayer.prototype.getSelectionLength = function()
                            {
                             cpt=0 ;
                             /*S for(val in this.selection)
                             {
                              cpt++ ;
                             }

                             return cpt ;*/
                             return this.selection.length ;
                            }


AzkTableDisplayer.prototype.getRows = function()
                            {
                             return this.body_div.firstChild.rows ;
                            }


AzkTableDisplayer.prototype.scrollToRow = function(prow)
                            {
                             dec = prow.offsetTop - (this.body_div.offsetHeight/2)+(prow.offsetHeight) ;
                             this.body_div.scrollTop = dec ;

                             if(this.row_header)
                             {
                              this.row_header_div.scrollTop = dec ;
                             }
                            }

AzkTableDisplayer.prototype.addCell = function(pid, pvalue)
                            {
                             if(!this.displayer)
                             {
                              if(this.col==0 && this.row_header)
                              {
                               this.row_header_str.push("<tr'><td>"+pvalue+"</td></tr>") ;
                              }
                              else
                              {
                               this.body_str.push("<td id='"+pid+"' class='c_"+pid+"'><div class='cd_content'>"+pvalue+"</div></td>") ;
                              }

                              this.col++ ;
                             }
                             else
                             {
                              row = this.cur_row ;

                              col = row.cells.length ;

                              if(this.row_header_div)
                              {
                               if(!this.cur_row.header_row.cells.length)
                               {
                                row = this.cur_row.header_row ;
                               }
                               else
                               {
                                col++ ;
                               }

                               delete row ;
                              }

                              cell=document.createElement("TD");
                              row.appendChild(cell);

                              cell.style.overflow = 'hidden' ;

                              cell.innerHTML = pvalue ;

                              cell.className =  "c_"+pid ;

                              cell.innerHTML = "<div class='cd_content'>"+pvalue+"</div>" ;

                              cell.setAttribute('id',pid) ;

                              //Set width
                              if(hstyle = this.head_tbody.firstChild.childNodes[cell.cellIndex].style)
                              {
                               cell.style.width = hstyle.width ;
                              }

                              delete cell ;
                             }
                            }


AzkTableDisplayer.prototype.bind = function()
                            {
                             // DIV // TABLE // TBODY

                             pcols = Array() ;
                             for(col in this.cols)
                             {
                              pcols.push(col) ;
                             }

                             this.displayer = document.getElementById(this.id) ;

                             //**s this.displayer.style.width = this.width+'px' ;

                             this.displayer.className = 'AzkTableDisplayer' ;

                             this.displayer.style.MozUserFocus = 'normal' ;

                             div_cnt = 0 ;
                             odd = 1 ;

                             for(div_idx in this.displayer.childNodes)
                             {
                              pdiv = this.displayer.childNodes[div_idx] ;

                              if(pdiv.tagName == 'DIV')
                              {

                               table = pdiv.childNodes[0] ;

                               if(!table.tagName)
                               {
                                table = pdiv.childNodes[1] ;
                               }

                               tbody = table.childNodes[0] ;

                               if(!tbody)
                               {
                                // créer la table vide.
                                tbody = document.createElement('tbody') ;
                                table.appendChild(tbody) ;
                               }

                               if(!tbody.tagName)
                               {
                                tbody = table.childNodes[1] ;
                               }

                               tbody.parentNode.style.width = 0;
                               tbody.parentNode.style.tableLayout = 'fixed' ;

                               //this._clearTextNode(tbody) ;

                               for(tr_idx in tbody.childNodes)
                               {
                                ptr = tbody.childNodes[tr_idx] ;

                                if(ptr && (ptr.tagName == 'TR'))
                                {
                                 ptr.className = '' ;

                                 if(this.row_class_cb)
                                 {
                                  if(pid = ptr.getAttribute('id'))
                                  {
                                   ptr.className = this.row_class_cb(this.datas[pid]) ;
                                  }
                                 }

                                 if(odd && div_cnt)
                                 {
                                  ptr.className += ' odd' ;
                                 }

                                 if(!div_cnt)
                                 {
                                  ptr.className += ' header' ;
                                 }

                                 odd = !odd ;

                                 this._clearTextNode(ptr) ;

                                 col = 0 ;
                                 for(td_idx in ptr.childNodes)
                                 {
                                  ptd = ptr.childNodes[td_idx] ;

                                  if(ptd && ptd.tagName == 'TD')
                                  {
                                   //ptd.style.width = '55px'
                                   ptd.className =  "c_default c_"+pcols[col] ;

                                   ptd.innerHTML = "<DIV class='cd_content'>"+ptd.innerHTML+"</DIV>" ;

                                   col++ ;
                                  }
                                  //alert(col) ;
                                 }
                                }
                               }

                               switch(div_cnt)
                               {
                                case 0:
                                 this.head_tbody = tbody ;
                                 this.head_div =  tbody.parentNode.parentNode ;
                                break;
                                case 1:
                                 this.body_tbody = tbody ;
                                 this.body_div =  tbody.parentNode.parentNode ;
                                break;
                               }

                               div_cnt++ ;
                              }
                             }

                             this._init() ;
                            }

AzkTableDisplayer.prototype.draw = function(pparent)
                            {
                             // build table
                             table = new Array() ;

                             table.push("<div id='"+this.id+"' class='AzkTableDisplayer' style='visibility: hidden'>") ;

                             /*if(this.row_header)
                             {
                              table.push("<div ><table>") ;
                              this.header_str.push("</table></div>") ;
                              table.push(this.header_str.join('')) ;
                             }*/

                             //build header
                             table.push("<div class='header_div' style='overflow: hidden;'><table style='table-layout: fixed; width:0px'><tr>") ;
                             this.column_header_str.push("</tr></table></div>") ;
                             table.push(this.column_header_str.join('')) ;

                             /*if(this.row_header)
                             {//**TODO
                              table.push("<div id='"+this.id+"_row_header' style='overflow: hidden;'><table "+this.table_pres+">") ;
                              this.row_header_str.push("</table></div>") ;
                              table.push(this.row_header_str.join('')) ;
                              table.push("</td>") ;
                             }*/

                             //**s table.push("<div class='body_div' style='overflow: auto; height:"+this.height+"px; width:"+this.width+"px'><table style='table-layout: fixed; width:0px;'>") ;
                             table.push("<div class='body_div' style='overflow: auto;'><table style='table-layout: fixed; width:0px;'>") ;

                             if(this.body_str.length)
                             {
                              this.body_str.push("</tr>")
                             }

                             this.body_str.push("</table></div>")

                             table.push(this.body_str.join('')) ;

                             table.push('</div>');

                             var writestring = table.join('');

                             //pparent.innerHTML = writestring ;

                             if(pparent)
                             {
                              pparent.innerHTML += writestring ;
                             }
                             else
                             {
                              document.write(writestring) ;
                             }

                             //document.getElementById('source').value=writestring ;

                             delete this.column_header_str ;
                             delete this.row_header_str ;
                             delete this.body_str ;
                             delete table ;
                             delete writestring ;

                             this._init() ;
                            }

AzkTableDisplayer.prototype._init = function()
                            {
                             // Set the overflow
                             this.displayer = document.getElementById(this.id) ;
                             //**s this.displayer.style.width = this.width+'px' ;
                             this.displayer.style.MozUserFocus = 'normal' ;

                             this.head_div =  this.displayer.childNodes[0] ;
                             this.head_tbody = this.head_div.childNodes[0].childNodes[0] ;

                             this.body_div = this.displayer.childNodes[1] ;
                             /*this.body_div.style.height = (this.height - this.head_div.offsetHeight)+'px' ;*/
                             
                             //this.head_div.style.width = this.body_div.clientWidth+'px'  ;
                             
                             if(!this.body_div.childNodes[0].childNodes[0])
                             {
                              tbody = document.createElement('tbody') ;
                              this.body_div.childNodes[0].appendChild(tbody) ;
                             }

                             this.body_tbody =  this.body_div.childNodes[0].childNodes[0] ;

                             this.body_tbody.parentNode.style.MozUserFocus = 'ignore' ;

/*                             this.head_div.className='header_div' ;
                             this.body_div.className='body_div' ;

                             this.head_div.style.overflow='hidden' ;
                             this.head_tbody.parentNode.style.width = '0px';
                             this.head_tbody.parentNode.style.tableLayout = 'fixed' ;



                             this.body_tbody.parentNode.style.width = '0px';
                             this.body_tbody.parentNode.style.tableLayout = 'fixed' ;

                             this.body_div.style.overflow='auto' ;

                             // Let focus event pass thru
                             this.body_tbody.parentNode.style.MozUserFocus = 'ignore' ;
*/

                             //*this._resizeDiv() ;
                             this._setScrollCB() ;

                             var vthis = this ;

                             this.displayer.onclick = function(e) { return vthis._mouseClick(vthis.getEvent(e)) }
                             this.displayer.ondblclick = function(e) { return vthis._mouseDblClick(vthis.getEvent(e)) } ;
                             this.displayer.onmousedown = function(e) { return vthis._mouseDown(vthis.getEvent(e)) }
                             this.displayer.onmousemove = function(e) { return vthis._mouseMove(vthis.getEvent(e)) }
                             this.displayer.onmouseup   = function(e) { return vthis._mouseUp(vthis.getEvent(e)) }
                             this.displayer.onkeypress   = function(e) { return vthis._keyPress(vthis.getEvent(e)) }
                             //document.onselectstart = function(e) { _select_on = true ; }
                             //function(e) { return vthis._keyPress(vthis.getEvent(e)) }

                             this.addEventHandler(window, 'load', function() { vthis._load() ; } ) ;
                            }

AzkTableDisplayer.prototype._resizeDiv = function()
                            {
                             //**s this.body_div.style.height = (this.height - this.head_div.offsetHeight)+'px' ;
                             //alert(this.height+" "+this.head_div.offsetHeight) ;
                             //**s this.body_div.style.width = this.width+'px' ;

                             this.head_div.style.width = this.body_div.clientWidth+'px'  ;
                            }

AzkTableDisplayer.prototype._setScrollCB = function()
                            {
                             var vthis = this ;
                             this.body_div.onscroll = function() { vthis.head_div.scrollLeft = vthis.body_div.scrollLeft };
                            }


AzkTableDisplayer.prototype.onRowSelect = function(prow, pevent)
                            {
                             this.selectRow(prow, pevent) ;
                            }

AzkTableDisplayer.prototype.selectRow = function(prow, pevent)
                            {
                             select_cb = true ;

                             key_mod = 0 ;

                             if(pevent)
                             {
                              if(pevent.ctrlKey || e.metaKey)
                              {
                               key_mod = 'ctrlKey' ;
                              }
                             }

                             if(!this.multi_sel || ((this.multi_sel == AZK_TABLE_KEY_MULTI_SEL) && (key_mod!='ctrlKey') && pevent))
                             {         
                              if((this.cur_row != prow) || (this.multi_sel == AZK_TABLE_KEY_MULTI_SEL))
                              {
                               if(this.cur_row)
                               {
                                // Delete all selected
                                //S for(vsel in this.selection)
                                for(vsel in this.selection.items)
                                {
                                 //S vrow = this.getRowById(this.selection[vsel]) ;
                                 vrow = this.getRowById(this.selection.items[vsel]) ;
                                 // Correction quand surcharge object
                                 if(vrow)
                                 {
                                  this.changeRowClass(vrow, vrow.getAttribute('oldClassName')) ;
                                 }
                                }
                               }

                               //S this.selection = new Array() ;
                               this.selection.clearItems() ;
                               
                               this.cur_row = prow ;
                               
                               //S this.selection[prow.getAttribute('id')] = prow.getAttribute('id') ;
                               this.selection.set(prow.getAttribute('id'), prow.getAttribute('id')) ; 

                               this.cur_row.setAttribute('oldClassName', this.cur_row.className) ;

                               // Toggle classes
                               this.changeRowClass(prow, this.cur_row.getAttribute('oldClassName')) ;
                               this.changeRowClass(this.cur_row, this.cur_row.className?'selected_row':this.cur_row.className+' selected_row') ;
                               
                              }
                             }
                             else
                             {
                            
                              //trace('multi '+this.multi_sel+"="+AZK_TABLE_MULTI_SEL+" "+pevent)
                              this.cur_row = prow ;
                              if((this.multi_sel == AZK_TABLE_MULTI_SEL) || ((this.multi_sel == AZK_TABLE_KEY_MULTI_SEL) && !pevent) || ((this.multi_sel == AZK_TABLE_KEY_MULTI_SEL) && (key_mod=='ctrlKey')))
                              {
                               //trace('kikou')
                               //S if(this.selection[prow.getAttribute('id')] && this.cur_row.oldClassName)
                               
                               
                               if(this.selection.exists(prow.getAttribute('id')) /*&& this.cur_row.oldClassName*/)                                                              
                               {
                                
                                //if(this.multi_sel != AZK_TABLE_KEY_MULTI_SEL)
                                
                                 select_cb = false ;
                                // this.cur_row.oldClassName = null;
                                 
                                 
                                 this.changeRowClass(this.cur_row, this.cur_row.getAttribute('oldClassName')) ;
                                 //S delete(this.selection[prow.getAttribute('id')]) ;
                                 this.selection.remove(prow.getAttribute('id')) ;
                                
                               }
                               else
                               {
                             
                                //S this.selection[prow.getAttribute('id')] = prow.getAttribute('id') ;
                                this.selection.set(prow.getAttribute('id'), prow.getAttribute('id')) ;
                                
                                this.cur_row.setAttribute('oldClassName', this.cur_row.className) ;
                              
                               this.changeRowClass(this.cur_row, this.cur_row.className?(this.cur_row.className+' selected_row'):'selected_row') ;
                              
                               }
                              }
                             }

                             if(this.row_select_cb && select_cb)
                             {
                              this.row_select_cb(this.cur_row) ;
                             }

                             if(!this.is_ie && this.displayer.style.visibility != 'visible')
                             {
                              //this.focus() ; //** supprimer pour pb de perfo
                             }
                            }

AzkTableDisplayer.prototype.changeRowClass = function(prow, pclass)
                            {
                             prow.className = pclass ;
                             if(prow.header_row)
                             {
                              prow.header_row.className = pclass ; 
                             }
                            }


AzkTableDisplayer.prototype.getEvent = function(pevent)
                            {
                             return pevent?pevent:window.event ;
                            }

AzkTableDisplayer.prototype.getTarget = function(pevent)
                            {
                             e = this.getEvent(pevent) ;

                             return this.is_ie?e.srcElement:e.target ;
                            }


AzkTableDisplayer.prototype._setDragCursor = function(pevent)
                            {
                             ret = false ;
                             target = this.getTarget(pevent) ;

                             vtarget = target ;

                             if(target.parentNode.tagName == 'TD')
                             {
                              vtarget = target.parentNode ;
                             }

                             // Verify that we are on a TD
                             if(vtarget.tagName == 'TD')
                             {
                              x=0;

                              if(this.is_ie)
                              {
                               x = window.event.offsetX + this.body_div.scrollLeft ;
                              }
                              else
                              {
                               pos = getPosX(vtarget) ;
                               x = (pevent.clientX) - pos + document.body.scrollLeft + this.body_div.scrollLeft ;
                              }

                              //document.getElementById('debug').value = pevent.clientX+" "+x+"  - "+vtarget.clientWidth+" / "+vtarget.pageLeft+" "+(vtarget.offsetWidth-x)+" "+this.body_div.scrollLeft ;
                              //document.getElementById('debug').value = (vtarget.offsetWidth-x)+"  - "+vtarget.clientWidth+" / "+vtarget.offsetWidth+" "+vtarget.offsetLeft ;

                              if(((vtarget.offsetWidth-x) < this.drag_col_area_w) || this.start_drag)
                              {
                               target.style.cursor = 'e-resize';
                               ret = true ;
                              }
                              else
                              {
                               target.style.cursor = 'pointer';
                              }
                             }

                             return ret ;
                            }

AzkTableDisplayer.prototype._mouseClick = function(pevent)
                            {
                             //document.getElementById('debug').value = pevent.type+"\n"+document.getElementById('debug').value;
                             if(!this.cancel_click)
                             {
                              vtarget = this._getTDTarget(pevent) ;
                              if(vtarget.parentNode.parentNode == this.body_tbody)
                              {
                               this.onRowSelect(vtarget.parentNode, pevent) ;
                              }
                              else
                              {
                               if(vtarget.parentNode.parentNode == this.head_tbody)
                               {
                                this.sortRows(vtarget) ;
                               }
                              }
                             }

                             return true ;
                            }

AzkTableDisplayer.prototype._mouseDblClick = function(pevent)
                            {
                             if(!this.cancel_click)
                             {
                              vtarget = this._getTDTarget(pevent) ;

                              if(vtarget.parentNode.parentNode == this.body_tbody)
                              {
                               if(this.row_cbl_click_cb)
                               {
                                this.row_cbl_click_cb(this.cur_row) ;
                               }
                              }
                             }

                             return false ;
                            }


AzkTableDisplayer.prototype._mouseDown = function(pevent)
                            {
                             //document.onselectstart = function(e) { return false; }
                             var ret = true ;  
                              
                             if(this._setDragCursor(pevent))
                             {
                              this.start_drag = this._getTDTarget(pevent) ;
                             }

                             this.dragx = (pevent.pageX)?pevent.pageX:window.event.clientX + this.body_div.scrollLeft ;
                             
                             if(pevent)
                             {
                              if(pevent.ctrlKey || e.metaKey)
                              {
                               ret = false ;
                              }
                             }
                             
                             return ret ;
                            }

AzkTableDisplayer.prototype._mouseMove = function(pevent)
                            {
                             ret = false ;
                             drag_on = this._setDragCursor(pevent);

                             vtarget = this._getTDTarget(pevent) ;

                             //drag_on &= (vtarget == this.start_drag)

                             if(drag_on && this.start_drag)
                             {
                              x = (pevent.pageX)?pevent.pageX:window.event.clientX + this.body_div.scrollLeft ;

                              // Init width
                              if(!this.start_drag.style.width)
                              {
                               this.start_drag.style.width = this.start_drag.offsetWidth+'px' ;
                              }

                              fcell = this.start_drag.parentNode.parentNode.firstChild.childNodes[this.start_drag.cellIndex] ;
                              fcell.style.width = (parseInt(this.start_drag.style.width)-Number(this.dragx-x))+'px' ;
                              this.start_drag.style.width = fcell.style.width ;

                              this.dragx = x ;

                              //Resize the link div
                              linktab = null;
                              if(this.start_drag.parentNode.parentNode == this.body_tbody)
                              {
                               linktab = this.head_tbody ;
                              }
                              else
                              {
                               linktab = this.body_tbody ;
                              }

                              row = linktab.childNodes[0] ;
                              if(row)
                              {
                               idx = this.start_drag.cellIndex ;

                               row.childNodes[idx].style.width = fcell.style.width ;
                              }
                             }
                             else
                             {
                              //document.getElementById('debug').value = pevent.pageX ;
                              //* document.onselectstart = function(e) { return true; }
                              ret = true ;
                             }

                             return ret ;
                            }


AzkTableDisplayer.prototype._mouseUp = function(e)
                            {
                             //document.onselectstart = function(e) { return true; }
                             //alert(window.getSelection()) ;

                             this.cancel_click = this.start_drag ;

                             this.start_drag = false ;

                             return false ;
                            }
                            
AzkTableDisplayer.prototype._keyPress = function(e)
                            {
                             switch(e.keyCode)
                             {
                              case 40:
                               if(this.cur_row)
                               {
                                pnext = this.cur_row.nextSibling ;

                                if(pnext)
                                {
                                 pid = pnext.getAttribute('id') ;
                                 this.setSelection(pid, 1) ;
                                }
                               }
                              break
                              case 38:
                               if(this.cur_row)
                               {
                                pnext = this.cur_row.previousSibling ;

                                if(pnext)
                                {
                                 pid = pnext.getAttribute('id') ;
                                 this.setSelection(pid, 1) ;
                                }
                               }
                              break ;
                              case 13:
                               this.selectRow(this.cur_row) ;
                              break ;
                             }

                             return false ;
                            }

AzkTableDisplayer.prototype._getTDTarget = function(pevent)
                            {
                             ptarget = this.getTarget(pevent) ;

                             vtarget = ptarget ;

                             if(ptarget.parentNode.tagName == 'TD')
                             {
                              vtarget = ptarget.parentNode ;
                             }

                             return vtarget ;
                            }

AzkTableDisplayer.prototype._clearTextNode = function(pelem)
                            {
                             //Clear TextNode
                             for(idx in pelem.childNodes)
                             {
                              elem = pelem.childNodes[idx] ;
                              // clear text node
                              if(elem)
                              {
                               // TODO use constant Node.TEXT_NODE not working in ie
                               if(elem.nodeType==3)
                               {
                                pelem.removeChild(elem) ;
                               }
                              }
                             }
                            }

AzkTableDisplayer.prototype.sortRows = function(pheader_col, sdir)
                            {
                             col_num = pheader_col.cellIndex ;

                             stype = pheader_col.getAttribute("stype") ;

                             pheader_col.sdir = !pheader_col.sdir ;

                             rows = null ;
                             header = false ;
                             
                           
                             var div_table = document.getElementById(this.id);
                             var table = div_table.getElementsByTagName("table")[1];
                             var tableBody = table.getElementsByTagName("tbody")[0];
                             var tableRows = tableBody.getElementsByTagName("tr"); 
                             
                             /*for(vsel in this.selection.items)
                             {
	                             vrow = this.getRowById(this.selection.items[vsel]) ;
	                             alert(vrow);
	                             //this.changeRowClass(vrow, vrow.oldClassName) ;
	                            }*/
                             
                            
                             if(this.body_div.firstChild.firstChild)
                             {
                              rows = this.body_div.firstChild.firstChild.rows  ;
                         
                              if(this.row_header)
                              {
                               if((col_num==0) && (pheader_col.parentNode.parentNode.parentNode.parentNode.getAttribute('id')==this.id+"_header"))
                               {
                                header=true ;
                                rows = this.row_header_div.firstChild.firstChild.rows  ;
                               }
                              }
                              
                              compare_tab = new Array();

                              var l = rows.length ;
                              for(i=0;i<l;i++)
                              {
	                               col_content = this.datas[rows[i].id][pheader_col.id] ;
	
	                               if(!stype)
	                               {
	                                if(col_content!='')
	                                {
	                                 stype = !isNaN(col_content)?'number':'text' ;
	                                }
	                               }
	
	                               //col_content = col_content.replace(/[<>"\/= .]/g,'');
	
	                               if(stype=='number')
	                               {
	                                col_content*=1;
	                                if(isNaN(col_content))
	                                {
	                                 col_content = 0;
	                                }
	                               }
                                else
                                {
                                 col_content = col_content.toLowerCase() ;
                                 
                                 // Parse type
                                 if(vtype = this.cols[pheader_col.id][1])
                                 {
                                  if(!String.prototype.reverse)
                                  {
                                   String.prototype.reverse = function()
                                   {
                                    splitext = this.split("");
                                    revertext = splitext.reverse();
                                    reversed = revertext.join("");
                                    return reversed;
                                   }
                                  }
                                 
                                  col_content = col_content.reverse() ;
                                 }
                                }
	                                                                
	                               if(header)
	                               {
	                                compare_tab[i] = [rows[i].body_row, rows[i],col_content];
	                               }
	                               else
	                               {
	                                compare_tab[i] = [rows[i], null, col_content];
	                                if(this.row_header_div)
	                                {
	                                 compare_tab[i][1] = rows[i].header_row ;
	                                }
	                               }
	                               compare_tab[i].oldIndex = i; 
                              }
                              
                              
                             
                               if(sdir==undefined)
                              {
                               sdir = !pheader_col.sdir ;
                              }
                             
                              if(sdir)
                              {
                               compare_tab.sort(compare_up) ;
                              }
                              else
                              {
                               compare_tab.sort(compare_down) ;
                              }
                             
                             
                              
                              var l = compare_tab.length ;
                              /*for(i=0; i<l; i++)
                              {
                               this.body_div.firstChild.firstChild.insertBefore(compare_tab[i][0], this.body_div.firstChild.firstChild.rows[i]);

                               if(this.row_header_div)
                               {
                                this.row_header_div.firstChild.firstChild.insertBefore(compare_tab[i][1], this.row_header_div.firstChild.firstChild.rows[i]);
                               }
                              }*/
                              
                              // Create a new tbody and copy old rows
                              // using the sorted index 
                              var sortedTableBody = document.createElement("tbody");
                              for (var i=0; i < l; i++)
                              {
                               sortedTableBody.appendChild(tableRows[compare_tab[i].oldIndex].cloneNode(true));
                              }
                              // Replace old table with new one
                               
                             
                              table.replaceChild(sortedTableBody, tableBody);
                              
                              this.body_tbody = sortedTableBody ;
                             
                              // Set first row cols width
                              if(this.body_tbody.firstChild)
                              {
                               hcols = this.head_tbody.firstChild.childNodes ;
                               cols = this.body_tbody.firstChild.childNodes ;
                               for(col in cols)
                               {
                                if(hcols[col].style && hcols[col].style.width)
                                {
                                 cols[col].style.width = hcols[col].style.width ;
                                }
                               }
                              }
  
                             //alert(this.selection.length);
                             }
                             
                             window.status = "";
                            }

AzkTableDisplayer.prototype.addEventHandler = function(pobject, paction, pfct)
                            {
                             if(this.is_ie)
                             {
                              if(navigator.platform=='Win32')
                              {
                               pobject.attachEvent("on"+paction, pfct);
                              }
                              else
                              {
                               var old_fcts = eval("pobject.on"+paction) ? eval("pobject.on"+paction) : function () {};
                               eval("pobject.on"+paction+" = function () {old_fcts(); pfct()}") ;
                              }
                             }
                             else
                             {
                              pobject.addEventListener(paction, pfct, true );
                             }
                            }

AzkTableDisplayer.prototype._load = function(pobject, paction, pfct)
                            {
                             this.init = false ;

                             //**this._resizeDiv() ;

                             if(this.pre_selection && this.pre_selection.length && this.body_div.firstChild.firstChild.rows)
                             {
                              this.setSelection(this.pre_selection) ;
                             }
                             
                             // Cacul des borders
                             var elem = this.body_div.parentNode ;

                             var vborder = parseInt(this.getStyle(elem, 'border-top-width'), 10) + parseInt(this.getStyle(elem, 'border-bottom-width'), 10) ;
                             this.body_div.style.height = (this.displayer.offsetHeight - this.head_div.offsetHeight - vborder)+'px' ;
                             this.head_div.style.width = this.body_div.clientWidth+'px'
                             
                             this.displayer.style.visibility = 'visible' ;
                            }

AzkTableDisplayer.prototype.isEmpty = function()
                            {
                             ret = false ;

                             if(this.body_div.firstChild.rows)
                             {
                              ret = this.body_div.firstChild.rows.length ;
                             }

                             return !ret ;
                            }

AzkTableDisplayer.prototype.getRows = function()
                            {
                             return this.body_div.firstChild.rows ;
                            }

AzkTableDisplayer.prototype.focus = function()
                            {
                             this.displayer.setAttribute('tabindex', "-1") ;
                             this.displayer.focus() ;
                            }

AzkTableDisplayer.prototype.getStyle = function(el,styleProp)
                            {
							 if(this.is_ie)
							 {
							  var parts = styleProp.split('-') ;
							  if(l = parts.length)
							  {
							   for(idx=1; idx<l; idx++)
							   {
								parts[idx] = parts[idx].replace(/\b[a-z]/g, function(w){trace(w); return w.toUpperCase()});

								styleProp = parts.join('') ;
							   }							   
							  }
							 }
							
                             var ret = null ;
                             if(el.currentStyle)
                             {
                              ret = el.currentStyle[styleProp];
                             }
                             else 
                             {
                              if(window.getComputedStyle)
                              {
                               ret = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
                              }
                             }
                             return ret ;
                            }
                            