$(document).ready(function(){

	function ratelimit(fn, ms) {
		var last = (new Date()).getTime();
		return (function() {
			var now = (new Date()).getTime();
			if (now - last > ms) {
				last = now;
				fn.apply(null, arguments);
			}
		});
	}

	function hidePaypal() {
		$('div.paypal_form').hide();
		$('a.paypal_link').show();
		$('a.paypal_link').click(function(){
			var pp_id = $(this).attr('href');
			var theForm = $(pp_id).children('form');
			var theForm = theForm[0];
			$(theForm).submit();
			return true;
		});
	};	

	// external links -> new window
	$('a[href^="http://"]').attr("target", "_blank");
	$('a[href^="https://"]').attr("target", "_blank");

	// DO NOTHING ON H2 CLICK
	$('#content div.article h2 a').live('click', function(e) {
		e.preventDefault();
	});
	
	// MENU PAGES - SLIDEOUT 

	$('#menu-content').children().hide().appendTo('#menu'); // move up from bottom of page
	
// 	$('#menu a.page').click(function(e){
// 		e.preventDefault();
// 		$thePage = $('#'+$(this).attr('id').substr(5));
// 		if($thePage.hasClass('open')) {
// 			$($thePage).slideUp(showSpeed/1.2).removeClass('open');
// 			$(this).removeClass('active');		
// 		} else {
// 			$('div.open').slideUp(showSpeed/1.2).removeClass('open');
// 			$('#menu a.page').removeClass('active');
// 			$($thePage).slideDown(showSpeed/1.2).addClass('open');
// 			$(this).addClass('active');
// 		}
// 	});

	$('div.slideout a.close').click(function(e){
		e.preventDefault();
		$('#menu a.active').removeClass('active');
		$('div.open').slideUp(showSpeed/1.2);
		$.scrollTo($('#header'), {
			'offset': { left: 0, top: 0 },
			'duration': scrollSpeed
		});
	});



	// READ MORE LINKS

	scrollSpeed = 500;
	showSpeed = 500;

	// $('#content div.article div.more').hide();

	$('#content div.article p.readmore a').live('click', function(e) {
		e.preventDefault();
		$theLink = $(this);
		theUrl = $theLink.attr('href') + ' #full-content';
		preHTML = $theLink.html();
		$theLink.html('Loading');
		$(this).parent().next('div.more').load(theUrl, function() {
			$(this).append('<p><a href="/" class="close">Close</a></p>');
			$theLink.html(preHTML);
			$(this).slideDown(showSpeed);
		});
	});

	$('#content div.article a.close').live('click', function(e) {
		e.preventDefault();
		$(this).parents('div.more').slideUp(showSpeed);
		$.scrollTo($(this).parents('div.article'), {
			'offset': { left: 0, top: -10 },
			'duration': scrollSpeed
		});
	});
	
	
	
	// SCROLL TO LOAD MORE
	
	$('#paginator').hide();
	$window = $(window);
	pageNumber = 1;
	reloadAt = .7;
	contentLoading = false;
	
	
	function hiddenContent() {
		windowHeight = $window.height();
		visibleContent = $window.scrollTop() + windowHeight;
		availableContent = $('#container').height() + 600;
		stillHidden = visibleContent / availableContent;
		return stillHidden;
	}
	
	
	checkScroll = function() {
		if(hiddenContent() > reloadAt && pageNumber < maxPages && !contentLoading) {
			contentLoading = true;
			pageNumber++;
			if (get_query != '') theUrl = get_query + pageNumber + ' #content';
			else theUrl = '?page='+pageNumber+' #content';
			$.get(theUrl, function(data) {
				$html = $(data).find('#content');
				$('#content').append($html);
				contentLoading = false;
				hidePaypal();
			});
		}
		// $('#search').html(hiddenContent()).css('position','fixed');
	}

	$window.scroll(ratelimit(checkScroll,500));
	
	hidePaypal();
	
	



});
