(function($) {
  style = {
    name: 'Backstage Skinny Jean',
    has_multiple_sizes: true
  };
  skus = [];
  
    
  // COLOR: Dark Wash
  upcs = [];
  
  // SIZE: Size 0
  upc = { 
    size:         '0', 
    upc_code:     '883889046334',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['0'] = upc;

  
  // SIZE: Size 4
  upc = { 
    size:         '4', 
    upc_code:     '883889046433',
    can_sell:     true,
    avail_msg:    'Low Stock. Order soon as product may sell out.',
    atp_quantity: 3
  };
  upcs['4'] = upc;

  
  // SIZE: Size 6
  upc = { 
    size:         '6', 
    upc_code:     '883889046457',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['6'] = upc;

  
  // SIZE: Size 8
  upc = { 
    size:         '8', 
    upc_code:     '883889046471',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['8'] = upc;

  
  // SIZE: Size 10
  upc = { 
    size:         '10', 
    upc_code:     '883889046358',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 18
  };
  upcs['10'] = upc;

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

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

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/12/27/BAh7CGkKIgwyODB4NDAyaQtsKwdAWo1LaQhpAtJH.jpg");
  lightbox_images.push("http://www.oakley.com/a/b8/90/BAh7CGkKIgo2MDB4MGkLbCsHQFqNS2kIaQLSRw.jpg");


  
  
  large_images.push("http://www.oakley.com/a/c3/75/BAh7CGkKIgwyODB4NDAyaQtsKwc_Wo1LaQhpAs5H.jpg");
  lightbox_images.push("http://www.oakley.com/a/6e/5f/BAh7CGkKIgo2MDB4MGkLbCsHP1qNS2kIaQLORw.jpg");


  
  
  large_images.push("http://www.oakley.com/a/79/ec/BAh7CGkKIgwyODB4NDAyaQtsKwc_Wo1LaQhpAtdH.jpg");
  lightbox_images.push("http://www.oakley.com/a/df/bb/BAh7CGkKIgo2MDB4MGkLbCsHP1qNS2kIaQLXRw.jpg");


  
  
  large_images.push("http://www.oakley.com/a/cc/a7/BAh7CGkKIgwyODB4NDAyaQtsKwc_Wo1LaQhpAttH.jpg");
  lightbox_images.push("http://www.oakley.com/a/86/3c/BAh7CGkKIgo2MDB4MGkLbCsHP1qNS2kIaQLbRw.jpg");


  
  
  large_images.push("http://www.oakley.com/a/f1/44/BAh7CGkKIgwyODB4NDAyaQtsKwc_Wo1LaQhpAt9H.jpg");
  lightbox_images.push("http://www.oakley.com/a/27/42/BAh7CGkKIgo2MDB4MGkLbCsHP1qNS2kIaQLfRw.jpg");


  
  
  sku  = {
    id:                   'sku_100702', 
    color:                'Dark Wash', 
    long_name:            'Backstage Skinny Jean in Dark Wash', 
    number:               '521149-909', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$20.00',
    msrp:                 'Was $90.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_100702'] = sku;
  
  
  // COLOR: Sheet Metal Denim
  upcs = [];
  
  // SIZE: Size 0
  upc = { 
    size:         '0', 
    upc_code:     '883889046341',
    can_sell:     false,
    avail_msg:    'Sorry, the item you have selected is out of stock or no longer available.',
    atp_quantity: 1
  };
  upcs['0'] = upc;

  
  // SIZE: Size 2
  upc = { 
    size:         '2', 
    upc_code:     '883889046426',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 12
  };
  upcs['2'] = upc;

  
  // SIZE: Size 4
  upc = { 
    size:         '4', 
    upc_code:     '883889046440',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['4'] = upc;

  
  // SIZE: Size 6
  upc = { 
    size:         '6', 
    upc_code:     '883889046464',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['6'] = upc;

  
  // SIZE: Size 8
  upc = { 
    size:         '8', 
    upc_code:     '883889046488',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['8'] = upc;

  
  // SIZE: Size 10
  upc = { 
    size:         '10', 
    upc_code:     '883889046365',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['10'] = upc;

  
  // SIZE: Size 12
  upc = { 
    size:         '12', 
    upc_code:     '883889046389',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 13
  };
  upcs['12'] = upc;

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

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://www.oakley.com/a/76/54/BAh7CGkKIgwyODB4NDAyaQtsKwcg5bBKaQhpAuJH.jpg");
  lightbox_images.push("http://www.oakley.com/a/62/f4/BAh7CGkKIgo2MDB4MGkLbCsHIOWwSmkIaQLiRw.jpg");


  
  
  sku  = {
    id:                   'sku_100703', 
    color:                'Sheet Metal Denim', 
    long_name:            'Backstage Skinny Jean in Sheet Metal Denim', 
    number:               '521149-92V', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$20.00',
    msrp:                 'Was $90.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_100703'] = sku;
  
  
  
  $(document).ready(function() {
    
    current_sku = skus['sku_100703'];
    
    // 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)
