var activeTab = 0;

jQuery(window).load(function() {

  // define the popup default path
  TopUp.images_path = '/js/topup/images/';
  
  // -------------------
  // PRODUCT GALLERY
  //
  if(jQuery('li.product-preview').length > 0) {
    jQuery('li.product-preview')
      .click(handleClickProductPreview)
      .mouseover(function() {
        jQuery(this).css('cursor', 'pointer');
      });
    
    
    jQuery('#prevBtn').click(function() {
      activeTab--;
      changePreviewState();
    }).hide();
    
    jQuery('#nextBtn').click(function() {
      activeTab++;
      changePreviewState();
    });
    
    jQuery("div.gallery-holder").easySlider({
      prevId: 'prevBtn',
      nextId: 'nextBtn',
      controlsShow: false,
      continuous: false,
      speed: 400
    });
    
    jQuery('form.tr_shop_product_form').each(function() {
      var qty = parseInt(jQuery('input[name="tr_shop_product[quantity]"]', this).val()) || 0;
      
      jQuery('span.number', this).html(qty);
      jQuery('td.quantity', this).hide();
    });
    
    // handle plus value
    jQuery('a.value-plus').click(function(event){
      event.preventDefault();
      
      var form = jQuery(this).parents('form');
      var qty = 1 + ( parseInt(jQuery('input[name="tr_shop_product[quantity]"]', form).val()) || 0);
  
      jQuery('input[name="tr_shop_product[quantity]"]', form).val(qty);
      jQuery('span.number', form).html(qty);
    });
    
    // handle minus value
    jQuery('a.value-minus').click(function(event){
      event.preventDefault();
      
      var form = jQuery(this).parents('form');
      var qty = -1 + ( parseInt(jQuery('input[name="tr_shop_product[quantity]"]', form).val()) || 0);
  
      if(qty < 1)
      {
        qty = 1;
      }
      jQuery('input[name="tr_shop_product[quantity]"]', form).val(qty);
      jQuery('span.number', form).html(qty);
    });
    
    // handle the radio button
    jQuery('input[name="tr_shop_product[tr_shop_product_id]"]').change(function(event) {
      var form = jQuery(this).parents('form');
      jQuery('td.quantity', form).hide();
      jQuery('td.quantity', jQuery(this).parents('tr')).show();
    });
    
    jQuery('input:checked').trigger('change');
    
    jQuery('div.tr_shop_product_quantity').hide();
    
    // handle submit button
    jQuery('a.btn-add-basket').click(function(event) {
      event.preventDefault();
      
      jQuery(this).parents('form').submit();
    });
  }
  
  // -------------------
  // REGISTRATION FORM
  //
  jQuery('#tr_shop_registration_different_billing_address').change(function(event) {
    if(jQuery(this).attr("checked")) {
      jQuery('#billing_address').show();
    }
    else
    {
      jQuery('#billing_address').hide();
    }
  }).trigger('change');
  
});

function changePreviewState() {
  jQuery('li.product-preview').each(function(index) {
    jQuery(this).removeClass('open');
    
    if(index == activeTab)
    {
      jQuery(this).addClass('open');
    }
  });
}

function handleClickProductPreview(event)
{
  event.preventDefault();
  
  var pos = jQuery(this).prevAll().length;
  
  var move = pos - activeTab;
  
  if(move > 0)
  {
    while(move > 0)
    {
      jQuery('#nextBtn').trigger('click');
      move--;
    }
  }
  else if(move < 0)
  {
    while(move < 0)
    {
      jQuery('#prevBtn').trigger('click');
      move++;
    }
  }
}