// GLOBAL JAVASCRIPT ACTIONS
// REQUIRES JQUERY


// set variable for total width
var totalWidth = 0;


// modal actions
function modalActions() {

	// $('.modalClose').wrap('<div class="closeLink"></div>').text('Close').attr('href', '#');
	
	$('img.modalData').css({ border: '5px solid #fff' });
	
	$('#modalOverlay, #modalContainer').click(function() {
	
		$.modal.close();
	});
}


// animates images
function scrollImages(direction) {

	var distance = $(window).width();
	var position = $('#photos ul').position();
	var visibleWidth = $(window).width();
	
	if(direction == "left") {
	
		var spaceRemaining = totalWidth - (visibleWidth - position.left);
		var increment = '-=';
	}
	else {
	
		var spaceRemaining = position.left * -1;
		var increment = '+=';
	}
	
	if(spaceRemaining > 0 && spaceRemaining < distance) {
	
		distance = spaceRemaining;
	}
	else if(spaceRemaining == 0) {
	
		distance = 0;
	}
	
	$('#photos ul').animate({ left: increment + distance + 'px' }, {
		duration: 2000,
		easing: 'easeInOutQuad'
	});
}


// on dom ready
$(function() {

	// contact info
	$('#contact , #about').hide();
	$('#footer a , #about').click(function() {
	
		var x = $(this).attr('hash')
	
		$(x).modal({
			onShow: modalActions,
			overlay: 75
		});
		
		return false;
	});

	// add nav arrows to slideshow
	$('<a href="#" id="prev">&lt;</a><a href="#" id="next">&gt;</a>').prependTo('#photos').css({ opacity: 0.5 }).hide();
	
	// hover actions
	$('#prev, #next').hover(function() {
	
		$(this).fadeTo('normal', 0.75);
	}, function() {
	
		$(this).fadeTo('normal', 0.5);
	});
	
	// show and hide arrows
	$('#photos').bind('mouseenter', function() {
	
		$('#prev, #next').fadeIn('slow');
	}).bind('mouseleave', function() {
	
		$('#prev, #next').fadeOut('slow');
	});

	// set up the slideshow
	$('#photos ul').addClass('slideshow');
	
	// position elements
	$('#photos ul li').each(function() {
	
		totalWidth += $(this).width();
		
		if($(this).prev('li').position()) {
		
			var prevPosition = $(this).prev('li').position();
			var prevWidth = $(this).prev('li').width();
			
			$(this).css({ left: prevPosition.left + prevWidth + 'px' });
		}
	});
	
	// set width of ul
	$('#photos ul').width(totalWidth);
	
	// scroll images
	$('#next').click(function() {
		
		if ($('#photos ul').is(':animated')) {
		} else {
		scrollImages('left');
		};
		
		return false;
	});
	$('#prev').click(function() {
		
		if ($('#photos ul').is(':animated')) {
		} else {
		scrollImages('right');
		};
		
		return false;
	});
	
	// set up modals
	
	
	
	$('#photos ul li a').click(function() {
	
		$('<img src="' + $(this).attr('href') + '" alt="" />').modal({
			onShow: modalActions,
			overlay: 75
		});
		
		return false;
	});
});
