// use head.js to load our javascript files
head.js('/js/jquery-1.6.min.js', '/js/jquery.scrollTo-1.4.2-min.js');

// js has loaded and dom is scriptable
head(function(){

	md.set_strip_heights();
	md.make_nav_rollovers();
	md.setup_nav();
	md.scroll_to_correct_page(); // disabled this, it's kind of annoying
	autoclear.init();
	md.handle_contact_form();
	
});


// create an object with functions inside it, nice and clean.
var md = {

	animating: false,	// disable hover actions while this variable is true

	set_strip_heights: function() {
		var max_height = $('#shell').height();
		$('#bars div').each(function(){
			$(this).css('height', max_height + 'px');
		});
	},

	make_nav_rollovers: function() {
		$('.nav ul li a').hover(
			function(){
				if (md.animating == false) {
					var id = $(this).parent().attr('class');
					$('#bars #' + id + '_bar').stop(true).animate({ opacity: 0.25 }, 300);
				}
			},
			function(){
				if (md.animating == false) {
					var id = $(this).parent().attr('class');
					$('#bars #' + id + '_bar').stop(true).animate({ opacity: 0.06 }, 1000);
				}
			}
		);
	},

	setup_nav: function() {
		$('.nav ul li a').bind('click', function(){
			md.animating = true;
			var id = $(this).parent().attr('class');
			$('#bars #' + id + '_bar').css('opacity',1).stop(true).animate({ opacity: 0.06 }, 2000);
			$('html, body').stop(true).scrollTo('#' + id + '_page', 800, {
				onAfter: function(){ md.animating = false; }
			});
			
			track_event( 'Navigation' , id , 'User has switched to ' + id );
		});
	},

	scroll_to_correct_page: function() {
		if ( window.location.href.indexOf('#') != -1 ) {		// only run this if there's a hash in the url
			var page = window.location.href.slice( window.location.href.indexOf('#') + 1 );

			if (page != 'home') {
				md.animating = true;
				$('html, body').stop(true).scrollTo('#' + page + '_page', 50, {
					onAfter: function(){ md.animating = false; }
				});
			}
		}
	},

	handle_contact_form: function() {
		$('#submit').bind('click', function(){
			// disable the submit button
			$(this).attr('disabled', 'disabled').addClass('disabled');
			
			if ( md.validate_contact_form() ) {
				md.send_contact_email();
				
				track_event( 'Contact' , 'Successful' , 'User submitted the form successfully' );
			} else {
				$(this).attr('disabled', null).removeClass('disabled');
				
				track_event( 'Contact' , 'Unsuccessful' , 'Invalid user submission' );
			}
			return false;
		});
	},

	validate_contact_form: function() {
		var form_valid = true;
		
		if ( !validate.str( $('#name').val(), 3, 255 ) ) {
			msg.show($('#name').siblings('div.msg'), 'error', 'name must be a minimum of 3 characters long');
			form_valid = false;
		} else {
			msg.hide($('#name').siblings('div.msg'));
		}
		if ( !validate.email( $('#email').val() ) ) {
			msg.show($('#email').siblings('div.msg'), 'error', 'the email you entered is not valid');
			form_valid = false;
		} else {
			msg.hide($('#email').siblings('div.msg'));
		}
		if ( !validate.str( $('#subject').val(), 3, 255 ) ) {
			msg.show($('#subject').siblings('div.msg'), 'error', 'the email subject is too short');
			form_valid = false;
		} else {
			msg.hide($('#subject').siblings('div.msg'));
		}
		if ( !validate.str( $('#message').val(), 5, 9999 ) ) {
			msg.show($('#message').siblings('div.msg'), 'error', 'your message is too short');
			form_valid = false;
		} else {
			msg.hide($('#message').siblings('div.msg'));
		}
		
		return form_valid;
	},

	send_contact_email: function() {
		var data = {
			name: $('#name').val(),
			email: $('#email').val(),
			subject: $('#subject').val(),
			message: $('#message').val(),
			
			// pass value of the honeypot field to the php script
			honeypot: $('#something').val()
		}
		$.post('/contact.php', data, function(e){
			$('#submit').attr('disabled', null).removeClass('disabled');
			var msg_div = $('#submit').siblings('div.msg');
			
			if (e == 'success') {
				msg.show(msg_div, 'success', 'your email has been sent successfully, thanks');
				msg_div.delay(5000).fadeOut();
				md.clear_form();
				autoclear.setup_events();
			} else if (e == 'error') {
				// email not sent
				msg.show(msg_div, 'error', 'your email was not sent, please try again');
			} else if (e == 'spam') {
				// probably a spam bot
				msg.show(msg_div, 'error', 'unable to process form due to spam protection');
			} else {
				// unknown
				msg.show(msg_div, 'error', 'an unknown error occured, try again later');
			}
			
			msg_div.css({
				'left': '60px',
				'top': '5px',
				'width': '290px'
			});
		});
	},

	clear_form: function() {
		$('#name, #email, #subject, #message').val('');
		$('#name, #email, #subject, #message').siblings('label').animate({ opacity:1 }, 400);
	}

}

var autoclear = {
	
	init: function() {
		this.setup_events();
	},

	setup_events: function() {
		if ($.browser.webkit) {
			$('input').attr('autocomplete', 'off'); // disable autocomplete for chrome/shitfari
		}
		$('#name, #email, #subject, #message').unbind().bind('focus', function() {
			if ( $(this).val().length < 1 ) {
				var lbl = $(this).siblings('label');
				lbl.stop(true).animate({ opacity: 0.4 }, 200);
			}
		}).bind('blur', function() {
			if ( $(this).val().length < 1 ) {
				var lbl = $(this).siblings('label');
				lbl.stop(true).animate({ opacity: 1 }, 200);
			}
		}).bind('keypress, keydown, keyup', function(){
			if ( $(this).val().length > 0 ) {
				var lbl = $(this).siblings('label');
				lbl.stop(true).animate({ opacity: 0 }, 0);
			} else {
				var lbl = $(this).siblings('label');
				lbl.stop(true).animate({ opacity: 0.4 }, 0);
			}
		});
	}
	
}

var msg = {

	show: function(field, type, message) {
		field.addClass( type ).text( message ).hide().fadeIn();
	},

	hide: function(field) {
		field.fadeOut();
	}

}

var validate = {

	str: function(str, min_len, max_len) {
		var l = str.length;
		if (l < min_len || l > max_len) {
			return false;
		}

		return true;
	},

	email: function(email) {
		var regex = /^[a-z0-9\-_\.]+@[a-z0-9\-_\.]+\.[a-z]{2,4}$/gi
		return (email.match( regex ) == null) ? false : true;
	}

}
