var upIntervalId;
var downIntervalId;
var scrollTimeoutId;
var dontScroll;
var dontScrollTimeout;
var sponsorIndex=0;
var sponsorList;
var rollLength=11;
var rollCounter=0;
var rollLimit=10;
var sponsorBarHeight=503;
var myWidth=0;
var myHeight=0;
var fixedLogos=1;

function updateTitleSponsor(){
    var element = document.getElementById("titlesponsorlogoid");
    /* update div */
    element.innerHTML=linkArray[0]+linkArray[1];
}

function buildList(){
    sponsorList="";
    offsetIndex=sponsorIndex;

    for (i=0; i<rollLength; i++, offsetIndex++)
    {
        if (offsetIndex>=linkArray.length)
            offsetIndex=fixedLogos;
        else if (offsetIndex<=0)
            offsetIndex=linkArray.length-fixedLogos;
    
        sponsorList+=linkArray[offsetIndex];
        //sponsorList+=offsetIndex + '<br />';
    }
}

function roll(up, click){
    if ((dontScroll || dontScrollTimeout) && !click)
        return;
    var element = document.getElementById("sponsorlogoid");
    if (up)
    {
        /* update sponsorIndex */
        sponsorIndex--;
        if (sponsorIndex<0)
            sponsorIndex=linkArray.length;
    }
    else
    {
        if (rollLength==linkArray.length)
        {
            /* update sponsorIndex */
            sponsorIndex++;
            if (sponsorIndex>=linkArray.length)
                sponsorIndex=0;
        }
    }
    buildList(up);

    /* update div */
    element.innerHTML=sponsorList;
    if (rollLength<linkArray.length)
        rollLength++;
}

function scrollUp(){
    var element = document.getElementById("sponsorlogoid");
    if (element.scrollTop > 10)
        element.scrollTop -= 10;
    else
    {
        element.scrollTop = 0;
        if ((rollCounter++)%rollLimit == 0)
            roll(true, true);
    }
}

function scrollDown(){
    var element = document.getElementById("sponsorlogoid");
    if (element.scrollTop+10 < (element.scrollHeight-sponsorBarHeight))
    {
        element.scrollTop += 10;
    }
    else
    {
        element.scrollTop = element.scrollHeight-sponsorBarHeight;
        if ((rollCounter++)%rollLimit == 0)
            roll(false, true);
    }
}

function jumpUp(){
    var i;
    for (i=0; i<30; i++)
    	scrollUp();
}

function jumpDown(){
    var i;
    for (i=0; i<30; i++)
    	scrollDown();
}

function scrollStart(down){
    dontScroll=true;
    dontScrollTimeout=true;
    if (down)
        downIntervalId = setInterval("scrollDown()",50);
    else
        upIntervalId = setInterval("scrollUp()",50);
}

function scrollStop(down){
    if (down)
        clearInterval (downIntervalId);
    else
        clearInterval (upIntervalId);
    dontScroll=false;
    clearTimeout (scrollTimeoutId);
    scrollTimeoutId = setTimeout("allowScroll()", 5000);
}

function allowScroll(){
    dontScrollTimeout=false;
}

function startTimer(timeout){
    var i = Math.round(100*Math.random());
    sponsorIndex = i%(linkArray.length-fixedLogos);

    roll(true, false);
    setInterval ( "roll(true, false)", timeout);
    updateTitleSponsor();
}

/* gets size of window for resize modifications */
function getSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
}

function resizeSponsorBar(){
    getSize();
    var windowheight = myHeight - 162;
    element = document.getElementById("sponsorWrapId");
    element.style.height=windowheight +"px";
    element = document.getElementById("sponsorlogoid");
    element.style.height=windowheight +"px";
    sponsorBarHeight=windowheight;
}

