(function() {
	var ua = navigator.userAgent.toLowerCase(),
			is = function(t){ return ua.indexOf(t) != -1; },
			h = document.getElementsByTagName('html')[0],
			b = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua)) ? ((is('mac') ? 'ieMac ' : '') + 'ie ie' + RegExp.$1)
				: is('gecko/') ? 'gecko' : is('opera') ? 'opera' : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '',
			os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : '';
	var c = b+os+' js'; 
	if(is('firefox')) {
		var ver = parseFloat(ua.replace(/^.*firefox\/(.*).*$/, '$1'));
		if(ver >= 3.1) c += " gecko-new";
	}
	h.className = h.className.replace('noscript', '') + h.className?' '+c:c;
})();

// ads.js v0.1 by M@ McCray, find the source at http://gist.github.com/252391
var Ads=(function(B){function A(D){var C=B.location.search.substring(1);var G=C.split("&");for(var E=0;E<G.length;E++){var F=G[E].split("=");if(F[0]==D){return F[1]}}return""}return{Google:function(D,F,E,C){B.google_ad_client=D;B.google_ad_slot=F;B.google_ad_width=E;B.google_ad_height=C;document.write('<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js"><\/script>')},ProjectWonderful:function(J,G,D,F){var I=document,H=new String(Math.random()*9999).substr(0,5),E='<script src="http://www.projectwonderful.com/gen.php?',C=A("pw_highlight_code");E+="id="+J+"&";E+="type="+G+"&";E+="r="+H+"&";if(I.referrer){E+="referer="+escape(I.referrer)+"&"}if(I.location){E+="location="+escape(I.location);+"&"}if(D){E+="fg="+D+"&"}if(F){E+="bg="+escape(F)+"&"}if(C!=""){E+="hl="+escape(C.match(/[0-9]+/g))}E+='"><\/script>';I.write(E)}}})(this);

var ZdC = (function(){
  function $(id) {
    return document.getElementById(id);
  }
  
  function moveAndClear(from, to) {
    $(to).innerHTML = $(from).innerHTML;
    $(from).innerHTML = "";
  }
  
  function moveAds() {
    moveAndClear('pwa_head', 'banner_advert');
    moveAndClear('pwa_side_top', 'side_top_advert');
    moveAndClear('pwa_side_bottom', 'side_bottom_advert');
    moveAndClear('pwa_footer', 'footer_advert');
  }
	
	function trackPage() {
		if(!window._gaq) window._gaq = [];
		_gaq.push(['_setAccount', 'UA-632821-2']);
		_gaq.push(['_trackPageview']);

		var ga = document.createElement('script');
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		ga.setAttribute('async', 'true');
		document.documentElement.firstChild.appendChild(ga);
	}
	
	function showTweets(twitters) {
		var statusHTML = [];
		for (var i=0; i<twitters.length; i++){
			var username = twitters[i].user.screen_name;
			var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
				return '<a href="'+url+'">'+url+'</a>';
			}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
				return	reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			});
			statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
		}
		document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
		
	}
	
	function loadTweets() {
		try {
			var s = document.createElement('script');
			s.src = 'http://twitter.com/statuses/user_timeline/zoodotcom.json?count=5&callback=ZdC.twitterCallback';
			s.setAttribute('async', 'true');
			document.documentElement.firstChild.appendChild(s);
		} catch(ex) {
			document.getElementById('twitter_update_list').display = 'none';
			if(window.console && console.log) {
				console.log("Could not generate SCRIPT NODE");
				console.log(ex);
			}
		}
	}
	
	function setRelativeTimes() {
		if(document.getElementsByClassName) { // Only on browsers that support this
			var elems = document.getElementsByClassName('relative-time');
			for (var i=0; i < elems.length; i++) {
				var elem = elems[i],
						date = new Date(Date.parse( elem.getAttribute('rel') ));
				elem.innerHTML = relative_time(date);
			};
		}
	}
	
	function checkForDisqusLinks() {
		var links = document.getElementsByTagName('a'),
				query = '?',
				re = /.*(\.jpg|\.png|\.gif|\.jpeg)$/gi;
		for(var i = 0; i < links.length; i++) {
			if(links[i].href.indexOf('#disqus_thread') >= 0) {
				query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
			} else if(re.test(links[i].href)) {
				links[i].setAttribute('target', '_blank');
//				links[i].setAttribute('rel', 'shadowbox'); // This will go away, methinks...
			}
		}
		if(query != '?')
			document.write('<scr'+'ipt type="text/javascript" src="http://disqus.com/forums/zoodotcom/get_num_replies.js' + query + '"></scr' + 'ipt>');
	}
	
	function checkForDisqusForm() {
	  if(document.getElementById('disqus_thread')) {
      document.write('<scr'+'ipt type="text/javascript" src="http://disqus.com/forums/zoodotcom/embed.js"></scr' + 'ipt>');
	  }
	}
	
	// Helper methods...
	function relative_time(time_value) {
		if(typeof time_value == 'string') {
			var values = time_value.split(" ");
			time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
			var parsed_date = Date.parse(time_value);
		} else {
			var parsed_date = time_value;
		}
		var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
		var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
		delta = delta + (relative_to.getTimezoneOffset() * 60);

		if (delta < 60) {
			return 'less than a minute ago';
		} else if(delta < 120) {
			return 'about a minute ago';
		} else if(delta < (60*60)) {
			return (parseInt(delta / 60)).toString() + ' minutes ago';
		} else if(delta < (120*60)) {
			return 'about an hour ago';
		} else if(delta < (24*60*60)) {
			return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
		} else if(delta < (48*60*60)) {
			return '1 day ago';
		} else {
			return (parseInt(delta / 86400)).toString() + ' days ago';
		}
	}
	
	// Public API
	return {
		
		twitterCallback: showTweets, 
	
		init: function() {
			loadTweets();
//			setRelativeTimes();
			if(document.location.href.indexOf('.local') > 0) {
			  window.disqus_developer = 1;
			}
			checkForDisqusLinks();
			checkForDisqusForm();
			trackPage();
			moveAds();
		}
	}
})();