// JavaScript Document
$(document).ready(function(){
	//fix png files in IE6/7
	$(document).pngFix(); 
	
	$('div.contactInfo button#submit').click(function() {

		$('#waiting').show(500);
		$('#contactForm').hide(0);
		$('#feedback').hide(0);

		$.ajax({
			type : 'POST',
			url : 'submit-form.php',
			dataType: "json",
			cache : false,
			data: {
				name : $('#name').val(),
				subject : $('#subject').val(),
				email : $('#email').val(),
				message : $('#message').val(),
				body : $('#body').val()
			},
			success : function(data){
				console.log('booh');
				$('#waiting').hide(500);
				$('#feedback').removeClass().addClass((data.error === true) ? 'error' : 'success');
				
				if( data.error === true)
				{
					$('#error-list').empty();
					var errors = data.msg.split(",");
					$(errors).each(function(i, error){
							console.log(error);
							if(error == "bot")
							{
								$('#feedback h2').empty().append("Hmmmm, looks like you are a bot, I'm not going to send this mail...");
								return;
							}else{
								$('#error-list').append('<br/>'+error);	
							}
					});
					$('#contactForm').removeClass().addClass('errors').show(500);
					$('#feedback').show(500);
					//get_question();
				}else{
					$('#message-sent').show(500);
				}
					
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$('#waiting').hide(500);
				$('#feedback').removeClass().addClass('error')
					.text('There was an error.').show(500);
				$('#contactForm').show(500);
			}
		});
		return false;
	});

	
						   
	//check if the url has a hash and scroll to it
	var menuItemClicked = false;
	
	//set up anchor scrolling
	$("a[href^=#][href!=#]").live('click', function(e){
														$('html, body').animate({'scrollTop': $($(this).attr('href')).offset().top+'px'}, function(){menuItemClicked = false;});
														e.preventDefault();
													});

	//set up fancybox gallery
	$("a.modal").fancybox({
						  overlayOpacity:.7,
						  overlayColor:'#06273a'
						  }); 
						  
	//set the main navigation to follow the scroll
	$( '#nav' ).scrollFollow({
		speed: 200,
		offset: 30
	});
	
	//button to load more folio examples
	var folioIndex = 2;
	if($("div.boundry-blue.folio").length > folioIndex)
	{
		$('a.more-examples').text( $("div.boundry-blue.folio").length-(folioIndex+1) +" more examples +");
		$("div.boundry-blue.folio").each(function( i, folio ){					
			if( i > folioIndex )
			{	
				$(this).hide();	
			}
		});
		$('a.more-examples').click(function(){
				folioIndex += 5;
				for(var b = folioIndex -5; b< folioIndex; b++)
				{
					$("div.boundry-blue.folio:eq("+b+")").show('normal');
				}
				if ( $("div.boundry-blue.folio").length < folioIndex )
				{
					$('a.more-examples').hide('normal');
				}
				$(this).text( $("div.boundry-blue.folio").length-(folioIndex) +" more examples +");
				return false;			
		})
		.show();
	}
	
	
	
	//hide all innder content divs
	$("div.menu_body").hide();
	
	
	$("h4.menu_head a").each(function(){
		$(this).append('<span class="twister"></span>')
	});
	
	
	//set up the link click behavior
	$("h4.menu_head").click(function()
	{
		if( $(this).parent().is('.open') )
					   {
						$(this)
						.parent()
						.removeClass('open')
						.find('.menu_body')
						.slideUp('slow');
						return false;
					   }
		$(this)
		.parent()
		.addClass('open')
		.find("h4.menu_head")
		.next("div.menu_body")
		.slideToggle(300)
		.parent()
		.siblings(".boundry-blue")
		.find('.menu_body')
		.slideUp("slow")
		.parent()
		.removeClass("open");
		return false;
	});
	
	
	//parent hover / click behavior
	$("div.boundry-blue:has('.menu_head a')").hover(function(){
		$(this).addClass("hover");
	},
	function(){
		$(this).removeClass("hover");
	})
	.click(function(){
		$(this).find("h4.menu_head").click();
	});
	
	
	
	//open first folio entry by default
	$("h4.menu_head:eq(0)").click();
	
	//set up paging of folio examples
	$('ul.paging').each(function(){
		$(this)
		.data('current',1)
		.data('max', $(this).find('li a').length - 2)
		.find('li a')
		.each(function(e){
			$(this).click(function(){
				var _top = $(this).parent().parent();
				if( $(this).is('.back') )
				{
					if( $(this).parent().parent().data('current') > 1 )
					{
						_top.find('a.current').removeClass('current');
						_top.data('current', _top.data('current')-1);
						_top.find("li a:eq("+ _top.data('current') +")").addClass('current');
					}
				}else if( $(this).is('.next'))
				{
					if( _top.data('current') < _top.data('max') )
					{
						_top.find('a.current').removeClass('current');
						_top.data('current', _top.data('current')+1);
						_top.find("li a:eq("+ _top.data('current') +")").addClass('current');
					}
				}else{
					_top.find('a.current').removeClass('current');
					_top.data('current', Number( $(this).text() ) );
					$(this).addClass('current');
				};
				_top.parent().find('.gallery-wrapper').animate({marginLeft: -((_top.data('current')-1)*860)+'px'});
				return false;
			}) ;
			
		})
	});
	
	//set up the info overlay toggles
	$(".boundry-blue .infoToggle a").click(function(){
		var $thisDetail = $(this).parent().prev().prev(".menu_body").find(".folioDetail");
		toggleInfo($thisDetail, $(this));
		return false;									   
	});
	
	function toggleInfo( detail, handle )
	{
		if( !detail.data('state') || detail.data('state') == "closed" )
		{
			detail.stop().animate({bottom:0});
			detail.data("state", "open");
			handle.text('- info');
		}else if( detail.data('state') == "open")
		{
			detail.stop().animate({bottom:-75});
			detail.data("state", "closed");
			handle.text('+ info');
		}
	}
	
	//main navigation
	$("#nav li a.over").hover(function(){
									   		$(this)
											.stop()
											.animate({opacity:1}, 500 )
											.parent()
											.siblings()
											.find(" a:eq(1)")
											.removeClass('active')
											.stop()
											.animate({opacity:0}, 200);
									   },
									   function(){
										   if(!$(this).is('.active') )
											  {
												$(this).css({opacity:0}, 300 );
											  }
									   })
										.click(function(){
												menuItemClicked = true;
												selectNav($(this));
											});
										
	function selectNav(_this )
	{
		_this.addClass("active").stop().animate({opacity:1}, 400 ).parent().siblings().find(" a:eq(1)").removeClass('active').stop().animate({opacity:0}, 200);
	}
										
	window.onscroll = function () {
				var curScroll = $(document).scrollTop();
				var home_pos = Math.round($("h2.home").position().top);
				var folio_pos = Math.round($("h2.folio").position().top);
				var resume_pos = Math.round($("h2.resume").position().top);
				var contact_pos = Math.round($("h2.contact").position().top);
				
				if(!menuItemClicked)
				{
					if( curScroll < folio_pos && !$("#nav li:eq(0) a:eq(1)").is(".active") )
					{
							selectNav( $("#nav li:eq(0) a:eq(1)") );
					}else if( curScroll < resume_pos && curScroll > folio_pos && !$("#nav li:eq(1) a:eq(1)").is(".active") )
					{
							selectNav( $("#nav li:eq(1) a:eq(1)") );
					}else if( curScroll < contact_pos && curScroll > resume_pos && !$("#nav li:eq(2) a:eq(1)").is(".active") )
					{
							selectNav( $("#nav li:eq(1) a:eq(1)") );
					}else if( curScroll > contact_pos && !$("#nav li:eq(3) a:eq(1)").is(".active")){
							selectNav( $("#nav li:eq(2) a:eq(1)") );
					}
				}
	}
});
