// GLOBAL JAVASCRIPT ACTIONS
// REQUIRES JQUERY


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


// modal actions
function modalActions() {


	$('img#largeimage').wrap('<div id="largeimagewrap"></div>');
	
	var title = $('img#largeimage').attr('alt');
	
	$('#largeimagewrap').append('<span>' + title + '</span>');
	
	$('img.modalData').css({ border: '5px solid #fff' , background: 'white' });
	
	$('#modalOverlay, #modalContainer').click(function() {
	
		$.modal.close();
	});
}


// animates images
function scrollImages(direction) {

	var distance = $('#photos').width();
	var position = $('#photos ul').position();
	var visibleWidth = $('#photos').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').hide();
	$('#footer a').click(function() {
	
		$('#contact').modal({
			onShow: modalActions,
			overlay: 75
		});
		return false;
	});

	// add nav arrows to slideshow
	$('<a href="#" id="prev"></a><a href="#" id="next"></a>').appendTo('#photos').css({ opacity: 0.4 }).hide();
	
	// hover actions
	$('#prev, #next').hover(function() {
	
		$(this).fadeTo('fast', 0.75);
	}, function() {
	
		$(this).fadeTo('fast', 0.5);
	});
	
	// show and hide arrows
	$('#photos').bind('mouseenter', function() {
	
		$('#prev, #next').fadeIn('medium');
	}).bind('mouseleave', function() {
	
		$('#prev, #next').fadeOut('fast');
	});
	
	// hide arrows if animation
	$("#photos").mousemove(function(){
		if ($("ul:animated") == 0) {
			$('#prev, #next').hide();
		} else {
			$('#prev, #next').fadeIn('medium');
		}
	});

	// 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() {
		
		// get the image attr alt
		var title = $(this).children('img').attr('alt');
	
		$('<img src="' + $(this).attr('href') + '" alt="' + title + '" id="largeimage" />').modal({
			onShow: modalActions,
			overlay: 85
		});
		
		return false;
	});
	
	
	// change elements 
	
	$('.backgrounds > img:gt(0)').hide();
	$('#navigation ul li').click(function() {
	
		var index = $('#navigation ul li').index(this);
		
		$('#navigation ul li').removeClass('active');
		$(this).addClass('active');
		$('.backgrounds img').fadeOut('slow');
		$('.backgrounds img').eq(index).fadeIn('slow');
		$('#rooster').removeClass().addClass('index'+index);
		$('#photos').removeClass().addClass('message'+index);
		
		return false;
	
	});
});
