$(document).ready(function() {

    $('#feature-banner').slides({
        play : 8000,
        container: 'slides-container',
        generateNextPrev : false,
        effect : 'fade',
        crossfade : false,
        generatePagination : false,
        fadeSpeed : 400,
        hoverPause: true
    });
    
    $('#news-column, #events-column').slides({
        generateNextPrev : false,
        container: 'slides-container',
        slideEasing: "easeOutExpo",
        slideSpeed: 800,
        hoverPause: true
    });

    // Set starting slide to 1
    var startSlide = 1;
    // Get slide number if it exists
    if (window.location.hash) {
        startSlide = window.location.hash.replace('#','');
    }
    $('#tab-content').slides({
        generateNextPrev : false,
        effect : 'fade',
        autoHeight : true,
        generatePagination : false,
        container: 'slides-container',
        fadeSpeed : 100,
        paginationClass : 'tab-nav',
        start: startSlide,
        animationComplete: function(current){
            // Set the slide number as a hash
            window.location.hash = '#' + current;
            try {
                var tabName = $('#tab-content ul').children().get(current-1);
                tabName = tabName.firstChild.innerHTML.replace(/&amp;/g,'and').replace(/ /g,'-').replace(/[^a-zA-Z0-9-~!*%_.\s]/g,'');
                //if url ends in a /
                if (document.location.pathname.match(/\/$/))
                    _gaq.push(['_trackPageview',document.location.pathname + tabName]);
                else
                    _gaq.push(['_trackPageview',document.location.pathname + '/' + tabName]);   
            }
            catch(e){}
        }
    });
    
    $("#main-nav li a").click(function() {
            $(window).hashchange(function(){
               window.location.reload();
            });
    });
    
$('a.overlay').fancybox({
        'transitionIn' : 'fade',
        'transitionOut' : 'fade',
        'speedIn' : 600, 
        'speedOut' : 200,
        'scrolling' : 'yes',
        'centerOnScroll' : true,
        'autoScale' : false,
        'overlayColor' : '#000',
        'overlayOpacity' : '0.8',
        'padding' : 15
    });
    
    $('a.gallery').fancybox({
        'transitionIn' : 'fade',
        'transitionOut' : 'fade',
        'speedIn' : 600, 
        'speedOut' : 200,
        'scrolling' : 'yes',
        'centerOnScroll' : true,
        'autoScale' : false,
        'overlayColor' : '#000',
        'overlayOpacity' : '0.8',
        'padding' : 15,
        'titlePosition' : 'inside',
        'type' : 'iframe',
        'width' : 680,
        'height' : 580
    }); 
    
    
   $("a.fancyBoxLink").fancybox({
    'showNavArrows': false});
});

/******************************************
 * The following was added by UDit to fix an issue 
 * in web-kit browsers where document.ready        
 * fires before images are loaded meaning tab
 * heights were miscalculated
 ******************************************/
function onClickTabHeightAdjust(tabNum){
    $('#tab-content .slides-container .slides_control > div:eq('+tabNum+')').each(function(){
        var $this = $(this);
        var divHeight = $this.height();

        if((divHeight) >  0) {
            $('#tab-content .slides-container').css({'min-height': divHeight});
            $('#tab-content .slides-container .slide-control').css({'min-height': divHeight});
        }
    });
}

function onLoadTabHeightAdjust(){
    $('#tab-content .image-column img').each(function(){
        var $this = $(this);
        var imageHeight = $this.height();

        if((imageHeight) >  0) {
            $('#tab-content .slides-container').css({'min-height': imageHeight});
            $('#tab-content .slides-container .slide-control').css({'min-height': imageHeight});
        }
    });
}

$(window).load(function(){
  onLoadTabHeightAdjust();

  $('#tab-content ul.tab-nav li a').click(function(){
    var tabNum = $(this).attr('href').charAt(1);
    onClickTabHeightAdjust(tabNum);
  });
});
