var divContent = null;
var divHeaderRow = null;
var divHeaderColumn = null;
var divHeaderRowColumn = null;
var headerRowFirstColumn = null;
var x;
var y;
var horizontal = false;
var vertical = false;

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// Copy table to top and to left
function CreateScrollHeader(content, scrollHorizontal, scrollVertical)
{
    horizontal = scrollHorizontal;
    vertical = scrollVertical;
    divContent = content;
    
    if(BrowserDetect.browser == "Konqueror") {
        window.onload = CreateScrollHeaderActual;    
    } else {
        CreateScrollHeaderActual();
    }    
}

function CreateScrollHeaderActual() {
    if (divContent != null)
    {
        
        var widthCosmeticAdd = 0;
        var heightCosmeticAdd = 0;

        // browser specific cosmetic tweaks    
        if (BrowserDetect.browser == "Explorer") {
            widthCosmeticAdd = 2;
            heightCosmeticAdd = 2;
        } else if(BrowserDetect.browser == "Opera" || BrowserDetect.browser == "Konqueror" || BrowserDetect.browser == "Safari") {
            widthCosmeticAdd = 1;
            heightCosmeticAdd = 1;        
        }    
		//added so there is a border between what is scrolled.
        widthCosmeticAdd = widthCosmeticAdd + 1; 
		heightCosmeticAdd = heightCosmeticAdd + 1;
		//**
		
        var originalTable = findFirstElementByType(divContent, 'table');
        var headerRow = findFirstElementByType(originalTable, 'thead');

        x = originalTable.offsetWidth;
        y = originalTable.offsetHeight;        
        
        divHeaderRow = divContent.cloneNode(true);        
        if (horizontal)
        {
            divHeaderRow.style.height = (headerRow.offsetHeight + heightCosmeticAdd) + 'px';
            divHeaderRow.style.overflow = "hidden";
            
            divContent.parentNode.insertBefore(divHeaderRow, divContent);
            originalTable.style.position = "absolute";
            originalTable.style.top = "-" + (headerRow.offsetHeight + heightCosmeticAdd) + 'px';
            

            y = y - headerRow.offsetHeight;
        }

        divHeaderRowColumn = divHeaderRow.cloneNode(true);            
        headerRowFirstColumn = findFirstElementByType(findFirstElementByType(headerRow,'tr'), 'th');
        divHeaderColumn = divContent.cloneNode(true);
        divContent.style.position = "relative";
        if (vertical)
        {
            divContent.parentNode.insertBefore(divHeaderColumn, divContent);
            divContent.style.left = headerRowFirstColumn.offsetWidth + 'px';
            
            originalTable.style.position = "absolute";
            originalTable.style.left = "-" + headerRowFirstColumn.offsetWidth + 'px';
        }
        else
        {
            divContent.style.left = "0px";
        }

        if (vertical)
        {
            divHeaderColumn.style.width = (headerRowFirstColumn.offsetWidth + widthCosmeticAdd) + 'px';
            divHeaderColumn.style.overflow = "hidden";
            divHeaderColumn.style.zIndex = "99";
            
            divHeaderColumn.style.position = "absolute";
            divHeaderColumn.style.left = "0px";
            divHeaderColumn.style.top = (headerRow.offsetHeight + heightCosmeticAdd) + "px";
            addScrollSynchronization(divHeaderColumn, divContent, "vertical");
            x = x - headerRowFirstColumn.offsetWidth;
        }

        if (horizontal)
        {
            if (vertical)
            {
                divContent.parentNode.insertBefore(divHeaderRowColumn, divContent);
            }
            divHeaderRowColumn.style.position = "absolute";
            divHeaderRowColumn.style.left = "0px";
            divHeaderRowColumn.style.top = "0px";
            divHeaderRowColumn.style.width = (headerRowFirstColumn.offsetWidth + widthCosmeticAdd) + 'px';
            divHeaderRowColumn.overflow = "hidden";
            divHeaderRowColumn.style.zIndex = "100";
            divHeaderRowColumn.style.backgroundColor = "#ffffff";
            
        }
        
        if (horizontal)
        {
            addScrollSynchronization(divHeaderRow, divContent, "horizontal");
        }

        if (horizontal || vertical)
        {
            window.onresize = ResizeScrollArea;
            ResizeScrollArea();
        }
    }
}

function findFirstElementByType(startNode, search) {
    if (! startNode.hasChildNodes()) return null;
    
    var children = startNode.childNodes;
    var i;
    for (i = 0; i < children.length; i ++) {
        if (children[i].nodeName.toUpperCase() == search.toUpperCase()) {
            return children[i];
        }
    }
}

