/******************************************************************************
 *
 * Simple slider gallery on products images.
 *
 ******************************************************************************/
Drupal.behaviors.tremixProductImages = {
  attach: function (context) {
    jQuery('.field-product-image .field-items img', context).click(function(e) {
      var $this = jQuery(this);
      if ($this.hasClass('active')) return;

      $this.siblings().removeClass('active');
      $this.addClass('active');
      var img = $this.attr('src')
                .replace('cropped-50x50', 'cropped-183x187');
      img = jQuery('<img />').attr('src', img);

      $this.parent().prevAll('.image-current').html(img);
    }).filter(':first-child').addClass('active');
  }
};



/******************************************************************************
 *
 * On products page there is list on products at the top of page.
 * When clicked on link scroll down page to selected product.
 *
 ******************************************************************************/
Drupal.behaviors.tremixNodeScroll = {
  attach: function (context) {
    var $block = jQuery('#block-custom-prodcat-nodes', context);
    if ($block.length < 1) return;

    jQuery('a', $block).click(function(e) {
      e.preventDefault();

      var $node = jQuery(jQuery(this).attr('href'));

      jQuery('html, body').animate({'scroll-top': $node.offset().top - 20}, 500);
    });
  }
}



/******************************************************************************
 *
 * Fix menu for IE7.
 *
 ******************************************************************************/
Drupal.behaviors.tremixIE7Fix = {
  attach: function (context) {
    if (!jQuery.browser.msie || parseInt(jQuery.browser.version, 10) > 7) return;

    jQuery('#block-system-main-menu ul.menu a').each(function() {
      jQuery(this).prepend(jQuery('<span class="before" />'));
    });
  }
}



/******************************************************************************
 *
 * Set default value of search form textfield.
 *
 ******************************************************************************/
Drupal.behaviors.tremixSearchForm = {attach: function(context, settings) {
  var $form = jQuery('#search-block-form', context);

  if ($form.length < 1) return;

  var $input = jQuery('input[name=search_block_form]', $form);
  var title  = Drupal.t('Product Search');

  /**
   * Set default state of input field
   */
  if ($input.val() == '') {
    $input.val(title);
  }

  $input.blur(function() {
    if ($input.val() == '') {
      $input.val(title);
    }
  }).focus(function() {
    if ($input.val() == title) {
      $input.val('').removeClass('processed');
    }
  });

  $form.submit(function() {
    if ($input.val() == title) {
      $input.val('');
    }
  });
}}




/******************************************************************************
 *
 * Show dealers on the map when hover dealer address.
 *
 ******************************************************************************/
Drupal.behaviors.tremixDealersMap = {attach: function(context, settings) {
  var $map = jQuery('.dealers-map', context);

  if ($map.length < 1) return;

  jQuery('.dealer-address', context).hover(
    function() {
      var id = jQuery(this).attr('rel');
      jQuery(id).fadeIn('fast');
    },
    function() {
      var id = jQuery(this).attr('rel');
      jQuery(id).stop(true, true).fadeOut('fast');
    }
  );

  var maxTop = jQuery('#content').height() - $map.height();
  jQuery(window).scroll(function() {
    if (jQuery('#content').offset().top > jQuery(window).scrollTop()) {
      $map.removeClass('map-fixed');
      jQuery('#content').css('padding-bottom', 0);
    }
    else {
      $map.addClass('map-fixed');
      jQuery('#content').css('padding-bottom', 50);
    }
  });
}}




/******************************************************************************
 *
 * Show dealers on the map when hover dealer address.
 *
 ******************************************************************************/
Drupal.behaviors.tremixDDocumentsLinks = {attach: function(context, settings) {
  var $table = jQuery('table.documents-table', context);

  if ($table.length < 1) return;

  var shown = null;
  jQuery('.item-list > h3', $table).click(function() {
    // Initialize default value.
    if (typeof this.popupShown == 'undefined') {
      this.popupShown = false;
    }

    if (this.popupShown) {
      shown = null;
      this.popupShown = false;
      jQuery(this).next('ul').removeClass('show');
      jQuery(this).parent().removeClass('item-list-above');
    }
    else {
      if (shown !== null) {
        shown.removeClass('show');
        shown.parent().removeClass('item-list-above');
        shown.prev('h3').get(0).popupShown = false;
      }

      shown = jQuery(this).next('ul');
      this.popupShown = true;
      shown.addClass('show');
      shown.parent().addClass('item-list-above');
    }

    return false;
  });

  jQuery('.popup-close', $table).click(function(e) {
    e.preventDefault();

    shown.removeClass('show');
    shown.parent().removeClass('item-list-above');
    shown.prev('h3').get(0).popupShown = false;
  });

  jQuery(document).click(function(e) {
    
    if (shown !== null && jQuery(e.target).parents('.item-list').length == 0) {
      shown.removeClass('show');
      shown.parent().removeClass('item-list-above');
      shown.prev('h3').get(0).popupShown = false;
    }
  });
}}
;

