$(function () {
	maxHeight = 0;
	$('ul#navigation ul').each(function(){
		if ( $(this).height() > maxHeight ) { maxHeight = $(this).height() }
	});
	$('ul#navigation li a.top_level').click( function () {
		if ( $('div#navigation_wrap').height() > 20 ) {
			$('div#navigation_btm').fadeOut(200);
			$('div#navigation_wrap').animate({ height: "20px" },{easing: "easeOutQuint"});
		}else{
			$('div#navigation_wrap').animate({ height: Math.max((maxHeight + 50), 120)+ 'px'}, {easing: "easeOutQuint"});
			$('div#navigation_btm').fadeIn(200);
		}
		return false;
	});
	
});


$(document).ready(function(){
	
	// NEWSLETTER
	
	// Insert Newsletter-Box
	var box_html = '';
	$('body').append('<div id="newsletter_box"><form id="newsletter_form" method="POST" action="http://blocks.sct.com.au/forms/U6muJWVrvZGeMBF"><h2>Newsletter</h2><div id="nl_form_close">x</div><p id="newsletter_info">Please enter your name and email to receive our newsletter.</p><input type="text" id="first_name" name="first_name" value="NAME"><input type="text" id="email" name="email" value="EMAIL"><input type="submit" value="Submit"></form></div>');
	
	
	
	// Add action to link
	$('a[href="http://www.blocksglobal.com/#newsletter"]').live('click',function(){
		
		var open_obj = $('a[href="http://www.blocksglobal.com/#newsletter"]');
		var open_location = open_obj.position();
		var open_x = open_location.left;
		var open_y = open_location.top - $('#newsletter_box').height();
		
		$('#newsletter_box').css({left: open_x+'px', top : open_y+'px'});
		$('#newsletter_box').fadeIn(350);
		
		return false;
	});
	
	$('#nl_form_close').live('click',function(){
		$('#newsletter_box').fadeOut(350);
	});
	
	
	// ON SUBMIT
	$('#newsletter_form').live('submit', function(){
		
		var inputs = $("input", this);
		var something_wrong = 0;
		for(var i = 0; i < inputs.length; i++) {
			if($(inputs[i]).attr("type") != 'checkbox' && inputs[i].id != 'email' && inputs[i].id != 'submit' && ( $(inputs[i]).val() == '' || $(inputs[i]).val() == 'NAME') ) {
				alert('Please enter your name.');
				$(inputs[i]).focus();
				something_wrong = 1;
				return false;
			} else if(inputs[i].id == 'email') {
				if(!$(inputs[i]).val().match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/)) {
					alert('Please enter a valid email address.'); 
					$(inputs[i]).focus();
					something_wrong = 1;
					return false;	
				}
			} 
		}
		
		if( something_wrong == 0 )
		{
			var aj_action = $('#newsletter_form').attr('action') + '.json?callback=?';
			var aj_name = $('#newsletter_form input[name=first_name]').val();
			var aj_email = $('#newsletter_form input[name=email]').val();
			aj_action = aj_action + "&interests[]=27&first_name="+aj_name+"&email="+aj_email; 

			
			$('#newsletter_info').html('<p><b>Sending... please wait!</b></p>');
			
			$.getJSON(aj_action, function(){
				$('#newsletter_info').html('<p><b>Subscription successfull!</b><br>Thank you for subscribing to our newsletter.</p>');
				$('#newsletter_box').delay(3500).fadeOut(350);
			})
			
		}
		return false;
	});
	
	$('input#first_name, input#email')
	.live('focus',function(){
		
		if($(this).val() == "EMAIL" || $(this).val() == "NAME" )
		{
			$(this).val('');
		}
		
	})
	.live('focusout', function(){
		if( $(this).val() == "" )
		{
			if( $(this).attr('name') == "first_name")
			{
				$(this).val('NAME');
			}
			else
			{
				$(this).val('EMAIL');
			}
		}
	});
	
	// end : NEWSLETTER
	
});