var Macco = {
	
	submenu_duration:1000,
	submenu_int:0,
	submenu_type:'',
	
	init : function()
	{
		// macco init
		$('li.hassubmenu').bind('mouseover', Macco.submenu_over);
		$('#content').bind('mouseover', Macco.submenu_hide);
		$('#tell_a_friend').bind('click', Macco.tell_a_friend);
	},
	
	tell_a_friend : function()
	{
		var you = $('#your_email').val();
		var them = $('#friend_email').val();
		var pattern = new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);
		
		if(pattern.test(you) && pattern.test(them))
		{
			$.post(
				'/ajax/tell_a_friend',
				{ your_email:you, friend_email:them },
				function(resp)
				{
					$('#tell_a_friend_form').html(resp);
				}
			);
		}
		else
		{
			alert('Please enter two valid email addresses.');
		}
	},
	
	submenu_show : function(type)
	{
		$('#submenu_'+type).show();
	},
	
	submenu_over : function()
	{
		clearTimeout(Macco.submenu_int);
		$('div.submenu').each(function(index) {
			$(this).hide();
		});
		
		var type = $(this).attr('id');
		if(this.tagName == 'LI')
		{
			if(type) 
			{
				Macco.submenu_show(type);
				Macco.submenu_type = type;
			}
		}
	},
	
	submenu_hide : function()
	{
		$('div.submenu').each(function(index) {
			$(this).hide();
		});
	},
	
	submenu_out : function()
	{
		Macco.submenu_int = setTimeout(Macco.submenu_hide,Macco.submenu_duration);
		
	},

	validate_contact_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked))
			{
				$(this).parents("div.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		if( !submit_it ) $('#error_message').fadeIn(250);
		return submit_it;
	}
}

$(Macco.init);
