var fadeTime = 1000;
var currentImage;
var currentText;
var looper;

$(document).ready(function() {
    $('#thumbribbon img').each(function(i, el) {
	    $(el).data('num', i);
	}).click(thumbClick);
    goToImage(0);
    // $('#textribbon').text(texts[0]);
    $('#textribbon-bg').fadeTo(0,0.40);
    initLooper();
    nextText();
});

function thumbClick(e)
{
    var t = $(e.target);
    initLooper();
    goToImage(t.data('num'));
}

function initLooper()
{
    if(typeof(looper) != 'undefined')
	window.clearInterval(looper);
    looper = window.setInterval(nextImage, 10000);
}

function nextImage()
{
    if(!$('#slideshow .largepic').eq(currentImage + 1).length)
	goToImage(0);
    else
	goToImage(currentImage + 1);
    nextText();
}

function nextText()
{
    var tr = $('#textribbon');
    var nText;
    if(typeof(currentText) == 'undefined' || typeof(texts[currentText + 1]) == 'undefined') {
	nText = texts[0];
	currentText = 0;
    } else {
	nText = texts[currentText + 1];
	currentText++;
    }
    
    tr.fadeOut(fadeTime, function() {
	    tr.text(nText);
	    tr.fadeIn(fadeTime);
	});
}

function goToImage(num)
{
    if(typeof(currentImage) != 'undefined') {
	$('#slideshow .largepic').eq(currentImage).fadeOut(fadeTime);
    }
    var lp = $('#slideshow .largepic').eq(num);
    lp.fadeIn(fadeTime);
    currentImage = num;
}
