$('body').prepend(''+
	'<div id="tbPush"></div> ' +
	'<div id="tb-">' + 
		'<ul id="tb-siteNav">' +
			'<li><a href="http://trypolyphasic.com/"><img id="tb-logo" src="http://trypolyphasic.com/images/bloglogo.png"/></a></li>' +
			'<li><ul id="tb-siteNavDown">' +
				'<li><a href="http://forums.trypolyphasic.com/">Discussion</a></li>' +
				'<li><a href="http://trypolyphasic.com/alarm">Alarm</a></li>' +
				'<li><a href="http://trypolyphasic.com/map">Map</a></li>' +
			'</ul></li>' +
		'</ul>' +
		'<div id="tb-blogNav">' +
			'<img id="tb-unread" alt="" src="http://trypolyphasic.com/images/unread.png"/><img id="tb-unread_comment" alt="" src="http://trypolyphasic.com/images/unread_comments.png"/><a href="#" style="float:left" id="tb-prv">Newer</a><a href="#" id="tb-nxt" style="float:left">Older</a> ' +
	'</div>');

$(document).ready(function(){
	tb.setup();
})

tb = {
	setup : function(){
		
		if($('.post').length == 1) {
			tb.postid = rgrb($('.post').attr('id').split('-'), 1);
			tb.domain = rgrb(location.href.split('.com'), 0) + '.com';
			tb.subdomain = rgrb(tb.domain.replace('http://', '').split('.'), 0);
			tb.checkRead();
			$.getJSON('http://trypolyphasic.com/ajax/blogdata?callback=?', {
				num: 5, subdomain : tb.subdomain, postid : tb.postid
			}, function(data){
				var blogs = data.blogs;
				var found = false;				
				for (b in blogs) {
					if(found){
						tb.nxt = blogs[b];
						$('#tb-nxt').attr('href', tb.nxt.permalink);
						break;
					}
					if((blogs[b].link == tb.domain+'/?p='+tb.postid) || (blogs[b].link == location.href)) {
						found = true;
					}
					if(!found) {
						tb.prv = blogs[b];
						$('#tb-prv').attr('href', tb.prv.permalink);
					}
				}
				if(!tb.prv){
					$('#tb-prv').hide();
				}
				$('#tb-blogNav').show();
			});
			
			tb.markTimeout = setTimeout(tb.markRead, 2500);
		}
		$('#tb-siteNav')
			.mouseover(function(){
				$('#tb-siteNavDown').show();
			})
			.mouseout(function(){
				$('#tb-siteNavDown').hide();
			})
	},
	markRead : function(){
		$.getJSON('http://trypolyphasic.com/ajax/blogpostread?callback=?', {
				act : 'mark', subdomain : tb.subdomain, postid : tb.postid
			}, function(data){
				$('#tb-unread').hide();
			}
		);
	},
	checkRead : function(){
		$.getJSON('http://trypolyphasic.com/ajax/blogpostread?callback=?', {
				act : 'check', subdomain : tb.subdomain, postid : tb.postid
			}, function(data){
				if(data.status*1 > 0){
					$('#tb-unread').hide();
					$('#tb-unread_comment').show();
					$('#tb-unread_comment').fadeOut(2500);
				}
				else if(data.status != 'unread') {
					clearTimeout(tb.markTimeout);
					//$('#tb-unread').hide();
				}
				else{
					$('#tb-unread').show();
					$('#tb-unread').fadeOut(2500);
				}
			}
		);
	},
	data : 'tt'
}

function rgrb(r, i){
	return r[i];
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 return {width : myWidth, height : myHeight };
}
