var clickMore = '<small class="highlight">(click for more)</small>';

//Add the "click for more" links to headers
$(function(){
	$('body.page-template-default #content .content h2:gt(0)')
		.append(clickMore)
		.addClass('hover')
		.nextUntil('h2')
			.hide();
	
	$('body.page-template-default #content .content h2')
		.click(function(){
			$('body.page-template-default #content .content h2 small')
				.remove();
			
			$('body.page-template-default #content .content h2')
				.removeClass('hover')
				.not($(this))
					.addClass('hover')
					.append(clickMore);
			
			$('body.page-template-default #content .content > *:not(h2)')
				.hide();
			
			$(this)
				.nextUntil('h2')
				.show();
		})
});

//Add the carousel to home page
$(function(){
	if($('body.page-template-index-php').length == 0)
	{
		return;
	}
	if($('#content-top .nav').height() <= $('#content-top .navigation').height())
	{
		return;
	}

    $('#content-top .nav').jcarousel({
        wrap: 'circular',
        vertical: true,
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
        scroll: 1,
        auto: 2,
        size: mycarousel_itemList.length,
        animation: 'slow',
        buttonNextHTML: '<div><img src="' + templateDir + '/images/nav-scroll-down.png" alt="" /></div>',
        buttonPrevHTML: '<div><img src="' + templateDir + '/images/nav-scroll-up.png" alt="" /></div>'
    });
    
    $('#content-top .navigation')
    	.hover(function(){
    		$('#content-top .navigation .jcarousel-prev, #content-top .navigation .jcarousel-next')
    			.fadeIn();
    	}, function(){
    		$('#content-top .navigation .jcarousel-prev, #content-top .navigation .jcarousel-next')
    			.fadeOut();
    	});
    
    $('#content-top .navigation .jcarousel-prev, #content-top .navigation .jcarousel-next')
		.delay(3000)
			.fadeOut();
});

//Do some things with headers for IE6
$(function(){
	if($.browser.msie && parseFloat($.browser.version) == 6)
	{
		$('#content .content h2:first-child').addClass('first');
		$('#content .content h2+h3').addClass('first');
	}
});

//Add lightbox to gallery page
$(function(){
	$('body.page-template-default .gallery .main')
		.not('body.page-template-default .gallery.vimeo .main')
			.append('<div class="navigation"><ul><li><a href="#" >View gallery</a></li></ul></div>');
	
	$('body.page-template-default .gallery .main a')
		.not('body.page-template-default .gallery.vimeo .main a')
		.click(function(e){
			e.preventDefault();
			$('body.page-template-default .gallery .images a:first')
				.click();
		});
		
	$('body.page-template-default .gallery.vimeo .main')
			.append('<div class="navigation"><ul><li><a href="#" >View more LATV Videos</a></li></ul></div>');
			
	$('body.page-template-default .gallery.vimeo .main a')
		.click(function(e){
			e.preventDefault();
			var videoLink = document.getElementById('video_link');
			videoLink.target = '_blank';
			window.open(videoLink);
		});
		
	/*
	Method in which to support multiple groups of lightbox within one page.
	*/
	$('body.page-template-default .gallery')
			.each(function(){
				$('a[rel^=lightbox]', $(this)).lightBox({
					fixedNavigation: true,
					overlayBgColor: '#000000',
					imageBtnPrev: templateDir + '/images/lightbox-btn-prev.gif',
					imageBtnNext: templateDir + '/images/lightbox-btn-next.gif',
					imageBtnClose: templateDir + '/images/lightbox-btn-close.gif',
					imageLoading: templateDir + '/images/lightbox-ico-loading.gif'
				});
			});
	
	$('body.page-template-gallery-php #content-top a, body.page-template-default .gallery .images a')
		.not('body.page-template-default .gallery.vimeo a')
			.lightBox({
				fixedNavigation: true,
				overlayBgColor: '#000000',
				imageBtnPrev: templateDir + '/images/lightbox-btn-prev.gif',
				imageBtnNext: templateDir + '/images/lightbox-btn-next.gif',
				imageBtnClose: templateDir + '/images/lightbox-btn-close.gif',
				imageLoading: templateDir + '/images/lightbox-ico-loading.gif'
			});

/* Temp embedded gallery fix */
if($('.post.content').children('h2').length == 1) {
	$('.post.content p').appendTo('.post.content .text');
}


});

jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a href="' + item.link + '"><img src="' + item.url + '" width="215" height="116" alt="' + item.title + '" /></a>';
};
