/* 

Cheryl Caron Bootup Script
v. 1.0

last mod: 2-14-09
by: Mark

jQuery based onloads.


*/


/*##################################################################################*/

function doHover( current, previous ) {
	if (previous != '') {
		$('#gallery ul li' + previous + ' .photo-info').stop().fadeTo('fast',0);
	}
	$('#gallery ul li' + current + ' .photo-info').stop().fadeTo('fast',.70).fadeTo(4000,.70).fadeTo('slow',0);
	$('#gallery ul li.current').hover(function() {
		$('#gallery ul li.current .photo-info').stop().fadeTo('fast',.70);							
	},function() {
		$('#gallery ul li.current .photo-info').stop().fadeTo('fast',0);								
	});
}

$(document).ready(function() {
	
	// ** Initiate/Set Up Gallery Photo Scroll
	$('#gallery').addClass('reel');
	$('#gallery ul li .photo-info').fadeTo('fast',0).css({'top':'auto','bottom':'0'});	
	$('#gallery ul').append('<li class="reel-end"><img src="/images/b_back-reel.gif" alt="Back to Beginning" class="end" /></li>');
	$('#number-nav ul').append('<li class="arrow"><a href="#">Next</a></li>');
	
	// Set to 1st Photo
	$('#number-nav li').eq(0).find('a').addClass('selected');
	$('#gallery ul li img.photo').fadeTo('fast', .25); // fade out was Eileen's idea. mad props!
	$('#gallery ul li').eq(0).find('img.photo').fadeTo('fast',1);
	// Set current to 1st
	var previous = '';
	var current = '#' + $('#gallery ul li').eq(0).attr('id');
	$('#gallery ul li').eq(0).addClass('current');
	doHover(current, previous);
	
	// ** Gallery Interface
	// If Number Menu is clicked (except next arrow)
	$('#number-nav li[class!=arrow] a').click(function() {
		var target = $(this).attr('href');
		var targetPos = $(target).position();
		if (targetPos.left == 0) { targetPos.left = 100; }
		
		previous = '#' + $('#gallery ul li.current').attr('id');
		current = '#' + $(target).attr('id');
		$('#gallery ul li').removeClass('current');
		$(target).addClass('current');
		
		$('#gallery ul li img.photo').fadeTo('fast', .25);
		$(target + ' img.photo').fadeTo('fast',1);
		
		$('#gallery ul').stop().stop().animate({ 'left' : '-' + (targetPos.left - 100) + 'px' }, 1000, 'swing');

		doHover(current, previous);
		
		$('#number-nav li a').removeClass('selected');
		$(this).addClass('selected');
		
		return false;
	});
	
	
	// If Next Arrow is clicked
	$('#number-nav li.arrow a').click(function() {		
		var target = '#' + $(current).next().attr('id');
		var targetPos = $(target).position();
		if (targetPos.left == 0) { targetPos.left = 100; }
		
		previous = '#' + $('#gallery ul li.current').attr('id');
		current = '#' + $(target).attr('id');
		$('#gallery ul li').removeClass('current');
		$(target).addClass('current');
		
		$('#gallery ul li img.photo').fadeTo('fast', .25);
		$(target + ' img.photo').fadeTo('fast',1);
		
		$('#gallery ul').stop().stop().animate({ 'left' : '-' + (targetPos.left - 100) + 'px' }, 1000, 'swing');
		
		doHover(current, previous);
		
		$('#number-nav li a').removeClass('selected');
		$('#number-nav li a[href=' + target + ']').addClass('selected');
		
		return false;
	});
	
	
	// If Photo is clicked
	$('#gallery li img.photo, #gallery li span.glass, #gallery li .photo-info').click(function() {
		var target = '#' + $(this).parent().attr('id');
		var targetPos = $(this).parent().position();
		if (targetPos.left == 0) { targetPos.left = 100; }
		
		previous = '#' + $('#gallery ul li.current').attr('id');
		current = '#' + $(this).parent().attr('id');
		$('#gallery ul li').removeClass('current');
		$(target).addClass('current');
		
		$('#gallery ul li img.photo').fadeTo('fast', .25);
		$(this).parent().find('img.photo').fadeTo('fast',1);
		
		$('#gallery ul').stop().stop().animate({ 'left' : '-' + (targetPos.left - 100) + 'px' }, 1000, 'swing');
		
		doHover(current, previous);
		
		$('#number-nav li a').removeClass('selected');
		var thisID = $(this).parent().attr('id');
		$('#number-nav li a[href=#' + thisID + ']').addClass('selected');
		
		return false;
	});
	
	
	// If Back button at the end of the reel is clicked
	$('#gallery li img.end').click(function() {
		var target = '#' + $('#gallery li').eq(0).attr('id');
		var targetPos = $('#gallery li').eq(0).position();
		
		previous = '#' + $('#gallery ul li.current').attr('id');
		current = '#' + $('#gallery li').eq(0).attr('id');
		$('#gallery ul li').removeClass('current');
		$(target).addClass('current');
		
		$('#gallery ul li img.photo').fadeTo('fast', .25);
		$(this).parent().parent().find('li').eq(0).find('img.photo').fadeTo('fast',1);
		
		$('#gallery ul').stop().stop().animate({ 'left' : '-' + targetPos.left + 'px' }, 1000, 'swing');
		
		doHover(current, previous);
		
		$('#number-nav li a').removeClass('selected');
		$('#number-nav li').eq(0).find('a').addClass('selected');
		
		return false;
	});
	
	if ($('#publications').length > 0) {
		$('#publications ul').ready(function() {
			$('#publications ul').cycle({ 
				fx: 'fade',
				speed: 'slow',
				timeout: 5000
			});
		});
	}

});