/**
 * JS for general carousels
 */

// URL of where the blank.gif image will live.
var URL_images = '../img/';

// Execute when the DOM is ready
$(document).ready(function(){
	init_general_carousel();
});

/*******************************************************************************
 * Function to initiate carousels on the page
 */
function init_general_carousel() {

	// Set up active slide and first panel to display
	$('div.general_carousel div.gc-links li:first').addClass('active');

	var num_carousels = $('.general_carousel').length;
	
	// Loop through carousels on page
	for (var i = 0; i < num_carousels; i++) {
	
		var this_carousel = $('div.general_carousel:eq('+i+')');
		var num_panelsets = this_carousel.find('div.gc-panelset').length;
		var next = this_carousel.find('div.gc-right a');
		var prev = this_carousel.find('div.gc-left a');
		
		// Loop through panelsets in carousels
		for (var x = 0; x < num_panelsets; x++) {
		
			var panels = this_carousel.find('div.gc-panelset:eq('+x+') div.gc-panels');

			panels.cycle({
				fx: 'scrollHorz',
				speed: 500,
				timeout: 7500,
				next: next, 
				prev: prev,
				pager: this_carousel.find('div.gc-panelset:eq('+x+') div.gc-pagination'),
				pagerAnchorBuilder:function(idx, slide) {
					return '<a href="#"><img src="'+URL_images+'blank.gif" alt=""><\/a>';
				}
			});

			this_carousel.find('div.gc-panelset:eq('+x+') div.gc-pagination a').focus(function(){
				this.blur();
			});

			$('div.general_carousel:eq('+i+') div.gc-left a, div.general_carousel:eq('+i+') div.gc-right a').focus(function(){
				this.blur();
			});
		}
	}
	
	// Make tabs switch panelsets
	$('div.general_carousel div.gc-links a').click(function(){
		var this_carousel = $(this).parents('div.general_carousel');
		$(this).parents('ul').find('.active').removeClass('active');
		$(this).parent('li').addClass('active');
		this_carousel.find('div.gc-panels').cycle('stop');
		var nextPanelset = '#'+$(this).attr('class');
		this_carousel.find('div.gc-panelset:not('+nextPanelset+')').fadeOut('animspeed', function() {
			this_carousel.find(nextPanelset).fadeIn(animspeed, function(){
				this_carousel.find(nextPanelset+' div.gc-panels').cycle('resume');
				this_carousel.find(nextPanelset+' div.gc-pagination a:first').trigger('click');
			});
		});
		this.blur();
		return false;
	});
}

