function focusSiteForm() {
  var elements = $("#content .site-form :input:visible");
  try {
    if (elements.length > 0) elements[0].focus();
  }
  catch(e) { /* Do Nothing */ }
}

$(document).ready(function() {
  Shadowbox.init({
    assetURL: '/',
    loadingImage: 'images/loader.gif',
    text: {
      close: 'Close'
    }
  });

  $("a[href$='/help/terms'], a[href$='/help/privacy']").bind('click', function() {
    Shadowbox.open({
      content: this.href + '/0.plain',
      type: 'iframe',
      width: 640,
      height: 480
    });
    
    return false;
  });
  
  focusSiteForm();
  
  var forms = $('#content .site-form');
  for (var i=0; i<forms.length; i++) {
    $(forms[i]).validate({
      showErrors: function(errorMap, errorList) {
        for(var i=0; i<errorList.length; i++) {
          var error = errorList[i];
          this.settings.highlight.call(this, error.element, this.settings.errorClass);
          $(error.element.parentNode).children('span').html(error.message);
        }
      },
      highlight: function(element, errorClass) {
        $(element.parentNode).addClass(errorClass);
      },
      unhighlight: function(element, errorClass) {
        $(element.parentNode).removeClass(errorClass);
      }
    });
  }
  
  $("#content .add-item").click(function() {
    var _this = $(this);
    var corners = $("#core i.corner.ll, #core i.corner.lr");
    
    corners.css('display', 'none');
    
    $("#content .site-form").slideToggle(function() {
      var corners = $("i.corner");
      corners.css('display', 'block');
      
      _this.toggleClass('remove');
      
      focusSiteForm();
    });
    
    return false;
  });
  
  var flash = $("#flash");
  if (flash.find('li').length > 0) $("#flash").fadeIn();
  flash.bind('click', function() {
    $(this).fadeOut();
  });
  window.setTimeout(function() { flash.click(); }, 30000);
});