// Script for the Home Page Portfolio switcher

$(document).ready(function() {
  
    $('#thumbs a').each(function() {
        var theAnchor = $(this);
        var theImage = $(this).find('img');
        var theImageAlt = $(this).find('img').attr('alt');
        $(this).append('<span class="title">' + $(this).find('img').attr('alt') + '</span>');
    });
    loadFirstImage();
    
});

var advanceInterval;
$('#largeImg').css('height','274px');
var totalItems = $('#thumbs').find('a').size();
var currentItem = 0;

function advanceImage() {
    currentItem++;

    if (currentItem >= totalItems) {
        currentItem = 0;
    }
    
    var currentAnchor = $('#thumbs').find('a:eq(' + currentItem + ')');
    var imageSource = currentAnchor.attr("title");
    var link = currentAnchor.attr('href');
    
    showImage(imageSource, link);
    
}

function showImage(src,thelink) {
    
    $('#largeImg img').fadeOut('normal').remove();
    var largeImage = new Image();
    $(largeImage).load(function() {
        $(this).hide();
        $('#largeImg').append(this).removeClass('loading');
        $(this).wrap('<a href="' + thelink + '"></a>');
        $(this).fadeIn('slow');
    });
    $(largeImage).attr('src', src);
    //$('#largeImg img').wrapAll('<a href="' + thelink + '"></a>');
    
}

var advanceInterval = setInterval("advanceImage()", 5000);

function loadFirstImage() {
    var firstImageSource = $('#thumbs a:first').attr('href');
    var firstImageLink = $('#thumbs a:first').attr('href');

    showImage(firstImageSource, firstImageLink);
}
