/*
 * Author: Mat Price - dev.msp@gmail.com
 * Date: Jan 19, 2011
 * Description: Functions and vars to help with the scrolling of the images on the www.concordia.ca homepage
 */

/*
 * Inits
 */
var active_jcarousel_index = 1;
var scroller_auto = true;
var scroller_auto_interval = 9000;
var scroller_intervalID;
var $scroller_pages;

/*
 * main onload
 */
jQuery(document).ready(function() {
	
	$("a[rel^=\'prettyPhoto\']").prettyPhoto();
	
	// Store the scroller pages
	$scroller_pages = jQuery('#scroller_items a');
	var tmp_size = $scroller_pages.length;
	
	// Call jcarousel
	jQuery('#scroller_nav').jcarousel({
		wrap: ((tmp_size > 4) ? "circular" : null),
		scroll:4,
		size:tmp_size,
		initCallback: scroller_initCallback,
		itemFirstInCallback: {onBeforeAnimation: scroller_itemFirstInCallback},
		buttonNextHTML:'<div id="scroller_next"></div>',
		buttonPrevHTML:'<div id="scroller_prev"></div>'
	});
});

/*
 * Resume (or start) the timer on the scroller
 */
function resume_slide(interval)
{
	scroller_intervalID = window.setInterval(on_auto_next, interval);
}

/*
 * Pause (or stop) the timer on the scroller
 */
function pause_slide()
{
	window.clearInterval(scroller_intervalID);
	scroller_intervalID = '';
}

/*
 * This function takes care of the auto incrementing of the scroller
 */
function on_auto_next()
{
	// Get the carousel instance and size
	var carousel = jQuery('#scroller_nav').data('jcarousel');
	var scroller_size = carousel.size();

	// Get the indexes of previous and next pages
	var prev_index = active_jcarousel_index;
	var next_index = active_jcarousel_index + 1;
	
	var prev_base_index = Math.abs(prev_index) % scroller_size;
	var next_base_index = Math.abs(next_index) % scroller_size;
	
	
	
	// Get the corresponding jQuery objects, or at least, set them up
	var $prev_li = jQuery('#scroller_nav li[jcarouselindex='+prev_index+']');
	var $next_li = null;

	// Remove the active class from the previously shown nav tab
	jQuery('#scroller_nav a').removeClass('active');

	// If this is the last item before having to scroll, increase the scroller tabs page
	// and adjust the active class and animation to the next tab in the callback. Otherwise, do it in this function
	if (active_jcarousel_index % 4 == 0) {
		
		if (scroller_size < 5) {
			next_index = next_base_index;

			$next_li = jQuery('#scroller_nav li:first');
			$next_li.children('a').eq(0).addClass('active');
			//jQuery('#scroller_nav li[jcarouselindex='+next_base_index+'] a').addClass('active');
			scroller_item_transition( ((prev_index-1) % scroller_size), ((next_index-1) % scroller_size) );
		}
		else {
			carousel.next();
		}
	}
	else {
		$next_li = $prev_li.next();
		$next_li.children('a').eq(0).addClass('active');
		jQuery('#scroller_nav li[jcarouselindex='+next_base_index+'] a').addClass('active');
		scroller_item_transition( ((prev_index-1) % scroller_size), ((next_index-1) % scroller_size) );
	}

	// Increase the index counter for the tab/pages
	active_jcarousel_index = next_index;
}

/*
 * Manage the animation between pages
 */
function scroller_item_transition(from, to)
{
	$scroller_pages.eq(to).css('z-index', '9').show();
	$scroller_pages.eq(from).fadeOut(500, function() {
		jQuery(this).css('z-index', '0');
		$scroller_pages.eq(to).css('z-index', '10');
	});
}

/*
 * Take care of clicks on a scroller tab
 */
function scroller_tab_click(self, index)
{
	// Inits
	var carousel = jQuery('#scroller_nav').data('jcarousel');
	var scroller_size = carousel.size();
	var $self = jQuery(self);
	var $next_li = $self.parent();

	// Get the indexes of previous and next pages and tabs
	var prev_index = active_jcarousel_index;
	var next_index = parseInt($next_li.attr('jcarouselindex'));
	var prev_base_index = Math.abs(prev_index) % scroller_size;
	var next_base_index = index + 1;
	
	// Exit if the tab that was clicked is already active
	if (prev_base_index == next_base_index) return;

	// Get the jQuery object of the previously active tab
	var $prev_li = jQuery('#scroller_nav li[jcarouselindex='+prev_index+']');

	// Remove the active class from all DOM tabs (for robustness)
	jQuery('#scroller_nav a').removeClass('active');

	// Make the root(?) and currently visible tabs the active ones. The root tab is 
	// the one that gets cloned and that's why it needs to have the active class as well
	jQuery(self).addClass('active');
	jQuery('#scroller_nav li[jcarouselindex='+next_base_index+'] a').addClass('active');
	
	// logging
	jQuery('#trans_from').text(prev_index);
	jQuery('#trans_to').text(next_index);
	
	// Perform the transition animation
	scroller_item_transition(prev_base_index-1, index);

	// Increase the index counter for the tab/pages
	active_jcarousel_index = next_index;
}

/**
 * Use the initCallback callback
 * to assign functionality to the controls
 */
function scroller_initCallback(carousel)
{
	// Hovering over the slider causes it to pause. Take care of the hover event here
	jQuery('#scroller').hover(
		function () {
			if (scroller_auto) {
				pause_slide(); // disable autoscrolling on hover in
			}
		}, 
		function () {
			if (scroller_auto) {
				resume_slide(scroller_auto_interval); // enable autoscrolling on hover in
			}
		}
	);
	
	// Clicking a next/prev button causes the slider to halt
	jQuery('#scroller_next,#scroller_prev').bind('click', function() {
		if (scroller_auto) {
			scroller_auto = false;
			pause_slide();
		}
	});

	// Start autoscrolling
	if (scroller_auto) {
		resume_slide(scroller_auto_interval);
	}
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function scroller_itemFirstInCallback(carousel, item, idx, state) {
	// If autoscrolling is enabled (and therefore a user did not cause this callback to occur since the
	// next/prev buttons disable the autoscrolling), add the active class to the first visible tab (and root tab)
	if (scroller_auto)
	{
		var scroller_size = carousel.size();
		var prev_index = idx - 1;
		var next_index = idx;
		var prev_base_index = Math.abs(prev_index) % scroller_size;
		var next_base_index = (prev_base_index + 1) % scroller_size;
	
		var ph = idx % scroller_size;
		scroller_item_transition(prev_base_index-1, next_base_index-1)
		jQuery(item).children('a').addClass('active');
		jQuery('#scroller_nav li[jcarouselindex='+next_base_index+'] a').addClass('active');
	}
};
