var urlSource = $.url.attr("source");
if(/\/stream\/?/.test(urlSource)){
	$("head").append('<link rel="alternate" title="Drew Wilson\'s Stream" type="application/rss+xml" href="http://drewwilson.com/feed/lifestream/1">');
	frSuccess = function(){
		if(/\/tweet\/?/.test(urlSource)){
		    var urlReg = / ((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
		    var hashReg = / [\#]+([A-Za-z0-9-_]+)/gi;
		    var userReg = /[\@]+([A-Za-z0-9-_]+)/gi; 
	        var h2_html = $("header h2.twitter").html();
	        h2_html = h2_html.replace(userReg," <a href=\"http://twitter.com/$1\">@$1</a>");
	        h2_html = h2_html.replace(hashReg," <a href=\"http://search.twitter.com/search?q=%23$1\">#$1</a>");
	        $("header h2.twitter").html(h2_html.replace(urlReg," <a href=\"$1\">$1</a>"));
		}
		var dt = $("header.entry time").html();
		dt = dt.split(/\s/g);
		var time = dt[4];
		if(time.length < 5){ time = "0"+time; }
		var day = dt[1].slice(0,dt[1].length-1);
		if(day.length < 2){ day = "0"+day; }
		$("header.entry time").attr("datetime",dt[2]+"-"+dt[0]+"-"+day+"T"+time).html(dt[0] + " <strong>" + toWords(dt[1].slice(0,dt[1].length-1), "date") + "</strong>" + toWords(dt[2], "digit") + " <span>" + dt[4] + " " + dt[5] + "</span>");
	}
} else if(/\/profile\/?/.test(urlSource)){
	$(function(){
		$("#ft-bottom ul li").removeClass("active");
		$("#ft-bottom ul li:eq(2)").addClass("active");
	});
} else if(/\/selections\/?/.test(urlSource)){
	$(function(){
		$("#ft-bottom ul li").removeClass("active");
		$("#ft-bottom ul li:eq(1)").addClass("active");
	});
	frSuccess = function(){
		$("img").fullsize({useIcon: false, useRel: true, forceWidth: true, autoResize: true});
		$("article.selection dd img").not("dd.five div:first-child img, dd.three div:first-child img, dd.four div:first-child img, dd.two img").imgCenter();
		$("dd.five div:first-child img, dd.two img, dd.three div:first-child img, dd.four div:first-child img").imgCenter({centerVertical: false});
	}
} else {
	$(function(){
		$("head").append('<link rel="alternate" title="Drew Wilson\'s Stream" type="application/rss+xml" href="http://drewwilson.com/feed/lifestream/1">');
		var content_limit = 10;	
		var content_offset = 0;		
		var stream_cntrl = $.controller.array("lifestream/lifestream_items",{limit: content_limit, lifestream_id: "1"});

		function stream_formatter(){	
			$("article.stream").format(function(elem, data){
				var element = $(elem);	
				if(data._model == "blog_entry" && data.blog_category_id == "1"){
					element.children("a.link").attr("href","http://www.drewwilson.com/stream/blog/"+data.sef_title);
					element.attr("id","entry-"+data.import_id);
					element.find("header h2 a").attr("href",data.title_link).text(data.title);
					element.children("p").html(data.short_description);
					var date_fn = $.formatDate("F j Y", "created_at", true);
					date_fn(element.find("time span:first"), data);
					var time_fn = $.formatDate("g:i a", "created_at", true);
					time_fn(element.find("time span:last"), data);
				} else if(data._model == "blog_entry" && data.blog_category_id == "2"){
					element.children("a.link").attr("href","http://code.drewwilson.com/entry/"+data.sef_title);
					element.addClass("code");
					element.find("header h2 a").attr("href",data.title_link).text(data.title);
					element.attr("id","entry-"+data.import_id);
					element.children("time").remove();
					element.children("p").text(data.short_description);
				} else if(data.service == "twitter"){
					element.children("a.link").attr("href","http://www.drewwilson.com/stream/tweet/"+data.sef_title);
					element.addClass("twitter");
					element.children("header, time").remove();
					element.children("p").text(data.text);
				} else if(data._model == "gallery"){
					element.children("a.link").attr("href","http://www.drewwilson.com/stream/gallery/"+data.sef_title);
					element.addClass("gallery").css({"background-image":"url(http://drewwilson.com/upload/media/"+data.original+")"});
					element.find("header h2 a").attr("href",data.title_link).text(data.title);
					element.find("header").append($("<p></p>").html(data.short_description));
				}
			});	
		}
		stream_formatter();
		$("#the_stream").template(stream_cntrl, {success: function(){
			$.controller.array("data/entry-backgrounds", {}, {success: function(data){
				for(var i=0;i < data.count;i++){
					$("#entry-"+data.items[i].entry_id).css({"background-image":"url(http://drewwilson.com/upload/data/2/"+data.items[i].image+")"}).show();
				}
			}});
		}});

		var last_call = false;
		if($.browser.safari){ 
			$("body").scroll(function(){
				if (($("body div.container:first").offset()['top']) == ($(document).height() - $("body div.container:first").height() -80)){
					scroll_templater();
				}
			}); 
		} else {
			$(window).scroll(function(){
				if ($(window).scrollTop() == ($(document).height() - $(window).height())){
					scroll_templater();
				}
			}); 
		}
		
		function scroll_templater(){
			if(!last_call){
				$("#loading").show();
				content_offset = content_offset + 10;
				var article = $('<div id="articles-'+content_offset+'" style="display:none;"><article class="stream"><a href="" class="link">More</a><header><h2><a href=""></a></h2></header><p></p><time pubdate datetime="2009-05-23T11:38Z"><span></span> <span></span></time></article></div>');
				$("#the_stream").append(article);
				var new_stream_cntrl = $.controller.array("lifestream/lifestream_items",{limit: content_limit, offset: content_offset, lifestream_id: "1"},{success: function(data){
					if($.objCount(data.items[0]) <= 0){ 
						last_call = true;
					}
				}});
				stream_formatter();
						
				$("#articles-"+content_offset).template(new_stream_cntrl, {success: function(){
					$("#articles-"+content_offset).show();
					$.controller.array("data/entry-backgrounds", {}, {success: function(data){
						for(var i=0;i < data.count;i++){
							$("#articles-"+content_offset+" #entry-"+data.items[i].entry_id).css({"background-image":"url(http://drewwilson.com/upload/data/2/"+data.items[i].image+")"}).show();
						}
						$("#loading").hide();
					}});
				}});
			}
		}
	});
}

// Convert Dates & Numbers to Words
// Original Funciton is copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
// Function enhanced to handle Dates by Drew Wilson http://www.drewwilson.com
var th = ['','thousand','million','billion','trillion']; var date_dg = ['zero','first','second','third','fourth', 'fifth','sixth','seventh','eighth','nineth']; var date_tn = ['tenth','eleventh','twelveth','thirteenth', 'fourteenth','fifteenth','sixteenth', 'seventeenth','eighteenth','nineteenth']; var digit_dg = ['zero','one','two','three','four','five','six','seven','eight','nine']; var digit_tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']; function toWords(s,type){if(type == "date"){ var dg = date_dg; var tn = date_tn; } else { var dg = digit_dg; var tn = digit_tn; } s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != String(parseFloat(s))) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} if(type == "date"){ if(str == "twenty"){str == "twentieth";}if(str == "thirty"){str == "thirtieth";} } return str.replace(/\s+/g,' ');}

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */
var hexcase=0;function hex_md5(a){return rstr2hex(rstr_md5(str2rstr_utf8(a)))}function hex_hmac_md5(a,b){return rstr2hex(rstr_hmac_md5(str2rstr_utf8(a),str2rstr_utf8(b)))}function md5_vm_test(){return hex_md5("abc").toLowerCase()=="900150983cd24fb0d6963f7d28e17f72"}function rstr_md5(a){return binl2rstr(binl_md5(rstr2binl(a),a.length*8))}function rstr_hmac_md5(c,f){var e=rstr2binl(c);if(e.length>16){e=binl_md5(e,c.length*8)}var a=Array(16),d=Array(16);for(var b=0;b<16;b++){a[b]=e[b]^909522486;d[b]=e[b]^1549556828}var g=binl_md5(a.concat(rstr2binl(f)),512+f.length*8);return binl2rstr(binl_md5(d.concat(g),512+128))}function rstr2hex(c){try{hexcase}catch(g){hexcase=0}var f=hexcase?"0123456789ABCDEF":"0123456789abcdef";var b="";var a;for(var d=0;d<c.length;d++){a=c.charCodeAt(d);b+=f.charAt((a>>>4)&15)+f.charAt(a&15)}return b}function str2rstr_utf8(c){var b="";var d=-1;var a,e;while(++d<c.length){a=c.charCodeAt(d);e=d+1<c.length?c.charCodeAt(d+1):0;if(55296<=a&&a<=56319&&56320<=e&&e<=57343){a=65536+((a&1023)<<10)+(e&1023);d++}if(a<=127){b+=String.fromCharCode(a)}else{if(a<=2047){b+=String.fromCharCode(192|((a>>>6)&31),128|(a&63))}else{if(a<=65535){b+=String.fromCharCode(224|((a>>>12)&15),128|((a>>>6)&63),128|(a&63))}else{if(a<=2097151){b+=String.fromCharCode(240|((a>>>18)&7),128|((a>>>12)&63),128|((a>>>6)&63),128|(a&63))}}}}}return b}function rstr2binl(b){var a=Array(b.length>>2);for(var c=0;c<a.length;c++){a[c]=0}for(var c=0;c<b.length*8;c+=8){a[c>>5]|=(b.charCodeAt(c/8)&255)<<(c%32)}return a}function binl2rstr(b){var a="";for(var c=0;c<b.length*32;c+=8){a+=String.fromCharCode((b[c>>5]>>>(c%32))&255)}return a}function binl_md5(p,k){p[k>>5]|=128<<((k)%32);p[(((k+64)>>>9)<<4)+14]=k;var o=1732584193;var n=-271733879;var m=-1732584194;var l=271733878;for(var g=0;g<p.length;g+=16){var j=o;var h=n;var f=m;var e=l;o=md5_ff(o,n,m,l,p[g+0],7,-680876936);l=md5_ff(l,o,n,m,p[g+1],12,-389564586);m=md5_ff(m,l,o,n,p[g+2],17,606105819);n=md5_ff(n,m,l,o,p[g+3],22,-1044525330);o=md5_ff(o,n,m,l,p[g+4],7,-176418897);l=md5_ff(l,o,n,m,p[g+5],12,1200080426);m=md5_ff(m,l,o,n,p[g+6],17,-1473231341);n=md5_ff(n,m,l,o,p[g+7],22,-45705983);o=md5_ff(o,n,m,l,p[g+8],7,1770035416);l=md5_ff(l,o,n,m,p[g+9],12,-1958414417);m=md5_ff(m,l,o,n,p[g+10],17,-42063);n=md5_ff(n,m,l,o,p[g+11],22,-1990404162);o=md5_ff(o,n,m,l,p[g+12],7,1804603682);l=md5_ff(l,o,n,m,p[g+13],12,-40341101);m=md5_ff(m,l,o,n,p[g+14],17,-1502002290);n=md5_ff(n,m,l,o,p[g+15],22,1236535329);o=md5_gg(o,n,m,l,p[g+1],5,-165796510);l=md5_gg(l,o,n,m,p[g+6],9,-1069501632);m=md5_gg(m,l,o,n,p[g+11],14,643717713);n=md5_gg(n,m,l,o,p[g+0],20,-373897302);o=md5_gg(o,n,m,l,p[g+5],5,-701558691);l=md5_gg(l,o,n,m,p[g+10],9,38016083);m=md5_gg(m,l,o,n,p[g+15],14,-660478335);n=md5_gg(n,m,l,o,p[g+4],20,-405537848);o=md5_gg(o,n,m,l,p[g+9],5,568446438);l=md5_gg(l,o,n,m,p[g+14],9,-1019803690);m=md5_gg(m,l,o,n,p[g+3],14,-187363961);n=md5_gg(n,m,l,o,p[g+8],20,1163531501);o=md5_gg(o,n,m,l,p[g+13],5,-1444681467);l=md5_gg(l,o,n,m,p[g+2],9,-51403784);m=md5_gg(m,l,o,n,p[g+7],14,1735328473);n=md5_gg(n,m,l,o,p[g+12],20,-1926607734);o=md5_hh(o,n,m,l,p[g+5],4,-378558);l=md5_hh(l,o,n,m,p[g+8],11,-2022574463);m=md5_hh(m,l,o,n,p[g+11],16,1839030562);n=md5_hh(n,m,l,o,p[g+14],23,-35309556);o=md5_hh(o,n,m,l,p[g+1],4,-1530992060);l=md5_hh(l,o,n,m,p[g+4],11,1272893353);m=md5_hh(m,l,o,n,p[g+7],16,-155497632);n=md5_hh(n,m,l,o,p[g+10],23,-1094730640);o=md5_hh(o,n,m,l,p[g+13],4,681279174);l=md5_hh(l,o,n,m,p[g+0],11,-358537222);m=md5_hh(m,l,o,n,p[g+3],16,-722521979);n=md5_hh(n,m,l,o,p[g+6],23,76029189);o=md5_hh(o,n,m,l,p[g+9],4,-640364487);l=md5_hh(l,o,n,m,p[g+12],11,-421815835);m=md5_hh(m,l,o,n,p[g+15],16,530742520);n=md5_hh(n,m,l,o,p[g+2],23,-995338651);o=md5_ii(o,n,m,l,p[g+0],6,-198630844);l=md5_ii(l,o,n,m,p[g+7],10,1126891415);m=md5_ii(m,l,o,n,p[g+14],15,-1416354905);n=md5_ii(n,m,l,o,p[g+5],21,-57434055);o=md5_ii(o,n,m,l,p[g+12],6,1700485571);l=md5_ii(l,o,n,m,p[g+3],10,-1894986606);m=md5_ii(m,l,o,n,p[g+10],15,-1051523);n=md5_ii(n,m,l,o,p[g+1],21,-2054922799);o=md5_ii(o,n,m,l,p[g+8],6,1873313359);l=md5_ii(l,o,n,m,p[g+15],10,-30611744);m=md5_ii(m,l,o,n,p[g+6],15,-1560198380);n=md5_ii(n,m,l,o,p[g+13],21,1309151649);o=md5_ii(o,n,m,l,p[g+4],6,-145523070);l=md5_ii(l,o,n,m,p[g+11],10,-1120210379);m=md5_ii(m,l,o,n,p[g+2],15,718787259);n=md5_ii(n,m,l,o,p[g+9],21,-343485551);o=safe_add(o,j);n=safe_add(n,h);m=safe_add(m,f);l=safe_add(l,e)}return Array(o,n,m,l)}function md5_cmn(h,e,d,c,g,f){return safe_add(bit_rol(safe_add(safe_add(e,h),safe_add(c,f)),g),d)}function md5_ff(g,f,k,j,e,i,h){return md5_cmn((f&k)|((~f)&j),g,f,e,i,h)}function md5_gg(g,f,k,j,e,i,h){return md5_cmn((f&j)|(k&(~j)),g,f,e,i,h)}function md5_hh(g,f,k,j,e,i,h){return md5_cmn(f^k^j,g,f,e,i,h)}function md5_ii(g,f,k,j,e,i,h){return md5_cmn(k^(f|(~j)),g,f,e,i,h)}function safe_add(a,d){var c=(a&65535)+(d&65535);var b=(a>>16)+(d>>16)+(c>>16);return(b<<16)|(c&65535)}function bit_rol(a,b){return(a<<b)|(a>>>(32-b))};