/*------------------------------------------------------------------
Project:		Get Latched
Author:			Jay Contonio
Last change:	2009-08-25
-------------------------------------------------------------------*/

$(document).ready(function() {
	init();
})

// Gets called onload on each page
function init() {
	$('.removeLastBorder li:last-child').css({ 'border':'none' }).addClass('last');
	initResources();
	initializeAccordion();
	initializeCarousel();
	
	$('.videoList a').fancybox({
		'frameWidth': 320,
		'frameHeight': 180,
		'callbackOnClose': stopPlayingVideo
	});
	
}

function stopPlayingVideo() {
	$("#fancy_content").empty();
}



/* Carousel
--------------------------------------------- */
function initializeCarousel() {
	
	// Get the total number of items in the carousel and multiply that by the width of the first image
	var totalSlides = $('#carousel li').length;
	var slideWidth = $('#carousel img:first').width();
	var totalWidth = totalSlides * slideWidth;
	$('#carousel ol:first').width(totalWidth);
	
	// Set the images to float next to each other and set the container to absolute positioning
	$('#carousel ol:first').css({ 'position':'absolute' });
	$('#carousel ol:first li').css({
		'display':'block',
		'float':'left',
		'position':'relative',
		'width':slideWidth + "px"
	});
	
	// Generate navigation for each slide
	$('#carousel ol:first').after('<ol id="carouselNav"></ol>');
	for(var i=0; i < totalSlides; i++) {
		$('#carouselNav').append('<li><a href="#' + i + '">' + (i + 1) + '</a></li>');
	}
	$('#carouselNav').css({ 'position':'absolute', 'z-index':'8' });
	
	// Set click listeners for the navigation and set the first item active
	$('#carouselNav a:first').addClass('active');
	$('#carouselNav a').click(function(e) {
		var positions = stripPermalinkFromURL($(this).attr('href'));
		e.preventDefault();
		makeAllNavigationItemsInactive();
		$(this).addClass('active');
		moveCarousel(positions,slideWidth);
	});
}

function makeAllNavigationItemsInactive() {
	$('#carouselNav a').removeClass('active');
}

function moveCarousel(pos,width) {
	$('#carousel ol:first').animate({ left: '-' + pos * width + 'px' });
}

function stripPermalinkFromURL(url) {
	if ( url.indexOf("#") > -1 ) {
		var strQueryString = url.substr(url.indexOf("#")).toLowerCase();
		var aQueryString = strQueryString.split("#");
		return aQueryString[1];
	}
}


/* Resources tabs
--------------------------------------------- */
function initResources() {
	$('.resources ol').hide();
	$('.resources ol:first').show();
	$('.resources ul li a').click(function(e) {
		// Prevent the link from moving to the anchor
		e.preventDefault();
		// Remove the active class from all the tabs then set the active tab
		$('.resources ul li a').removeClass('active');
		$(this).addClass('active');
		// Get the anchor we're dealing with so we can show it
		var listToShow = $(this).attr('href');
		var strQueryString = listToShow.substr(listToShow.indexOf("#")).toLowerCase();
		var aQueryString = strQueryString.split("#");
		$('.resources ol').hide();
		$("#"+aQueryString[1]).show();
	});
}

/* Accordion
--------------------------------------------- */
function initializeAccordion(args) {
	$('.accordion a').click(function(e) {
		e.preventDefault();
		if($(this).attr('class') == "active") {
			location.href = $(this).attr('href');
			return false;
		}
		collapseAccordion();
		showTheDefintionFor(this);
	});
}


function collapseAccordion() {
	$('.accordion a').removeClass('active');
	$('.accordion a').css({
		'height':'61px'
	})
}

function showTheDefintionFor(item) {
	$(item).addClass('active');
	$(item).animate({
		height:"130px"
	}, 200);
}
