$(document).ready(function(){
	$("#tiles ul li").hide();
	$
  $('input[name=email]').focus(function() {
    	$('input[name=email]').val("");
	});
});
$(window).load(function () {
	$(".tile_rollover").each(function (i) {
		var tiles = "#tile_" + i;
		var tileimages = "#tile_image_" + i;
		var tileimage = $(tileimages).attr("src");
		var rollovers = "input[name=label_image_"+i+"]";
		var rollover = $(rollovers).val();
		$(tiles).hover(
		  function () {
			$(tileimages).attr('src', '../media/labels/'+rollover);
		  }, 
		  function () {
			$(tileimages).attr('src', tileimage);
		  }
		);					
	});								
	$(".thumbs ul a").click(function() {
		var newImage = $(this).attr('rel');
		var whichViewer = '#'+$(this).attr('id');
		var id = $(this).attr('id').substr(6);
		var viewerId = '#viewer_' + id;
		var captionId = '#description_' + id;
		var titleId = '#title_' + id;
		var title = $(this).children().attr('title');
		var caption = $(this).children().attr('alt');
		$(whichViewer).fadeOut('fast', function() {
			$(viewerId).addClass('loading');
			var img = new Image();
			$(whichViewer).load(function () {
			  $(viewerId).removeClass('loading');
			  $(titleId).html(title);
			  $(captionId).html(caption);
			  $(whichViewer).fadeIn();
			})
			.error(function () {
			  //alert("IMAGE COULD NOT BE FOUND");
			})
			.attr('src', newImage);
		});
	});
	(function(jQuery) {
	jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
	{
		//Default Values
		timeBetween = typeof(timeBetween) == 'undefined' ? 0 : timeBetween;
		 fadeInTime = typeof(fadeInTime) == 'undefined' ? 500 : fadeInTime;
	
		//The amount of remaining time until the animation is complete.
		//Initially set to the value of the entire animation duration.
		var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);
	
		var i=0; //Counter
		return jQuery(this).each(function()
		{
			//Wait until previous element has finished fading and timeBetween has elapsed
			jQuery(this).delay(i++*(fadeInTime+timeBetween));
	
			//Decrement remainingTime
			remainingTime -= (fadeInTime+timeBetween);
	
			if(jQuery(this).css('display') == 'none')
			{
				jQuery(this).fadeIn(fadeInTime);
			}
			else //If hidden by other means such as opacity: 0
			{
				jQuery(this).animate({'opacity' : 1}, fadeInTime);
			}
	
			//Delay until the animation is over to fill up the queue.
			jQuery(this).delay(remainingTime+timeBetween);
	
		});	
	
	};
	
	})(jQuery);
	$("#tiles ul li").fadeInSequence(100);
	
	$(".viewer").each(function (i) {
		var id = $(this).attr('id').substr(7);
		var thumbs = "#thumbs_" + id;
		var next = "#next_" + id;
		if ($(thumbs).width() > 800) {
			$(next).fadeIn();
		} 
	});
	$(".thumbnavleft").click(function() {									  
		var id = $(this).attr('id').substr(5);
		var thumb = "#thumbs_" + id;
		var prev = "#prev_" + id;
		var next = "#next_" + id;
		markerpos = $('.post').position().left;
		thumbpos = $(thumb).position().left;
		if (markerpos > thumbpos) {
			$(thumb).animate({"left": "+=800px"}, "slow",
			function() { 
				thumbpos = $(thumb).position().left;
				if (markerpos <= thumbpos) {
					$(prev).fadeOut();		
				}
			});
			$(next).fadeIn();
		}
	});
	$(".thumbnavright").click(function() {
		var id = $(this).attr('id').substr(5);
		var thumb = "#thumbs_" + id;
		var prev = "#prev_" + id;
		var next = "#next_" + id;
		markerpos = $('.post').position().left;
		thumbpos = $(thumb).position().left;
		$(prev).fadeIn();
		if ($(thumb).width() + thumbpos > markerpos + 800) {
			$(thumb).animate({"left": "-=800px"}, "slow", 
				function() { 
					thumbpos = $(thumb).position().left;
					if ($(thumb).width() + thumbpos < markerpos + 800) {
						$(next).fadeOut();		
					}
				});
		} 
	});	
});
