

jQuery.noConflict();



/*----------------------------*/

var $timelineTrigger = '#timeline li a.handle'; /* what should we click on to do things with the timeline */
var $timelineHandle = '#timeline li'; /* what are we manipulating */
var $activeClass = 'active_item'; /* class to be applied to the active timeline item */
var $open_item_width = '780'; /* In pixels, the desired width of the active timeline item, also present in CSS on div.image-container */
var $numItems = 8;

/* don't fuck with these, or they will break things */
var $selectionInProgress = false; 
var arrows = '<a href="javascript:void(0);" class="slideshow_control prev">Previous</a><a href="javascript:void(0);" class="slideshow_control next">Next</a>';
var handle = '<a class="handle" href="javascript:void(0);">Select This Item</a>'

/*----------------------------*/

jQuery(document).ready(function() {
	if ('body.page-template-cycling_legend-php') {
		jQuery('div.legend_content').hide();
		initializeTimeline();
		setTimeout(checkTimeline, 300);
	}
			
	if ( jQuery('body.home').length ) {
		jQuery('#feature_content').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 6 * 1000);
	}
	
	jQuery('#connect_with_greg_toggle').bind('click', toggleConnectWithGreg);
	jQuery('#connect_with_greg_close').bind('click', closeConnectWithGreg);	
	
});

/*----------TIMELINE----------*/
function initializeTimeline() {
	jQuery($timelineTrigger).bind('click', selectItem);
	jQuery(window).bind('resize', resizeItems);
	randSelectItem();
}

function checkTimeline() {
	if( !jQuery( $activeClass ).length ) {
		initializeTimeline();
	}
}
		
function randSelectItem() {
	var $start_value = Math.floor(Math.random()*$numItems+1);
	jQuery($timelineTrigger).eq($start_value).trigger('click');
}

function selectItem() {

	if ( !$selectionInProgress ) {
	
		if ( jQuery(this).parent('li').hasClass($activeClass) ) {
			return false;
		}
		$selectionInProgress = true;

		var $remaining_width = (Math.floor(jQuery(window).width()) - $open_item_width);
		var $closed_item_width = (Math.floor($remaining_width / ($numItems - 1))) + 'px';
	
		if ( jQuery($timelineHandle + '.' + $activeClass).length > 0 ) {
			cleanUpItems();
		}
	
		jQuery(this).parent('li').addClass($activeClass);
		
		jQuery('.legend_content').hide();
		jQuery('.image-container').hide();
		jQuery('.legend_sidebar').hide();
		var imageSetId = '#images-' + jQuery(this).parent().attr('id');
		var contentId = '#content-' + jQuery(this).parent().attr('id');
		var sidebarId = '#sidebar-' + jQuery(this).parent().attr('id');
		jQuery(imageSetId).show();
		jQuery(sidebarId).show();
		jQuery(contentId).fadeIn(700);		
	
		jQuery($timelineHandle).not($activeClass).animate({
			width: $closed_item_width
		}, 300);
	
		jQuery(this).parent('li').animate({
			width: $open_item_width
		}, 600);
		
		// closing up shop, allow new selections after animations complete
		function sweepTheFloors () {
			$selectionInProgress = false;
			showLegendPhotos(sidebarId);
		}
		var cleanUp = setTimeout(sweepTheFloors, 750);
	
	}
	
	return false;
}

function cleanUpItems() {
	jQuery($timelineHandle + '.' + $activeClass).children('.slideshow_control').remove();
	jQuery($timelineHandle + '.' + $activeClass).append(handle);
	jQuery($timelineHandle + '.' + $activeClass).removeClass('loading');
	jQuery($timelineTrigger).bind('click', selectItem);
	jQuery($timelineHandle + '.' + $activeClass).removeClass($activeClass);
}

function resizeItems() {
	var $remaining_width = (Math.floor(jQuery(window).width()) - $open_item_width);
	var $closed_item_width = (Math.floor($remaining_width / ($numItems - 1))) + 'px';
	jQuery($timelineHandle).not($timelineHandle + '.' + $activeClass).width($closed_item_width);
}

function showLegendPhotos(sidebarId) {
	jQuery('li.active_item a.handle').remove();
	jQuery('li.active_item').addClass('loading').append(arrows);
	jQuery('a.slideshow_control.prev').bind('click', prevPhoto).fadeIn(300);
	jQuery('a.slideshow_control.next').bind('click', nextPhoto).fadeIn(300);
	jQuery('li.active_item').children('div').eq(0).children('img').eq(0).addClass('current_image');
	jQuery(sidebarId).children('div').eq(0).addClass('current_caption');
	jQuery('.current_caption').fadeIn(500);
	jQuery('.current_image').fadeIn(500);
}

function nextPhoto() {
	var currentImage = jQuery('li.active_item div.image-container img.current_image');
	var currentCaption = jQuery('div.current_caption');
	if ( currentImage.next().not('a').length ) {
		var nextImage = currentImage.next();
	} else {
		var nextImage = jQuery('li.active_item div.image-container img').eq(0);
	}
	if ( currentImage.next().length ) {
		var nextCaption = currentCaption.next();
	} else {
		var nextCaption = jQuery('.current_caption').siblings().eq(0);
	}
	jQuery('li.active_item div.image-container img.current_image').removeClass('current_image').fadeOut(300);
	jQuery('div.current_caption').removeClass('current_caption').hide();
	nextCaption.addClass('current_caption').fadeIn(700);
	nextImage.addClass('current_image').fadeIn(700);
}

function prevPhoto() {
	var currentImage = jQuery('li.active_item div.image-container img.current_image');
	var currentCaption = jQuery('div.current_caption');
	if ( currentImage.prev('li.active_item div.image-container img').length == 0 ) {
		var lastImage = jQuery('li.active_item div').children('img').length;
		var prevImage = jQuery('li.active_item div.image-container img').eq(lastImage-1);
	} else {
		var prevImage = currentImage.prev('li.active_item div.image-container img');		
	}
	if ( currentCaption.prev('div').length == 0 ) {
		var lastCaption = jQuery('div.current_caption').siblings('div').length;
		var prevCaption = jQuery('div.current_caption').siblings('div').eq(lastCaption-1);
	} else {
		var prevCaption = currentCaption.prev('div');		
	}
	currentImage.removeClass('current_image').fadeOut(300);
	jQuery('div.current_caption').removeClass('current_caption').hide();
	prevCaption.addClass('current_caption').fadeIn(700);
	prevImage.addClass('current_image').fadeIn(700);
}

/////////////

function toggleConnectWithGreg() {
	jQuery('#connect_with_greg').slideToggle('fast');
	jQuery('#connect_with_greg_toggle').toggleClass('activeToggle');
	jQuery(this).text(jQuery(this).text() == '+ Connect with Greg' ? '- Connect with Greg' : '+ Connect with Greg');
	return false;
}

function closeConnectWithGreg() {
	jQuery('#connect_with_greg').slideToggle('fast');
	jQuery('#connect_with_greg_toggle').toggleClass('activeToggle');
	jQuery('#connect_with_greg_toggle').text('+ Connect with Greg');
	return false;
}
