$(function() {
  window.$$ = window.$$ || new Object();

  $$.swap_image = function(id) {
    $('#product_images > div.thumbnail').each(function() {
      $(this).removeClass('current');
    });
    $('#img_thumbnail_' + id).parent().addClass('current');
    $('#product_images > img:visible').each(function() {
      var image_id = $(this).attr('id').replace(/^img_/, '');
      if(image_id != id)
        $(this).fadeOut('fast', function() {
          $('#img_' + id).fadeIn('fast');
        });
    });
    
  };
  
  $$.check_inventory_selection = function(params) {
    if(!params) params = $('#buyFrm').serialize();
    $.post('/index_selection', params, function(data) {
      $('#inventory-selection').show().html(data);
    });
  };

  $('#product_images > div.thumbnail > img').click(function() {
    var product_image_id = $(this).attr('id').replace(/^img_thumbnail_/, '');
    $$.swap_image(product_image_id);
    var product_option_id = $(this).attr('product_option_id');
    if(product_option_id) {
      $('#buyFrm select[@name=product_option_id] option').each(function() {
        if($(this).val() == product_option_id) this.selected = true;
      });
      $('#buyFrm input[@name=product_option_id]').each(function() {
        if($(this).val() == product_option_id) this.checked = true;
      });
      $$.check_inventory_selection();
    }
    return false;
  });
  
  if($('#buyFrm')[0]) {
    var setup_handlers = function() {
      $('#inventory-options select[@name=product_option_id]')
        .change(function() { $$.check_inventory_selection(); })
        .change(function() {
          var id = $(this[this.selectedIndex]).attr('product_image_id');
          if(id != '') $$.swap_image(id);
        });
      $('#inventory-options input[@name=product_option_id]')
        .click(function() { $$.check_inventory_selection(); })
        .click(function() {
          if(!this.checked) return;
          var id = $(this).attr('product_image_id');
          if(id != '') $$.swap_image(id);
        });
    };

    var check_inventory = function() {
      $('#inventory_refresh').text('loading...');
      var params = $('#buyFrm').serialize();
      $.post('/index_options', params, function(data) {
        $('#inventory-options').html(data);
        setup_handlers();
        $$.check_inventory_selection(params);
        $.post('/index_total', params, function(data) {
          $('#inventory-total').html(data);
          $('#inventory_refresh').click(check_inventory);
        });
      });
    };

    $('#inventory_refresh').click(check_inventory);
    setup_handlers();
    $$.check_inventory_selection();
  }

  $('#stafBtn').click(function() {
    var dlg = $('#stafDlg').clone();
    $('button', dlg).click(function() {
      $('img', dlg).show();
      var data = $('form', dlg).serialize();
      $('form input, form textarea, form button', dlg).each(function() { this.disabled = true; });
      $.post('/staf', data, function() {
        setTimeout(function() { $.modal.close(); }, 250);
      });
      return false;
    });
    dlg.modal({ persist: true });
    return false;
  });
});