// Resize scroll area to window size.
function ResizeScrollArea()
{
    var height = getInnerHeight() - findPosY(divHeaderRow) - 75;

    if (!vertical)
    {
        height -= divHeaderRow.offsetHeight;
    }
	//var width = getInnerWidth() - 50;
	var width = 910;
    if (!horizontal)
    {
        width -= divHeaderColumn.offsetWidth;
    }
    var headerRowsWidth = 0;
//    divContent.childNodes[0].style.width = x + 'px';
//    divContent.childNodes[0].style.height = y + 'px';

    if (divHeaderRowColumn != null)
    {
        headerRowsWidth = divHeaderRowColumn.offsetWidth;
    }


    // width
    if (findFirstElementByType(divContent, 'table').offsetWidth > width)
    {
        divContent.style.width = Math.max(width - headerRowsWidth, 0) + 'px';
        divContent.style.overflowX = "scroll";
        divContent.style.overflowY = "auto";
    }
    else
    {
        divContent.style.width = x + 'px';
        divContent.style.overflowX = "auto";
        divContent.style.overflowY = "auto";
    }

    if (divHeaderRow != null)
    {
        divHeaderRow.style.width = (divContent.offsetWidth + headerRowsWidth) + 'px';
    }

    // height
    if (findFirstElementByType(divContent, 'table').offsetHeight > height)
    {
        divContent.style.height = Math.max(height, 80) + 'px';
        divContent.style.overflowY = "scroll";
    }
    else
    {
        divContent.style.height = y + 'px';
        divContent.style.overflowY = "hidden";
    }
    if (divHeaderColumn != null)
    {
        divHeaderColumn.style.height = divContent.offsetHeight + 'px';
    }

    // check scrollbars
    if (divContent.style.overflowY == "scroll")
    {
        divContent.style.width = (divContent.offsetWidth + 17) + 'px';
    }
    if (divContent.style.overflowX == "scroll")
    {
        divContent.style.height = (divContent.offsetHeight + 17) + 'px';
    }

    divContent.parentNode.style.width = (divContent.offsetWidth + headerRowsWidth) + 'px';
}

// next two functions from quirksmode.org

function getInnerHeight() {
    var y;
    if (self.innerHeight) // all except Explorer
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
        // Explorer 6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
        y = document.body.clientHeight;
    }
    return y;
}

function getInnerWidth() {
    var x;
    if (self.innerWidth) // all except Explorer
    {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
        // Explorer 6 Strict Mode
    {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) // other Explorers
    {
        x = document.body.clientWidth;
    }
    return x;
}


// ********************************************************************************
// Synchronize div elements when scrolling
// from http://webfx.eae.net/dhtml/syncscroll/syncscroll.html
// ********************************************************************************
// This is a function that returns a function that is used
// in the event listener
function getOnScrollFunction(oElement, srcElement) {
    return function () {
        if (oElement._scrollSyncDirection == "horizontal" || oElement._scrollSyncDirection == "both")
            oElement.scrollLeft = srcElement.scrollLeft;
        if (oElement._scrollSyncDirection == "vertical" || oElement._scrollSyncDirection == "both")
            oElement.scrollTop = srcElement.scrollTop;
    };

}

// This function adds scroll syncronization for the fromElement to the toElement
// this means that the fromElement will be updated when the toElement is scrolled
function addScrollSynchronization(fromElement, toElement, direction) {
    removeScrollSynchronization(fromElement);
    
    fromElement._syncScroll = getOnScrollFunction(fromElement, toElement);
    fromElement._scrollSyncDirection = direction;
    fromElement._syncTo = toElement;
    if (toElement.addEventListener) {
        toElement.addEventListener("scroll", fromElement._syncScroll, false);
    } else {
        toElement.attachEvent("onscroll", fromElement._syncScroll);
    }
}

// removes the scroll synchronization for an element
function removeScrollSynchronization(fromElement) {
    if (fromElement._syncTo != null) {
        if (fromElement._syncTo.removeEventListener) {
            fromElement._syncTo.removeEventListener("scroll", fromElement._syncScroll, false);
        } else {
            fromElement._syncTo.detachEvent("onscroll", fromElement._syncScroll);
        }
    }

    fromElement._syncTo = null;
    fromElement._syncScroll = null;
    fromElement._scrollSyncDirection = null;
}

// browser detection routines from quirksmode.org
var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++)    {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {     string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {        // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {         // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
BrowserDetect.init();
