jQuery.fn.extend({
	openCallback : function() {
		//console.log($(this).attr("id")+" is open.");
		switch($(this).attr("id")) {
			case "design":
				portfolioOpen();
				break;
			case "blog":
				blogOpen();
				break;
			case "twitter":
				twitterOpen();
				break;
			case "listen":
				lastfmOpen();
				break;
			case "shoot":
				flickrOpen();
				break;
		}
	},
	closeCallback : function() {
		//console.log($(this).attr("id")+" is closed.");
		switch($(this).attr("id")) {
			case "design":
				portfolioClose();
				break;
			case "blog":
				blogClose();
				break;
			case "twitter":
				twitterClose();
				break;
			case "listen":
				lastfmClose();
				break;
			case "shoot":
				flickrClose();
				break;
		}
	}
});

function awwwwwYeaHitIt() {
	$("#overlay").hide();
	$("#ieWarning").remove();
	
	$(".item").each(function() {
		$(this).bind("click", function() {
			if($(this).hasClass("expanded")) {
				closeItem(this);
			} else {
				openItem(this);
			}
		});
	});
	
	$("#brew a.bucket").bind("click", function(){
		var imgEnlarged = $("<div />").addClass("imgEnlarged");
		var thumbSrc = $("img", this).attr("src");
		var enlargedSrc = "images/coffee/"+thumbSrc.substring(21);
		$("<img/>").attr({"src":enlargedSrc,"alt":""}).appendTo(imgEnlarged);
		imgEnlarged.appendTo("#overlay");
		$("#overlay").fadeIn("normal").bind("click", function() {
			$(".imgEnlarged").fadeOut("normal", function() { $(this).remove(); });
			$(this).unbind("click").fadeOut("normal");
		});
	});
	
	$(".item a, .item object").bind("click", function(event){ event.stopPropagation(); });
	
	setupKeyboardCommands();
}

function openItem(whichItem) {
	if($(whichItem).length && !$(whichItem).hasClass("expanded")) {
		$(".item.expanded").each(function() {
			closeItem(this);
		});
		$(".item.selected").removeClass("selected");
		$(".plus", whichItem).fadeOut("normal");
		$(whichItem).addClass("selected").animate({"height":"280px"}, "normal", function() {
			$(whichItem).addClass("expanded");
			$(".content", whichItem).fadeIn("normal", function(){
				$(this.parentNode).openCallback();
			});
		});
	}
}

function closeItem(whichItem) {
	if($(whichItem).length && $(whichItem).hasClass("expanded")) {
		$(whichItem).animate({"height":"70px"}, "normal", function() {
			$(whichItem).removeClass("expanded");
		});
		$(".content", whichItem).fadeOut("normal", function(){
			$(".plus", this.parentNode).fadeIn("normal");
			$(this.parentNode).closeCallback();
		});
	}
}

function portfolioOpen() {
	portfolioClose();
	$.getJSON("servers/portfolio.server.php", function(items) {
		$("#design .content .loading").fadeOut("normal");
		for(var i in items) {
			var thisItem = items[i];
			var p = $("<p/>", {"class":"portfolioItem"});
			var a = $("<a/>", {"href":"http://portfolio.jayeb.com/#"+thisItem.id}).appendTo(p);
			var img =
				$("<img/>", {
					"src":stripslashes(thisItem.thumbnail),
					"alt":""
				}).appendTo(a);
			var title =
				$("<span/>", {
					"class":"itemTitle",
					"text":stripslashes(thisItem.title)
				}).hide().css({
					"background-image":"url(http://jayeb.com/alpha/"+thisItem.background+"/90)",
					"color":"#"+thisItem.foreground
				}).appendTo(a);
			var brief =
				$("<span/>", {
					"class":"itemBrief",
					"text":stripslashes(thisItem.brief)
				}).hide().css({
					"background-image":"url(http://jayeb.com/alpha/"+thisItem.background+"/90)",
					"color":"#"+thisItem.foreground
				}).appendTo(a);
			p.insertBefore("#portfolioItems .clear").hide().fadeIn("normal").hover(
				function() {						
					$(".itemTitle", this).slideDown(300);
					$(".itemBrief", this).slideDown(300);
				},
				function() {
					$(".itemTitle", this).slideUp(300);
					$(".itemBrief", this).slideUp(300);
				}
			);
		}
		$("#portfolioItems a").bind("click", function(event){ event.stopPropagation(); });
	});
}
function portfolioClose() {
	$("#portfolioItems *").unbind("click");
	$("#portfolioItems .portfolioItem").remove();
	$("#design .content .loading").show();
}

function blogOpen() {
	blogClose();
	$.getJSON("servers/blog.server.php", {}, function(data) {
		$("#blog .content .loading").fadeOut("normal");
		$.each(data, function(b,blogData) {
			var blog = $("<div/>").addClass("blog");
			var blogTitle = $("<h4/>").addClass("blogTitle").appendTo(blog);
			$("<a/>").attr({"href":blogData.url}).html(blogData.title).appendTo(blogTitle);
			//$("<p/>").addClass("blogDate").text(blogData.date).appendTo(blog);
			$("<p/>").addClass("blogDescription").html(blogData.description).appendTo(blog);
			$("<a/>").addClass("blogReadMore").attr({"href":blogData.url}).text("Read more...").appendTo(blog);
			blog.insertBefore("#recentBlogs .clear").hide().fadeIn("normal");
			$("#recentBlogs a").bind("click", function(event){ event.stopPropagation(); });
		});
	});
}

