// Data Map
var map = {
  'edit-attributes-1-1': 37,  // 5050
  'edit-attributes-1-2': 34,  // beef
  'edit-attributes-1-3': 25,  // fire
  'edit-attributes-1-4': 28,  // turkey
  'edit-attributes-1-5': 31,  // chicken
  'edit-attributes-1-6': 22, // veggie  
  /* size it  - n/a */
  'edit-attributes-3-7': 6,  
  'edit-attributes-3-8': 6,  
  'edit-attributes-3-9': 6,  
  /* bread */
  'edit-attributes-4-10': 1,   // white
  'edit-attributes-4-11': 2,   // wheat
  'edit-attributes-4-12': 3,   // ciabatta
  'edit-attributes-4-13': 4,   // lettuce wrapped 
  'edit-attributes-4-79': 5,   // Slider
  /* cheese */
  'edit-attributes-5-14': 47,  // sharp chedar
  'edit-attributes-5-15': 41,  // swiss
  'edit-attributes-5-16': 49, // danish blue cheese
  'edit-attributes-5-17': 50, // sliced american
  'edit-attributes-5-18': 48, // brie
  'edit-attributes-5-19': 43, // pepper jack
  'edit-attributes-5-20': 45, // gruyere
  'edit-attributes-5-21': 44, // mozzarella
  'edit-attributes-5-22': 40, // creamy cheese sauce
  'edit-attributes-5-23': 42, // pesto goat cheese
  'edit-attributes-5-24': 46, // feta
  
  /* toppings */
  'edit-attributes-2-25': 63, // grilled onions
  'edit-attributes-2-26': 56, // red onion
  'edit-attributes-2-27': 60, // dill pickle chips
  'edit-attributes-2-28': 62, // jalapenos
  'edit-attributes-2-29': 59, // pico de gallo
  'edit-attributes-2-31': 51, // green leaf lettuce
  'edit-attributes-2-32': 57, // raw yellow onion
  'edit-attributes-2-34': 58, // grilled pine apple
  'edit-attributes-2-35': 52, // tomato
  'edit-attributes-2-36': 68, // cole slaw
  'edit-attributes-2-37': 54, // sweet relish
  'edit-attributes-2-38': 65, // cucumber slices
  'edit-attributes-2-39': 53, // sardines
  'edit-attributes-2-40': 55, // roasted red peppers
  'edit-attributes-2-41': 66, // cranbery sauce
  'edit-attributes-2-42': 61, // pepperocinis
  'edit-attributes-2-43': 64, // roasted green chilis
  'edit-attributes-2-44': 67, // roasted corn & black bean
  
  /* premium toppings */
  'edit-attributes-6-45': 71, // thick cut bacon
  'edit-attributes-6-80': 70, // turkey bacon
  'edit-attributes-6-46': 82, // egg any way: sunny side up
  'edit-attributes-6-93': 82, // egg any way: over easy
  'edit-attributes-6-94': 82, // egg any way: over medium
  'edit-attributes-6-95': 82, // egg any way: over hard
  'edit-attributes-6-96': 82, // egg any way: hard boiled
  'edit-attributes-6-47': 79, // guacamole
  'edit-attributes-6-48': 85, // avocado
  'edit-attributes-6-49': 78, // jalepeno relish
  'edit-attributes-6-50': 76, // olive tapenade
  'edit-attributes-6-51': 83, // steel city chili
  'edit-attributes-6-52': 81, // fried onion strings
  'edit-attributes-6-53': 74, // PB &J
  'edit-attributes-6-55': 80, // Fried pickle chips
  'edit-attributes-6-56': 72, // holiday stuffing
  'edit-attributes-6-58': 75, // pastrami
  'edit-attributes-6-81': 77, // sauteed mushrooms
  
  /* sauces */
  'edit-attributes-7-60': 17, // mayo
  'edit-attributes-7-61': 11, // baconaise
  'edit-attributes-7-69': 9, // 1000 island
  'edit-attributes-7-62': 7, // 5050 bbq sauce
  'edit-attributes-7-63': 6, // balsamic vinaigrette
  'edit-attributes-7-64': 19, // home made pesto
  'edit-attributes-7-67': 14, // garlic aoili
  'edit-attributes-7-70': 18, // mushroom cream sauce
  'edit-attributes-7-73': 20, // sage mayo
  'edit-attributes-7-74': 8, // ranch dressing
  'edit-attributes-7-75': 13, // chipotle mayo
  'edit-attributes-7-76': 21, // sweet chili sauce
  'edit-attributes-7-77': 12, // caesar dressing
  'edit-attributes-7-78': 16, // horseradish mayo
  'end': 10000000
};

var topbun = {
  1: 87, // classic white
  2: 88, // whole wheat squaw
  3: 89, // chibatta
  4: 90,  // lettuce wrap
  5: 91  // slider
};

var radioGroups = {
    'edit-attributes-4':1
};
var checkBoxes = {};
var patty = 0;

