/**
 * @license In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009-2010 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1.2
 */
(function ($) {

  $.InFieldLabels = function (label, field, options) {
    // To avoid scope issues, use 'base' instead of 'this'
    // to reference this class from internal events and functions.
    var base = this;
  
    // Access to jQuery and DOM versions of each element
    base.$label = $(label);
    base.label  = label;

    base.$field = $(field);
    base.field  = field;

    base.$label.data("InFieldLabels", base);
    base.showing = true;

    base.init = function () {
      // Merge supplied options with default options
      base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);

      // Check if the field is already filled in
      if (base.$field.val() !== "") {
        base.$label.hide();
        base.showing = false;
      }

      base.$field.focus(function () {
        base.fadeOnFocus();
      }).blur(function () {
        base.checkForEmpty(true);
      }).bind('keydown.infieldlabel', function (e) {
        // Use of a namespace (.infieldlabel) allows us to
        // unbind just this method later
        base.hideOnChange(e);
      }).bind('paste', function (e) {
        // Since you can not paste an empty string we can assume
        // that the fieldis not empty and the label can be cleared.
        base.setOpacity(0.0);
      }).change(function (e) {
        base.checkForEmpty();
      }).bind('onPropertyChange', function () {
        base.checkForEmpty();
      });
    };

    // If the label is currently showing
    // then fade it down to the amount
    // specified in the settings
    base.fadeOnFocus = function () {
      if (base.showing) {
        base.setOpacity(base.options.fadeOpacity);
      }
    };

    base.setOpacity = function (opacity) {
      base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
      base.showing = (opacity > 0.0);
    };

    // Checks for empty as a fail safe
    // set blur to true when passing from
    // the blur event
    base.checkForEmpty = function (blur) {
      if (base.$field.val() === "") {
        base.prepForShow();
        base.setOpacity(blur ? 1.0 : base.options.fadeOpacity);
      } else {
        base.setOpacity(0.0);
      }
    };

    base.prepForShow = function (e) {
      if (!base.showing) {
        // Prepare for a animate in...
        base.$label.css({opacity: 0.0}).show();

        // Reattach the keydown event
        base.$field.bind('keydown.infieldlabel', function (e) {
          base.hideOnChange(e);
        });
      }
    };

    base.hideOnChange = function (e) {
      if (
          (e.keyCode === 16) || // Skip Shift
          (e.keyCode === 9) // Skip Tab
        ) {
        return; 
      }

      if (base.showing) {
        base.$label.hide();
        base.showing = false;
      }

      // Remove keydown event to save on CPU processing
      base.$field.unbind('keydown.infieldlabel');
    };

    // Run the initialization method
    base.init();
  };

  $.InFieldLabels.defaultOptions = {
    fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
    fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
  };


  $.fn.inFieldLabels = function (options) {
    return this.each(function () {
      // Find input or textarea based on for= attribute
      // The for attribute on the label must contain the ID
      // of the input or textarea element
      var for_attr = $(this).attr('for'), $field;
      if (!for_attr) {
        return; // Nothing to attach, since the for field wasn't used
      }

      // Find the referenced input or textarea element
      $field = $(
        "input#" + for_attr + "[type='text']," + 
        "input#" + for_attr + "[type='search']," + 
        "input#" + for_attr + "[type='tel']," + 
        "input#" + for_attr + "[type='url']," + 
        "input#" + for_attr + "[type='email']," + 
        "input#" + for_attr + "[type='password']," + 
        "textarea#" + for_attr
      );

      if ($field.length === 0) {
        return; // Again, nothing to attach
      } 

      // Only create object for input[text], input[password], or textarea
      (new $.InFieldLabels(this, $field[0], options));
    });
  };

}(jQuery));


var lmm = (function($) {
  // Define html5 elements in DOM for styling support in IE
  var e = "abbr,article,aside,audio,canvas,datalist,details,"
       + "figure,footer,header,hgroup,mark,menu,meter,nav,output,"
       + "progress,section,time,video";
  $.each(e.split(","), function() { document.createElement(this) });
  
  
  var displayLogin = function() {
    // If called twice, but the form is still displayed ignore.
    if($("#login").size()) return;
    // Create the login form
    var loginBox = $( '<div id="login">' +
                       '<form action="index.cfm?event=do.login" method="post">' +
                        '<div><label for="login_email_field">Email Address</label><input id="login_email_field" name="email" type="email"></div>' +
                        '<div><label for="login_password_field">Password</label><input id="login_password_field" name="password" type="password"></div>' +
                        '<input type="submit" value="Submit">' +
                        '<p><a href="index.cfm?event=recover">Forgot your password?</a></p>' +
                       '</form>' +
                      '</div>');
    $("#user-menu").parent().append(loginBox);
    loginBox.find("label").inFieldLabels();
  }

  var displayCouponForm = function() {
    // If called twice, but the form is still displayed ignore.
    if($("#couponForm").size()) return;
    // Create the login form
    var couponBox = $( '<div id="coupon-form">' +
                       '<form action="index.cfm?event=checkout.doApplyCoupon" method="post">' +
                       '<div><label for="coupon">Coupon</label><input id="coupon" name="coupon" type="text"></div>' +
                       '<input type="submit" value="Apply Coupon">' +
                       '<p><a href="index.cfm?event=purchaseMarketReports">Cancel</a></p>' +
                       '</form>' +
                       '</div>');
    $("#cart-container").parent().append(couponBox);
    couponBox.find("label").inFieldLabels();
  }
  
  // If javascript is available, we should hide tabs until they are selected
  $(document).append($('<style>.tab { display:none; }</style>'));
  
  $(document).ready(function() {
    $("a.login").click(function(event) {
      event.preventDefault();
      $(this).addClass("current");
      displayLogin();
    });
    
    $("a.coupon").click(function(event) {
      event.preventDefault();
      $(this).addClass("current");
      displayCouponForm();
    });
	
    $("label").inFieldLabels();
    
    /*
    Market reports multiple sections show/hide
    */
    var allTabs = $("div[data-tab-id]").hide();
    var allTabLinks = $(".tab-links a");
    allTabLinks.each(function() {
      var link = $(this);
      var tabs = $('div[data-tab-id="'+ link.attr("href").replace("#", "") +'"]');
      if(tabs.size())
        link.click(function(event) {
          event.preventDefault();
          allTabs.hide();
          allTabLinks.parent().removeClass("disabled");
          tabs.show();
          link.parent().addClass("disabled");
        });
    })
    .first().click();
  });
  
  return {
    /*
    Displays the login form.
    */
    login: displayLogin
  }
})(jQuery);



