var bannerPaths = [];
var animationInterval = 10000;

bannerPaths.push({
        'order':'1',
        'name':'examined',
        'path':'/images/banners/examined.jpg',
        'target':'http://examinedlife.wheatstoneacademy.com',
        'color':'#f1c600'
        });
bannerPaths.push({
        'order':'2',
        'name':'video',
        'path':'/images/banners/watchthevideo.jpg',
        'target':'http://vimeo.com/14379401',
        'color':'#941f10'
        });
bannerPaths.push({
        'order':'3',
        'name':'regina',
        'path':'/images/banners/testimonials.jpg',
        'target':'/conference/mariners',
        'color':'#5d0f32'
        });
bannerPaths.push({
        'order':'4',
        'name':'alumni',
        'path':'/images/banners/alumni.jpg',
        'target':'#alumni',
        'color':'#267242'
        });
/*
Colors reference
    'color':'#1FA09B' <- teal
    'color':'#941f10' <- red
    'color':'#267242' <- green
    'color':'#f1c600' <- yellow
    'color':'#5d0f32' <- purple
*/
var activeBannerIndex = 1;
var animationIntervalObj = null;

function loadBanner(bannerObj){
    $("#bannerContainer").unbind('click');
    if(bannerObj.target.substring(0,1) == '#'){
        f = function(){ $(bannerObj.target).trigger('click'); };
    }else if(bannerObj.target.substring(0,1) == '/' || bannerObj.target.substring(0,4) == 'http'){
        f = function(){ window.location = bannerObj.target; }; 
    }else {
        // no clue - just don't do anything with this click
        f = function(){ return; };
    }
    $("#bannerContainer").click(f);
    
    activeBannerIndex = bannerObj.order;
    $("#bannerContainer").fadeOut(
        function(){ $("#bannerContainer").css('background-image','url('+bannerObj.path+')').fadeIn(); }
    );
}

function loadBannerByOrderIndex(ind){
    for (i=0; i < bannerPaths.length; i++) {
        if(ind == bannerPaths[i].order){
            loadBanner(bannerPaths[i]);
        }
    }
}

function swapBanner(){
    next = (parseInt(activeBannerIndex) >= bannerPaths.length) ? 1 : parseInt(activeBannerIndex)+1;
    for(i=0;i<bannerPaths.length;i++){
        if(bannerPaths[i].order == next){
            activeBannerIndex = next;
            loadBanner(bannerPaths[i]);
        }
    }
}

function initThumbEvents(){
    $(".bannerLink").click(
        function(event){
            event.stopPropagation();
            clearInterval(animationIntervalObj);
            var n = $(this).attr('id').split('_')[1];
            for (ind=0; ind < bannerPaths.length; ind++) {
                if(bannerPaths[ind].name == n){
                    loadBanner(bannerPaths[ind]);
                    break;
                }
            }
        }
    );
    $(".bannerLink").each(
        function(){
            var n = $(this).attr('id').split('_')[1];
            for (ind=0; ind < bannerPaths.length; ind++) {
                if(bannerPaths[ind].name == n){
                    $(this).css('background-color',bannerPaths[ind].color);
                    break;
                }
            }
        }
    );
}

function bannerInit(){
    loadBannerByOrderIndex(activeBannerIndex);
    animationInterval = 5000;
}

function cycleBanners(){
    animationIntervalObj = setInterval("swapBanner()",animationInterval);
}

$(function(){
    bannerInit();
    initThumbEvents();
    cycleBanners();
});
