﻿// (c) Lachlan Keown 2007

function cellRowIndex(aCell) {
    return aCell.parentNode.rowIndex;
}

function trueCellIndex(aCell) {
    //var row = aCell.parentNode;

    var rowCells = aCell.parentNode.cells;
    for (var i = 0;i < rowCells.length;i++) {
        if (rowCells[i] == aCell) {
            return i;
        }
    }

}

function cellAbove(aCell) {
    //Compute cell immediately above current, or null if at top
    var rowIdx = cellRowIndex(aCell);
    if (rowIdx == 0 ) { return null;}

    var table = aCell.parentNode.parentNode;

    var row = table.rows[rowIdx - 1]

    var tci = trueCellIndex(aCell);
    for (var i = 0;i < row.cells.length;i++) {
        if (i == tci) {
            return row.cells[i];
        }
    }
    //return table.rows[rowIdx - 1].cells[trueCellIndex(aCell)];
}

function cellBelow(aCell) {
    //Compute cell immediately above current, or null if at top
    var rowIdx = cellRowIndex(aCell);
    //if (rowIdx == aCell.parentNode.parentNode.rows.count - 1) { return null;}

    var table = aCell.parentNode.parentNode;

    var row = table.rows[rowIdx + 1]
    var rowCells = row.cells;
    var tci = trueCellIndex(aCell);
    return rowCells[tci];
    /*
    for (var i = 0;i < rowCells.length;i++) {
        if (i == tci) {
            return rowCells[i];
        }
    }
    */
}

function cellLeft(aCell) {
    var row = aCell.parentNode;

    for (var i = 0;i < row.cells.length;i++) {
        if (i == trueCellIndex(aCell) - 1) {
            return row.cells[i];
        }
    }
    //return row.cells[aCell.cellIndex - 1];

}


function makeRowSpanBetweenCells(cell1, cell2, useBackgroundImg) {
    ////try {

    var startCell;
    var currCell;
    var down = true;
    var crI1 = cellRowIndex(cell1);
    var crI2 = cellRowIndex(cell2);
    var crICurr = crI2; // cellRowIndex(currCell);

    if (crI1 < crI2) {
        startCell = cellBelow(cell1);
        crICurr = crI1 + 1;
    } else {
        //startCell = cellAbove(cell2);
        startCell = cellLeft(cell2);
        down = false; // going up!
    }


    //startCell.innerHTML = "S";

    // Delete cells going up or down until we hit cell.rowIndex
    currCell = startCell;
    var colSpan = 1;


    while ((crICurr != crI2 && down) || (!down && crICurr != crI1 - 1)) {
        // Move currCell up or down one cell

        var nextCell;

        nextCell = cellBelow(currCell);


        // Delete currCell if not startCell
        if (currCell != startCell) {
            //currCell.parentNode.deleteCell(currCell.cellIndex);
            currCell.style.display = "none";
            //currCell.width = "1px";
            //alert("hi");
        }

        currCell = nextCell;
        colSpan += 1;

        //crICurr = cellRowIndex(currCell);
        crICurr += 1;
    }

    if (colSpan > 1) {
        // Delete currCell too
        //currCell.parentNode.deleteCell(currCell.cellIndex);
        currCell.style.display = "none";
        //currCell.width = "1px";
        //alert("hi");
    }

    // Set image size
    startCell.rowSpan = colSpan;

    /*
    var prop = "padding-left"; // Moz
    if (document.all) {
        prop = "paddingLeft"; // IE
    }
    */
    var padLeft = 0; // NO LONGER DOING THIS - VERY SLOW IN FIREFOX !! //parseInt(getStyle(startCell, prop));
    /*
    if (isNaN(padLeft)) {
        padLeft = 0;
    } */

    var buff = new StringBuffer();
    if (useBackgroundImg) {
        // Uses cache in IE, and won't alter table cell widths, but not printable :(
        //var img = "url(diagLine.php?width=" + (startCell.clientWidth - (padLeft * 2)) + "&height=" + startCell.clientHeight;
        buff.append("url(diagLine.php?width=");
        buff.append((startCell.clientWidth - (padLeft * 2)));
        buff.append("&height=");
        buff.append(startCell.clientHeight);
    }else{
        // Printable, but doesn't use cache in IE so slower to load first time.
        //var img = "<IMG src=diagLine.php?width=" + (startCell.clientWidth - (padLeft * 2)) + "&height=" + startCell.clientHeight;
        buff.append("<IMG src=diagLine.php?width=");
        buff.append((startCell.clientWidth - (padLeft * 2)));
        buff.append("&height=");
        buff.append(startCell.clientHeight);
    }

    //img += "&rowSpan=" + colSpan;
    buff.append("&rowSpan=");
    buff.append(colSpan);

    if (crI1 < crI2) {
        //img += "&up=false";
        buff.append("&up=false");
        startCell.title = "Pollen Parent";
        //startCell.style.verticalAlign = "top";
    } else {
        //img += "&up=true";
        buff.append("&up=true");
        startCell.title = "Seed Parent";
        //startCell.style.verticalAlign = "bottom";
    }

    if (useBackgroundImg) {
        //img += ")";
        buff.append(")");
        startCell.style.backgroundImage= buff.toString(); // img;
    } else {
        buff.append(" />");
        //img += " />";
        startCell.innerHTML = buff.toString(); // img;
    }



    //alert(img);


   //// } catch (ex) {
   ////     alert(ex);
   //// }
}

