var scrollerOldOnload = window.onload;

window.onload = function()
{
    if (typeof scrollerOldOnload == 'function')
    {
         scrollerOldOnload();
    }

    window.scrollerInstance = new scroller();
    scrollerInstance.init();

}



function scroller()
{
    this.box = null;
    this.element = null;
    this.scrollInterval = 20; // ms

    this.paused = false;

}

scroller.prototype.init = function()
{
    // locate elements
    this.box = document.getElementById('slidingTextBox');
    if (!this.box)
    {
        return null;
    }
    var divs = this.box.getElementsByTagName('div');
    if (!divs)
    {
        return null;
    }
    this.element = divs[0];
    if (!this.element)
    {
        return null;
    }

    var self = this;
    this.box.onmouseover = function()
    {
        self.pauseScroll();
    }
    this.box.onmouseout = function()
    {
        self.resumeScroll();
    }
    // set initial styles

    this.box.style.overflow = 'hidden';
	this.element.style.position = 'absolute';

	this.resizeElementToOneLine();


	//return null;


	this.boxWidth      = parseInt(this.box.offsetWidth);
    this.elementWidth  = parseInt(this.element.offsetWidth);
//   alert (this.elementWidth);

    this.leftPos = this.boxWidth;

    this.element.style.visibility = 'visible';


    this.moveElement( this.leftPos );

    // this.doScroll();
    setInterval('window.scrollerInstance.doScroll()', this.scrollInterval);
}

scroller.prototype.doScroll = function()
{
    if (this.paused)
    {
        return;
    }
	if(this.leftPos <= this.elementWidth * -1)
	{
		this.leftPos = this.boxWidth;
	}

	this.moveElement(this.leftPos);
    this.leftPos--;

}

scroller.prototype.moveElement = function ( pos )
{
    this.element.style.left = this.addPx(pos);
}

scroller.prototype.addPx = function ( number )
{
    return number.toString().concat('px');
}

scroller.prototype.pauseScroll = function()
{
    this.paused = true;
}

scroller.prototype.resumeScroll = function()
{
    this.paused = false;
}

scroller.prototype.resizeElementToOneLine = function()
{


    var megaSize = 32000;
    var acceptableRange = 50;
    var maxIterations = 10;

    // make the element ridiculously wide
    this.element.style.width = this.addPx(megaSize);

    // assume the content now is in one line
    var oneLineHeight = this.element.offsetHeight;

    // find the smallest width that still has the height of one line

    this.log('start');

    var currentTestRange = megaSize;
    var nextTestRange;

    var testHeight;
    var testWidth;

    var offset = 0;

    var sizeFound = false;
    var oneLineWidth = 0;
    var lastFit;

    var safety = 0;
    var iteration = 0;

    this.log('oneLineHeight: '.concat(oneLineHeight));

    while ((!sizeFound) && (iteration < maxIterations))
    {
        iteration++;

        this.log('\ncycle: '.concat(safety));

        // divide current range in half
        // nextTestRange = Math.round(currentTestRange / 2);
        nextTestRange = parseInt(currentTestRange / 2);

        this.log('currentTestRange: '.concat(currentTestRange) );
        this.log('nextTestRange: '.concat(nextTestRange) );
        this.log('offset: '.concat(offset) );

        // set width of text to the half of current range + offset
        testWidth = offset + nextTestRange;
        this.element.style.width = this.addPx(testWidth);
        testHeight = this.element.offsetHeight;

        this.log('testWidth: '.concat(testWidth));
        this.log('testHeight: '.concat(testHeight));

        // check how high the element is at this width
        if (testHeight <= oneLineHeight)
        {
            this.log('fits');
            // fits in one line
            lastFit = testWidth;
            if (nextTestRange <= acceptableRange)
            {
                this.log('within acceptable range');
                sizeFound = true;
                oneLineWidth = testWidth;
            }
            else
            {
                // continue splitting in half
                currentTestRange = nextTestRange;
            }
        }
        else
        {
            // does not fit in one line, try a smaller range
            this.log('does not fit');

            offset += nextTestRange;
            currentTestRange = currentTestRange - nextTestRange;
        }
    }
    if (!sizeFound)
    {
        this.log('\ndid not find in '.concat(maxIterations).concat(' passes. using lastFit: ').concat(lastFit));
        oneLineWidth = lastFit;
    }


    this.log('\noneLineWidth: '.concat(oneLineWidth));
    this.element.style.width = this.addPx(oneLineWidth);
    // alert (oneLineHeight);

}


scroller.prototype.log = function (msg)
{
    return null; // disable the function
    if (!this.logBox)
    {
        this.logBox = document.createElement('textarea');

        document.body.appendChild( this.logBox );

        this.logBox.style.position = 'absolute';
        this.logBox.style.width = '500px';
        this.logBox.style.height = '300px';
        this.logBox.style.top = '300px';
        this.logBox.style.left = '0px';
    }

    this.logBox.value = this.logBox.value.concat(msg, "\n");

}
