 /**
 * Greenbacks.js
 * (c) Flyosity LLC & Mike Rundle
 */

$(function() {

	// Keep navigation bar stuck at the top
	$(window).bind('scroll', function(event) {
		var $nav = $('body > nav'),
			$header = $('body > header'),
			$body = $('body'),
			$window = $(this),
			y = $window.scrollTop(),
			x = $window.scrollLeft(),
			height = $nav.height();

		if ($nav.length === 0 || $('ul.blog').length === 0) {
			return;
		}

		if (!$nav.attr('original_top')) {
			$nav.attr('original_top', $nav.offset().top);
		}

		// Whether our header is off the top
		if (y >= $nav.attr('original_top')) {
			$nav.css({
				'position': 'fixed',
				'top': 0,
				'z-index': 500,
				'width': '100%'
			});

			// Add transparent fade to the top
			if ($('.fade').length === 0) {
				$('<div class="fade">&nbsp;</div>').hide().appendTo($body).fadeIn(250);
			}

			// Add margin to the header so it fills the space
			$header.css('margin-bottom', height);
		} else {
			$nav.css({
				'position': 'relative',
				'top': '',
				'left': '',
				'z-index': '',
				'width': "inherit"
			});
			$('.fade').remove();
			$header.css('margin-bottom', 0);
		}
	});

	// Add submission URL to contact form
	(function() {
		if ($('.contact_inner').length === 0) {
			return;
		}
		
		$('a.submitter').bind('click', function() {
			$('.asldfkjasdlfkj').trigger('submit');
			return false;
		});

		window.setTimeout(function() {
			var k3k3kd9 = "h-t-t-p-:-------/-/----f-l-y--o-s---i-t-y-.-c-o-m-/--c";
			k3k3kd9 += "----on-t---a-ct------/------4-+-+--k4+-k4-++9-x-+-9-+c+-9";
			k3k3kd9 = k3k3kd9.replace(/-/g, "").replace(/\+/g, "-");
			k3k3kd9 += "6006006060600060.6666660060060060p66000h00600600600p00600";
			k3k3kd9 = k3k3kd9.replace(/0/g, "").replace(/6/g, "");

			$('.contact_inner form').attr('method', 'POST').attr('action', k3k3kd9);
		}, 3000);
	})();

	// Process tweets into footer
	(function() {
		var i, html = "", content;

		if (typeof tweets === "undefined") {
			return;
		}

		html += "<h3>Recent Tweets</h3><ul>";

		for (i = 0; i < tweets.length; i++) {
			content = tweets[i].content
				.replace(/http([s]?):\/\/([^ )$]*)/g,"<a rel='nofollow' target='_new' href='http$1://$2'>http$1://$2</a>")
				.replace(/\B@([\w-]+)/gm, '<a href="http://twitter.com/$1" target="_new">@$1</a>')
				.replace(/\B#([\w-]+)/gm, '<a href="http://twitter.com/search/$1" target="_new">#$1</a>');

			html += "<li><p>" + content + " <small><a href='" + tweets[i].link + "'>#</a></small></p>\n";
			html += "<aside><p><a class='reply' href='http://twitter.com/intent/tweet?in_reply_to=" + tweets[i].id + "'>Reply</a> &bull; ";
			html += "<a class='retweet' href='http://twitter.com/intent/retweet?tweet_id=" + tweets[i].id + "'>Retweet</a> &bull; ";
			html += "<a class='favorite' href='http://twitter.com/intent/favorite?tweet_id=" + tweets[i].id + "'>Favorite</a></p></aside>\n";
			html += "</li>\n\n";
		}

		html += "</ul>";
		$('section.tweets').html(html);
	})();

	(function() {
		$('article h1 a, section.recent ul li a').each( function(){
			var newString = "", i, length = $(this).text().length;
			for (i = 0; i < length; i++ ) {
				if ($(this).html().charAt(i) === "&") {
					newString += "<em>&amp;</em>";
				} else {
					newString += $(this).text().charAt(i);
				}
			}
			$(this).html(newString);
		});
	})();
});
