$(function() {
    // Ensure all blocks on the main homepage are the same height
    var tallest = 0;
    $('body.associates.front div.content-after div.block').each(function() {
        if ($(this).height() > tallest) {
            tallest = $(this).height();
        }
    }).height(tallest);

    // Expands the footer height to fill the whole page
    if ($('body').hasClass('front')) {
    	var winHeight = $(window).height();
	    var wrapHeight = $('#wrapper').height();
    	var footerHeight = $('#footer').height();

        if ($('body').hasClass('admin-menu')) {
            winHeight -= $('body').css('marginTop').replace(/px$/, '');
        }

    	if (winHeight > wrapHeight) {
	    	$('#footer').height(footerHeight + (winHeight - wrapHeight));
    	}
    }

    Cufon.replace('h2, div.content-after > div.block > h3, div.content-before > div.block > h3');

    // Navigation hovering. Doing this so we can add a timeout
    var $nav = $('#header div.block-menu_block > div.content > div > ul.menu');
    var $active;
    var navTimer;

    var navName = function($item) {
        return $('>a', $item).html();
    };
    var showNav = function($item) {
        if ($active == $item) return; // Skip if already active
        if ($active) hideNav($active); // Hide the currently active one
        if (navTimer) clearTimeout(navTimer); // Clear the hide timer

        $active = $item;
        var pos = $('>a', $active).css('backgroundPosition');
        if (pos == 'undefined' || pos == null) {
            pos = $('>a', $active).css('background-position-x') + ' ' + $('>a', $active).css('background-position-y');
        }

        $('>a', $active).css('backgroundPosition', pos.replace(/0px$/, '-32px'));
        $('>ul', $active).show();
    };
    var hideNav = function() {
        if (!$active) return; // Only process if we have an active item
        var pos = $('>a', $active).css('backgroundPosition');
        if (pos == 'undefined' || pos == null) {
            pos = $('>a', $active).css('background-position-x') + ' ' + $('>a', $active).css('background-position-y');
        }

        $('>a', $active).css('backgroundPosition', pos.replace(/-32px$/, '0px'));
        $('>ul', $active).hide();
        $active = null;
    };

    $('>li', $nav).hover(function() {
        showNav($(this));
    }, function() {
        navTimer = setTimeout(hideNav, 500);
    });

    $('form#contact_old').bind('submit', function() {
      if ($('input#name', this).val() == "") {
        $('input#name').siblings('p.error').show();
      } else {
        $('input#name').siblings('p.error').hide();
      }

      if (!ValidEmail($('input#email', this).val())) {
        $('input#email').siblings('p.error').show();
      } else {
        $('input#email').siblings('p.error').hide();
      }

      return $('p.error:visible', this).length == 0;
    });
});

var ValidEmail = function(email) {
  return /[a-z0-9_.+-]+@[a-z0-9_-]+\.[a-z0-9_.-]+/i.test(email);
};

