$(function() {
  // Buttons Hover Images
  $('input.hover[type="image"]').hover(function() {
    var id = $(this).attr('src').replace('.', '_hover.');
    $(this).attr('src', id);
  }, function() {
    var id = $(this).attr('src').replace('_hover.', '.');
    $(this).attr('src', id);
  });

  // Overlay Labels
  $('label input').focus(function() {
    $(this).siblings('span').hide();
  });
  
  // ColorBox
  var localurl = document.location.protocol + '//' + document.location.hostname + url;
  if (jQuery().colorbox) {
    $('.colorbox').colorbox({transition: 'fade'});
    $('.colorbox-enabled a').each(function() {
      // skip local, mailto, PDF links along with links with certain class
      if ($(this).hasClass('normal') || $(this).attr('href') === undefined || 
        ($(this).attr('href') !== undefined && ($(this).attr('href').indexOf('mailto:') == 0|| $(this).attr('href').indexOf(url) == 0 || $(this).attr('href').indexOf(localurl) == 0))) {
        return;
      }
      else if ($(this).attr('href').indexOf('.pdf') != -1 || $(this).attr('href').indexOf('.PDF') != -1) {
        $(this).attr('target', '_blank');
        return;
      }

      if ($(this).attr('href').indexOf('.jpg') != -1 
        || $(this).attr('href').indexOf('.jpeg') != -1
        || $(this).attr('href').indexOf('.gif') != -1
        || $(this).attr('href').indexOf('.png') != -1
        || $(this).attr('href').indexOf('.JPG') != -1
        || $(this).attr('href').indexOf('.JPEG') != -1
        || $(this).attr('href').indexOf('.GIF') != -1
        || $(this).attr('href').indexOf('.PNG') != -1) {
        $(this).colorbox({transition: 'fade'});
      }
      else if ($(this).hasClass('video')) {
        $(this).colorbox({iframe: true, innerWidth: 425, innerHeight: 344});
      }
      else {
        $(this).colorbox({iframe: true, width: '80%', height: '80%'});
      }
    });
  }

  // External links
  $(function() {
    $('a[rel=external]').each(function(){
      var href = $(this).attr('href').replace(/http:\/\//i, '').split('/');
      $(this).attr('target', '_blank');
    });
  });

  // Center photogallery on content page
  var $gallery = $('#column-middle').find('.gallery'),
      gallery_width = $gallery.outerWidth(false),
      $gallery_item = $gallery.find('li:first-child'),
      gallery_item_width = $gallery_item.outerWidth(false),
      gallery_item_width_margin = $gallery_item.outerWidth(true),
      gallery_item_margin = gallery_item_width_margin - gallery_item_width,
      count_items_on_line = Math.floor((gallery_width + gallery_item_margin)/gallery_item_width_margin),
      gallery_width_adjusted = count_items_on_line * gallery_item_width_margin,
      gallery_margin = Math.floor(((gallery_width - gallery_width_adjusted) - gallery_item_margin)/2),
      gallery_margin_left = gallery_margin + gallery_item_margin,
      gallery_margin_right = gallery_margin;

  $gallery.width(gallery_width_adjusted).css('margin-left', gallery_margin_left).css('margin-right', gallery_margin_right);

  // Auto-submit
  $('.filter .vendors input[type="checkbox"]').bind('change', function(e) {
    $(this).parents('form')[0].submit();
  }).siblings('input[type="submit"]').hide();
  
  $('.filter #sort').bind('change', function(e) {
    $(this).parents('form')[0].submit();
  }).siblings('input[type="submit"]').hide();

  $('.filter #view').bind('change', function(e) {
    $(this).parents('form')[0].submit();
  }).siblings('input[type="submit"]').hide();
  
  $('.filter .view').bind('change', function(e) {
    $(this).parents('form')[0].submit();
  }).siblings('input[type="submit"]').hide();
  
  $('.variations_select select').bind('change', function(e) {
    $(this).parents('form')[0].submit();
  }).siblings('input[type="submit"]').hide();

  // Show Submenu On Hover
  $('.d-fly li ul').hide();
  $('.d-fly li').hover(function() {
    $('> ul', $(this)).stop(true, true).show();
  }, function() {
    $('> ul', $(this)).stop(true, true).hide();
  });
  
  // Update total order price with current payment method
  $('#payment-method').change(function() {
    var $price = $('#price-total');
    
    var regex = /\s+/g;
    var old_price = $price.attr('title').replace(regex, '');
    var payment = $(this).val().split(':');

    var new_price = payment.length > 1 ? 
      (parseInt(old_price) + parseInt(payment[1])).toString() : old_price;

    regex = /(\d+)(\d{3})/;
  	while (regex.test(new_price)) {
      new_price = new_price.replace(regex, '$1 $2');
  	}

    $price.text(new_price);
  });
  
  // Update product price and link destination on preview
  $('.variations_select_preview').change(function() {
    var price = $(':selected', this).attr('title');
    $(this).parents('form').prev('.price').html(price);

    var href,
        variation_id = $(':selected', this).val().split('.')[1],
        $a = $(this).parents('.product').find('a');

    $a.each(function() {
      href = $(this).attr('href').split('?')[0] + '?v=' + variation_id;
      $(this).attr('href', href);
    });
  });
});

