var PAGE_NUMBER = 1;
var ENABLE_ENDLESS_SCROLL = true;

function chatBox() {
		var pos = $(window).height()- 65+$(window).scrollTop();
		$('#imOnline').css( {top:pos});
}

function loadPage(page){
	if(page=='resume'){
		location.href = $('.resume').attr('href');
	}else{
		var splash = document.getElementById('splash_fo');
		if(splash){ splash.newPageIsLoading(); }
		$('#main').slideUp('fast',function(){
			$(this).attr('class', page).load(page+'.inc.php', function(){
				contentIsReady();
				if(splash){ splash.newPageIsDoneLoading(); }
				$(this).slideDown();
			});
		resetEndlessScroll();								   
		});
	}
}

function replaceHeadings(){
	var txt = $('h1.rockwell').html();
	$('h1.rockwell').html('<img src="/heading.php?text='+txt+'&case=upper&size=21" alt="'+txt+'" />');
}

function feedIsLoaded(){
		$('#feed img').load(function(){
			var width = $(this).width();
			var height = $(this).height();
			$(this).parent().css({'width':width,'height':height,'margin-top':141-(height/2)});
			$(this).siblings().css({'width':width-20, bottom:'5px'});
		});
		$('#feed li a').hover(function(){
			$(this).find('span').fadeIn();
		},function(){
			$(this).find('span').fadeOut();
		}).colorbox();
	
}

function disableEndlessScroll(){
	ENABLE_ENDLESS_SCROLL = false;
}
function resetEndlessScroll(){
	PAGE_NUMBER = 1;
	ENABLE_ENDLESS_SCROLL = true;
}
function initiateFlickrFeedPage(){

		if(document.getElementById('feed')){ //if #feed exists
			$('#feed').load("get_flickr_rss.php",'feed='+$('#main').attr('class'), function(){
			feedIsLoaded();
		});

		/*
			$(document).endlessScroll({
				bottomPixels: 450,
				fireDelay: 10,
				callback: function(p){
				PAGE_NUMBER++;
					if(ENABLE_ENDLESS_SCROLL == true){
						$.get("get_flickr_rss.php", { page: PAGE_NUMBER,feed:$('#main').attr('class')}, function(data){
						if(data.length > 28){
							$('#feed').append(data);
							feedIsLoaded();
							
						}else{
							disableEndlessScroll();
						}  
						 
					
					}); }
				}
			});
	} else {
	disableEndlessScroll();*/
	}/*end if feed*/
}

function attachShowCodeFunctions(){
	$('.showCode').click(function(e){
		e.preventDefault();
		$(this).fadeOut();
		var blockHeight = $(this).siblings().height();
		$(this).parent().animate({height:blockHeight});
	});
}
function contentIsReady(){
	replaceHeadings();
	attachShowCodeFunctions();
	initiateFlickrFeedPage();
	$('#contactForm').ajaxSubmit();
}

$(document).ready(function(){
	contentIsReady();
	
	if(document.getElementById('imOnline')){
		chatBox();
		$(window).scroll(chatBox);
		$(window).resize(chatBox);
	}

	$('#footerNav a').click(function(e){
		loadPage($(this).attr('class'));
		e.preventDefault();
	});
	$('#footerCodeStream li').hover(function(){$(this).css('opacity',0.75);},function(){$(this).css('opacity',1);});
});
