function annotateExternalLinks() {
	$('#maincontent a, #header a').each( function(idx, link) {
		var href = $(link).attr('href');
		if(!href.match(/^https?:\/\/blog.oxff.net\/|.(png|jpg)$/) && href[0] != '#') {
			$(link).addClass('extlink');
		}
	} );

	$('#maincontent a[rel=lightbox]').each( function(idx, link) {
		$(link).click(function(link) { return function(ev) {
			var orig_img = link.find('img');
			var popup_img = $(document.createElement('img'));

			try {
				popup_img.attr('src', orig_img.attr('src'));
				popup_img.addClass('popup');

				popup_img.appendTo($(document.body)).hide();
				$('#maincontent, #navi, #header').animate({'opacity': 0.25}, 'fast');
				popup_img.offset({
					'top': window.pageYOffset + window.innerHeight/2 - popup_img.height()/2,
					'left': window.pageXOffset + window.innerWidth/2 - popup_img.width()/2,
				}).fadeIn('slow', function() {
					popup_img.click(function(ev) {
						$('#maincontent, #navi, #header').animate({'opacity': 1}, 'fast');

						popup_img.fadeOut('fast', function() {
							popup_img.remove();
						} );
					} );
				} );
			} catch(e) { }

			return false;
		} }($(link)) );
	} );
}

function navigateToPost(url) {
	var once = false;

	$("#maincontent > *").animate({ 'opacity': 0 }, 400, function() {
		if(once)
			return;
		once = true;

		var hdr = $('#maincontent > h1');
		
		indicator = $(document.createElement('img'));
		indicator.attr('src', '../static/load2.gif');
		indicator.addClass('loadpost');
		indicator.css({ 'position': 'absolute', 'top': (hdr.offset().top + hdr.height()) + 'px',
			'left': ($('#maincontent').offset().left + $('#maincontent').width() / 2 - 16) + 'px' });
		indicator.appendTo($(document.body)).hide().fadeIn('fast');

		$.get(url, function(url) { return function(result) {
			var post;

			indicator.remove();

			$('#maincontent').css({'width': $('#maincontent').width() + 'px'});

			if((post = result.match(/<!--\s+POST:START\s+-->\n[\s\S]*\n<!--\s+POST:END\s+-->/))) {
				var curr_height = $("#maincontent").height();
				var flag = false;

				$("#maincontent > *").replaceWith($(post[0]));
				annotateExternalLinks();
				var new_height =  $("#maincontent").height();

				$("#maincontent").css({ 'height': curr_height + 'px' });
				$("#maincontent > *").hide();
				$("#maincontent").animate({'height': new_height}, 'slow', function() {
					var hash;

					if(hash = url.match(/-([a-z0-9]+)\.html$/)) {
						try {
							location.hash = hash[1];
						} catch(e) { }
					}

					$("#maincontent > *").fadeIn('fast');
					$("#maincontent").css({'height': ''});
				} );
			}
			else
				$("#maincontent > *").fadeIn('fast');
		} }(url) );
	} );
}

$(document).ready( function() {
	annotateExternalLinks();

	$('#navi > ul > li.doc > a').each( function(idx, link) {
		$(link).click( function(link) { return function(ev) {
			navigateToPost(link.href);
			return false;
		} }(link) );
	} );

	$('#idxlink').attr('href', index_tag);
	$('#idxlink').click( function(link) {
		navigateToPost('/posts/-' + index_tag.substr(1) + '.html');
	} );

	if(location.hash && location.hash != '' && location.hash != index_tag) {
		navigateToPost('/posts/-' + location.hash.substr(1) + '.html');
	}
	else {
		location.hash = index_tag;
	}

	$.getJSON('http://search.twitter.com/search.json?q=from:ochsff&callback=?&rpp=25', function (result) {
		var h1 = $('#navi').height();

		$.each(result.results, function(idx, result) {
			var tweet = $(document.createElement('li'));
			var text = result.text;
			var found = false;
			var pic;

			text = text.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_new">$1</a>');

			if(text.match(/^RT @.*?:/)) {
				text = text.replace(/^RT @(.*?):/, '<i>Via <a href="http://twitter.com/#!$1" target="_new">@$1</a></i>:');
				tweet.addClass("retweet");
			}
			else {
				tweet.addClass("tweet");
			}

			if(result.from_user != 'ochsff') {
				text = '<i>Via <a href="http://twitter.com/#!' + result.from_user + '" target="_new">'
					+ result.from_user + '</a></i>: ' + text;
			}
			
			text = text.replace(/(\s|^)@([a-zA-Z0-9_-]+)/g, '$1<a href="http://twitter.com/#!$2" target="_new">@$2</a>');
			
			tweet.html(text);
			
			if((pic = text.match(/https?:\/\/(?:www\.)?twitpic.com\/([^\s]{6})/i))
					|| (pic = text.match(/https?:\/\/[^\s]+\.(?:png|jpe?g|gif)/i)) ) {
				var link;
				
				tweet.find("a").each( function(idx, l) {
					if(l.href == pic[0])
						link = $(l);
				} );
				
				if(pic.length == 2)
					pic = $('<img src="http://twitpic.com/show/large/' + pic[1] + '" alt="twitpic Preview" />');
				else
					pic = $('<img src="' + pic[0] + '" alt="twitpic Preview" />');

				link.mouseover( function(tweet, link, pic) { return function(ev) {
					pic.remove();
					pic.appendTo($(document.body)).hide();
					pic.css({ 'position': 'absolute',
						'top': (tweet.offset().top + tweet.height() + 7) + 'px',
						'left': (tweet.offset().left + tweet.width() - pic.width()) + 'px',
						'border': '1px solid #94bd8d',
						'padding': '3px',
						'background-color': 'white',
						'opacity': 1,
					});

					pic.fadeIn('fast');

					var hidden = $('#maincontent, #header');
					hidden.animate({'opacity': 0.25}, 'fast');

					link.mouseout( function(ev) {
						hidden.stop();
						hidden.animate({'opacity': 1}, 'fast');

						pic.stop();
						pic.fadeOut('fast', function() {
							pic.remove();
						} );

						tweet.mouseout(null);
					} );
				} }(tweet, link, pic) );
			}

			tweet.appendTo("#navi > ul");
		} );

		var h2 = $('#navi').height() - $('#navi > img.loadpost').height();
		$("#navi > ul > li.tweet, #navi > ul > li.retweet").hide();
		$('#navi').height(h1);

		var flag = true;

		$('#navi > img.loadpost').fadeOut('fast', function() {
			$('#navi').animate({ 'height': h2 }, { duration: 400, 'complete': function() {
				$("#navi > ul > li").fadeIn('slow');
				$("#navi").css({'height': ''});
			} } );
		} );
	} );
} );

