// ===================================================================
// Author: Denis Howlett <feedback@isocra.com>
// WWW: http://www.isocra.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however we
// would appreciate it if at least the web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download.
// If you wish to share this code with others, please just point them
// to the URL instead.
//
// Please DO NOT link directly to this .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

// This is just to code that runs the demo page

var table = document.getElementById('table-1');
var tableDnD = new TableDnD();
// Redefine the findDropTargetRow to put some debug information in
tableDnD.findDropTargetRow = function(y) {
    var rows = this.table.tBodies[0].rows;
	var debugStr = "y is "+y+"<br>";
    for (var i=0; i<rows.length; i++) {
        var row = rows[i];
        var rowY    = this.getPosition(row).y;
        var rowHeight = parseInt(row.offsetHeight)/2;
		if (row.offsetHeight == 0) {
			rowY = this.getPosition(row.firstChild).y;
			rowHeight = parseInt(row.firstChild.offsetHeight)/2;
		}
		debugStr += "row["+i+"] between "+(rowY-rowHeight)+' and '+(rowY+rowHeight)+"<br>";
        // Because we always have to insert before, we need to offset the height a bit
        if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
            // that's the row we're over
			document.getElementById('debug').innerHTML = debugStr+"found row";
            return row;
        }
    }
	document.getElementById('debug').innerHTML = debugStr+"no matching row";
    return null;
}
// Redefine the onDrop so that we can display something
tableDnD.onDrop = function(table, row) {
    var rows = this.table.tBodies[0].rows;
	var debugStr = "rows now: ";
    for (var i=0; i<rows.length; i++) {
		debugStr += rows[i].id+" ";
	}
	document.getElementById('debug').innerHTML = 'row['+row.id+'] dropped<br>'+debugStr;
}

tableDnD.init(table);

// And now for the second table
var table2 = document.getElementById('table-2');
var tableDnD2 = new TableDnD();
tableDnD2.init(table2);
