﻿$(function() {
  var fontsize = 12;

  //-- ajax global settings --//
  $.ajaxSetup({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    dataFilter: function(data) {
      // This boils the response string down into a proper JavaScript Object().
      var msg = eval('(' + data + ')');

      // If the response has a ".d" top-level property, return what's below that instead.
      if (msg.hasOwnProperty('d'))
        return msg.d;
      else
        return msg;
    }
  });

  //-- form validation --//
  $('form').validate({ onsubmit: false });
  $('fieldset.form-validation .causesValidation').click(validateAndSubmit);
  $('fieldset.form-validation :text, fieldset.form-validation :password').keydown(function(evt) {
    if (evt.keyCode == 13) {
      $(':text.novalidation').val('');
      evt.preventDefault();
      $(this).closest('fieldset.form-validation').find('.causesValidation').click();
    }
  });

  function validateAndSubmit(evt) {
    var $group = $(evt.currentTarget).closest('fieldset.form-validation');
    var isValid = true;
    var offenderTop = -1;

    $group.find(':input').each(function(i, item) {
      if (!$(item).valid()){
        isValid = false;
        
        if (offenderTop < 0)
          offenderTop = $(item).offset().top - 20;
      }
    });
    
    if (!isValid) {
      var scrollTop = $(window).scrollTop();
      
      if (offenderTop >= 0 && offenderTop < scrollTop)
        $('html, body').animate({ scrollTop:offenderTop }, 500);
      
      evt.preventDefault();
      return false;
      
    } else if (isValid && $(evt.currentTarget).is('a.causesValidation[href]')){
      window.location = $(evt.currentTarget).attr('href');
    }
    
    return true;
  }
  
  //-- used to trigger click events for anchor tags that have an href (mainly for submit buttons) --//
//  $('a.causesValidation[href]').click(function(){
//    
//  });

  //-- nav --//
  $('#nav').superfish({
    autoArrows: false,
    disableHI: true,
    dropShadows: false,
    delay: 200,
    speed: 0,
    onBeforeShow: function() {
      var li = $(this).parents('li');
      li.addClass('on');
      li.prev('li').addClass('nosep');

      var indexLi = li.parent().find('li').index(li);

      if (indexLi == 0 && $(window).width() <= 1024) {
        li.find('ul.subnav').css({
          marginLeft: -100,
          width: 780
        });
      }
    },
    onHide: function() {
      var li = $(this).parents('li');
      li.removeClass('on');
      li.prev('li').removeClass('nosep');
    }
  });

  //-- fontsize --//
  $('#fontsize-up').click(function() {
    if (fontsize < 16) {
      $('#pagecontent > *:not(.nosize)').each(function() {
        var fs = parseInt($(this).css('font-size'), 10) + 1;
        $(this).css('font-size', fs);
      });
      fontsize++;
    }
  }).disableSelection();
  $('#fontsize-down').click(function() {
    if (fontsize > 12) {
      $('#pagecontent > *:not(.nosize)').each(function() {
        var fs = parseInt($(this).css('font-size'), 10) - 1;
        $(this).css('font-size', fs);
      });
      fontsize--;
    }
  }).disableSelection();

  //-- offers --//
  $(modalityObjects.offersbtn).click(function(e){
    var isValid = validateAndSubmit(e);
    
    if (!isValid)
      return false;
    
    $.ajax({
      url: modalityObjects.rootpath + 'webservices/WebServices.asmx/SubmitSpecialOffersEmail',
      data:'{"email":"' + $(modalityObjects.offerstb).val() + '" }',
      beforeSend:function(){
        $(modalityObjects.offerstb).attr('disabled', 'disabled');
        if ($('#offer-progress').length == 0)
          $(modalityObjects.offersbtn).before('<span id="offer-progress" class="offer-progress"></span>');
          
        $('#offer-progress').fadeIn();
      },
      complete:function(){
        $('#offer-progress').fadeOut();
      },
      success:function(data){
        if (data){
          $(modalityObjects.offersbtn).unbind('click').css('cursor', 'default');
        } else {
          $(modalityObjects.offersty).html('<p>Sorry, there was an error adding your email address, please try again.</p>');
        }
        
        $(modalityObjects.offersty).dialog('open');
      }
    });
    
    e.preventDefault();
    return false;
  });
  $(modalityObjects.offerstb).watermark('enter email address for special offers & more', { className: 'watermark' });

  $(modalityObjects.offersty).dialog({
    autoOpen: false,
    modal: true,
    width: 300
  });
  $('div.ui-widget-overlay').live('click', function() {
    $(modalityObjects.offersty).dialog('close');
  });

  //-- search --//
  $(modalityObjects.searchtb).watermark('enter search term', { className: 'watermark' });

  //-- print --//
  $('#print').click(function() {
    window.print();
  });

  //-- IE6 PNG fix --//
  if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
    $('h3.logo').ifixpng();
    $('a.fontsize-control').ifixpng();
  }
});
