/*********************************************************
*
* application.js
*
*
* Copyright (C) 2010 Carbonfrost Systems LLC.
* All rights reserved.
*
* Comments or questions on this document? Contact the
* Web presence team.
*
**********************************************************/

/** NAVIGATION **********************************************************/
function clearNavTimeout() {
  while (navCloseMenu.timeout.length > 0)
    clearTimeout(navCloseMenu.timeout.pop());
}
function setNavTimeout(time) {
  clearNavTimeout();
  navCloseMenu.timeout.push(setTimeout(navCloseMenu, time));
}
function navCloseMenu() {
  $("#prologueMenu li").removeClass("on");
}
function navOpenMenu(a) {
  var $li = $(a).closest("li");
  navCloseMenu();
  $li.addClass("on");
  clearNavTimeout();
}
$(document).ready(function() {
  navCloseMenu.timeout = [];
  var $a = $("#prologueMenu a.top-l");
  var $pm = $("#prologueMenu .pm-container");

  $a.mouseenter(function() { navOpenMenu($(this)); });
  $a.mouseleave(function() { setNavTimeout(3000); });
  $pm.mouseleave(function() { setNavTimeout(1000); });
  $pm.mouseenter(function() { navOpenMenu($(this)); });
});

/** CONTROLS ************************************************************/
$(document).ready(function() {

  $('.reveal-up').each(function() {
    $(this).hover(
      function() {
        $(this).stop().animate({top: "-80px", opacity: 1.0 }, 300);
      },
      function() {
        $(this).stop().animate({top: "0px", opacity: 0.6 }, 300);
      }
    )
  });
  
  $(".submit-form-link").click(function() {
    var $form = $(this).closest("form");
    if ($form.hasClass("ajax-submit"))
      $form.ajaxSubmit({ dataType: "script" });
    else
      $form.submit();
  });
  
  $(".watermarked")
    .focus(function() {
      var $this = $(this);
      if ($this.val() == $this.attr("title"))
        $this.val("");
    })
    .blur(function() {
      var $this = $(this);
      if ($this.val() == "")
        $this.val($this.attr("title"));
    });

});


var Carbonfrost = window.Carbonfrost || {};
Carbonfrost.StandardForms = Carbonfrost.StandardForms || {};
Carbonfrost.StandardForms.getLabelForEdit = function(elementId) {
  return $('label[for=' + elementId + ']:not(label[generated])');
};
Carbonfrost.StandardForms.standardValidationSettings = {
    onfocusout: false,
    onkeyup: false,
    onclick: false,
    onsubmit: true,
    invalidHandler: function(form, validator) {
      $("#errors").slideDown();
    },
    errorPlacement: function(label, element) {
      $("#errors").append(label);
      
      var anchor = label.children("a");
      anchor.attr("href", "javascript:void(0)");
      anchor.click(function() {
        $("#" + $(this).attr("htmlfor")).focus();
      });
      Carbonfrost.StandardForms.getLabelForEdit(element.attr("id")).attr("title", anchor.text());
    },
    highlight: function(element, errorClass) {
      Carbonfrost.StandardForms.getLabelForEdit(element.id).addClass("highlighted-field");
    },
    unhighlight: function(element, errorClass) {
      Carbonfrost.StandardForms.getLabelForEdit(element.id).removeClass("highlighted-field");
	  Carbonfrost.StandardForms.getLabelForEdit(element.id).attr("title", "");
    },            
    wrapper: "li",
    errorElement: "a"
};
$(document).ready(function() {

  $(".reset-form-link").click(function() {
    $(this).closest("form")[0].reset();
  });

});