//------------------------------------
//	ENGAGE.JS
//	Author: 	Engage Interactive
//	Requires:	jquery.js
//------------------------------------


$(function(){
//BEGIN jQuery

	//Replacement for target="_blank"
	
	$('.external').attr('target','_blank');

	//CLICKY FORM TITLES
	
	$('form input[title]').each(function(){
		$(this).attr('value', $(this).attr('title'));
	});
	$('form input[title]').focus(function(){
		if($(this).attr('value') == $(this).attr('title')){
			$(this).attr('value', '');
		}
	});
	$('form input[title]').blur(function(){
		if($(this).attr('value') == $(this).attr('title') || $(this).attr('value') == ''){
			$(this).attr('value', $(this).attr('title'));
		}
	});
	
	
	// TERTIARY NAV
	
	mouse = true;
	$('li.active').addClass('current');
	
	$('li.tertiary a.top').click(function(){
		
		if( $(this).parent('li').hasClass('active') && mouse == true ){

			if($('li.current').hasClass('active') == false){
				showHide('show', $('li.current ul'));
			}
			showHide('hide', $(this).next('ul'));

		}else if( mouse == true ){

			showHide('hide', $('li.active ul'));
			showHide('show', $(this).next('ul'));

		}
		
		function showHide(action, ul){
			height = ul.height();
			if(action == 'show'){
				ul.parent('li.tertiary').addClass('active');
				ul.stop([]).css({height:0}).animate({height:height},400,'easeInOutExpo',function(){
					$(this).removeAttr('style');
					mouse = true;
				});
			}else{
				ul.stop([]).animate({height:0},400,'easeInOutExpo',function(){
					$(this).removeAttr('style').parent('li.tertiary').removeClass('active');
					mouse = true;
				});
			}
		}
		
		mouse = false;
		
		return false;
	});


	//EQUAL HEIGHTS
	bigHeight = 0;
	$('.office:last').addClass('last');
	$('.office').each(function(){
		if($(this).height() > bigHeight){
			bigHeight = $(this).height();
		}
		if($(this).hasClass('last')){
			$('.office').height(bigHeight);
		}
	});


	//CONTACT FORM COLOURBOX
	
	$('a.quote_now').colorbox({inline:true, href:"#contact_form"},function(){
		subject = $('#content h2:first').text();
		$('#subject').val(subject);
	
	});


//END jQuery
});

//EASING

jQuery.extend(jQuery.easing,{
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	}
});


//CONTACT FORM


$(function(){
	$("#submit").click(function() {
		if(checkValidation() == false){
			$('#error_message').fadeIn();
		}else{
			var dataString = $("#contact_form").serialize();
			$.ajax({
				type: "POST",
				url: "/contact/send",
				data: dataString,
				success: function() {
					$('#contact_form').html("<div id='message'></div>");
					$('#message').html("<h2>Request submitted</h2>")
					.append("<p>We will be in contact soon.</p>")
					.hide()
					.fadeIn(1500, function(){
						//After fade
					});
				}
			});
		}
	    return false;
	});
	
	//E-MAIL VALIDATION
	$("#email").blur(function(){
		var email = $(this).val();
		
		if($('#phone').val().length < 10 || $('#phone').val() == $('#phone').attr('title')){
			if(email.length != 0){
				if(isValidEmailAddress(email)){
					$(this).parent('div').removeClass('error');
					$('#phone').blur();
				}else{
					$(this).parent('div').addClass('error');
				}
			}else{
				$(this).parent('div').removeClass('error');
			}
		}else{
			$(this).parent('div').removeClass('error');
		}
	});
	
	$("#phone").blur(function(){
		var phone = $(this).val();
		
		if(isValidEmailAddress($("#email").val()) != true || $('#email').val() == $('#email').attr('title')){
			if(phone != $(this).attr('title')){
				$('#email').parent('div').removeClass('error');
				$(this).parent('div').removeClass('error');
			}else{
				$(this).parent('div').addClass('error');
			}
		}else{
			$(this).parent('div').removeClass('error');
		}
	});
	
	$('#name').blur(function(){
		c = $(this).val().length;
		
		if(c != 0){
			if(c < 3 || $(this).val() == $(this).attr('title')){
				$(this).parent('div').addClass('error');
			}else{
				$(this).parent('div').removeClass('error');
			}
		}else{
			$(this).parent('div').addClass('error');
		}
	});

	function checkValidation(){
		//Innocent until proven guilty!
		valid = true;
		
		//Check the required fields
		$('#email, #name, #phone').blur();
		
		//Check for error classes
		if($('.error').size() > 0){
			valid = false;
		}
		
		return valid;
	}

});

//CHECK FOR VALID EMAIL
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

