var input_has_focus = false;

$(document).ready(function()
{
   $("#layout hr").not("div.hr hr").wrap("<div class='hr'></div>");
   $("#sideBar hr").not("div.hr hr").wrap("<div class='hr'></div>");
   $("body").removeClass("js_disabled").addClass("js_enabled");

   //pull the hash from the URL (if it exists)
   var hash = window.location.hash;
   hash = hash.replace("#","");
   //_debug(hash);

   // 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') && !$("body").hasClass("blog") && hash == "") $('.toggle:first').toggleClass("up").toggleClass("down").next().show();

   if (hash != "") window.location.hash = hash;

   ($("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();
   }//end if accesskey_list
   
   if($("#outer_wrapper").hasClass("layout_youtube")) 
   {
      //$("#media_library").append("<div id='media_tooltip'>&nbsp;</div>");
      // initialize tooltip 
      $("#media_library li[title]").tooltip({ 
         // use single tooltip element for all tips 
         //tip: '#media_tooltip',  
         // tweak the position 
         offset: [42,0],
         position: "top center",
         relative: true
         //lazy: false
         // add dynamic plugin  
      });//.dynamic({ bottom: { direction: 'down' } });;
   }//end layout_youtube
   
   
   //hide empty img_descr tags
  //if($("#default .img_descr_wrap:empty")) $("#img_descr").hide();
   
   //add basic JS verification on Stay Informed forms
   if($("#newsletter form").length > 0) {
      $("#newsletter form button").click(function(){
         $(".error").hide();
         var hasError = false;
         var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

         var email_val = $("#email").val();
         var auth_val = $("#authcode").val();

         if(email_val == '' || email_val == $("#email").siblings("label").text()) {
            $("#email").after('<span class="error">Please enter your email address.</span>');
            hasError = true;
         }
         
         if(auth_val == '' || auth_val == $("#authcode").siblings("label").text()) {
            $("#authcode").after('<span class="error">Please verify the code.</span>');
            hasError = true;
         }

         else if(!emailReg.test(email_val)) {
            $("#email").after('<span class="error">Enter a valid email address.</span>');
            hasError = true;
         }

         if(hasError == true) { return false; }
      });
   }
});
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).attr("oldtitle"))) {
				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 || $(this).attr("oldtitle")); }
      });
   });
}
