﻿//editable variables
var IRdelay = 7000;     //milliseconds between slides
var IRduration = 1000;  //milliseconds for fade start to finish
var IRwrapper = 'IR-wrapper';

//static variables, DON'T TOUCH THESE
var IRcurrent = 0;
var IRtimeout;
var IRlocked = false;

function IRinitialize()
{    
    var html = '';
    var buttons = '<div id="IR-button-wrapper">';

    for (var i = 0; i < IRimages.length; i++)
    {
        html += '<div id="IR-' + i + '" class="IR-base ' + IRimageWrapperClass(i) + '">';
        buttons += '<a href="javascript:IRtransition(' + i + ');" id="IR-button-' + i + '" class="IR-button ' + IRbuttonClass(i) + '">';

        if (IRimages[i].buttonText == '')
            buttons += (i + 1) + '</a>';
        else
            buttons += IRimages[i].buttonText + '</a>';

        if (i == (IRimages.length - 1))
            html += '<a id="IR-link" href="' + IRimages[0].url + '">' + IRimageCaption(IRimages[i].src, IRimages[i].caption) + '</a>';
        else
            html += IRimageCaption(IRimages[i].src, IRimages[i].caption);

        html += '</div>';
    }
    buttons += '</div>';

    document.getElementById(IRwrapper).innerHTML = html + buttons;

    setTimeout('IRtransition()', IRdelay);
}

function IRtransition(index)
{
    if (IRlocked || index == IRcurrent)
        return;

    IRlock();
    clearTimeout(IRtimeout);

    var next;

    if (typeof index != 'undefined')
        next = index;
    else        
        next = IRcurrent + 1;

    if (next == IRimages.length)
        next = 0;

    var faderOut = new Spry.Effect.Fade('IR-' + IRcurrent, {duration:IRduration, from:100, to:0});
    var faderIn = new Spry.Effect.Fade('IR-' + next, {duration:IRduration, finish:IRrelease});
    setTimeout('document.getElementById(\'IR-' + next + '\').style.nofade = 1;', IRduration);
    
    faderOut.start();
    document.getElementById('IR-link').href = IRimages[next].url;
    document.getElementById('IR-button-' + IRcurrent).setAttribute('class', 'IR-button');
    document.getElementById('IR-button-' + IRcurrent).setAttribute('className', 'IR-button');
    document.getElementById('IR-button-' + next).setAttribute('class', 'IR-button IR-button-selected');
    document.getElementById('IR-button-' + next).setAttribute('className', 'IR-button IR-button-selected');
    faderIn.start();
    IRtimeout = setTimeout('IRtransition()', IRdelay);
    IRcurrent = next;
}


//helper functions
function IRlock()
{
    IRlocked = true;
}

function IRrelease()
{
    IRlocked = false;
}

function IRcaption(caption)
{
    if (caption != '')
        return '<span class="IR-caption">' + caption + '</span>';
    else
        return '';
}

function IRimageWrapperClass(index)
{
    if (index == 0)
        return 'IR-show';
    else
        return 'IR-hide';
}

function IRbuttonClass(index)
{
    if (index == 0)
        return 'IR-button-selected';
    else
        return '';
}

function IRimageCaption(src, caption)
{
    return '<img class="IR-image" src="' + src + '" />' + IRcaption(caption);
}