/**
 * Created by IntelliJ IDEA.
 * User: mikehamilton
 * Date: Dec 30, 2010
 * Time: 1:14:05 PM
 */

/**
 * Internal flag used to track the current state of the newsletter form (open or closed)
 * @type Boolean
 */
var _closed = true;

$(document).ready(function()
{
  //__________________________________________________________________________________________ NEWSLETTER SIGN-UP FORM
  /**
   * Global onclick even handler for the <body>. Ensures that the panel closes if you click anywhere outside it.
   */
  $(this).click(function() {
    if (!_closed) {
      closePanel($('#newsletterSignup'));
    }
  });

  /**
    * Event handler for the click event of the e-mail address textbox
    */
    $('#txtNewsletterEmail').click(function (e) {
      e.stopPropagation();

      if ($(this).val() == 'Email Address') {
        $(this).val('');
      }
    });

    /**
    * Event handler for the blur event of the e-mail address textbox
    */
    $('#txtNewsletterEmail').blur(function () {
      if ($(this).val() == '') {
        $(this).val('Email Address');
      }
    });

    /**
    * Event handler for the click event of the submit button
    */
    $('#btnNewsletterRegister').click(function (e) {
      e.stopPropagation();

      var customerAddress = $('#txtNewsletterEmail').val();
      
      if (customerAddress != '') {
        $.ajax({
          url     : "/NewsletterSignup.aspx?customerAddress=" + customerAddress,
          context : document.body,
          success : function(){
            $('#newsletterSignup p').html("Thanks! You've been registered for our newsletter.<br /><br />");
          }
        });
      } else {
        alert('Please enter your e-mail address to register for our newsletter.');
      }
    });
  
  /**
   * Event handler for the newsletter signup box. Responsible for hiding/showing the panel
   */
  $('#newsletterSignup').click(function(e) {
    e.stopPropagation();

    if (_closed) {
      openPanel($(this));
    } else {
      closePanel($(this));
    }
  });


  //_______________________________________________________________________________________________________ SEARCH BOX
  /**
   * Event handler for the focus (click) event of the search term textbox
   */
  $('#search').focus(function() {
    if ($(this).val() == 'Search') {
      $(this).val('');
    }
  });

  /**
   * Event handler for the blur event of the search term textbox
   */
  $('#search').blur(function() {
    if ($(this).val() == '') {
      $(this).val('Search');
    }
  });
  
  /**
   * Event handler for the click event of the search button
   */
  $('#searchButton').click(function(e) {
    e.stopPropagation();
    window.location.href = '/Search.aspx?search=' + $('#search').val();
  });
});


//___________________________________________________________________________________________________________ FUNCTIONS!
/**
 * Causes the Newsletter panel to slide down (open).
 * @param panelDiv The <div> wrapper of the sliding panel
 */
function openPanel(panelDiv) {
  _closed = false;

  $(panelDiv).animate({top:'0'},{queue:false, duration:500});
  $(panelDiv).css({'background-image':'url(images/newsletterSignupBG_open.gif)'});
}

/**
 * Causes the Newsletter panel to slide up (close).
 * @param panelDiv The <div> wrapper of the sliding panel
 */
function closePanel(panelDiv) {
  _closed = true;

  $(panelDiv).animate({top:'-68px'},{queue:false, duration:500});
  $(panelDiv).css({'background-image':'url(images/newsletterSignupBG_closed.gif)'});
}