function getStyle(el,styleProp)
{
	var x = el; //document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

/**
 *
 * @access public
 * @return void
 **/
function StringBuffer(){
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string){
    this.buffer.push(string);
    return this;
}

StringBuffer.prototype.toString = function toString() {
    return this.buffer.join("");
}



function makeRowSpanBetweenCellsOLD(cell1, cell2) {
    try {
    var startCell;
    var currCell;
    var down = true;

    if (cellRowIndex(cell1) < cellRowIndex(cell2)) {
        startCell = cellBelow(cell1);
    } else {
        //startCell = cellAbove(cell2);
        startCell = cellLeft(cell2);
        down = false; // going up!
    }

    startCell.innerHTML = "S";

    // Delete cells going up or down until we hit cell.rowIndex
    currCell = startCell;
    var colSpan = 1;
    while ((cellRowIndex(currCell) != cellRowIndex(cell2) && down) || (!down && cellRowIndex(currCell) != cellRowIndex(cell1) - 1)) {
        // Move currCell up or down one cell

        var nextCell;

        nextCell = cellBelow(currCell);


        // Delete currCell if not startCell
        if (currCell != startCell) {
            //currCell.parentNode.deleteCell(currCell.cellIndex);
            currCell.style.display = "none";
            //currCell.width = "1px";
            //alert("hi");
        }

        currCell = nextCell;
        colSpan += 1;
    }

    if (colSpan > 1) {
        // Delete currCell too
        //currCell.parentNode.deleteCell(currCell.cellIndex);
        currCell.style.display = "none";
        //currCell.width = "1px";
        //alert("hi");
    }

    // Set image size
    startCell.rowSpan = colSpan;
    var img = "<IMG width=" + startCell.clientWidth + "px";

    if (colSpan > 1) {
        img += " height=" + (startCell.clientHeight * 0.95) + "px ";
    }else{
        img += " height=" + (startCell.clientHeight * 0.6) + "px ";
    }
    if (cellRowIndex(cell1) < cellRowIndex(cell2)) {
        img += " src=diagonalDown.jpg />";
        startCell.style.verticalAlign = "top";
    } else {
        img += " src=diagonalUp.jpg />";
        startCell.style.verticalAlign = "bottom";
    }
    startCell.innerHTML = img;

    } catch (ex) {
        alert(ex);
    }
}

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*-"; // +/";

function numToBase64(number, strLen){
    var output = "";

    while(strLen > 0){
        var idx = (number & (63 << (6 * (strLen - 1)))) >> (6 * (strLen - 1));
        //alert(idx);
        output += chars.charAt(idx);
        strLen--;

    } // while

    return output;
}