﻿// inputarray is the array of rotated text
// outputdiv is the ID of the div containign the rotated data
// leftbutton is the ID of the left button (prior)
// rightbutton is the ID of the right button
function TextRotater(inputarray, outputdiv, leftbutton, rightbutton)
{
    this.RotateRight = fRotateRight;
    this.RotateLeft = fRotateLeft;
    this.inputarray = inputarray;
    this.outputdiv = document.getElementById(outputdiv);

    document.getElementById(rightbutton).onclick = this.RotateRight;
    document.getElementById(rightbutton).ondblclick = this.RotateRight;
    document.getElementById(rightbutton).rotater = this;
    document.getElementById(leftbutton).onclick = this.RotateLeft;
    document.getElementById(leftbutton).ondblclick = this.RotateLeft;
    document.getElementById(leftbutton).rotater = this;
    
    // Calculate initial value
    this.CurrentTextBlock = Math.floor(Math.random() * inputarray.length);
    this.outputdiv.innerHTML = this.inputarray[this.CurrentTextBlock];
}

function fRotateRight()
{
    var rot = this.rotater;
    rot.CurrentTextBlock +=1;
    if(rot.CurrentTextBlock >= rot.inputarray.length) rot.CurrentTextBlock = 0;
    rot.outputdiv.innerHTML = rot.inputarray[rot.CurrentTextBlock];
}

function fRotateLeft()
{
    var rot = this.rotater;
    rot.CurrentTextBlock -=1;
    if(rot.CurrentTextBlock < 0) rot.CurrentTextBlock = rot.inputarray.length - 1;
    rot.outputdiv.innerHTML = rot.inputarray[rot.CurrentTextBlock];
}

