var resize_container = function() {
  $('#container').css('height','auto');
  var h = Math.max(Math.max(600,$('div#container').height()), $(window).height());
  $('#container').css('height',h);
};

var fix_form_fields = function() {
  $('div.form_container label').each(function() {
      var l_w = $(this).width();
      var p_w = $(this).parent().width();
      $(this).parent().find('input[type="text"]').width(p_w - l_w - 12);
      $(this).parent().find('textarea').width(p_w - l_w - 12);
    });
  $('div.form_container label.own-line').each(function() {
      var p_w = $(this).parent().width();
      $(this).parent().find('input[type="text"]').width(p_w);
      $(this).parent().find('textarea').width(p_w);
    });
  $('div.fields input[type="text"]:first').focus();
  $('div.form_container textarea').autoResize({
      animate: true,
      animateCallback : resize_container,
      extraSpace : 10
    });
}

$(document).ready(function(){
    $('div.sociable').hide();
    $('div.sociable a').each(function() {
      $(this).append('<span>'+$(this).attr('title')+'</span>');
      });
    $('a.share').mouseenter(function() {
        $('div.sociable').css({ 'top': ($(this).offset()['top'] + 15) + 'px', 'left': $(this).position()['left'] + 'px' });
        $('div.sociable').fadeIn('normal');
	var f = function() {
          $('div.sociable').fadeOut('normal');
	  $('div.sociable').unbind('mouseleave', f);
        }
        $('div.sociable').bind('mouseleave', f);
    });

    $("h6.nav_title").mouseenter(function() {
      $(this).parent().addClass('visible');
    });
    $("div.nav").mouseleave(function() {
      $(this).removeClass('visible');
    });

    $(window).resize(resize_container);
    $(window).load(resize_container);

    if ($('div#front_intro_text') != null && $('div.latest_tweet') != null) {
      var margin = $('div#front_intro_text').offset()['top'] - $('div.latest_tweet').offset()['top'] - $('div.latest_tweet').height();
      $('div.latest_blog').css('margin-top', margin + 'px');
    }
    $('a#join_enews').click(function() {
      var f = function() {
        $('#popup').fadeOut('normal');
        $('#lightbox').fadeOut('normal',function() {
          $('#lightbox').unbind('click', f);
          $('#close_popup').unbind('click', f);
        });
      }
      $('#popup').fadeIn('normal',function() {
        fix_form_fields();
        $('#popup input[type="text"]:first').focus();
      });
      $('#lightbox').fadeIn('normal',function() {
        $('#lightbox').bind('click', f);
        $('#close_popup').bind('click', f);
      });
    });
    $('div#show_buddy a').click(function() {
      var f = function() {
        $('#buddy').fadeOut('normal');
        $('#lightbox').fadeOut('normal',function() {
          $(document).unbind('click', f);
        });
      }
      $('#buddy').fadeIn('normal');
      $('#lightbox').fadeIn('normal',function() {
        $(document).bind('click', f);
      });
    });

    heightArray = new Array();
    $('div.extra div.content').each(function(i) {
        $(this).data('index',i);
        heightArray[i] = $(this).height();
        });
    $('div.extra p.header').click(function() {
      $(this).parent().children('div.content').css('height', heightArray[$(this).parent().children('div.content').data('index')]);
      $(this).parent().children('div.content').slideToggle('normal', resize_container);
      $(this).find('a').toggleClass("closed");
      });
    fix_form_fields();

    $.localScroll({duration: 200});

    $.validator.addMethod("phone", function(phone) {
      phone = phone.replace(/^\s+/, "");
      phone = phone.replace(/\s+$/, "");

      if(phone.length == 0) {
        return true;
      }

      if(phone.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/)) {
        return true;
      }
      return false;
    }, "Please specify a valid phone number");

    $.validator.addMethod("zipcode", function(zip) {
      // matches US ZIP code
      // allow either five digits or nine digits with an optional '-' between
      zip = zip.replace(/^\s+/, "");
      zip = zip.replace(/\s+$/, "");

      if(zip.length == 0) {
        return true;
      }

      if(zip.match(/^\d{5}([- ]?\d{4})?$/)) {
        return true;
      }
      return false;
    }, "Please specify a valid ZIP code");

    $('form.validate').validate({
      errorElement: "p",
      errorPlacement: function(error, element) {
        if (element.attr('tabindex') == '-1') return;
        error.appendTo( element.parent() );
        error.css('left',element.position().left + 'px');
	resize_container();
      },
      focusCleanup: false,
      focusInvalid: false,
      onkeyup: false
    });
    $('form.validate input').click(function() {
      var f = $(this).closest('form');
      var k = f.find('#key');
      if (k.size() < 1) {
        f.append('<input id="key" type="hidden" name="formkey" value="'+f.attr('action')+'" />');
      }
    });
 });
