var sfTimeout = null;

function getNextPrevious(postId, what, autoplay){
	if(typeof(what) == 'undefined'){
		what = 'next';
	}
	var postDiv = $("#post_"+postId);
	if(what == 'next'){
		if(postDiv.find(".photo a:visible").next().length>0){
			postDiv.find(".photo a:visible").hide().next().show();
		}else{
			postDiv.find(".photo a:visible").hide();
			postDiv.find(".photo a:first").show();
		}
	}else{
		if(postDiv.find(".photo a:visible").prev().length>0){
			postDiv.find(".photo a:visible").hide().prev().show();
		}else{
			postDiv.find(".photo a:visible").hide();
			postDiv.find(".photo a:last").show();
		}
	}
	setThumbNo(postDiv);
	if(autoplay){
		sfTimeout = setTimeout("getNextPrevious("+postId+", 'next', true)", 3000);
	}
}

function showImage(postId, index){
	var postDiv = $("#post_"+postId);
	postDiv.find(".photo").show();
	postDiv.find(".thumbnails").hide();
	postDiv.find(".photo a:visible").hide();
	$(postDiv.find(".photo a").get(index)).show();
	setThumbNo(postDiv);
}

function setThumbNo(postDiv){
	postDiv.find(".currentPhoto").html(
		postDiv.find(".photo a:visible").index()+1
	);
}

function setActiveThumb(postDiv, index){
	postDiv.find(".thumbnails a").removeClass("current");
	$(postDiv.find(".thumbnails a").get(index)).addClass("current");
}

$(function(){
	var username='conormasterson'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
	
	$.getJSON(url,function(tweet){ // get the tweets
		$("#twitter").html(tweet[0].text); // get the first tweet in the response and place it inside the div
	});
	
	$("div[id^='post_'] a.thumbnailsLink").click(function(){
		clearTimeout(sfTimeout);
		var postDiv = $(this).closest(".post");
		postDiv.find(".autoplay img").attr("src", 
			postDiv.find(".autoplay img")
				.attr("src").replace("-pause", "-play")
		);
		var iIndex = postDiv.find(".photo a:visible").index();
		if(postDiv.find(".thumbnails").length>0){
			postDiv.find(".thumbnails").toggle()
				.end().find(".photo").toggle()
				.end().find(".photoNav").toggle();
			setActiveThumb(postDiv, iIndex);
			return false;
		}
		var id = postDiv.attr("id").split("_")[1];
		$.ajax({url:$(this).attr("href"), success: function(html){
			postDiv.find(".gallery").append(html)
				.closest(".post").find(".photo").hide();
			postDiv.find(".gallery .thumbnails a").click(function(){
				postDiv.find(".photoNav").show();
				showImage(postDiv.attr("id").split("_")[1], $(this).index());
				return false;
			});
			postDiv.find(".photoNav").hide();
			setActiveThumb(postDiv, iIndex);
		}});
		return false;
	});
	
	$(".archive .year a").click(function(){
		$("."+$(this).parent().attr("id")).toggle();
		var img = $(this).find("img");
		if($("."+$(this).parent().attr("id")).is(":visible")){
			$(img).attr("src", $(img).attr("src").replace("_more", "_down"));
		}else{
			$(img).attr("src", $(img).attr("src").replace("_down", "_more"));
		}
		return false;
	}).hover(function(){
		var img = $(this).find("img");
		$(img).attr("src", $(img).attr("src").replace(".gif", "_over.gif"));
	}, function(){
		var img = $(this).find("img");
		$(img).attr("src", $(img).attr("src").replace("_over.gif", ".gif"));
	});
	
	$("blockquote").html("&ldquo;"+$("blockquote").html()+"&rdquo;");
	
	var tags = $(".tagCloud li");
	var maxTags = 30;
	if(tags.length > maxTags){
		$(".tagCloud li:gt("+maxTags+")").hide();
		$(tags[tags.length-1]).after("<li class='tags-more'><a href='#' id='tags-more'>more</a></li>");
		$("#tags-more").click(function(){
			if($(tags[tags.length-1]).is(":visible")){
				$(".tagCloud li:gt("+maxTags+")").hide();
				$("#tags-more").html("more").closest("li").show();
			}else{
				$(".tagCloud li:gt("+maxTags+")").show();
				$("#tags-more").html("less");
			}
			return false;
		});
	}
	
	$("div[id^='post_'] .photo a").each(function(i, photoLink){
		if($(photoLink).siblings().length == 0){
			$(photoLink).after($(this).html());
			$(photoLink).remove();
		}
	});
	
	$("div[id^='post_'] .photo a").click(function(){
		var id = $(this).closest(".post").attr("id").split("_")[1];
		getNextPrevious(id, 'next');
		return false;
	}).hover(function(){
		$(this).closest(".post").find(".image-description").show();
	}, function(){
		$(this).closest(".post").find(".image-description").hide();
	});
	
	$("div[id^='post_'] a.nav-next").click(function(){
		var id = $(this).closest(".post").attr("id").split("_")[1];
		getNextPrevious(id, 'next');
		return false;
	});
	
	$("div[id^='post_'] a.nav-prev").click(function(){
		var id = $(this).closest(".post").attr("id").split("_")[1];
		getNextPrevious(id, 'prev');
		return false;
	});
	
	$("div[id^='post_'] a.autoplay").click(function(){
		var id = $(this).closest(".post").attr("id").split("_")[1];
		if($(this).find("img").attr("src").indexOf("-play")>0){
			sfTimeout = setTimeout("getNextPrevious("+id+", 'next', true)", 1500);
			$(this).find("img").attr("src", 
				$(this).find("img").attr("src").replace("-play", "-pause")
			);
		}else{
			$(this).find("img").attr("src", 
				$(this).find("img").attr("src").replace("-pause", "-play")
			);
			clearTimeout(sfTimeout);
		}
		return false;
	});
	
	$(".recentComment").each(function(){
		if($(this).find(".short").html() != $(this).find(".long").html()){
			if($(this).hasClass("conor")){
				$(this).find(".short").append("<img src='/v2/i/recent_comments_more_dark.gif'/>");
				$(this).find(".long").append("<img src='/v2/i/recent_comments_less_dark.gif'/>");
			}else{
				$(this).find(".short").append("<img src='/v2/i/recent_comments_more.gif'/>");
				$(this).find(".long").append("<img src='/v2/i/recent_comments_less.gif'/>");
			}
			$(this).find(".short").html(
				"<a class='toggleComment' href='#'>"+$(this).find(".short").html()+"</a>"
				);
			$(this).find(".long").html(
				"<a class='toggleComment' href='#'>"+$(this).find(".long").html()+"</a>"
				);
			$(this).find("a.toggleComment").click(function(){
				$(this).closest(".recentComment").find(".short").toggle();
				$(this).closest(".recentComment").find(".long").toggle();
				return false;
			}).hover(function(){
				var imgs = $(this).closest(".recentComment").find("img");
				imgs.each(function(i, img){
					$(img).attr("src", $(img).attr("src").replace(".gif", "_over.gif"));
				});
			}, function(){
				var imgs = $(this).closest(".recentComment").find("img");
				imgs.each(function(i, img){
					$(img).attr("src", $(img).attr("src").replace("_over.gif", ".gif"));
				});
			});
		}
	});
});

