(function(){
	
	var nn = nn || {};
	nn.log = function (msg){
		if(console !== undefined && console.log){
			console.log(msg);
		}
	};
	
	nn.contact = function(){
		
		var dataObj = {
			email:$('#email').val(),
			name:$('#name').val(),
			message:$('#message').val()
		};
		
		$.ajax({
			type:"POST",
			url:'email.php', 
			data:dataObj,
			
			// @ data: the data returned from the server, formatted according to the dataType parameter
			// @ textStatus: a string describing the status
			// @ jqXHR: object
			success: function(data, textStatus, jqXHR){
				$('#contact_content form input, #contact_content form textarea, #contact_content form label, #submit').remove();
				$('#contact_confirmation').html("Thanks for your interest.  You will be contacted in the flashiest of flashes.<br/>An immediate and generic response is being sent to <span class='_pink'>" + dataObj.email + "</span>.<br/>However, Nathan will respond directly to your inquiry shortly.");
				$('#contact_confirmation').show();
			},
			
			// @ jqXHR: object
			// @ textStatus: a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror" 
			// @ errorThrown: when an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error"
			error: function(jqXHR, textStatus, errorThrown){
				// 4xx – Transport level error
				// 5xx – Transport level error
				$('#contact_content form input, #contact_content form textarea, #contact_content form label, #submit').remove();
				$('#contact_confirmation').html("Ugh... for some reason the contact form did not work.<br/>Since there is no complaint line or customer support number, please send an email the old fashioned way to <a class='_pink' href='mailto:n8@nathanneufeldt.com'>n8@nathanneufeldt.com</a>.");
				$('#contact_confirmation a._pink').css('cursor','pointer').css('text-decoration','none');
				$('#contact_confirmation a._pink').hover(function(){
					$(this).css('color','#f3028d').css('text-decoration','underline');
				});
				$('#contact_confirmation a._pink').mouseout(function(){
					$(this).css('text-decoration','none');
				});
				$('#contact_confirmation').show();
			}
		});
	};
	
	$(document).ready(function(){
		
		var showLoadSequence = true, currentSection = '', prevSection = '', vars = [], hash, 
		hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++){
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		
		if(vars['section']){
			showLoadSequence = false;
			currentSection = vars['section'];
		}else{
			currentSection = 'about';
		}
		
		prevSection = currentSection;
		
		/* Navigation click handler */
		$('.navigation').click(function(){
			
			prevSection = currentSection;
			currentSection = $(this).attr('id');
			
			if(currentSection != prevSection){
				var $hideDiv = $(String('#' + prevSection + '_content'));
				var $showDiv = $(String('#' + currentSection + '_content'));				
				// $('footer').fadeOut();				
				$hideDiv.fadeOut(400,function(){
					$showDiv.fadeIn(500);
					// $('footer').fadeIn(500);
				});
			}
			return false;
		});
		
		/* Project filter functionality */
		$('#project_filter span.checkbox').not('checked').click(function(){
			$('#project_filter span').removeClass('checked');
			$('#project_filter input').removeAttr('checkded');
			$(this).addClass('checked');
			$(this).siblings('input').attr('checked','checked');
			
			var tag = $(this).siblings('label').text().toLowerCase();
			
			if(tag == 'all'){
				$('#work_content article').fadeIn(200);
			}else{
				$('#work_content article').each(function(){
					var tagStr = $(this).find('.project_tags').text();
					if(tagStr.toLowerCase().indexOf(tag) > 0){
						$(this).fadeIn(200);
					}else{
						$(this).fadeOut(200);
					}
				});			
			}
			return false;
		});
		
		
		/* View project demo links click hijack */
		$('#work_content article a').click(function(){
			var loc = $(this).attr('href');
			window.open(loc,'_blank');
			return false;
		});
		
		/* Project link click hander -- goes to detail page */
		$('#work_content article').click(function(){
			window.location = $(this).find('a').attr('href');
			return false;
		});
		

		// validate on form
		$('#contact_content form').validate( 
			{messages: {
				name: {
					 required: "Please provide a name.  The auto-response will not know how to address you and will become upset without one.",
					 letterswithbasicpunc: "Please write your name using only letters and the most basic punctuation."
				},
				email: {
					 required: "This goes nowhere without a valid email address.",
					 email: "The key here is to use a validly formed email address."
				},
				message:{
					letterswithbasicpunc: "Please use letters and basic punctuation. This will not be read by a cyborg."
				}
		}});
		
		$('#submit').click(function(e){
			e.preventDefault();
			e.stopPropagation();
			if(!$('#contact_content form').valid()){
				$('#contact_content form').showErrors();
				return;
			}
			$(this).unbind();
			nn.contact();
		});
		
		// hide contact confirmation first
		$('#contact_confirmation').hide();
		
		if(!showLoadSequence){
			$('footer, #about_content, #work_content, #resume_content, #contact_content, #loader img.second').hide();
			//$('footer, #about_content, #work_content, #resume_content, #contact_content').hide();
			var $showDiv = $(String('#' + currentSection + '_content'));
			$showDiv.fadeIn(500);
			$('footer').fadeIn(500,function(){
				$('#loader img.first').fadeOut(250);
				$('#loader').fadeOut(500);
			});
			return;
		}
		
		/* Hide elements on init load */
		$('nav, header, footer, #subhead, #content_wrapper, #about_content, #work_content, #resume_content, #contact_content').hide();
		
		
		/**
		* Load sequence, sequences in animation
		*/	
		function loadSequence(e){
						
			switch(e.animNumber){
				case 0:
					$('#loader img.second').fadeOut(3000, function(){
						$(this).trigger({type:'loadSequence',animNumber:1});
					});
				break;
				
				case 1:
					$('#loader img.first').fadeOut(2000, function(){
						$(this).trigger({type:'loadSequence',animNumber:2});
					});
				break;
				
				case 2:
					$('#loader').fadeOut(1000, function(){
						$(this).trigger({type:'loadSequence',animNumber:3});
					});
				break;
				
				case 3:
					$('#content_wrapper').fadeIn(1000, function(){
						$(this).trigger({type:'loadSequence',animNumber:4});
					});
				break;
				
				case 4:
					$('header').css('margin-top','170px');
					$('header').fadeIn(1000, function(){
						$(this).trigger({type:'loadSequence',animNumber:5});
					});
					$('#subhead').fadeIn(1000);
				break;
				
				case 5:
					$('nav').fadeIn(1000, function(){
						$(this).trigger({type:'loadSequence',animNumber:7});
					});
					$('header').css('margin-top','133px');
				break;
				
				case 7:
					$('#about_content').fadeIn(500, function(){
							$(this).trigger({type:'loadSequence',animNumber:8});
					});					
				break;
				
				case 8:
					$('#loader, #loader.img, header, #content_wrapper, nav, footer').unbind('loadSequence');
					$('footer').fadeIn(1000);
				break;
				
				default:
					nn.log('loadSequence did not work right... ' + e.animNumber);
				
			}
		};
		
		// binding to dom elements
		$('#loader, #loader.img, header, #content_wrapper, nav, footer').bind('loadSequence',loadSequence);
		
		// initial call
		loadSequence({animNumber:0});
		
	});
	

})();
