var prevButton = null;
var nextButton = null;

function photoplayerInit(carousel){
    prevButton = $('.jcarousel-prev');
    nextButton = $('.jcarousel-next');
    var next = function(){
      clearTimeout(prevButton.data('timeout'));
      clearTimeout(nextButton.data('timeout'));
      nextButton
        .click()
        .data('timeout', setTimeout(next, 500));
    };
    nextButton.hover(
        next,
        function(){
            clearTimeout(nextButton.data('timeout'));
        }
    );
    var prev = function(){
      clearTimeout(nextButton.data('timeout'));
      clearTimeout(prevButton.data('timeout'));
      prevButton
        .click()
        .data('timeout', setTimeout(prev, 500));

    };
    prevButton.hover(
        prev,
        function(){
            clearTimeout(prevButton.data('timeout'));
        }
    );
}

$(function(){
    $('a#disclaimer-show').click(function(event){
        event.preventDefault();
        $('div#disclaimer-container').show();
    });
    $('a.close').click(function(event){
        event.preventDefault();
        $(this).parent('div.popup-container').hide();
    });
    var photoPlayer = $('div#photoplayer');
    if(photoPlayer.length == 1)
    {
        var photoPlayerItems = $('ul li', photoPlayer);
        if(photoPlayerItems.length > 4)
        {
            photoPlayer.jcarousel({
                scroll: 1,
                initCallback: photoplayerInit
            });
        }
        $('ul li a.lightbox', photoPlayer).lightBox();
        $('ul li a:not(.lightbox)', photoPlayer).click(function(event){
            event.preventDefault();
            var parent = $(this).parent('li');
            var next = parent.nextAll('li:has(a.lightbox)');
            if(next.length > 0)
            {
                $('a.lightbox', next.eq(0)).click();
            }
            else
            {
                var prev = parent.prevAll('li:has(a.lightbox)');
                if(prev.length > 0)
                {
                    $('a.lightbox', prev.eq(prev.length - 1)).click();
                }
            }
        });
    }
    var location = window.location.href.replace(/#(.*)$/, '');
	$('a[href^="#"]').each(function(){
        var el = $(this);
		var href = location + el.attr('href').replace('/#.*/i', '');
		el.attr('href', href);
	});
});
