$(function() {
	
	$('.feature-boxes > li:first').addClass('open');
	$('.feature-boxes img').click(function() {
		$(this).parents('li').toggleClass('open');
	});
	
	$('input,textarea').focus(function() {
		if (!$(this).is('.submit-button')) {
			$(this).addClass('highlight');
		}
	}).blur(function() {
		$(this).removeClass('highlight');
	});
	
	$('form.validate-form').each(function() {
		$(this).validate({
			submitHandler: function(form) {
				$(form).ajaxSubmit({
					type: 'POST',
					dataType: 'json',
					success: function(r, status) {
						$.facebox(r.message);
						if (r.error == 0) {
							$('.validate-form').find(':text,:password').val('');
						}
						
						if (r.proceed_url != null) {
							$('#facebox').find('a.close').attr('href', r.proceed_url);
							$('#facebox').find('a.close').click(function() {
								window.location = $(this).attr('href');
							});
						}
						
						/*if (r.url != null) {
							setTimeout('window.location="' + r.url + '";', 8000);
							return false;
						}
						
						if (r.refresh != null) {
							var d = new Date();
							window.location = window.location + '?' + d.getTime();
						}*/
					}
				});
				
				return false;
			}
		});
	});
	
	$('#navigation li > a').hover(function() {
		var $img = $(this).find('img');
		
		$img.attr('off_src', $img.attr('src'));
		$img.attr('src', $img.attr('on_src'));
	}, function() {
		var $img = $(this).find('img');
		
		$img.attr('src', $img.attr('off_src'));
	});
	
	$('#navigation li > a').mouseover(function() {
		if ($(this).next().is('.sub_menu')) {
			$(this).next().show();
			$(this).parent().addClass('active');
		}
	});
	
	$('#navigation li > a').mouseover(function() {
		if ($(this).next().is('.sub_menu')) {
			$(this).next().show();
		}
	});
	
	$('#navigation .active').live('mouseout', function(evt) {
		var x = evt.pageX, y = evt.pageY;
		var li_w = $(this).width(), li_h = $(this).height(), li_pos = $(this).position();
		
		if ($(this).find('.sub_menu').length > 0) {
			var $sub_menu = $(this).find('.sub_menu');
			var sub_menu_h = $sub_menu.height();
			
			var box_x1 = li_pos.left, box_y1 = li_pos.top,
				box_x2 = (li_pos.left + li_w), box_y2 = (li_pos.top + li_h + sub_menu_h)
			
			if (
				 (x >= box_x1 && x <= box_x2)
				 &&
				 (y >= box_y1 && y <= box_y2)
				) {
				// do nothing here, but hide menu in else
			} else {
				$sub_menu.hide();
			}
			
		}
	});
	
});