(function($) {
  style = {
    name: 'Engine Room Tee',
    has_multiple_sizes: true
  };
  skus = [];
  
    
  // COLOR: Black
  upcs = [];
  
  // SIZE: S
  upc = { 
    size:         'S', 
    upc_code:     '883889097343',
    can_sell:     true,
    avail_msg:    'Low Stock. Order soon as product may sell out.',
    atp_quantity: 3
  };
  upcs['S'] = upc;

  
  // SIZE: M
  upc = { 
    size:         'M', 
    upc_code:     '883889097299',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['M'] = upc;

  
  // SIZE: L
  upc = { 
    size:         'L', 
    upc_code:     '883889097244',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['L'] = upc;

  
  // SIZE: XL
  upc = { 
    size:         'XL', 
    upc_code:     '883889097398',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XL'] = upc;

  
  // SIZE: XXL
  upc = { 
    size:         'XXL', 
    upc_code:     '883889097442',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XXL'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/c3/7a/BAh7CGkKIgwyODB4NDAyaQtsKweIWfBKaQhpApv-.jpg");
  lightbox_images.push("http://www.oakley.com/a/03/6d/BAh7CGkKIgo2MDB4MGkLbCsHiFnwSmkIaQKb_g.jpg");


  
  
  sku  = {
    id:                   'sku_102389', 
    color:                'Black', 
    long_name:            'Engine Room Tee in Black', 
    number:               '451567SSFR-001', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$10.00',
    msrp:                 'Was $20.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          '', 
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_102389'] = sku;
  
  
  // COLOR: Heather Grey
  upcs = [];
  
  // SIZE: S
  upc = { 
    size:         'S', 
    upc_code:     '883889097367',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 8
  };
  upcs['S'] = upc;

  
  // SIZE: M
  upc = { 
    size:         'M', 
    upc_code:     '883889097312',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['M'] = upc;

  
  // SIZE: L
  upc = { 
    size:         'L', 
    upc_code:     '883889097268',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['L'] = upc;

  
  // SIZE: XL
  upc = { 
    size:         'XL', 
    upc_code:     '883889097411',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XL'] = upc;

  
  // SIZE: XXL
  upc = { 
    size:         'XXL', 
    upc_code:     '883889097466',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XXL'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/19/55/BAh7CGkKIgwyODB4NDAyaQtsKwcwYP1KaQhpA_cCAQ.jpg");
  lightbox_images.push("http://www.oakley.com/a/b2/48/BAh7CGkKIgo2MDB4MGkLbCsHMGD9SmkIaQP3AgE.jpg");


  
  
  sku  = {
    id:                   'sku_104046', 
    color:                'Heather Grey', 
    long_name:            'Engine Room Tee in Heather Grey', 
    number:               '451567SSFR-203', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$10.00',
    msrp:                 'Was $20.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          '', 
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104046'] = sku;
  
  
  // COLOR: Navy Blue
  upcs = [];
  
  // SIZE: S
  upc = { 
    size:         'S', 
    upc_code:     '883889097381',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 15
  };
  upcs['S'] = upc;

  
  // SIZE: M
  upc = { 
    size:         'M', 
    upc_code:     '883889097336',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 0
  };
  upcs['M'] = upc;

  
  // SIZE: L
  upc = { 
    size:         'L', 
    upc_code:     '883889097282',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['L'] = upc;

  
  // SIZE: XL
  upc = { 
    size:         'XL', 
    upc_code:     '883889097435',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XL'] = upc;

  
  // SIZE: XXL
  upc = { 
    size:         'XXL', 
    upc_code:     '883889097480',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XXL'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/4d/d8/BAh7CGkKIgwyODB4NDAyaQtsKwcgHBVLaQhpA4wIAQ.jpg");
  lightbox_images.push("http://www.oakley.com/a/ed/ef/BAh7CGkKIgo2MDB4MGkLbCsHIBwVS2kIaQOMCAE.jpg");


  
  
  large_images.push("http://www.oakley.com/a/9f/f3/BAh7CGkKIgwyODB4NDAyaQtsKwe2HBVLaQhpA48IAQ.jpg");
  lightbox_images.push("http://www.oakley.com/a/db/f1/BAh7CGkKIgo2MDB4MGkLbCsHthwVS2kIaQOPCAE.jpg");


  
  
  sku  = {
    id:                   'sku_102390', 
    color:                'Navy Blue', 
    long_name:            'Engine Room Tee in Navy Blue', 
    number:               '451567SSFR-60B', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$10.00',
    msrp:                 'Was $20.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          '', 
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_102390'] = sku;
  
  
  // COLOR: Red Line
  upcs = [];
  
  // SIZE: S
  upc = { 
    size:         'S', 
    upc_code:     '883889097374',
    can_sell:     true,
    avail_msg:    'Low Stock. Order soon as product may sell out.',
    atp_quantity: 2
  };
  upcs['S'] = upc;

  
  // SIZE: M
  upc = { 
    size:         'M', 
    upc_code:     '883889097329',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 0
  };
  upcs['M'] = upc;

  
  // SIZE: L
  upc = { 
    size:         'L', 
    upc_code:     '883889097275',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 0
  };
  upcs['L'] = upc;

  
  // SIZE: XL
  upc = { 
    size:         'XL', 
    upc_code:     '883889097428',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XL'] = upc;

  
  // SIZE: XXL
  upc = { 
    size:         'XXL', 
    upc_code:     '883889097473',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XXL'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/a7/93/BAh7CGkKIgwyODB4NDAyaQtsKwcNX_1KaQhpA_MCAQ.jpg");
  lightbox_images.push("http://www.oakley.com/a/8e/d5/BAh7CGkKIgo2MDB4MGkLbCsHDV_9SmkIaQPzAgE.jpg");


  
  
  sku  = {
    id:                   'sku_104132', 
    color:                'Red Line', 
    long_name:            'Engine Room Tee in Red Line', 
    number:               '451567SSFR-465', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$10.00',
    msrp:                 'Was $20.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          '', 
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104132'] = sku;
  
  
  // COLOR: White
  upcs = [];
  
  // SIZE: S
  upc = { 
    size:         'S', 
    upc_code:     '883889097350',
    can_sell:     true,
    avail_msg:    'Low Stock. Order soon as product may sell out.',
    atp_quantity: 2
  };
  upcs['S'] = upc;

  
  // SIZE: M
  upc = { 
    size:         'M', 
    upc_code:     '883889097305',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['M'] = upc;

  
  // SIZE: L
  upc = { 
    size:         'L', 
    upc_code:     '883889097251',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['L'] = upc;

  
  // SIZE: XL
  upc = { 
    size:         'XL', 
    upc_code:     '883889097404',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 0
  };
  upcs['XL'] = upc;

  
  // SIZE: XXL
  upc = { 
    size:         'XXL', 
    upc_code:     '883889097459',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['XXL'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/d7/d6/BAh7CGkKIgwyODB4NDAyaQtsKwfTWvBKaQhpAp_-.jpg");
  lightbox_images.push("http://www.oakley.com/a/a7/06/BAh7CGkKIgo2MDB4MGkLbCsH01rwSmkIaQKf_g.jpg");


  
  
  sku  = {
    id:                   'sku_104045', 
    color:                'White', 
    long_name:            'Engine Room Tee in White', 
    number:               '451567SSFR-100', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$10.00',
    msrp:                 'Was $20.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          '', 
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104045'] = sku;
  
  
  
  $(document).ready(function() {
    
    current_sku = skus['sku_102390'];
    
    // SKU/COLOR SELECTOR
    select_sku(current_sku.id);
    $("fieldset#colors a").mouseover(function() {
      select_sku($(this).attr("id"));
    })
    .mouseout(function() {
      select_sku(current_sku.id);
    })
    .click(function() {
      current_sku = skus[$(this).attr("id")];
      select_sku(current_sku.id);
      select_image_index(current_sku.current_image_index);
      update_image_violator(current_sku.violator);
      $("#sales-terms").html(current_sku.sales_terms);
      // eyewear doesn't have these additional product images
      $(".product_images.container").addClass("hide");
      new_image_selector = $("ul#image-selectors_" + $(this).attr("id"));
      if(new_image_selector.find("li").size() > 1) {
        $("ul#image-selectors_" + $(this).attr("id")).removeClass("hide");
      }
      return false;
    });
    
    // SIZE SELECTOR
    $("fieldset#sizes a").mouseover(function() {
      show_available_skus($(this).attr('rel'));
    })
    .mouseout(function() {
      $("fieldset#colors a").fadeTo("fast", 1);
      select_sku(current_sku.id);
    })
    .click(function() {
      $("fieldset#sizes a").removeClass("active");
      $(this).addClass("active");
      select_upc(current_sku, $(this).attr('rel'));
      return false;
    });
    
    // SELECT THUMBNAIL IMAGE
    $("div#product_desc ul.product_images a").click(function() {
      select_image_index($.inArray($(this).parent()[0], $(this).parents("ul").find("li")));
      return false;
    });
    
    // Open the 3D spin player SWF in Shadowbox
    $("a#three-d-view").click(function() {
      Shadowbox.open({
        player: 'swf',
        title: current_sku.long_name,
        content: '/swfs/view_3d/index.swf',
        width: 640,
        height: 480,
        options: {
          flashVars: { 
            flvpparams: 'src: ' + current_sku.three_d_spin + ' # autoplay: true # loop: false # screen: extended'
          }
        }
      });
      return false;
    });
    
    // hide loading image after every main image reload
    $("img.zoomer-img-main").load(function() {
      $(".loading").css("visibility", "hidden");
    });
    
    // QUANTITY SELECTOR
    display_quantity();
    $("#qty").change(function() {
      display_quantity();
    });
    
    // ADD TO CART BUTTON
    $(":submit#action_addtocart").click(function() {
      if($(this).hasClass("submit_active")) {
        $("form#product_form").submit();
      }
      return false;
    })
    .mouseover(function() {
      if($(this).hasClass("submit_active")) {
        window.status = 'Add ' + current_sku.long_name + ' to Your Cart'; 
      }
      return false;
    })
    .mouseout(function() {
      window.status='';
      return false;
    });
    
    $(".products_slide_wrap").scrollable({
      items: "div.products_slide",
      size: 1,
      prev: '.prev',
      next: '.next'
    });
    // bug in current version of scrollable keeps us 
    // from daisy-chainging seekTo (api is not returned)
    $(".products_slide_wrap").scrollable().seekTo(current_pane || 0);
    
    // add a show/hide toggle to .more_info
    $(".more_info").click(function () {
      $(".toggle_long_desc").slideToggle("normal");
      $(this).toggleClass("active");
      if($(this).hasClass("active")){
        $('html, body').animate({ 
          scrollTop: $('.product_desc_toggle').offset().top 
        }, 500);
      } else {
        $('html, body').animate({ 
          scrollTop: $('#product_header').offset().top 
        }, 500);
      }
    });
    
  }); // $(document).ready(function() {})
  
  
  // Shows all skus that have the current size in stock.
  function show_available_skus(size) {
    $("fieldset#colors a").each(function() {
      selected_sku = skus[$(this).attr("id")];
      if(selected_sku.upcs[size] && selected_sku.upcs[size].can_sell) {
        $(this).fadeTo(0, 1);
      } else {
        $(this).fadeTo(0, 0.25);
      }
    });
  }
  
  // When the sku is selected, it will populate the necessary page elements
  // with the proper information.
  function select_sku(sku_id) {
    selected_sku = skus[sku_id];
    $("div#product_title h5").html('SKU# ' + selected_sku.number);
    $("span#prices_starting_at").html(selected_sku.prices_starting_at);
    $("span#msrp").html(selected_sku.msrp);
    $("fieldset#colors span.configure_choice").html(selected_sku.color);
    $("fieldset#colors a").removeClass("active");
    $("fieldset#colors a#" + sku_id).addClass("active");
    
    if(selected_sku.can_sell && selected_sku.has_sellable_upcs) {
      if(style.has_multiple_sizes) {
        show_available_upcs(sku_id);
        active_upc_size = $("fieldset#sizes a.active").attr("rel");
        if(active_upc_size) { 
          select_upc(selected_sku, active_upc_size);
        }
      } else {
        select_upc(selected_sku, selected_sku.single_size);
      }
    } else {
      
      $("fieldset#sizes a").each(function() {
        $(this).addClass("disabled");
      });
      $("select#qty").attr("disabled", "disabled");
      $(":submit#action_addtocart").attr("class","submit_disabled gray");
      update_availability('Sorry, the item you have selected is out of stock or no longer available.');
    }
  }
  
  // Display a different product image using its image_index
  function select_image_index(image_index) {
    current_sku.current_image_index = image_index;
    $(".loading").css('visibility', 'visible');
    $("a.shadowbox-link").attr("href", current_sku.lightbox_images[image_index]);
    $("div#product_image img.zoomer-img-main").attr("src", current_sku.large_images[image_index]);
    Shadowbox.setup("a.shadowbox-link");
  }
  
  // Update the violator on the main product image
  function update_image_violator(violator) {
    $("#product_image .clearance").html(violator);
    if(violator == '') {
      $("#product_image .clearance").addClass("hide");
    } else {
      $("#product_image .clearance").removeClass("hide");
    }
  }
  
  // Shows all available upcs for the current sku by adding a class
  // of disabled to the upcs that are not available for sale.
  function show_available_upcs(sku_id) {
    selected_sku = skus[sku_id];
    $("fieldset#sizes a").each(function() {
      selected_upc = selected_sku.upcs[$(this).attr("rel")];
      if(selected_upc && selected_upc.can_sell) {
        $(this).removeClass("disabled");
      } else {
        $(this).addClass("disabled");
      }
    });
  }
  
  // When the upc is selected, it will populate the necessary page elements,
  // and disable the add to cart button if applicable.
  function select_upc(selected_sku, size) {
    $("fieldset#sizes span.configure_choice").html(size);
    if(selected_sku.can_sell) {
      selected_upc = selected_sku.upcs[size];
      update_availability(selected_upc.avail_msg);
      show_available_quantities(selected_upc);
      update_addtocart(selected_upc);
    }
  }
  
  // Shows all available quantities for the current upc by refreshing 
  // the values in the option-select or disabling it if upc is not 
  // available
  function show_available_quantities(selected_upc) {
    if(selected_upc.can_sell) {
      max_qty = 9;
      atp_qty = selected_upc.atp_quantity;
      $("select#qty").empty().data('options');
      for( i=1; i <= atp_qty && i <= max_qty; i++ ) {
        $("select#qty").append($('<option>').text(i).val(i));
      }
      display_quantity();
      $("select#qty").attr("disabled", null);
    } else {
      $("select#qty").attr("disabled", "disabled");
    }
  }
  
  // Displays the value of the quantity select as selected choice
  function display_quantity()  {
    $("fieldset#quantity span.configure_choice").html($("#qty").val());
  }
  
  // Updates the availability message on the page
  function update_availability(text) {
    $("fieldset#status ul li.availability").html(text);
  }
  
  // Updates the cart button based on whether the selected upc is 
  // available to sell
  function update_addtocart(selected_upc) {
    if(selected_upc.can_sell) {
      $("input#web_id").attr("value", selected_upc.upc_code);
      $(":submit#action_addtocart").attr("class","submit_active green");
    } else {
      $("input#web_id").attr("value", null);
      $(":submit#action_addtocart").attr("class","submit_disabled gray");
    }
  }
  
})(jQuery)
