/* Author: Aaron Whitman @ Able Sense Media Limited (aw@ablesense.com)

*/

$(document).ready(function(){
	// Main menu items
	// Offset for fixed vs static header
	var headerOffset = $('body>header').css('position') == 'fixed' ? 120 : 0;

	// Did we directly access a URL with an anchor?
	var anchor = location.hash;
	if(anchor) {
		$('html, body').animate({
		    scrollTop: $(anchor).offset().top - headerOffset
		}, 700);
	}

	// Set main menu item clicks to scroll to their sections
	$('a[href^=#]').click(function(event){
		event.preventDefault();
		$('.current').removeClass('current');
		$('a[href=' + $(this).attr('href') + ']').addClass('current');
		var section = $(this).attr('href');
		$('html, body').animate({
		    scrollTop: $(section).offset().top - headerOffset
		}, 700);
	});
	
	// Clickable figcaptions
	$('figcaption').each(function(){
		var anchors = $(this).find('a');
		if(anchors.length==1) {
			$(this).click(function(){window.location.href=this.getElementsByTagName('a')[0].href; return false;});
			$(this).addClass('clickable');
		}
	});

	// Contact Form
	var contactForm = $('#contact-form');
	
	// Setup placeholder focus/blur toggle
	$('[placeholder]').focus(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
	}).blur(function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		}
	}).blur();
	
// 	// We have javascript, so use an RPC style post
// 	contactForm.find('#rpc').attr('value', 1);
// 	contactForm.submit(function(event){
// 		event.preventDefault();
// console.log('submitted');
// // return confirm('sure?');
// 		// Clear any previous placeholder text and error messages
// 		clearPlaceholderText();
// 		$('div.error').remove();
// 		
// 		// Send the form data with post
// 		//var jqxhr = $.post("process_contact.php", $(this).serialize())
// 		var jqxhr = $.post('https://service.capsulecrm.com/service/newlead', $(this).serialize())
// 		.success(
// 			function(data){
// console.log(data);
// 				var result = $.parseJSON(data);
// 				if(result.status == 'success') {
// console.log(result);
// 					contactForm.wrap('<div class="rpc-form"/>');
// 					$('.rpc-form').append('<div style="display:none;"><h3>Thanks for your message!</h3><p class="notice">' + result.message + '</p></div>');
// 					contactForm.fadeOut(500);
// 					$('.rpc-form div').delay(500).fadeIn(500);
// 				} else {
// 					contactForm.before('<div class="error"><h3>Oops! Message not sent.</h3><p>' + result.message + '</p></div>');
// 					setupPlaceholderText($(this));
// 				}
// 			}
// 		)
// 		.error(
// 			function(){
// 				contactForm.before('<div class="error"><h3>Oops! Message not sent.</h3><p>Please complete all fields.</p></div>');
// 			}
// 		)
// 		.complete(function(){});
// 	});

	// Placeholer Text Helpers
	function setupPlaceholderText() {
		$('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		});
	}
	
	function clearPlaceholderText() {
		$('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
			}
		});
	}

	// Easter eggs
	$('.csstransforms .vcard li:last-child').addClass('clickable').click(function(event){
		event.preventDefault();
		var oldText = $(this).html();
		$('html').not('.touch').append('<div class="easterEggTip"><p>Press "Esc" to go back to normal</p></div>');
		$('html.touch').append('<div class="easterEggTip"><p>Tap anywhere to go back to normal</p></div>');
		var d=0;
		var body = $('body');
		var eerIntervalId = setInterval(function() {
			if(d<181) {
				body.css('-webkit-transform', 'rotate('+ d +'deg)');
				body.css('-moz-transform', 'rotate('+ d +'deg)');
				body.css('-ms-transform', 'rotate('+ d +'deg)');
				body.css('-o-transform', 'rotate('+ d +'deg)');
				body.css('transform', 'rotate('+ d +'deg)');
				d+=1;
			} else {
				stopEasterEgg(eerIntervalId);
			}
		},10);
		
		// Let the escape key stop the easter egg
		$(document).keyup(function(event) { 
			if (event.which == 27) {
				stopEasterEgg(eerIntervalId);
				removeEasterEgg();
			}
		});
		
		// Touch devices don't have escape keys do they?
		$('html.touch').bind("touchend", function(event) {
			stopEasterEgg(eerIntervalId);
			removeEasterEgg();
		});
		
	});
	
	// The easter egg off switch
	function stopEasterEgg(eerIntervalId) {
		var id = eerIntervalId;
		var body = $('body');
		clearInterval(id);
	}
	
	function removeEasterEgg() {
		var body = $('body');
		body.css('-webkit-transform', 'none');
		body.css('-moz-transform', 'none');
		body.css('-ms-transform', 'none');
		body.css('-o-transform', 'none');
		body.css('transform', 'none');
		$('.easterEggTip').remove();
	}
	
	
	// $('body').append('<p>' + $('html').attr('class') + '</p>');
	

});

