//function to itialize global content scripts on pageload and in callbacks
function init_scripts() {
	//scrollTo for links beginning with #
	$("a[href^='#']").click(function() {$.scrollTo($(this).attr('href'), 200);return false});
	//image grids
	$(".image_grid li").zoomGallery({fadeoninit:true});
	//portfolio navigation
	$("#portfolio_navigation").lavaLamp();	
	//rewrite portfolio nav URLs
	$('#portfolio_navigation a').each(function() {
		var port_url = $(this).attr('href');
		var port_ajax_url = port_url.replace('portfolio','portfolio/portfolio-ajax');
		$(this).attr('href',port_ajax_url);
	});
	//imagegrid sorting	
	$('#portfolio_navigation a').click(function(e) {
		$.get( $(this).attr('href'), function(data) {
			$('.image_grid').quicksand( $(data).find('li'),{}, function(){
				$(".image_grid li").zoomGallery({fadeoninit:false});	//reinit zoomgallery											  
			});
		});  
		e.preventDefault();  
	});
}

$(document).ready(function() {
	//navigation
	$("#primary_navigation").lavaLamp();	
	//first load title slidein
	$("#content h2.title").addClass("preslide").animate({top: '0'}, 750, 'easeInQuad');
	//logo zoom
	$("#head h1").hover(function(){$(this).stop().animate({scale:1.02})}, function(){$(this).stop().animate({scale:1})});
	//social navigation
	$("#social_navigation li").hover(function(){$(this).stop().animate({scale:1.2})}, function(){$(this).stop().animate({scale:1})});
	
	//init content scripts
	init_scripts();
	

	//PAGE LOAD AJAX AND HISTORY OH MY---------------------------------------------------------------------------------				   
	function pageload(hash) {
		//hash exists and begins with a folder slash
		if(hash && hash.charAt(0) == '/') {
			//grab the content
			var $page = $('<div />').load(hash + " #intro, #content",'',function() {
				//vars to segment content																					  
				var $intro = $page.find('#intro').children();
				var $content = $page.find('#content');
				//populate intro content, setup
				$("#intro").html($intro);
				$("#content_wrap").html($content);
				$("#content h2.title").addClass("preslide");
				//scripts re-init
				init_scripts();
				//animate in
				$("#intro").slideDown(250, function(){
					$("#intro_wrap2").animate({minHeight: $('#intro').innerHeight()}, 100, function(){
						$("#content_wrap").slideDown({
							duration:250,
							easing:"easeInExpo",
							complete:function(){
								$("#content h2.title").animate({top: '0'}, 500, 'easeInQuad');
								$("#page_loading").fadeOut(100, function() {$(this).remove();$('#page_overlay').remove()});
							}
						});
					});								
				});		
 			//set page title based on heading text
				var defaultTitle = 'Thomas R. Reid Jr. : Pittsburgh Front-End Web Developer : ';
				if (hash != '/') {document.title = defaultTitle + $('#primary_navigation a[href=' + hash + ']').text()} else {document.title = defaultTitle + 'Home';}
				//Add active state to nav item on page load
				$('#primary_navigation a[href=' + hash + ']').parent('li').addClass('current').trigger('click').siblings().removeClass('current');
			});
		} 
		//hash does not begin with a folder slash, do nothing
		else if (hash.charAt(0) != '/') {}
		//no hash
		else {alert("this is default, you need to fix this")}
	}
	$.historyInit(pageload);			   

	$('a.ajahload').live('click', function(){
		//get the hash						  
		var hash = $(this).attr('href');
		hash = hash.replace(/^.*#/, '');
		//hash = hash.substr(0,hash.length-4);
		hash = hash.substr(0,hash.length);
		//set the min-height of #intro's wrapper to prevent jumping
		$("#intro_wrap2").css({'min-height': $('#intro').innerHeight()});
		//loader
		$('#head_in').append('<div id="page_loading">LOADING...</div>');
		$('<div id="page_overlay" />').appendTo('body').css('opacity',.5);
		//hide content
		$("#intro").fadeOut(250,function(){
			$("#content h2.title").slideUp(100);
			$('#content_wrap').slideUp({
				duration: 'normal',
				easing:'easeInQuad', 
				complete: function(){
					setTimeout(function(){$.historyLoad(hash)}, 100)
				}
			});										   
		});
		
		return false;
	});
	
	
	//CONTACT FORM------------------------------------------------------------------------
	//form submit
	$("#contact-form").submit(function(e){
			$('#error').slideUp(100, function(){$(this).remove()});
			$('#form_loading').css('visibility','visible');
			$('#contact-form table').css('opacity',.1);
			$.post('/mailform-response/',$(this).serialize() + '&action[mailform-submit]=Submit',
				function(data){
					if(data.match('error')){
						$('#contact-form').prepend(data);
						$("#error").slideDown(500);
					//	$.validationEngine.buildPrompt("#captcha","* Wrong verification number!","error");
					}
					else
					{
						$("#contact-form").hide('slow').after(data);
						$('#foot_in img').animate({rotate:'360deg'});
					}
					$('#form_loading').css('visibility','hidden');
					$('#contact-form table').css('opacity',1);
				}
			
			);
		e.preventDefault();
	})


});
