/*****************************************************************************/
/*** MegaScroller - Class						   ***/
/***-----------------------------------------------------------------------***/
/*** (C) 2008 Bauer Softwaredesign und Entwicklung			   ***/
/*****************************************************************************/
function CMegaScroller(par_objName, par_divId)
{
    var objName = "";
    var divId = "";

    var blockWidth;
    var blockHeight;
    var scrollWidth;
    var clipLeft, clipRight;
    var margin = 25;
    var scrollOffset = 250 - margin;
    var firstvisibleblock = 0;
    var baseBlock = 0;
    var timer = 0;
    var pixPerCycle = 4;
    var pixPerCycleFast = 8;
	    
    var pixToMove = pixPerCycle;
    var timeToMove = 10;
    var bInitialized = false;
    var objEl;    
    
    // Init
    this.objName = par_objName;
    this.divId = par_divId;
    this.Init();
}

CMegaScroller.prototype.Init = function()
{
    this.bInitialized = false;
    this.blockWidth = 280;
    this.blockHeight = 165;
    this.margin = 0;
    this.scrollOffset = -this.margin;
    this.baseBlock = 0;
    this.firstvisibleblock = 0;
    this.timer = 0;
    this.pixPerCycle = 4;
    this.pixPerCycleFast = 8;
    this.pixToMove = this.pixPerCycle;
    this.timeToMove = 10;
    this.scrollWidth = 730;

    this.objEl = document.getElementById(this.divId);
    this.objEl.style.left = -this.scrollOffset + "px";
    this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";

    this.InsertContent(this.baseBlock);
    this.bInitialized = true;
}

/* Erzeuge Content des Scrollers beginnend mit startblock */
CMegaScroller.prototype.InsertContent = function(startblock)
{
    newHTML = '';
    for(i=startblock; i<startblock+4; i++)
    {
        if(i < content.length) newHTML += content[i];
    }
    this.objEl.innerHTML = newHTML;
}

CMegaScroller.prototype.mvRightStart = function()
{
    if(this.bInitialized) this.timer = setInterval(this.objName + ".mvToRight()", this.timeToMove);
}

CMegaScroller.prototype.mvRightStop = function()
{
    clearTimeout(this.timer);
}

CMegaScroller.prototype.mvLeftStart = function()
{
    if(this.bInitialized) this.timer = setInterval(this.objName + ".mvToLeft()", this.timeToMove);
}

CMegaScroller.prototype.mvLeftStop = function()
{
    clearTimeout(this.timer);
}

CMegaScroller.prototype.mvFaster = function()
{
    this.pixToMove = this.pixPerCycleFast;
}

CMegaScroller.prototype.mvNormal = function()
{	    
    this.pixToMove = this.pixPerCycle;
}
	    
CMegaScroller.prototype.mvToRight = function()
{
    if((this.scrollOffset+this.margin) + (this.baseBlock*this.blockWidth) >= 20 + (content.length*this.blockWidth) - (this.scrollWidth + this.margin)/*750*/)	// Anzahl gleichzeitig darstellbarer Elemente
    {
        return;
    }

    this.scrollOffset += this.pixToMove;
    currblock = Math.floor((this.scrollOffset+this.margin) / this.blockWidth) + this.baseBlock;
    if(currblock != this.firstvisibleblock)
    {
        // ein Element ist ausserhalb des sichtbaren Bereichs...
        //alert("first visible element changed to: " + currblock);
        this.firstvisibleblock = currblock;
	this.InsertContent(currblock);
    		    		
	// Korrigiere Offset
	this.scrollOffset -= this.blockWidth;
	this.baseBlock++;
	this.objEl.style.left = -this.scrollOffset + "px";
	this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";
    }

    this.objEl.style.left = -this.scrollOffset + "px";
    this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";
}

CMegaScroller.prototype.mvToLeft = function()
{
    if((this.scrollOffset+this.margin) + (this.baseBlock*this.blockWidth) <= 0)
    {
	this.scrollOffset = -this.margin + this.pixToMove;
	this.baseBlock = 0;
	//this.objEl.style.left = -this.scrollOffset + "px";
	//this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";
    }

    this.scrollOffset -= this.pixToMove;
    currblock = Math.floor((this.scrollOffset+this.margin) / this.blockWidth) + this.baseBlock;
    if(currblock != this.firstvisibleblock)
    {
        // ein Element ist ausserhalb des sichtbaren Bereichs...
        //alert("first visible element changed to: " + currblock);
        this.firstvisibleblock = currblock;
        this.InsertContent(currblock);
		    		
        // Korrigiere Offset
        this.scrollOffset += this.blockWidth;
        this.baseBlock--;
        this.objEl.style.left = -this.scrollOffset + "px";
        this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";
    }
    this.objEl.style.left = -this.scrollOffset + "px";
    this.objEl.style.clip = "rect(0px, " + (this.scrollOffset+this.scrollWidth+this.margin) + "px, " + this.blockHeight + "px, " + (this.scrollOffset+this.margin) + "px)";
}
