// Random manipulation
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
 
// Countdown Countdown

function calcage(seconds, num1, num2) {
	s = ((Math.floor(seconds/num1))%num2).toString();
	return s;
}

// Send it the birthday, get the days since.
function daysAlive(month, day, year){
	var birthday = new Date( year, month, day);
	var today = new Date();
	between = (today-birthday);
	days = addCommas(Math.floor((between/1000)/86400));
	$('#days_alive, #about_days').html(days);
}

function countDown(seconds) {		
	if (seconds > 0){
		var direction = 1
	}else{
		var direction = +1
	}
	DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, and %%S%% seconds";
	DisplayStr = DisplayFormat.replace(/%%D%%/g, addCommas(calcage(Math.abs(seconds),86400,100000)));
	DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(Math.abs(seconds),3600,24));
	DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(Math.abs(seconds),60,60));
	DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(Math.abs(seconds),1,60));
	$('#death_counter').html(DisplayStr);
	setTimeout("countDown(" + (seconds-direction) + ")", 1000);
}

function deathCount(target){
	var target_date = new Date(target);
	var now = new Date();

	var diff = new Date(target_date-now);
	var seconds = Math.floor(diff.valueOf()/1000);
	countDown(seconds);
}

// 2294188520 seconds
function percentageLived(){
	var average_age = 2294188520;

	var now = new Date();
	var now_seconds = now.getTime();
	var now_seconds_2 = now_seconds / 1000;

	var birthday = new Date('April 23, 1987 4:20:20 am PST');
	var birthday_seconds = birthday.getTime();
	var birthday_seconds_2 = birthday_seconds / 1000;

	var since_birthday = now_seconds_2 - birthday_seconds_2;
	var percentage_life = since_birthday / average_age * 100;
	var percentage_life = percentage_life.toPrecision(2);

	$('#percentage_lived').html(percentage_life);
}

