$(function() {
		
	if(window.location.hash+"">"") {
		$("#left").load("inc." + (window.location.hash+"").split("!")[1] + ".html",renderTitles);
	} else {
		window.location.hash="#!home";	
	}

	// highlight the active page
	$("#navHover").css({left:$("#nav a.active").position().left+10});
	
	function renderTitles() {
		// put caps on titles
		$("#right .title").each(function() {
			if($(this).find("img").length==0) {
				$(this).prepend("<img src='images/title_left.jpg' style='padding-right:10px; float:left;'>");
				$(this).append("<img src='images/title_right.jpg' style='margin-right:-2px; float:right;'>");				
			}
		});
		$("#left .title").each(function() {
			if($(this).find("img").length==0) {										
				$(this).prepend("<img src='images/title0_left.jpg' style='padding-right:10px; float:left;'>");
				$(this).append("<img src='images/title0_right.jpg' style='margin-right:-2px; float:right;'>");				
			}
		});
	}
	renderTitles();
	
	$("#logo").live("click",function() {
		$("#nav a:first").click();								 
	});

	// nav highlighter
	$("#nav a, #footer a").live("click",function(e) {
		
		e.preventDefault();
		if(window.location.hash!="#!" + $(this).attr("title")) {		
			$("#nav a.active").removeClass("active");
			$(this).addClass("active");
			if($(this).is("#nav a")) {
				$("#navHover").animate({left:$("#nav a.active").position().left+10},100);	
			}
			var tag = this;
			$("#left").fadeOut(function() {
				$("#left").load($(tag).attr("href"),function() {
					if($(tag).is("#footer a")) {
						$('html,body').animate({scrollTop: 560}, 1000);
					}
					renderTitles();
					$("#left").fadeIn();																				 
				});
			});
			window.location = "#!" + $(this).attr("title");
		}
	});
	
	// load tweets			
	$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=jasonmcleod&callback=?",function(msg) {
		for(var t=0;t<Math.min(msg.length,10);t++) {
			var timestamp = msg[t].created_at.replace(/(\+\S+) (.*)/, '$2 $1')
			var date = new Date(Date.parse(timestamp)).toLocaleDateString();
			var time = new Date(Date.parse(timestamp)).toLocaleTimeString();
			date = date.replace("2010","");
			time = time.split(":");
			time = time[0] + ":" + time[1] + time[2].split(" ")[1];
			var showdate = date +' • ' + time;
			$("#tweets").append("<div class='tweet'><img align='left' src='" + msg[t].user.profile_image_url + "' style='clear:both'><div class='text'>" + msg[t].text + "</div></div>");				
			//$("#tweets").append("<div class='tweet'><div class='text'>" + msg[t].text + "</div></div>");				
		}
	});
	
	// :D
	var tc = 0;
	$("#tower").live("click",function() {
		tc++;
		if(tc>=4) {
			$("#tower").raptorize({enterOn:'click'});
		}
	});
	
	$("form").submit(function() {
		return false;
	});
	
	// I KNOW I KNOW THIS IS TERRIBLE.
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID) {
		$("#tower").remove() // RAMPAGE!
	}
	
});
