// our namespace for the website, use this to avoid polluting the global namespace
nutrition = {};

$(function() {
  if ($.browser.msie && $.browser.version < 9 && $('#jquery-live-search').length) {
    $('#jquery-live-search').bgIframe({
      opacity: 0
    });
  }

  $("a.modifyBasket").click(function () {
    $('.rightArrowButton').replaceWith('<p style="text-align:right;">Updating Basket...</p>');
  });

  //match heights of left and right columns, courtesy of Ben
  autoheights = $('#brands, #categories');
  max_parent_height = 0; // $('div.centre-column').height();
  autoheights.each(function () {
    newheight = $(this).height();
    if (newheight > max_parent_height) {
      max_parent_height = newheight;
    }
  }).each(function() {
    newheight = $(this).height();
    if (newheight < max_parent_height) {
      add = max_parent_height - newheight;
      $(this).css('height', ($(this).height() + add) + 'px');
    }
  });
  
  $('.ecom_product').each(function() {
    var product = $(this);
    var variations = $('.ecom_product_variation[data-product-id = ' + product.data('product-id') + ']', product);
    
    if (variations.length > 1) {
      var select = $('<select/>');
      
      $(variations).filter(':gt(0)').hide();
      
      variations.each(function() {
        var variation = $(this);
        select.append($('<option/>', {text: $('.price_quantity strong', variation).text(), 'value': variation.data('variation-id')}));
        
        if (variation.height() > product.height()) {
          product.height(variation.height());
        }
      });
      
      select.change(function(event) {
        variations.filter('[data-variation-id = ' + $(this).attr('value') + ']', product).show();
        variations.filter(':not([data-variation-id = ' + $(this).attr('value') + '])', product).hide();
        
        $('option[selected]', this).removeAttr('selected');
        $('option[value = "' + $(this).closest('.ecom_product_variation').data('variation-id') + '"]', this).attr('selected', 'selected');
      });
      
      variations.each(function() {
        var variation = $(this);
        var variation_select = select.clone(true);
        
        $('option[value = ' + variation.data('variation-id') + ']', variation_select).attr('selected', 'selected')
        
        $('.price_quantity strong', variation).replaceWith(variation_select);
      });
    }
  });
});

/* jQuery Carousel */
/* NOTE 2008.04.18 : Haven't had it functioning without auto : true as yet */
if ($.browser.safari == false) {
  $(window).load(function () {
    $(".carousel").jMyCarousel({
      visible: '100%',
      eltByElt: false,
      auto : true,
      speed : 1500
    });
  });
}

function openUrl(url) {
  window.open(url, '_self');
}

function openUrlFromSelect(select) {
  var url = select.options[select.options.selectedIndex].value;
  openUrl(url);
}

/* 
 * single product gallery 
 */
$(function() {
  if( $('#product-gallery').length > 0 ){
    nutrition.productGallery.init();
  }
});

nutrition.productGallery = function(){
  
  var el = {};
  
  var init = function(){
    el.mainImgWrapper = $('#main-product-image');
    el.mainImg = el.mainImgWrapper.find('img');
    el.mainImgWrapper.height( el.mainImg.height() );
    el.thumbs = $('#product-gallery li');
    el.mainImgWrapper.children('a').fancybox({'type' : 'image'});

    el.thumbs.each(function(){
      $(this).live('click', function(e){
        $this = $(this);
        if( !$this.hasClass('selected') ){
          imageSelect( $(this) );
        }
        e.preventDefault();
      });
    });
  }//end: init()
  
  var imageSelect = function( $this ){  

    // set the selected thumb
    el.thumbs.removeClass('selected');
    $this.addClass('selected');
    var $link = $this.children('a');

    el.mainImg.animate({
      'opacity' : '0'
    }, 100, function(){
      el.mainImgWrapper.addClass('loading');
      el.mainImg.remove();
      // once the image is faded out swap to the new image      
      var img = new Image();  
      $(img)        
        .load(function () { 
          el.mainImg = $(this);
          el.mainImg.hide();
          el.mainImgWrapper.removeClass('loading');
          el.mainImgWrapper.children('a')
            .append( el.mainImg )
            .attr('href', '/thumb.php?file='+ $link.attr('data-imgid') +'&maxwidth=800&maxheight=600"');
          //el.mainImg.fadeIn('100');
          
          el.mainImgWrapper.animate({
            'height' : el.mainImg.height()
          }, 500, function(){
            el.mainImg.fadeIn('100');
          });
          
        })
        .error(function () {
          // notify the user that the image could not be loaded
        })
        .attr('src', $link.attr('href'));       
      
    });
      
  }//end: imageSelect
  
  return{
    init : init
  }
  
}();//end: nutrition.productGallery()