//fgnass.github.com/spin.js#v1.2.2
(function(a,b,c){function n(a){var b={x:a.offsetLeft,y:a.offsetTop};while(a=a.offsetParent)b.x+=a.offsetLeft,b.y+=a.offsetTop;return b}function m(a){for(var b=1;b<arguments.length;b++){var d=arguments[b];for(var e in d)a[e]===c&&(a[e]=d[e])}return a}function l(a,b){for(var c in b)a.style[k(a,c)||c]=b[c];return a}function k(a,b){var e=a.style,f,g;if(e[b]!==c)return b;b=b.charAt(0).toUpperCase()+b.slice(1);for(g=0;g<d.length;g++){f=d[g]+b;if(e[f]!==c)return f}}function j(a,b,c,d){var g=["opacity",b,~~(a*100),c,d].join("-"),h=.01+c/d*100,j=Math.max(1-(1-a)/b*(100-h),a),k=f.substring(0,f.indexOf("Animation")).toLowerCase(),l=k&&"-"+k+"-"||"";e[g]||(i.insertRule("@"+l+"keyframes "+g+"{"+"0%{opacity:"+j+"}"+h+"%{opacity:"+a+"}"+(h+.01)+"%{opacity:1}"+(h+b)%100+"%{opacity:"+a+"}"+"100%{opacity:"+j+"}"+"}",0),e[g]=1);return g}function h(a,b,c){c&&!c.parentNode&&h(a,c),a.insertBefore(b,c||null);return a}function g(a,c){var d=b.createElement(a||"div"),e;for(e in c)d[e]=c[e];return d}var d=["webkit","Moz","ms","O"],e={},f,i=function(){var a=g("style");h(b.getElementsByTagName("head")[0],a);return a.sheet||a.styleSheet}(),o=function r(a){if(!this.spin)return new r(a);this.opts=m(a||{},r.defaults,p)},p=o.defaults={lines:12,length:7,width:5,radius:10,color:"#000",speed:1,trail:100,opacity:.25,fps:20},q=o.prototype={spin:function(a){this.stop();var b=this,c=b.el=l(g(),{position:"relative"}),d,e;a&&(e=n(h(a,c,a.firstChild)),d=n(c),l(c,{left:(a.offsetWidth>>1)-d.x+e.x+"px",top:(a.offsetHeight>>1)-d.y+e.y+"px"})),c.setAttribute("aria-role","progressbar"),b.lines(c,b.opts);if(!f){var i=b.opts,j=0,k=i.fps,m=k/i.speed,o=(1-i.opacity)/(m*i.trail/100),p=m/i.lines;(function q(){j++;for(var a=i.lines;a;a--){var d=Math.max(1-(j+a*p)%m*o,i.opacity);b.opacity(c,i.lines-a,d,i)}b.timeout=b.el&&setTimeout(q,~~(1e3/k))})()}return b},stop:function(){var a=this.el;a&&(clearTimeout(this.timeout),a.parentNode&&a.parentNode.removeChild(a),this.el=c);return this}};q.lines=function(a,b){function e(a,d){return l(g(),{position:"absolute",width:b.length+b.width+"px",height:b.width+"px",background:a,boxShadow:d,transformOrigin:"left",transform:"rotate("+~~(360/b.lines*c)+"deg) translate("+b.radius+"px"+",0)",borderRadius:(b.width>>1)+"px"})}var c=0,d;for(;c<b.lines;c++)d=l(g(),{position:"absolute",top:1+~(b.width/2)+"px",transform:"translate3d(0,0,0)",opacity:b.opacity,animation:f&&j(b.opacity,b.trail,c,b.lines)+" "+1/b.speed+"s linear infinite"}),b.shadow&&h(d,l(e("#000","0 0 4px #000"),{top:"2px"})),h(a,h(d,e(b.color,"0 0 1px rgba(0,0,0,.1)")));return a},q.opacity=function(a,b,c){b<a.childNodes.length&&(a.childNodes[b].style.opacity=c)},function(){var a=l(g("group"),{behavior:"url(#default#VML)"}),b;if(!k(a,"transform")&&a.adj){for(b=4;b--;)i.addRule(["group","roundrect","fill","stroke"][b],"behavior:url(#default#VML)");q.lines=function(a,b){function k(a,d,i){h(f,h(l(e(),{rotation:360/b.lines*a+"deg",left:~~d}),h(l(g("roundrect",{arcsize:1}),{width:c,height:b.width,left:b.radius,top:-b.width>>1,filter:i}),g("fill",{color:b.color,opacity:b.opacity}),g("stroke",{opacity:0}))))}function e(){return l(g("group",{coordsize:d+" "+d,coordorigin:-c+" "+ -c}),{width:d,height:d})}var c=b.length+b.width,d=2*c,f=e(),i=~(b.length+b.radius+b.width)+"px",j;if(b.shadow)for(j=1;j<=b.lines;j++)k(j,-2,"progid:DXImageTransform.Microsoft.Blur(pixelradius=2,makeshadow=1,shadowopacity=.3)");for(j=1;j<=b.lines;j++)k(j);return h(l(a,{margin:i+" 0 0 "+i,zoom:1}),f)},q.opacity=function(a,b,c,d){var e=a.firstChild;d=d.shadow&&d.lines||0,e&&b+d<e.childNodes.length&&(e=e.childNodes[b+d],e=e&&e.firstChild,e=e&&e.firstChild,e&&(e.opacity=c))}}else f=k(a,"animation")}(),a.Spinner=o})(window,document);


$(function() {
		setTimeout("$('#newblog').slideDown()", 3000);

	$.get('/pagebuilder/dustinbar_top.html', function(data) {
		$('#dustinbar_top').html(data);
		
		if(barcolor == "black"){ $('.dustinbar.top').addClass('black'); }
		if(barcolor == "trans"){ $('.dustinbar.top').addClass('trans'); }
		if(barfixed == "yes"){ $('.dustinbar.top').addClass('fixed'); }
		
		$("a.next_link").attr("href", "/"+next_link).html(next_title);
		$("a.previous_link").attr("href", "/"+previous_link).html(previous_title);

		//$('#dustinbar_top').addClass('transition');
		//$('#dustinbar_top').css('top', '0px');
	});

	setTimeout(function(){
		$.get('/pagebuilder/fin.html', function(data) {
			$('#fin').html(data);
			$("a.next_link").attr("href", "/"+next_link).html(next_title);
			$("a.previous_link").attr("href", "/"+previous_link).html(previous_title);
			$("span#fin_title").html(pagetitle);
			$("strong#pub_date").html(date_published);
			$("span#pub_location").html(location_published);
			$("span#article_number").html(articlenumber);
			$("span#type_used").html(typography);
			
			$("a#permalink").attr("href", "/"+articlepath).html("/"+articlepath);
			percentageLived();
			daysAlive('04', '23', '1987');
			deathCount("01/03/2060 06:55 AM");
		});
	}, 1000)
});
