$(window).load(function () {
	$(".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();
		} 
	});
	$(".thumbs ul a").click(function() {
		var newImage = $(this).attr('rel');
		var id = $(this).attr('id');
		var whichViewer = '#large_'+id;
		var viewerId = '#viewer_'+id;
		var captionId = '#description_'+id;
		var titleId = '#title_'+id;
		var title = $(this).children().attr('title');
		var caption = $(this).children().attr('alt');
		$(viewerId).addClass('loading');
		if ($(this).attr('rel').substr(0, 1) == "<") {
			$(whichViewer).fadeOut('fast', function() {
				$(viewerId).removeClass('loading');
				$(viewerId).html(newImage);
			});
		} else {
			var html = '<img src="'+newImage+'" id="large_'+id+'" />'; // it's an image so create image base
			var img = new Image();
			//alert($(whichViewer).height());
			if ($(viewerId).find('iframe')) {
				$(viewerId).html(html);
				$(whichViewer).load(function () {
				  $(viewerId).removeClass('loading');
				  $(whichViewer).fadeIn();
				})
			} else {
				$(whichViewer).fadeOut('fast', function() {
					$(viewerId).html(html);
					$(whichViewer).load(function () {
					  $(viewerId).removeClass('loading');
					  alert($(whichViewer).height());
					  $(whichViewer).fadeIn();
					})
					.error(function () {
					  //alert("IMAGE COULD NOT BE FOUND");
					})
				});
			}
		}
		$(titleId).html(title);
		$(captionId).html(caption);
	});
	/*
	$(".thumbs ul a").click(function() {
		var newImage = $(this).attr('rel');
		var id = $(this).attr('id').substr(6);
		var whichViewer = '#large_'+id;
		var viewerId = '#viewer_'+id;
		var captionId = '#description_'+id;
		var titleId = '#title_'+id;
		var image = '#large_'+id;
		if ($(this).attr('rel').substr(0, 1) == "<") {
			var video = '#video_'+id;
			$(image).hide();
			$(titleId).html('');
			$(viewerId).addClass('film');
			$(viewerId).addClass('loading');
			$(video).html(newImage);
		} else {
			$('.video').html('');
			$(viewerId).removeClass('film');
			var title = $(this).children().attr('title');
			var caption = $(this).children().attr('alt');
			$(whichViewer).fadeOut('fast', function() {
				//$(whichViewer).attr('src', newImage);
				//$(whichViewer).fadeIn('fast');
				$(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);
			});
		}
	});
	*/
	$(".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();		
					}
				});
		} 
	});	
});
$(document).ready(function(){
	// Caption Changer
	// $('.caption').hide();
	$('.toggle').click(function () {
		if ($('.nav').is(':hidden')) {
			$('.nav').slideDown('fast');
		} else {
			$('.nav').hide();
		}
	});
	
	/*
	$('.show').each(function (i) {
		var whichShow = '#show'+i;
		var delay = i * 300;
		$(whichShow).delay(delay).fadeIn('slow', function() {
			if (i >= 1) {
				$(".viewer").each(function (i) {
				var id = $(this).attr('id').substr(7);
				var thumbs = "#thumbs_" + id;
				var next = "#next_" + id;
				var caption = "#caption_" + id;
				var title = "#title_" + id;
				
				$(this).hover(
				  function () { if ($(title).is(':empty')) { } else { $(caption).fadeIn(); }
				  }, 
				  function () { $(caption).fadeOut(); }
				);
				
				if ($(thumbs).width() > 800) {
					
					$(next).fadeIn();
				} 
			});		
			}
      	});
	});
	*/
});


