var hash = window.location.hash.replace('#', ''),
	requestedViaAjax = false;

$(function(){
	$.fn.wait = function(time, type) {
		time = time || 1000;
		type = type || "fx";
		return this.queue(type, function() {
			var self = this;
			setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
	};
	
	
	/*
	 * Contact Form
	 */
	var $contactFormContainer = $('#contactFormFloat'),
		$contactForm = $('#contactFormFloat form'),
		$contactButton = $('#contactButton');
	
	$contactButton.click(function(){
		if(!$contactFormContainer.is(':visible')){
			$contactFormContainer.animate({
				//top: '-' + ( $(this).height() / 2 ) + 'px',
				top: '-50px',
				opacity: 'show'
			}, 500);
		}
		return false;
	});
	
	$contactFormContainer.find('.close').click(function(){
		$contactFormContainer.animate({
			top: ( $(this).height() / 8 ) + 'px',
			opacity: 'hide'
		}, 250, function(){
			$(this).css('top', '0')
		});
	});
	
	$contactForm.submit(function(){
		var $email = $(this).find('#emailFloat'),
			email = $email.val(),
			emailIsValid = true,
			$message = $(this).find('#messageFloat'),
			message = $message.val(),
			messageIsValid = true,
			validEmailRegex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		
		if(!email){
			emailIsValid = false;
			$email.siblings('.note').text('Required');
		}else if(email.search(validEmailRegex) == -1){
			emailIsValid = false;
			$email.siblings('.note').text('Invalid');
		}
		
		if(!message){
			messageIsValid = false;
		}
		
		if(!emailIsValid){
			$email.focus();
		}else if(!messageIsValid){
			$message.focus();
		}
		
		return (emailIsValid && messageIsValid);
	});
	
	
	/*
	 * Footer
	 */
	var footer = $('#footer'),
		footerHeight = footer.height(), // changes with font resizing
		footerMarginTop = parseInt(footer.css('margin-top')),
		footerNewTop = footerHeight + footerMarginTop;
	
	if(!$.cookie('showFooter')){
		$.cookie('showFooter', 'false', { expires: 7, path: '/' });
	}
	
	if($.cookie('showFooter') == 'false'){
		footer.addClass('closed').css('top', '-' + footerNewTop + 'px');
	}
	
	function footerShow(){
		footerNewTop = $('body').outerHeight();
		
		footer.animate({
			top: footerNewTop + 'px'
		}, 1000, 'easeOutQuad', function(){
			footer.css('top', '');
			$('html, body').animate({scrollTop:footer.offset().top}, 'slow');
		}).removeClass('closed');
		
		$.cookie('showFooter', 'true', { expires: 7, path: '/' });
	}
	
	function footerHide(){
		footerHeight = footer.height();
		footerNewTop = footerHeight + footerMarginTop;
		
		$('html').animate({
			scrollTop: 0
		}, 250, function(){
			footer.animate({
				top: '-' + footerNewTop + 'px'
			}, 1000, 'easeOutBounce', function(){
				
			}).addClass('closed');
		});
		
		$.cookie('showFooter', 'false', { expires: 7, path: '/' });
	}
		
	$('#footerToggle').show().click(function(){
		if(footer.hasClass('closed')){
			footerShow();
		} else {
			footerHide();
		}
		
		return false;
	});
	
	
	/*
	 * Other
	 */
	$('.email').nospam({
		replaceText:true
	});
});