var input_has_focus = false;

$(document).ready(function() {
   // toggles the more_info box on clicking the More Info &darr; link 
   $('.toggle').click(function(){
     if ($(this).next('blockquote').length > 0) {
        $(this).next('blockquote').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     } else if ($(this).next('.toggle_content').length > 0) {
        $(this).next('.toggle_content').toggle(400);
        $(this).toggleClass("up").toggleClass("down");
     }
     return false;
   }).toggleClass("down");

   //hide the div's to begin with
   $('.toggle').next().hide().addClass("toggle_content");
   if(!$('.toggle:first').hasClass('init_hide')) $('.toggle:first').toggleClass("up").toggleClass("down").next().show();
   
   ($("label.autopopulate").length > 0) ? autopopulateLabels() : "";
   
   if ($("#accesskey_list").length > 0) {
      $("#accesskeys_anchor").attr("href","javascript:void(0);");
      $("#accesskeys_anchor").click(function(){
         displayaccesskeys();
      });
      $("#accesskey_list").hide();
      /* HIDING KEYBOARD SHORTCUT FOR NOW PER NHANNA on 2009-09-23
      $("input").focus(function(){ input_has_focus = true; }).blur(function(){ input_has_focus = false; });
      $("textarea").focus(function(){ input_has_focus = true; }).blur(function(){ input_has_focus = false; });
      $(document).keydown(function(event){
         switch (event.keyCode) {
            case 0: // "?" (shift + /)
               _debug(event.keyCode);
               displayaccesskeys();
            break;
            default:
               _debug(event.keyCode);
            break;
         }
      });
      */
   }//end if accesskey_list
});
function displayaccesskeys () {
   if (!input_has_focus) {
      tb_show('Accesskeys (Keyboard Shortcuts)','#TB_inline?height=500&width=480&inlineId=accesskey_list&customClass=accesskeys');
   }
}
//---------------------------------------
function _debug ($string) {
   try {
      console.log($string);
   } catch (err) { 
      //$("#footer").append($string + "<div class='hr'><hr /></div>"); 
   }
}
//---------------------------------------
function autopopulateLabels() {
   $.each($("label.autopopulate"), function(){
      //_debug("VAL: " + ($(this).next("input[type='text']").val()));
      if ($(this).next("input[type='text']").val() == "") {
         $(this).next("input[type='text']").attr("value", $(this).html());
      }
      $(this).next("input[type='text']").attr("title", $(this).html());
      $(this).hide();
      //REMOVE populated label on a focus
      $(this).next("input[type='text']").focus(function () {
         // If value and title are equal on focus, clear value
			if (this.value == this.title) {
				this.value = '';
				this.select(); // Make input caret visible in IE
			}
      });
      //ADDED it back if field is empty
      $(this).next("input[type='text']").blur(function () {
         if (!this.value.length) { this.value = this.title; }
      });
   });
}