// This is the default Blueprint javascript lib for this site.

function px(x) { return x + 'px' }

var Dropdown = function(_el, _settings) {
  if (Dropdown == this) return new Dropdown(_el, _settings);
  
  var el;
  var sheath;
  var options_height;
  var settings;
  var expanded = false;
  
  function init(_el) {
    el = _el;
    sheath = el.find('.sheath');
    options_height = sheath.find('.options').outerHeight();

    el.find('.display').mouseenter(expand);
    sheath.click(contract);
    // sheath.mouseleave(contract);
    el.mouseleave(contract);
    
    el.find('.option-header').click(function() { return false; });
    
    settings = _settings || {};
  }
  
  function expand() {
    if (expanded) return;
    
    el.addClass('expanded');
    sheath.stop(true, false).animate({height: px(options_height)}, 300);
    expanded = true;

    var f;
    if (f = settings.on_expand) settings.on_expand();
  }

  function contract() {
    if (!expanded) return;
    
    el.removeClass('expanded');
    sheath.stop(true, false).animate({height: 0}, 300);
    expanded = false;
    
    var f;
    if (f = settings.on_contract) settings.on_contract();
  }
  
  init(_el, _settings);
}

$(document).ready(function() {
  // jQuery.fx.off = true;
  $('.defaulter').each(function() {
    
    var el = $(this);
    
    el.focus(function() {
      var el = $(this);
      var _default = el.data('default');
      var val = $.trim(el.val());
      
      el.removeClass('default');

      if (val == _default) {
        el.val('');
      }
      
    });
    
    el.blur(function() {
      var el = $(this);
      var _default = el.data('default');
      var val = $.trim(el.val());

      if (val.length == 0) {
        el.val(_default);
        val = _default;
      }

      if (val == _default) {
        el.addClass('default');
      }
      
    });
    
    el.blur();
    
  });
  
  $('.validate').submit(function() {
    var form = $(this);
    var compliant = true;
    
    form.find('.required').each(function() {
      var el = $(this);
      var val = $.trim(el.val());
      var _default = el.data('default');
      
      if (_default && val == _default) {
        val = '';
      }
      
      if (val.length == 0) {
        el.addClass('error');
        compliant = false;
      }
      else {
        el.removeClass('error');
      }
            
    });
    
    if (compliant) {
      form.find('.defaulter').each(function() {
        var el = $(this);
        var val = $.trim(el.val());
        var _default = el.data('default');
        
        if (val == _default) {
          el.val('');
        }
      });
      
      return true;
    }
    
    return false;
    
  });
  
  $('#newsletter-signup-form .submit').click(function() {
    $('#newsletter-signup-form').submit();
  });
  
  // $()
  
  $('.product-static').hover(
    function() {
      var el = $(this);
      el.find('.caption').stop(true, false).animate({top: px(71)}, 250);
    },
    function() {
      var el = $(this);
      el.find('.caption').stop(true, false).animate({top: px(160)}, 250);

      if (el.hasClass('flipped')) {
        el.find('.two').animate({opacity: 0}, 250);
        el.find('.one').delay(170).animate({opacity: 1}, 250);
        el.removeClass('flipped');
      }
      else {
        el.find('.one').animate({opacity: 0}, 250);
        el.find('.two').delay(170).animate({opacity: 1}, 250);
        el.addClass('flipped');
      }
      
      // var two = el.find('.two');
      // if (two.length) {
      //   _opacity = (el.data('flipped') == 0) ? 1 : 0;
      //   el.find('.one').stop(true, false).animate({opacity: _opacity}, 250);
      // }
      // el.data('flipped', _opacity);
    }
  );
  
  // $('.product-static')
  
});

function scroll_to(hash) {
  target = $('#' + hash);
  if (target.offset()) {
    $('html, body').animate({scrollTop: target.offset().top + 'px'}, 500);
  }
}

