/*
  Formalize - version 1.1

  Note: This file depends on the jQuery library.
*/

// Module pattern:
// http://yuiblog.com/blog/2007/06/12/module-pattern
var FORMALIZE = (function($, window, document, undefined) {
  // Private constants.
  var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');
  var AUTOFOCUS_SUPPORTED = 'autofocus' in document.createElement('input');
  var IE6 = !!($.browser.msie && parseInt($.browser.version, 10) === 6);
  var IE7 = !!($.browser.msie && parseInt($.browser.version, 10) === 7);

  // Expose innards of FORMALIZE.
  return {
    // FORMALIZE.go
    go: function() {
      for (var i in FORMALIZE.init) {
        FORMALIZE.init[i]();
      }
    },
    // FORMALIZE.init
    init: {
      // FORMALIZE.init.full_input_size
      full_input_size: function() {
        if (!IE7 || !$('textarea, input.input_full').length) {
          return;
        }

        // This fixes width: 100% on <textarea> and class="input_full".
        // It ensures that form elements don't go wider than container.
        $('textarea, input.input_full').wrap('<span class="input_full_wrap"></span>');
      },
      // FORMALIZE.init.ie6_skin_inputs
      ie6_skin_inputs: function() {
        // Test for Internet Explorer 6.
        if (!IE6 || !$('input, select, textarea').length) {
          // Exit if the browser is not IE6,
          // or if no form elements exist.
          return;
        }

        // For <input type="submit" />, etc.
        var button_regex = /button|submit|reset/;

        // For <input type="text" />, etc.
        var type_regex = /date|datetime|datetime-local|email|month|number|password|range|search|tel|text|time|url|week/;

        $('input').each(function() {
          var el = $(this);

          // Is it a button?
          if (this.getAttribute('type').match(button_regex)) {
            el.addClass('ie6_button');

            /* Is it disabled? */
            if (this.disabled) {
              el.addClass('ie6_button_disabled');
            }
          }
          // Or is it a textual input?
          else if (this.getAttribute('type').match(type_regex)) {
            el.addClass('ie6_input');

            /* Is it disabled? */
            if (this.disabled) {
              el.addClass('ie6_input_disabled');
            }
          }
        });

        $('textarea, select').each(function() {
          /* Is it disabled? */
          if (this.disabled) {
            $(this).addClass('ie6_input_disabled');
          }
        });
      },
      // FORMALIZE.init.autofocus
      autofocus: function() {
        if (AUTOFOCUS_SUPPORTED || !$(':input[autofocus]').length) {
          return;
        }

        $(':input[autofocus]:visible:first').focus();
      },
      // FORMALIZE.init.placeholder
      placeholder: function() {
        if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
          // Exit if placeholder is supported natively,
          // or if page does not have any placeholder.
          return;
        }

        FORMALIZE.misc.add_placeholder();

        $(':input[placeholder]').each(function() {
          var el = $(this);
          var text = el.attr('placeholder');

          el.focus(function() {
            if (el.val() === text) {
              el.val('').removeClass('placeholder_text');
            }
          }).blur(function() {
            FORMALIZE.misc.add_placeholder();
          });

          // Prevent <form> from accidentally
          // submitting the placeholder text.
          el.closest('form').submit(function() {
            if (el.val() === text) {
              el.val('').removeClass('placeholder_text');
            }
          }).bind('reset', function() {
            setTimeout(FORMALIZE.misc.add_placeholder, 50);
          });
        });
      }
    },
    // FORMALIZE.misc
    misc: {
      // FORMALIZE.misc.add_placeholder
      add_placeholder: function() {
        if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
          // Exit if placeholder is supported natively,
          // or if page does not have any placeholder.
          return;
        }

        $(':input[placeholder]').each(function() {
          var el = $(this);
          var text = el.attr('placeholder');

          if (!el.val() || el.val() === text) {
            el.val(text).addClass('placeholder_text');
          }
        });
      }
    }
  };
// Alias jQuery, window, document.
})(jQuery, this, this.document);

// Automatically calls all functions in FORMALIZE.init
jQuery(document).ready(function() {
  FORMALIZE.go();
});;
// Module pattern.
var SS = (function($, window, document, undefined) {
  return {
    go: function() {
      for (var i in SS.init) {
        SS.init[i]();
      }
    },
    init: {
      input_error_focus: function() {
        if (!$(':input.error').length) {
          return;
        }

        $(':input.error').focus(function() {
          $(this).removeClass('error');
        });
      },
      create_button: function() {
        $("a.button-link").each(function(){  
          $(this).wrap("<span class='button-wrap' />");
        });
      },
      fix_buttons: function() {
        $("#sidebar-second .bean-link-button a, #highlighted .bean-link-button a, a.button-link").each(function() {
          if ($(this).text().length < 21) {
            $(this).css("padding", "21px 10px");
          }
        });
      },
      update_sm_links: function() {
        $("p.follow a, .custom-pager .next a, .subtitle a").append(" &raquo;");
        $(".custom-pager .prev a").prepend("&raquo; ");
      },
      fix_subtitle: function() {
        $("article p.subtitle:last-child, article p.intro:last-child").css("margin-bottom", "0");
      },
      fix_video: function() {
        $(".featured_vid").prev().css("margin-top", "25px");
      },
      fix_forms: function () {
        $('select[name*="[time]"]').css("width", "50");
      }
    },
    fix_height: function() {
      $('.page-frontpage .bg').height(function(){
        return $('.welcome').outerHeight();
      }).fadeIn();
    },
    add_fonts: function() {
      // $('h1, #sidebar-first h2, p.quote, p.intro').css('visibility', 'visible');
      if (jQuery.support.opacity) {
         $(".fancy, #home-welcome p, p.subtitle, p.intro, .content h2").css('visibility', 'visible').hide().fadeIn(200);
       } else {
         $(".fancy, #home-welcome p, p.subtitle, p.intro, .content h2").css('visibility', 'visible');
       }
    }
  };
})(jQuery, this, this.document);

// Fire it off!
jQuery(document).ready(function() {
  SS.go();

  WebFont.load({
    typekit: {
      // id: 'ikk0qos'
      id: 'alw0bby'
    },
    loading: function() {
      // alert('loading');
    },
    active: function() {
      SS.fix_height();
      SS.add_fonts();
    }
  });
});

(function($) {
  // this is needed so that fonts are reloaded after ajax render
  Drupal.behaviors.hgc = {
   // attach: function(context, settings) {
   //   SS.add_fonts();
   //   SS.fix_height();
   // }
  }
})(jQuery);;

