/*===========================================================================
	Behavior (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Preloading images
	* Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	* Decorate the DOM with presentational elements
	* Collapsing/expanding sections
	* Form validation
	* Popup windows
===========================================================================*/

/* Enable FireBug/etc. logging if logging widget is loaded
===========================================================================*/

if (window.YAHOO && YAHOO.widget.Logger) {
	YAHOO.widget.Logger.enableBrowserConsole();
}

Cufon.replace("div.modal h2");
Cufon.replace("div.section-intro h1");
Cufon.replace("div.section-intro p");
Cufon.replace("#home h1");

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

$(document).ready(function() {
  $("a[rel='external']").click(function(e) {
		// Adapted from: http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
		var oWin = window.open(this.getAttribute("href"), "_blank");
		if (oWin) {
			if (oWin.focus) oWin.focus();
			return false;
		}
		
		oWin = null;
		return true;
	});
  
  $("ul#primary-navigation li a").live("click", function(e) {
    e.preventDefault();
    
    var target = $(this).attr("href");
    $.scrollTo(target, 1000, {offset: {left: -210}});
  });
  
  $("#secondary-navigation-about-the-show a").live("click", function(e) {
    e.preventDefault();
    
    var target = $(this).attr("href");
    $.scrollTo(target, 1000, {offset: {left: -210}});
  });
  
  $("div#contact-show-chairman").jqm({modal: false, trigger:'a.contact-chairman', closeClass:'jqmOverlay', overlay:50});
  $("div#show-packet").jqm({modal: false, trigger:'a#show-packet-link', closeClass:'jqmOverlay', overlay:50});
  $("div#email-updates").jqm({modal: false, trigger:'a#email-updates-link', closeClass:'jqmOverlay', overlay:50});
  
  $("form#contact-form").validate({
    invalidHandler: function(e, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = "There was a problem submiting your form. Make sure all the fields highlighted below are filled out completly and submit again.";
        $("div#contact-show-chairman p").addClass("errors").html(message);
      }
    },
    
    rules: {
      name: "required",
      company: "required",
      phone: {
        required: true,
        phoneUS: true
      },
      email: {
        required: true,
        email: true//,
      }
    },

    messages: {
      name: "",
      company: "",
      phone: "",
      email: ""
    },

    submitHandler: function(form) {
      $(form).ajaxSubmit({
        target: "#contact-show-chairman",
        success: function() {
          Cufon.refresh();
          $("div#contact-show-chairman").jqmAddClose(".close-modal");
        }
      });
    }
  });
  
  $("form#show-packet-form").validate({
    invalidHandler: function(e, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = "There was a problem submiting your form. Make sure all the fields highlighted below are filled out completly and submit again.";
        $("div#show-packet h2").after("<p class=\"errors\">"+message+"</p>");
      }
    },
    
    rules: {
      "educator-name": "required",
      school: "required",
      "address-1": "required",
      city: "required",
      state: "required",
      zip: "required",
      "educator-email": {
        required: true,
        email: true
      }
    },
    
    messages: {
      "educator-name": "",
      school: "",
      "address-1": "",
      city: "",
      state: "",
      zip: "",
      "educator-email": ""
    },
    
    submitHandler: function(form) {
      $(form).ajaxSubmit({
        target: "#show-packet",
        success: function() {
          Cufon.refresh();
          $("div#show-packet").jqmAddClose(".close-modal");
        }
      });
    }
  });
  
  $("form#email-updates-form").validate({
    invalidHandler: function(e, validator) {
      var errors = validator.numberOfInvalids();
      if (errors) {
        var message = "There was a problem submiting your form. Make sure all the fields highlighted below are filled out completly and submit again.";
        $("div#email-updates p").addClass("error").html(message);
      }
    },
    
    rules: {
      "student-name": "required",
      "student-school": "required",
      "student-email": {
        required: true,
        email: true
      }
    },
    
    messages: {
      "student-name": "",
      "student-school": "",
      "student-email": ""
    },
    
    submitHandler: function(form) {
      $(form).ajaxSubmit({
        target: "#email-updates",
        success: function() {
          Cufon.refresh();
          $("div#email-updates").jqmAddClose(".close-modal");
        }
      });
    }
  });
  
  $("a#credits").toggle(function(e) {
    e.preventDefault();
    $(this).text("Close Credits");
    var target = $(this).attr("href");
    $(target).fadeIn(500);
  },
  
  function(e) {
    e.preventDefault();
    $(this).text("Site Credits");
    var target = $(this).attr("href");
    $(target).fadeOut(500);
  });
});