/* 
DC-publisher="Flipout Design"
DC-creator="David Vespoli"
DC-title="Flipout Design Javascript Functionality"
DC-type="Software"
DC-requires="jQeury"
DC-date="2010-06"
DC-revised="2010-10"
DC-version="3"
<link rel="schema.DC" href="http://dublincore.org/documents/dces/" />
*/


//init for js experience
document.getElementsByTagName('body')[0].className+=' js';

//keep track of the widow visible area
var viewportwidth;
if (typeof window.innerWidth != 'undefined') {
	viewportheight = window.innerHeight
} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight !='undefined' && document.documentElement.clientHeight != 0) {
	viewportheight = document.documentElement.clientHeight}
else {
	viewportheight = document.getElementsByTagName('body')[0].clientHeight
}






$(function(){

	//nav scrolling
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,") == this.pathname.replace(/^\//,") && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, {duration:1600,easing:'easeInOutExpo'});
				return false;
			}
		
		}
	
	});
	
	//additional projects
	$('#other-projects').toggle(function() {
		$(this).html('That\'s Enough! &#8593;');
		$(this).nextAll('.row').show('slow');
	}, function() {
		$(this).html('Show Me More &#8595;');
		$(this).nextAll('.row').hide('fast');
	})
	.nextAll('.row').hide();

	//keep the pages pretty by being at least as tall as the viewport
	$('.wrap').css('minHeight',viewportheight+'px');

	//contact form
	$('#submit').click(function(e) {
		$('#processing').fadeTo( 500, .95 );
		$('#contact-form').css('visibility','hidden');
		$('#message').hide(0);
		
		$.ajax({
			type : 'POST',
			url : 'post.php',
			dataType : 'json',
			data: {
				name		: $('#name').val(),
				company	: $('#company').val(),
				phone 	: $('#phone').val(),
				budget 	: $('#budget').val(),
				email 	: $('#email').val(),
				website : $('#website').val(),
				comment : $('#comment').val()
			},
			success : function(data){
				$('#processing').fadeTo( 0, .01 ).hide(0);
				
				if(data.error === true){
					$('#message').html('<p class="intro">Oops! '+data.msg+' </p>')
					.removeClass().addClass('error').show(500);
					$('#contact-form').css('visibility','visible');					
				} else {
					$('#message').html('<p class="intro">Thanks '+$('#name').val()+'! I\'ll get back to you shortly!</p>')
					.removeClass().addClass('success')
					.show(500);
				}
	
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$('#processing').fadeTo( 0, .01 ).hide(0);
				$('#message').removeClass().addClass('error')
					.html('<p class="intro">Something went wrong!</p>').show(500);
				$('#contact-form').css('visibility','visible');
			}
			
		});
		
		e.preventDefault();
	});
	
	$('<div id="processing"></div>').css({
		'height'  : $('#contact').height(),
		'width'   : $('#contact').width()
		})
		.appendTo('#contact')
		.fadeTo( 0, .01 )
		.hide(0);
	
	$('<div id="message"></div>')
		.insertAfter('#contact h2')
		.hide(0);
		
	//keep track of our sections
	$('section').each(function(){
			var offset = $(this).offset();
			flip.sections.push({'id':$(this).attr('id'),'top':offset.top,'height':$(this).height()});
	});
	//listen for scroll to determine section
  $(window).scroll(function () { 
 		  flip.whichSection();
	});

	//preload the groovy loading image cause it might take longer than actually processing the email
	preload_image = new Image; 
  preload_image.src="../m/111.gif"; 

}); //redAAY!






//namespace for my crap 
var flip = {
 animating : false,
 head : {
	 tucked :false,
	 animating : false
 },
 sections : [],
 hideHead : function(){
		if(!flip.head.tucked && !flip.head.animating){
			flip.head.animating = true;
			$('h1#logo').animate({fontSize:'18px',top:'5px',left:'0px'},300);
			$('header .wrap').stop().animate({height:"35px"},200, function () {flip.head.animating = false;});
			//$('nav').animate({top:"36px",right:"20px"},200);
		}
		flip.head.tucked = true;
	},
	showHead : function(){
		if(flip.head.tucked && !flip.head.animating){
			flip.head.animating = true;
			$('h1#logo').animate({fontSize:'130px',top:'100px',left:'57px'},300);
			$('header .wrap').stop().animate({height:"295px"},200, function () {flip.head.animating = false;});
			$('nav li a').removeClass('current');
			//$('nav').animate({top:"86px",right:"100px"},200, function () {flip.head.animating = false;});
		}
		flip.head.tucked = false;
	},
	whichSection : function(){
		if (!flip.animating) {
			flip.animating = true;
			var where = Math.abs(flip.sections[0].top-window.pageYOffset);
			var which = 0;
			//iterate through and get the best possible match
			for(var i = 0; i < flip.sections.length; i++){
				if ((where > Math.abs(flip.sections[i].top - window.pageYOffset))&&(flip.sections[i].top < window.pageYOffset+300)) {
					//where = Math.abs(flip.sections[i].top - window.pageYOffset);
					//which = i;
					//console.log(flip.sections[i].id);
					$('nav li a').each(function(){
						$(this).removeClass("current");
						if($(this).attr("href")=="#"+flip.sections[i].id){
							$(this).addClass("current");
						}
					});
				}
			}
			
			flip.animating = false;
		}
		
		if((flip.sections[0].height/2)>window.pageYOffset){
			flip.showHead();
		} else {
			flip.hideHead();
		}
	}
}

/*

$(document).ready(function() {

	
		
	
	//strip out useless anchors and assign destination
	$('nav li a').each(function(){
			var whereTo = $(this).attr('href');
			$(this).removeAttr('href').attr('data-whereto',whereTo);
	}).click(function(){
		$('nav li a').each(function(){
			$(this).removeClass("current");
		});
		$(this).addClass("current");
		flip.animating = true;
		$.scrollTo( '#'+$(this).attr('data-whereto') , 1800, {easing:'easeInOutQuart',onAfter:function(){
					flip.animating = false;
					flip.whichSection();
			}});
	});
	
	$('#logo a').removeAttr('href').click(function(){
		flip.animating = true;
		$.scrollTo( '#home' , 1800, {easing:'easeInOutQuart',onAfter:function(){
				flip.animating = false;
				flip.whichSection();
		}});
	});
	
		
	
});


*/