function blogClose() {
	$("#recentBlogs *").unbind("click");
	$("#recentBlogs .blog").remove();
	$("#blog .content .loading").show();
}

function twitterOpen() {
	twitterClose();
	$.getJSON("servers/twitter.server.php", {}, function(data) {
		$("#twitter .content .loading").fadeOut("normal");
		$.each(data, function(t,tweetData) {
			var tweet = $("<p/>").addClass("tweet");
			var lQuo = $("<span/>").addClass("lquote").html("&ldquo;");
			var rQuo = $("<span/>").addClass("rquote").html("&rdquo;");
			var tweetText = replaceTwitterNamesWithHTMLLinks(replaceURLWithHTMLLinks(tweetData.tweet));
			$("<span/>").addClass("tweetText").html(tweetText).prepend(lQuo).append(rQuo).appendTo(tweet);
			$("<span/>").addClass("tweetDate").text(tweetData.date).appendTo(tweet);
			tweet.insertBefore("#recentTweets .clear").hide().fadeIn("normal");
			$("#recentTweets a").bind("click", function(event){ event.stopPropagation(); });
		});
	});
}
function twitterClose() {
	$("#recentTweets *").unbind("click");
	$("#recentTweets .tweet").remove();
	$("#twitter .content .loading").show();
}

function lastfmOpen() {
	lastfmClose();
	$.getJSON("servers/lastfm.server.php", {}, function(data) {
		$("#listen .content .loading").fadeOut("normal");
		$.each(data.recentTracks, function(t,trackData) {
			var track = $("<a/>").addClass("track").attr({"href":trackData.url});
			$("<img/>").addClass("trackArt").attr({"src":trackData.artistImageUrl,"alt":""}).appendTo(track);
			$("<span/>").addClass("trackTitle").text(trackData.title).appendTo(track);
			$("<span/>").addClass("trackArtist").text(trackData.artist).appendTo(track);
			track.insertBefore("#recentTracks .clear").hide().fadeIn("normal");
			$("#recentTracks a").bind("click", function(event){ event.stopPropagation(); });
		});
		$.each(data.weeklyArtists, function(a,artistData) {
			var artist = $("<a/>").addClass("artist").attr({"href":artistData.url, "title":artistData.name+": "+artistData.playCount+" plays"});
			$("<img/>").addClass("artistImage").attr({"src":artistData.artistImageUrl,"alt":"Artist Image"}).appendTo(artist);
			$("<span/>").addClass("artistName").text(artistData.name).appendTo(artist);
			artist.insertBefore("#recentArtists .clear").hide().fadeIn("normal");
			$("#recentArtists a").bind("click", function(event){ event.stopPropagation(); });
		});
	});
}
function lastfmClose() {
	$("#recentTracks *").unbind("click");
	$("#recentArtists *").unbind("click");
	$("#recentTracks .track").remove();
	$("#recentArtists .artist").remove();
	$("#listen .content .loading").show();
}

function flickrOpen() {
	flickrClose();
	$.getJSON("servers/flickr.server.php", {}, function(data) {
		$("#shoot .content .loading").fadeOut("normal");
		$.each(data, function(p,photoData) {
			var photo = $("<a/>").addClass("photo").attr({"href":photoData.url,"title":photoData.title});
			$("<img/>").attr({"src":photoData.imgUrl,"alt":""}).appendTo(photo);
			$("#recentPhotos a").bind("click", function(event){ event.stopPropagation(); });
			photo.insertBefore("#recentPhotos .clear").hide().fadeIn("normal");
		});
	});
}
function flickrClose() {
	$("#recentPhotos *").unbind("click");
	$("#recentPhotos .photo").remove();
	$("#shoot .content .loading").show();
}

// Courtesy of http://www.reynoldsftw.com/
function replaceURLWithHTMLLinks(text) {  
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;  
	return text.replace(exp,"<a href='$1'>$1</a>");  
}

function replaceTwitterNamesWithHTMLLinks(text) {  
	var exp = /@([a-zA-Z0-9_]+)/g;  
	return text.replace(exp,"<a href='http://twitter.com/$1'>@$1</a>");  
} 

// addslashes and stripslashes functions from http://snipplr.com/view/13396/addslahes-stripslashes/
function addslashes(str) {
   str=str.replace(/\'/g,'\\\'');
   str=str.replace(/\"/g,'\\"');
   str=str.replace(/\\/g,'\\\\');
   str=str.replace(/\0/g,'\\0');
   return str;
}
 
function stripslashes(str) {
   str=str.replace(/\\'/g,'\'');
   str=str.replace(/\\"/g,'"');
   str=str.replace(/\\\\/g,'\\');
   str=str.replace(/\\0/g,'\0');
   return str;
}

