	(function($){
		
		var colors = [
			"#FA8072","#DC143C","#B22222","#FF69B4","#FF1493","#C71585","#DB7093",
			"#FF6347","#FF4500","#FF8C00","#FFA500","#6A5ACD","#483D8B","#7B68EE",
			"#32CD32","#00AA5A","#3CB371","#2E8B57","#228B22","#008000","#006400",
			"#008B8B","#008080","#40E0D0","#48D1CC","#00CED1","#4682B4","#00BFFF",
			"#1E90FF","#6495ED","#7B68EE","#4169E1","#0000CD","#000080" ];
		
		/* Generic Functions */
		
		var messages = [
			"-> -> -> Groove Spoon is a band that you might like. <- <- <-",
			"-> -> -> Groove Spoon plays shows around Ann Arbor. <- <- <-",
			"-> -> -> Did you know that Groove Spoon is awesome? <- <- <-"
		];
		var message_index = 0;
		var m_index = 0;
		var message_growing = true;
		
		function marquee() {
			var message = messages[m_index];
			var message_length = message.length;
			$("div#names").text(message.substr(0,message_index));
			if(message_growing && message_index < message_length) message_index += 1;
			else if(!message_growing && message_index > 0) message_index -= 1;
			else {
				message_growing = !message_growing;
				if(message_index == 0) {
					m_index += 1;
					if(m_index == messages.length) m_index = 0;
				}
			}
		}
		
		function random_color() {
			return colors[Math.floor(Math.random()*colors.length)];
		}
		
		function safe_tumblr_read(url,page) {
			url = "/getters/tumblr.php"+url;
			$.ajax({
				type:"GET",
				url:url,
				dataType:"script",
				error: function() {
					alert("error");
				},
				success: function() {
					page.tumblrize(tumblr_api_read);
				}
			});
		}
		
		/* Front Page */
		
		function FrontPage() {
			this.fit_to_screen();
			$("div#content").append(
				"<div id='form'><form id='email'><input type='text' name='email'/></form></div>"
				+"<div id='new_stuff'><div id='interior'><img src='public/image/loading-small.gif'/></div></div>");
			$("div#vector-logo").append("<div id='first' class='ill'></div>");
			this.first = Raphael("first",700,$(window).height()/2.7);
			this.second = Raphael("second",700,$(window).height()/2.7);
			this.letters = [];
			this.letters2 = [];
			this.opacity = 1.0;
			this.logo = 0;
			var that = this;
			$.ajax({
				type:"GET",
				url:"/public/vector/groove_spoon.svg",
				data:"xml",
				success:function(data){
					that.draw_name(data);
					that.colorize();
					safe_tumblr_read("?num=10",that);
				}
			});
			
			var email_message = "Provide your email and press enter! (to receive emails from us)";
			$("form#email input").attr("value",email_message);
			// listen for an add email event
			$("form#email input").focus(function(){
				if($(this).attr("value") == email_message)
					$(this).attr("value","");
			});
			$("form#email input").blur(function(){
				if($(this).attr("value") == "")
					$(this).attr("value",email_message);
			});
			$("form#email").submit(function(){
				email = $(this).find("input").attr("value");
				var that = this;
				jQuery.ajax({
					type:"POST",
					url:"/setters/mail.php",
					data:"email="+email,
					success:function() {
						alert("Thanks "+email+"! You will now receive emails from Groove Spoon... probably...");
						$(that).find("input").remove();
					}
				});
				return false;
			});
		}
		
		FrontPage.prototype.draw_name = function(data) {
			var that = this;
			$(data).find("path").each(function(){
				var path = $(this).attr('d');
				var id = $(this).attr('id');
				var scale = ($(window).height()/3.5)/250;
				that.letters[id] = that.first.path(path)
					.attr({"stroke":"white","stroke-width":"2px"}).scale(scale,scale,0,0);
				that.letters2[id] = that.second.path(path)
					.attr({"fill":"#ccc","stroke":"none"}).scale((scale),(scale),0,0);
			});
		};
		
		FrontPage.prototype.fit_to_screen = function() {
			var w_height = $(window).height();
			var half_height = (w_height/2);
			$("div#top").css('height',(half_height)+"px");
			$("div#bottom").css('height',($(window).height()/2)+"px");
			$("div#content").css('marginTop',(half_height)+"px");
			$("div#content").css('height',($(window).height())+"px");
			$("div#content").find("div.column").css('height',($(window).height())+"px");
			$("div#content").find("div.pad").css('height',($(window).height())+"px");
			$("div#first").css('height',$(window).height()/2.7).css('width',$(window).width()/1.5);
			$("div#second").css('height',$(window).height()/2.7).css('width',$(window).width()/1.5);
			$("div#input").css('bottom',($(window).height()/2)+5);
		};
		
		FrontPage.prototype.colorize = function() {
			var color = random_color();
			$("div#menu a").css('color',color);
			$("#top").css('backgroundColor',color);
			for(var l in this.letters2)
				this.letters2[l].attr({"fill":color});
			$("div#new_stuff").css('border-color',color);
		};
		
		FrontPage.prototype.tumblrize = function(tumblr_api_read) {
			$("div#interior").empty();
			for(var post in tumblr_api_read['posts']) {
				this.add_new_post(tumblr_api_read['posts'][post]);
			}
		};
		
		FrontPage.prototype.add_new_post = function(post) {
			var id = post['id'];
			var title = post['regular-title'];
			var tag = post['tags'][0];
			var content = post['regular-body'];
			if(tag == "videos") {
				var ytid = content.match(/\/v\/[^&]+(&|$)/)[0].replace(/(\/v\/|&)/g,"");
				content = "<img src='http://i2.ytimg.com/vi/"+ytid+"/default.jpg'/>";
			}
			else {
				content = "<p>"+$(content).text().substring(0,50)+".......</p>";
			}
			// and write out	
			$("div#interior").append(
				"<a href='/"+tag+"/"+id+"'>"+title+"<span>"+tag+"</span><small>"+content+"</small></a>");
		};
		
		/* Content Page */
		
		function ContentPage() {
			this.fit_to_screen();
			$("div#content").append("<div id='new_stuff'></div>");
			this.second = Raphael("second",700,$(window).height()/3.5);
			this.letters2 = [];
			var that = this;
			$.ajax({
				type:"GET",
				url:"/public/vector/groove_spoon.svg",
				data:"xml",
				success:function(data){
					that.draw_name(data);
					that.colorize();
					that.load_content();
					that.link_fake();
				}
			});
		}
		
		ContentPage.prototype.draw_name = function(data) {
			var opacity = 1.0;
			var that = this;
			$(data).find("path").each(function(){
				var path = $(this).attr('d');
				var id = $(this).attr('id');
				var scale = ($(window).height()/3.5)/250;
				that.letters2[id] = that.second.path(path)
					.attr({"fill":"deeppink","stroke":"none","stroke":"white","stroke-width":"2px","opacity":opacity})
					.scale((scale),(scale),0,0);
					opacity -= 0.05;
			});
		};
		
		ContentPage.prototype.fit_to_screen = function() {
			$("div#content").find("div.no-border").parent().css('height',($(window).height())+"px");
			$("div#content").find("div.no-border").parent().parent().css('height',($(window).height())+"px");
		};
		
		ContentPage.prototype.colorize = function() {
			var color = random_color();
			$("div#new_stuff").css('border-color',color).css('opacity',0.2);
			for(var l in this.letters2)
				this.letters2[l].attr({"fill":color});
		};
		
		ContentPage.prototype.link_fake = function() {
			$("div#second").css('cursor','pointer');
			$("div#second").click(function(){
				window.location.href = "/";
			});
			$("div#second").hover(
				function() {
					$(this).css("background-color",'white');
				},
				function() {
					$(this).css("background-color","transparent");
				}
			);
		};
		
		ContentPage.prototype.highlight = function(section) {
			$("div#menu a[href='/"+section+"']").addClass("highlighted");
		};
		
		ContentPage.prototype.load_content = function() {
			var parts = window.location.href.split("/");
			var section = parts[3];
			this.highlight(section);
			var post = parts[4];
			$("div#content").css('background','url(/public/image/sections/'+section+'.png) repeat-y');
			if(post == undefined) {
				var url = "?tagged="+section;
				this.single_post = false;
			}
			else {
				var url = "?id="+post;
				this.single_post = true;
			}
			safe_tumblr_read(url,this);
		};
		
		ContentPage.prototype.tumblrize = function(tumblr_api_read) {
			if(this.single_post) this.tumblrize_single(tumblr_api_read);
			else this.tumblrize_many(tumblr_api_read);
		};
		
		ContentPage.prototype.tumblrize_many = function(tumblr_api_read) {
			var posts = tumblr_api_read['posts'];
			$("div#tumblr").empty();
			for(var p in posts) {
				var tag = posts[p]['tags'][0];
				var id = posts[p]['id'];
				var url = "/"+tag+"/"+id;
				var title = posts[p]['regular-title'];
				var body = posts[p]['regular-body'];
				$("div#tumblr").append(
					"<div class='post'><h5><a href='"+url+"'>"+title+"</a></h5>"
					+"<div class='body'>"+body+"</div>"
					+"<div class='comment_bar'><a href='"+url+"#disqus_thread'>Leave a Comment // Read a Comment</a></div></div>"
				);
			}
		};
		
		ContentPage.prototype.tumblrize_single = function(tumblr_api_read) {
			var posts = tumblr_api_read['posts'];
			$("img#loading_graphic").remove();
			for(var p in posts) {
				$("div#tumblr").prepend(
					"<div class='post'><h5>"+posts[p]['regular-title']+"</h5>"
					+"<div class='body'>"+posts[p]['regular-body']+"</div></div>"
				);
			}
			$("#disqus_thread").fadeIn();
		};
		
		/* ----------- */
		/* Runner Code */
		/* ----------- */
		
		$.Grvspn = function() { };

		$.Grvspn.front = function() { // the front page
			var page = new FrontPage();
			
			setInterval(marquee,50);
			// watch out for resizing
			$(window).resize(function(){
				var scale = $(window).height()/485;
				page.fit_to_screen();
				scale = ($(window).height()/3.5)/250;
				for(var l in page.letters) {
					page.letters[l].scale(scale,scale,0,0);
					page.letters2[l].scale(scale,scale,0,0);
				}
			});
		};
		
		$.Grvspn.content = function() {
			var page = new ContentPage();
			
			// watch out for resizing
			$(window).resize(function(){
				var scale = $(window).height()/485;
				page.fit_to_screen();
				$("#second").css("width",$(window).width()/2.5).css("height",$(window).height()/3.5);
				scale = ($(window).height()/3.5)/250;
				for(var l in page.letters2) {
					page.letters2[l].scale(scale,scale,0,0);
				}
			});
			
			$("a[href='/jam']").click(function(){
				alert("You cannot jam... yet. But there is a video you can watch.");
				return false;
			});
		};
	
	})(jQuery);
