(function($) {
  style = {
    name: 'Eyepatch&Trade; Replacement Lenses',
    has_multiple_sizes: false
  };
  skus = [];
  
    
  // COLOR: Emerald Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-987',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/6c/99/BAh7CWkKIgw2Nzh4MzE2aQtsKwc8zhNLaQdpFmkIaQMWBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104631', 
    color:                'Emerald Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Emerald Iridium', 
    number:               '13-987', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$30.00',
    msrp:                 'Was $60.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Emerald Iridium', 
    lens_color:           'Emerald Iridium',
    three_d_spin:         '/videos/view_3d/13-987.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104631'] = sku;
  
  
  // COLOR: Fire Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-988',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/6d/e3/BAh7CWkKIgw2Nzh4MzE2aQtsKwcrzxNLaQdpFmkIaQMaBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104632', 
    color:                'Fire Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Fire Iridium', 
    number:               '13-988', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$30.00',
    msrp:                 'Was $60.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Fire Iridium', 
    lens_color:           'Fire Iridium',
    three_d_spin:         '/videos/view_3d/13-988.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104632'] = sku;
  
  
  // COLOR: Gold Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-989',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/12/43/BAh7CWkKIgw2Nzh4MzE2aQtsKwfvzxNLaQdpFmkIaQMeBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104633', 
    color:                'Gold Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Gold Iridium', 
    number:               '13-989', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$25.00',
    msrp:                 'Was $50.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Gold Iridium', 
    lens_color:           'Gold Iridium',
    three_d_spin:         '/videos/view_3d/13-989.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104633'] = sku;
  
  
  // COLOR: Gold Iridium Polarized
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-990',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/52/e8/BAh7CWkKIgw2Nzh4MzE2aQtsKwfc0BNLaQdpFmkIaQMiBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104634', 
    color:                'Gold Iridium Polarized', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Gold Iridium Polarized', 
    number:               '13-990', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$40.00',
    msrp:                 'Was $80.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Gold Iridium Polarized', 
    lens_color:           'Gold Iridium Polarized',
    three_d_spin:         '/videos/view_3d/13-990.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104634'] = sku;
  
  
  // COLOR: Grey
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-991',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/c0/07/BAh7CWkKIgw2Nzh4MzE2aQtsKweQ0RNLaQdpFmkIaQMmBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104635', 
    color:                'Grey', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Grey', 
    number:               '13-991', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$20.00',
    msrp:                 'Was $40.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Grey', 
    lens_color:           'Grey',
    three_d_spin:         '/videos/view_3d/13-991.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104635'] = sku;
  
  
  // COLOR: Ice Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-992',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/3e/1e/BAh7CWkKIgw2Nzh4MzE2aQtsKwc00hNLaQdpFmkIaQMqBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104636', 
    color:                'Ice Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Ice Iridium', 
    number:               '13-992', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$25.00',
    msrp:                 'Was $50.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Ice Iridium', 
    lens_color:           'Ice Iridium',
    three_d_spin:         '/videos/view_3d/13-992.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104636'] = sku;
  
  
  // COLOR: Ruby Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-993',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/41/9d/BAh7CWkKIgw2Nzh4MzE2aQtsKwcL0xNLaQdpFmkIaQMuBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104637', 
    color:                'Ruby Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Ruby Iridium', 
    number:               '13-993', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$30.00',
    msrp:                 'Was $60.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Ruby Iridium', 
    lens_color:           'Ruby Iridium',
    three_d_spin:         '/videos/view_3d/13-993.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104637'] = sku;
  
  
  // COLOR: Slate Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-994',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/9b/0b/BAh7CWkKIgw2Nzh4MzE2aQtsKwev0xNLaQdpFmkIaQMyBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104638', 
    color:                'Slate Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Slate Iridium', 
    number:               '13-994', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$25.00',
    msrp:                 'Was $50.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Slate Iridium', 
    lens_color:           'Slate Iridium',
    three_d_spin:         '/videos/view_3d/13-994.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104638'] = sku;
  
  
  // COLOR: Titanium Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-995',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/c1/d3/BAh7CWkKIgw2Nzh4MzE2aQtsKwdZ3RNLaQdpFmkIaQM2BgE.jpg");


  
  
  sku  = {
    id:                   'sku_104639', 
    color:                'Titanium Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Titanium Iridium', 
    number:               '13-995', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$30.00',
    msrp:                 'Was $60.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Titanium Iridium', 
    lens_color:           'Titanium Iridium',
    three_d_spin:         '/videos/view_3d/13-995.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104639'] = sku;
  
  
  // COLOR: Tungsten Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-996',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/59/12/BAh7CWkKIgw2Nzh4MzE2aQtsKwdP3hNLaQdpFmkIaQM6BgE.jpg");


  
  
  sku  = {
    id:                   'sku_104640', 
    color:                'Tungsten Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in Tungsten Iridium', 
    number:               '13-996', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$25.00',
    msrp:                 'Was $50.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'Tungsten Iridium', 
    lens_color:           'Tungsten Iridium',
    three_d_spin:         '/videos/view_3d/13-996.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104640'] = sku;
  
  
  // COLOR: VR28 Black Iridium
  upcs = [];
  
  // SIZE: One Size
  upc = { 
    size:         'One Size', 
    upc_code:     '13-998',
    can_sell:     true,
    avail_msg:    'Usually ships next business day',
    atp_quantity: 20
  };
  upcs['One Size'] = upc;

  
  large_images    = [];
  lightbox_images = [];
  
  
  
  large_images.push("http://origin-www.oakley.com/a/9e/92/BAh7CWkKIgw2Nzh4MzE2aQtsKwcE4BNLaQdpFmkIaQNCBgE.jpg");


  
  
  sku  = {
    id:                   'sku_104642', 
    color:                'VR28 Black Iridium', 
    long_name:            'Eyepatch&Trade; Replacement Lenses in VR28 Black Iridium', 
    number:               '13-998', 
    can_sell:             true,
    has_sellable_upcs:    true,
    prices_starting_at:   '$25.00',
    msrp:                 'Was $50.00',
    violator:             '',
    sales_terms:          '100% Satisfaction Guaranteed',
    upcs:                 upcs,
    single_size:          'One Size', 
    frame_color:          'VR28 Black Iridium', 
    lens_color:           'VR28 Black Iridium',
    three_d_spin:         '/videos/view_3d/13-998.mp4',
    current_image_index:  0,
    large_images:         large_images,
    lightbox_images:      lightbox_images
  };
  
  skus['sku_104642'] = sku;
  
  
  
  $(document).ready(function() {
    
    current_sku = skus['sku_104639'];
    
    // 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);
      // update the info below the eyewear image
      info_string = current_sku.lens_color;
      $(".product_view_info.container span.info1").html(info_string);
      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;
    });


    // *********************
    // Scrollable rack - Set it up and handle lazy loading
    // *********************

		$( '.products_slide_wrap img' ).lazyload({
	    event: 'load'
    });

    function loadIndex( objScrollable, intIndex )
    {
      objKids = objScrollable.getItems();
      $( 'img', objKids[ objScrollable.getIndex() ]).each( function()
      {
        // Trigger the lazy loader
        $( this ).trigger( 'load' );
      });

    }

    $(".products_slide_wrap").scrollable({
      items: "div.products_slide",
      size: 1,
      prev: '.prev',
      next: '.next'
    });

    // Grab the scrollable instance
    objScrollable = $( ".products_slide_wrap" ).scrollable();

    // Set it up to load the next index on seek
    objScrollable.getConf().onSeek = function( e )
    {
      loadIndex( objScrollable, objScrollable.getIndex());    
    }

    // Load the first page of the rack when the page loads
    loadIndex( objScrollable, 0 );

    // *********************
    // End scrollable rack
    // *********************

    
    // 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)

