var oCanvas;
var aVarElements;
var nTimerId = null;
var oCanvasAlign = {ha:'center', hc:0, va:'middle', vc:0};
var aCustomFunctions = new Array();

function getCanvas()
{
	if (document.width) this.w = document.width;
	else if (window.innerWidth) this.w = window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) this.w = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth) this.w = document.body.clientWidth;
	else this.w = 0;

	if (document.height) this.h = document.height;
	else if (window.innerHeight) this.h = window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) this.h = document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight) this.h = document.body.clientHeight;
	else this.h = 0;
	return this;
}

function setCanvas()
{
    var nCanvasLeft;
    var nCanvasTop;
    var nCanvasWidth;
    var nCanvasHeight;
    var oCurrCanvas;
    var nIndex;

    if (!oCanvas) return;

    oCurrCanvas = new getCanvas();

    nCanvasLeft = 0;
    nCanvasTop = 0;

    nCanvasWidth = getAbsoluteWidth(oCanvas);
    nCanvasHeight = getAbsoluteHeight(oCanvas);

    switch(oCanvasAlign.ha)
    {
        case 'left':
            nCanvasLeft = 0;
            break;
        case 'right':
            nCanvasLeft = oCurrCanvas.w - nCanvasWidth ;
            break;
        case 'center':
            nCanvasLeft = Math.round((oCurrCanvas.w - nCanvasWidth ) / 2);
            break;
    }

    switch(oCanvasAlign.va)
    {
        case 'top':
            nCanvasTop = 0;
            break;
        case 'bottom':
            nCanvasTop = oCurrCanvas.h - nCanvasHeight;
            break;
        case 'middle':
            nCanvasTop = Math.round((oCurrCanvas.h - nCanvasHeight) / 2);
            break;
    }


    nCanvasLeft += oCanvasAlign.hc;
    nCanvasTop += oCanvasAlign.vc;

    setStyleAttribute(oCanvas, 'left', nCanvasLeft + 'px');
    setStyleAttribute(oCanvas, 'top', nCanvasTop + 'px');

    for (nIndex=0; nIndex < aCustomFunctions.length; nIndex++)
    {
        eval(aCustomFunctions[nIndex] + '();');
    }
}

function setCanvasAlignment(sHAlign, sHCorrection, sVAlign, sVCorrection)
{
    if (typeof(sHAlign) != 'undefined')
    {
        sHAlign = sHAlign.toLowerCase();
        if (typeof(sHCorrection) == 'undefined') sHCorrection = 0;
        switch(sHAlign)
        {
            case 'left':
            case 'right':
            case 'center':
                oCanvasAlign.ha = sHAlign;
                oCanvasAlign.hc = sHCorrection;
                break;

        }
    }
    if (typeof(sVAlign) != 'undefined')
    {
        sVAlign = sVAlign.toLowerCase();
        if (typeof(sVCorrection) == 'undefined') sVCorrection = 0;
        switch(sVAlign)
        {
            case 'top':
            case 'bottom':
            case 'middle':
                oCanvasAlign.va = sVAlign;
                oCanvasAlign.vc = sVCorrection;
                break;
        }
    }

}

function addCustomFunction(sFunctionName)
{
    aCustomFunctions.push(sFunctionName)
}

function monitorCanvas()
{
    if (nTimerId != null)
    {
        clearTimeout(nTimerId);
        nTimerId = null;
    }
    setTimeout('unMonitorCanvas();', 200, 'Javascript');
}

function unMonitorCanvas()
{
        clearTimeout(nTimerId);
        nTimerId = null;
        setCanvas();
}

function initCanvas()
{
    oCanvas = document.getElementById('canvas');
    setCanvas();
    setStyleAttribute(oCanvas, 'visibility', 'visible');
    window.onresize = monitorCanvas;
}
