/*
    project: WestLB
    type: javascript
    description: teaser randomizer
    (c) Aperto AG 2007. All rights reserved.
*/

var currTime = new Date();
var myArray = new Array();
var rotationMode, bannerWidth;
var currPos;

function startRotation() {
    var output;

    if (myArray.length > 0) {
        if (rotationMode == "time") {
            getTimeBasedImage();
        } else if (rotationMode == "random") {
            getRandomizedImage();
        } else {
            output = "No rotation mode defined on given pagehandle";
        }
    } else {
        output = "No banners defined on given pagehandle";
    }

    if (!output && !currPos) {
        currPos = 0;
    }
    if (output) {
        document.write(output); 
    }

    if (currPos >= 0) {
        for (var k = 0; k < myArray.length; k++) {
            var pos = (k + parseInt(currPos)) % myArray.length;
            output = '<a class=\"icon arrow\" href=\"' + myArray[pos].link + '\" title=\"' + prefix + ' ' + myArray[pos].headline + '\">' + more + '</a>';
            output = '<p>' + myArray[pos].teasertext + ' ' + output + '</p>';
            output = '<h3>' + myArray[pos].headline + '</h3>' + output;
            output = '<h4>' + myArray[pos].label + '</h4>' + output;
            output = '<img src=\"' + myArray[pos].img + '\" alt=\"' + myArray[pos].alttext + '\" />' + output;
            output = '<div class=\"beta' + ((k == (myArray.length - 1)) ? ' last' : '') + '\" onmouseover=\"a_bteaser_mov(this); return true;\" onmouseout=\"a_bteaser_mou(this); return true;\" onclick=\"a_bteaser_cli(this);\">' + output + '</div>';
            document.write(output);
        }
    }
}



function getRandomizedImage() {
    var hoursArray = new Array();
    
    for (var i = 0; i < 24; i++) {
        hoursArray[i] = i;
    }

    // evens out all percentages to 100%, in case total is != 100%
    getTotalPercentage();

    // shuffles the hoursArray to get the "random" effect
    shuffleHoursArray(hoursArray);
    
    // creates timeBins, which store hours for each banner to display. amount of hours is being determined by each images's given percentage of appearance
    createTimeBins(hoursArray);
}


function getTotalPercentage() {
    var totalPercentage = 0;

    for (var j = 0; j < myArray.length; j++) {
        totalPercentage += myArray[j].seed;
    }

    if (totalPercentage != 100) {
        var multiplier = 100 / totalPercentage;
        for (var k = 0; k < myArray.length; k++) {
            myArray[k].seed = (myArray[k].seed * multiplier).toFixed();
        }
    }
}


function shuffleHoursArray(hoursArray) {
    var temp = 0;
    var newDay = 0;

    for (var i = 0; i < hoursArray.length; i++) {
        newDay = (Math.random() * hoursArray.length-1) + 1;
        newDay = parseInt(newDay, 10);
        temp = hoursArray[i];
        hoursArray[i] = hoursArray[newDay];
        hoursArray[newDay] = temp;
    }
}


function createTimeBins(hoursArray) {
    var currHour = currTime.getHours();
    var binHours = 0;
    var tmpBinName;

    for (var i in myArray) {
        binHours = (24/100 * myArray[i].seed).toFixed();
        tmpBinName = new Array();
        for (var j = 0; j < binHours; j++) {
            tmpBinName[j] = hoursArray.shift();
            if (tmpBinName[j] == currHour) {
                currPos = i;
                break;
            }
        }
    }
}


function getTimeBasedImage() {
    var referenceTime = new Date();
    referenceTime.setHours(0, 0, 0);

    // calculate elapsed time
    var timeDiff = currTime.getHours() - referenceTime.getHours();

    // decrease elapsed time by set seed values in the array till now and store last arrayPosition to remember image
    while (timeDiff > 0) {
        for (var i in myArray) {
            if (timeDiff < 0) break;
            timeDiff = timeDiff - myArray[i].seed;
            currPos = i; // count where we are while iterating through myArray
        }
    }
}

