(function($){
    $.fn.removeOrphans = function(){
    	$(this).each(function(){
            var $html = $(this).html();
            $html = $html.replace(/(\s)([\S])[\s]+/g, "$1$2&nbsp;");
            $(this).empty().html($html);
        });
    }
})(jQuery);

	$(function(){
		
		$(window).click(footerBottom);
		$(window).scroll(footerBottom);
		$(window).resize(function(){
			footerBottom();
			$("#people").people('scaleDown', $(window).width() < 1250 ? 0.15 : 0.2 );
		});
		footerBottom();
		setTimeout(footerBottom, 500);
		
		$('img#background-image').load(function(){
			$("img#background-image").attr({
				'original-width': $("img#background-image").attr('width'),
				'original-height': $("img#background-image").attr('height')
			});
			onWindowResize();
		});
		
		$('img#background-image').each(function(){
			$('#footer').css('display', 'none');
			$('#wrapper').css('position', 'fixed');
		});
		
		onWindowResize();
		$(window).bind("resize", onWindowResize);
		
		$('.btn').click(function(){
			var href = $(this).find('a').attr('href');
			document.location = href ? href : document.location;
		});
	
		$('.jq_btn_green').hover(function(){
			$(this).attr('class', $(this).attr('class').replace('btn_gray', 'btn_green') );
		},function(){
			$(this).attr('class', $(this).attr('class').replace('btn_green', 'btn_gray') );
		});
		
		$('.jq_btn_orange').hover(function(){
			$(this).attr('class', $(this).attr('class').replace('btn_gray', 'btn_orange') );
		},function(){
			$(this).attr('class', $(this).attr('class').replace('btn_orange', 'btn_gray') );
		});
		
		$('#categories_btn div.btn').unbind('click').click(function(){
			$('#categories_btn').css('left', 0).hide("slide", { direction: "right" }, 300, function(){
				$('#categories').css('left', 0).show("slide", { direction: "right" }, 500, categoryRollOut);
				$('#categories').hover( categoryRollOver, categoryRollOut ).mousemove(categoryRollOver);
			});
		});
		
		var categoryTimeout;
		
		var categoryRollOver = function(){
			clearTimeout(categoryTimeout);
			$('#categories').unbind('mousemove');
		};
		
		var categoryRollOut = function(){
			categoryTimeout = setTimeout(function(){
				$('#categories').unbind('mouseenter mouseleave');
				$('#categories').css('left', 0).hide("slide", { direction: "right" }, 500, function(){
					$('#categories_btn').css('left', 0).show("slide", { direction: "right" }, 300)							
				});
			}, 1000)
		};
		
		$('#show_related').click( function(){
			$('#show_related').animate({opacity: 0}, 200, function(){ $(this).css('display', 'none'); });
			$('#columns').animate({opacity: 0.0}, 500, function(){
				$('#columns').css('display', 'none');
				$('#blog').css('opacity', 0).css('display', 'block').animate({opacity: 1}, 500,function(){
					$('#hide_related').css('opacity', 0).css('display', 'inline').animate({opacity: 1}, 200);
				});
			});
		});
		
		$('#hide_related').click( function(){
			$('#hide_related').animate({opacity: 0}, 200, function(){ $(this).css('display', 'none'); });
			$('#blog').animate({opacity: 0}, 500, function(){
				$('#blog').css('display', 'none');
				$('#columns').css('display', 'block').animate({opacity: 1}, 500,function(){
					$('#show_related').css('display', 'inline').animate({opacity: 1}, 200);
				});
				footerBottom();
			});
		});
		
		$('#blog_top').click(function(){
			$(this).blur();
			$('html, body').animate({scrollTop:0}, 500);
			return false;
		});
		
		if ( $('#columns').length )
			$('#columns p').removeOrphans();
		else
			$('#content p.hyphenate').removeOrphans();
		
	});
	
	var infinityScrollPage = 1;
	var infinityScrollLock = false;
	
	function min(a, b) { return a > b ? b : a; }
	function onWindowResize () {
		
		var backgroundImage = $("img#background-image");
		
		var imageWidth = backgroundImage.attr('width');
		var imageHeight = backgroundImage.attr('height');
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		
		if ( !imageWidth || !imageHeight || !windowWidth || !windowHeight )	return;
		
		var scale = min ( imageWidth / windowWidth, imageHeight / windowHeight );
		
		backgroundImage.css('width', imageWidth/scale);
		backgroundImage.css('height', imageHeight/scale);
		backgroundImage.css('margin-top', (windowHeight - imageHeight/scale)/2);
		backgroundImage.css('margin-left', (windowWidth - imageWidth/scale)/2);
		
	}
	
	function footerBottom() {
		$('#footer').css('bottom', -1 * ( $(document).height() - $(window).height() - $(window).scrollTop() ) );
		$('#people').css('bottom', -1 * ( $(document).height() - $(window).height() - $(window).scrollTop() ) );
	}
