var scroll_pos;

google.setOnLoadCallback(function() {

	/****** General ******/

	function get_Y_offset(){
		scroll_pos = window.pageYOffset;
		return scroll_pos;
	}
	
	/****** Menu ******/
	
	function reset_menu(){
		// Set the menu's left position
		var offset = $('#logo').offset();
		var position = offset.left + 20;
		
		$('nav').css({'left': position});
	}	
	
	// Reset on window resize
	$(window).resize(function(){
		reset_menu();
	});
	
	// Set the vertical position if we're near the top
	
	function set_vertical(){		
		var x_start = 225;
		//var body_current = $("body").scrollTop();
		var body_current = window.pageYOffset;
		
		if(body_current <= 200){
			$('nav').css({'top': x_start - body_current + 'px'});
		}
		
		if(body_current >= 201){
			$('nav').css({'top': '32px'});
		}
	}
	
	// Show/hide extras on scroll
	
	function set_extras(){
		var offset = $('nav').offset();
		var position = offset.top;
				
		var critical_point = 270;
		
		if(position >= critical_point){
			$('#menu_top_dummy').hide();
			$('#menu_top').fadeIn('fast');
			$('#menu_bottom').fadeIn('fast');
		} else if(position < critical_point){
			$('#menu_top').hide();
			$('#menu_bottom').hide();
			$('#menu_top_dummy').show();
		}
	}
	
	$(window).scroll(function () {
		set_extras();
		set_vertical();
    });
	
	// Set up on load
	reset_menu();
	set_extras();
	set_vertical();
	setTimeout(function(){
		$('nav').show();
	}, 300);
	
	// Scrolling links (uses ScrollTo plugin)
	
	$('.m0').click(function() {
		$('body').scrollTo( $('#logo_container'), 1000 );
	});
	
	$('.m1').click(function() {
		$('body').scrollTo( $('#ux_container'), 1000 );
	});
	
	$('.m2').click(function() {
		$('body').scrollTo( $('#cases_container'), 1000 );
	});
	
	$('.m3').click(function() {
		$('body').scrollTo( $('#about_container'), 1000 );
	});
	
	$('.m4').click(function() {
		$('body').scrollTo( $('#contact_container'), 1200 );
	});
	
	/****** Internal buttons ******/
	
	$('#int1').click(function() {
		$('body').scrollTo( $('#cases_container'), 1000 );
		switcher(2,0);
	});
	
	$('#int2').click(function() {
		$('body').scrollTo( $('#contact_container'), 1200 );
	});

	/****** Case study toggles ******/
	
	// Clear the top button's background style
	function menu_item_clear(){
		$('.t1').css({'color': '#fff', 'background-color': '#000'});
		$('.t2').css({'color': '#fff', 'background-color': '#000'});
		$('.t3').css({'color': '#fff', 'background-color': '#000'});
		$('.t4').css({'color': '#fff', 'background-color': '#000'});
		$('.t5').css({'color': '#fff', 'background-color': '#000'});
	}
	
	// Set the active item's background style
	function menu_item_set(item){
		$('.t'+item).css({
			'color': '#a70000',
			'background-color': '#fff', 
			'-webkit-border-radius': '2px',
			'-moz-border-radius': '2px',
			'border-radius': '2px',
			'behavior': 'url(/pie/PIE.htc)'
		});
	}
	
	// Initally set item 1 active
	menu_item_set(1);
	
	// Hide all the case study <div>s
	function hide_all(){
		$('.s1').hide();
		$('.s2').hide();
		$('.s3').hide();
		$('.s4').hide();
		$('.s5').hide();
		$('#full_list').hide();
	}
	
	// Called by the links. Passes item no and if top or lower link
	function switcher(item,lower){
		get_Y_offset();
		hide_all();
		$('.s'+item).fadeIn('slow');
		
		if(lower){	
			$('body').scrollTo( $('#cases_container'), 500 );
		} else {
			$(window).scrollTop(scroll_pos);
		}

		menu_item_clear();
		menu_item_set(item);	
	}
	
	// Called by the full list link
	function show_full_list(){
		get_Y_offset();
		hide_all();
		menu_item_clear();
		$('#full_list').fadeIn('fast');
		$(window).scrollTop(scroll_pos);
	}
	
	// Top link event handlers
	$('.t1').click(function() {
		switcher(1,0);
	});
	
	$('.t2').click(function() {
		switcher(2,0);
	});
	
	$('.t3').click(function() {
		switcher(3,0);
	});
	
	$('.t4').click(function() {
		switcher(4,0);
	});
	
	$('.t5').click(function() {
		switcher(5,0);
	});
	
	// Lower link event handlers
	$('.lt1').click(function() {
		switcher(1,1);
	});
	
	$('.lt2').click(function() {
		switcher(2,1);
	});
	
	$('.lt3').click(function() {
		switcher(3,1);
	});
	
	$('.lt4').click(function() {
		switcher(4,1);
	});
	
	$('.lt5').click(function() {
		switcher(5,1);
	});
	
	// Full client list event handler
	$('#fl').click(function() {
		show_full_list();
	});
	
});

/****** Contact us form ******/

function send_message(){
	
	var name = $("#name").val();
	var email = $("#email").val();
	var message = $("#message").val();
	
	var queryString = 'name=' + name + '&email=' + email + '&message=' + message;

	$.ajax({
	type: "POST",
	url: "scripts/message.php",
	data: queryString,
	success: function() {
		// Hide form
		scroll_pos = window.pageYOffset;
		$('form').hide();
		// Show confirmaton
		$('#success').fadeIn('fast');
		$(window).scrollTop(scroll_pos);
	}
	});
	
}

function validate_email(strValue) {
	var objRegExp  = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
	
	// Check for valid email
	return objRegExp.test(strValue);
}

function submit_form(){

	$('#error').fadeOut('fast');
	
	// Check for blank fields
	if ($("#name").val() == "" || $("#email").val() == "" || $("#message").val() == "") {
		$('#error').fadeIn('fast');
		return false;
	}
	
	// Validate the email address
	email_string = $("#email").val();
	
	if(!validate_email(email_string)){
		$('#error').fadeIn('fast');
		return false;
	}
	
	// If all good call send function
	send_message();
}