function loadLayer(layer_id) {
  movieName = "myMovie";
  if (navigator.appName.indexOf("Microsoft") != -1) {
    movie =  window[movieName]
  }
  else {
    movie = document[movieName]
  }
  movie.callHamburger(layer_id); 
}

function fix_form() {
  $('#uc-product-add-to-cart-form-70 div.attribute > .form-item > label').each(function(x,y) {
    // add the selected div
    $(this).parents('div.attribute>div.form-item').append('<div class="selected"></div>');
    // move the description
    $(this).parent().find('.form-checkboxes').prepend($(this).parent().find('div.description'));
    // transform the attribute group label into an h3 tag
    $(this).replaceWith("<h3 class='design-h3'>" + $(this).text() + "</h3>");
  });
  
  $('div.model').remove();
  $('div .uc-price-product').remove();
  toggleRarity();
}

function updateCheckboxSelection(checkBox) {
  items = new Array();
  i = 0;
  checkBox.parents('div.form-checkboxes').find('label:has(input:checkbox:checked)')
  .each(function() {
    items[i++] = $(this).text();
  });
  checkBox.parents('div.attribute').find('div.selected')
    .html(items.join(','));

}

function updateRadioSelection(radio) {
  radio.parents('div.attribute').find('div.selected').html(radio.parent().text());
}

function toggleRadio(radio) {
  label = radio.attr('id');
  layer_id = map[label];
  group_id = label.replace(/-\d+$/, '');
  
  loadLayer(-radioGroups[group_id]);
  loadLayer(layer_id);
  radioGroups[group_id] = map[label]; 
}

function toggleCheckbox(checkbox) {
  label = checkbox.attr('id');
  layer_id = map[label];
  
  if (checkBoxes[layer_id]) {
    loadLayer(-layer_id);
    checkBoxes[layer_id] = false;
  } else {
    loadLayer(layer_id);
    checkBoxes[layer_id] = true;
  }
}

function isMinimumRequirementsMet() {
  burger = $('[name="attributes[1]"]').fieldValue() > 0;
  size = $('[name="attributes[3]"]').fieldValue() > 0;
  bread = $('[name="attributes[4]"]').fieldValue() > 0;
  return burger && size && bread;
}

function loadPattyByMeat(radio) {
  size = $('[name="attributes[3]"]').fieldValue(); 
  if (size == false) {
    $('#edit-attributes-3-7').click();
  }
  loadPatty();
}

function loadPattyBySize(radio) {
  burger = $('[name="attributes[1]"]').fieldValue(); 
  if (burger == false) {
    $('#edit-attributes-1-1').click();
  }
  loadPatty();
}

function loadPatty() {
  loadLayer(-patty);
  patty = $('[name="attributes[1]"]:checked').attr('id');
  layer_id = map[patty];
  // tweak for size
  size = $('[name="attributes[3]"]').fieldValue();
  if (size == 8) layer_id +=1; // 2/3 lb
  if (size == 9) layer_id +=2; // 1 lb
  loadLayer(layer_id);
  patty = layer_id;
}

function toggleRarity() {
  patty_id = $('[name="attributes[1]"]:checked').attr('id');

  if (patty_id == 'edit-attributes-1-2' ||
      patty_id == 'edit-attributes-1-3') {
    $('#edit-attributes-11-90').attr('checked', true);
    $('.attribute-11 .form-radios input:radio').attr('disabled', false);
  } else {
    $('#edit-attributes-11-92').attr('checked', true);
    $('.attribute-11 .form-radios input:radio').attr('disabled', true);
  }
}


$(document).ready(function() {

  fix_form();
  
  /*
  $('#uc-product-add-to-cart-form-70 .attributes').accordion( {
    header: '.form-item>h3',
    autoHeight: false,
    collapsible: true
  });
  */
  $('.attribute-1 .form-radios input:radio').click(function() {
    loadPattyByMeat($(this));
    toggleRarity();
  });
  
  $('.attribute-3 .form-radios input:radio').click(function() {
    loadPattyBySize($(this));
  });
  
  $('#uc-product-add-to-cart-form-70 .attribute .form-checkboxes input:checkbox')
  .click(function() {
    //updateCheckboxSelection($(this));
    toggleCheckbox($(this));
  });
  
  $('#uc-product-add-to-cart-form-70 .attribute:not(.attribute-1,.attribute-11,.attribute-3) .form-radios input:radio').click(function() {
    //updateRadioSelection($(this));
    if (! $(this).parents('div.attribute-3').length) {
      toggleRadio($(this));
    }
  });

  $('#edit-submit-70').click(function () {
    bottomBunId = radioGroups['edit-attributes-4'];
    topBunId = topbun[bottomBunId];
    if (isMinimumRequirementsMet()) {
      ok2add =  confirm('Are you sure? If you want to change your toppings later, you\'ll need to remove the old burger from your cart and start over again.');
      if (ok2add) {
        loadLayer(topBunId);
	    $('.attribute-11 .form-radios input:radio').attr('disabled', false);
        return true;
      }  else {
        return false;
      }
    } else {
      alert('Oops! We need to know which bread, patty and size you want.');
      return false;
    }
  });
  
});

