/**
 * MySQL Web JS Library
 * div_rotate.js
 * @author Dups <dups@mysql.com>
 */

/**
 * @author Dups <dups@mysql.com>
 * Resets all divs in the rotateIndex display to "imageRotateIndex"
 */
function rotateIndexReset()
{
        cnt = divContent.length;

        
        //Switch all the indexex of the rotateIndexes to the default
        for (i=0; i<cnt; i++)
        {
             ri = 'imageRotateIndex' + i;
             if (document.getElementById(ri))
             {
                document.getElementById(ri).className = 'imageRotateIndex';
             }
        }
}
/**
 * Sets the current imageRotateIndex + int div to class imageRotateIndexCurrent
 * @author Dups <dups@mysql.com>
 * @param string divid div to set to current
 */
function rotateIndexSetCurrent(divid)
{
        if (document.getElementById(divid))
        {
            document.getElementById(divid).className = 'imageRotateIndexCurrent';
        }
}

function previousRotateDiv(divid)
{  
    cnt = divContent.length;
    if (divRotateCurrentKey == 0) {
	divRotateCurrentKey = (cnt-1);
    }
    else {
	divRotateCurrentKey--;
    }
    showSpecificRotateDiv(divid, divRotateCurrentKey)
}


function nextRotateDiv(divid)
{  
    cnt = divContent.length;
    if (divRotateCurrentKey>=(cnt-1)) {
	divRotateCurrentKey = 0;
    }
    else {
	divRotateCurrentKey++;
    }
    showSpecificRotateDiv(divid, divRotateCurrentKey)
}


/**
 * rotateDiv
 * takes an element with id "divid" and changes the innerHTML of the
 * content based on array divContent
 * 
 * requires globalVariable divRotateCurrentKey (variable containing current or starting key of the divContent)
 * requires globalVariable divContent (array of div content)
 * 
 * @author Dups <dups@mysql.com>
 * @param string layerid id of the div
 * @param bool randomize true randomizes the next option
 * @param array divContent
 * 
 */
function rotateDiv(divid, randomize)
{   
        cnt = divContent.length;
        rotateLayer = document.getElementById(divid);
        
        rotateIndexReset();
        
        if (cnt < 2) return;
        if (randomize == true)
        {
            rand = Math.floor(Math.random()*cnt);
            
            if (rand != divRotateCurrentKey)
            {
                divRotateCurrentKey = rand;
            }
            else
            {
                rotateDiv(divid, true);
            }
            
            rotateLayer.innerHTML = divContent[rand];
            ri = 'imageRotateIndex' + rand;
            rotateIndexSetCurrent(ri);
        }
        else
        {
            if (divRotateCurrentKey == undefined)
            {
                rotateLayer.innerHTML = divContent[0];
                divRotateCurrentKey = 0;
            }
            else
            {

                if (divRotateCurrentKey<(cnt-1))
                {
                    divRotateCurrentKey++;
                }
                else
                {
                    divRotateCurrentKey = 0;
                }
                rotateLayer.innerHTML = divContent[divRotateCurrentKey];
                ri = 'imageRotateIndex' + divRotateCurrentKey;
                rotateIndexSetCurrent(ri);
            }
        }
}
/**
 * Show specific Layer by calling it, also clears the interval and stops the rotation
 * @author Dups <dups@mysql.com>
 * @param string layerid id of the div
 * @param int key of the element to show
 */
function showSpecificRotateDiv(divid, key)
{
        rotateIndexReset();
        
        clearInterval(imageRotateInterval);
        rotateLayer = document.getElementById(divid);
        rotateLayer.innerHTML = divContent[key];
        divRotateCurrentKey = key;
        ri = 'imageRotateIndex' + key;
        rotateIndexSetCurrent(ri);
}
