// JavaScript Document

var timer;
var images = [];

var currIndex = 0;
$(function() {
    /*
    $('div#photo ul li').each(function(i, el) {
    images.push($(el).attr('href'));
    });*/
    $('div#banner div#photo2').css({ 'backgroundImage': "url('" + images[3] + "')" });
    $('div#banner div#photo').css({ 'backgroundImage': "url('" + images[0] + "')" });
    
    setTimeout(startScroller, 2000);


    
    $('div#photo a').mouseleave(function() {
        timer = setInterval(switchHeaderImage, 5000);
    });
    $('div#photo a').mouseenter(function() {
        clearInterval(timer);
        $('div#photo a').removeClass('active');
        $(this).addClass('active');
        var index = $(this).attr('rel');
        setActivePhoto(index, false);
    });


});

startScroller = function() {
    images.pop();
    timer = setInterval(switchHeaderImage, 5000);
}
switchHeaderImage = function() {
    var nextImage = (currIndex + 1);
    if (nextImage > $('div#photo a:last').attr('rel')) {
        nextImage = 0;
    }
    $('div#photo a.active').removeClass('active');
    $('div#photo a:eq(' + currIndex + ')').addClass('active');
    currIndex++;

    setActivePhoto(nextImage, true);
   
};

setActivePhoto = function(index, animated) {
    currIndex = parseInt(index);
    if (animated) {
        if ($('div#banner div#photo2:visible').length > 0) {
            $('div#banner div#photo2').fadeOut(1000, function() {
                $('div#banner div#photo2').css({ 'backgroundImage': "url('" + images[index] + "')" });
            });
        }
        else {
            $('div#banner div#photo2').fadeIn(1000, function() {
                $('div#banner div#photo').css({ 'backgroundImage': "url('" + images[index] + "')" });
            });
        }
    }
    else {
        if ($('div#banner div#photo2:visible').length > 0) {
            $('div#banner div#photo2').hide();
        }
        $('div#banner div#photo').css({ 'backgroundImage': "url('" + images[index] + "')" });
    }
}

