(function() {
  var OnStageStockProductUnitPhoto;

  window.OnStageStockProductUnitPhoto = OnStageStockProductUnitPhoto = (function() {

    function OnStageStockProductUnitPhoto() {}

    OnStageStockProductUnitPhoto.preloadElemPhotos = function(elem) {
      var i, num_photos, _results;
      num_photos = $(elem).attr("data-photos");
      _results = [];
      for (i = 0; 0 <= num_photos ? i <= num_photos : i >= num_photos; 0 <= num_photos ? i++ : i--) {
        _results.push($.preLoadImages($(elem).attr("data-photo-" + i)));
      }
      return _results;
    };

    OnStageStockProductUnitPhoto.changePhotoColor = function(elem, base_elem) {
      var default_photo_url, i, num_photos, _results;
      console.log(base_elem);
      default_photo_url = $(elem).attr("data-photo-0");
      $("img", base_elem).attr("src", default_photo_url);
      num_photos = $(base_elem).attr("data-photos");
      for (i = 0; 0 <= num_photos ? i <= num_photos : i >= num_photos; 0 <= num_photos ? i++ : i--) {
        $(base_elem).attr("data-photo-" + i, null);
      }
      num_photos = $(elem).attr("data-photos");
      _results = [];
      for (i = 0; 0 <= num_photos ? i <= num_photos : i >= num_photos; 0 <= num_photos ? i++ : i--) {
        _results.push($(base_elem).attr("data-photo-" + i, $(elem).attr("data-photo-" + i)));
      }
      return _results;
    };

    OnStageStockProductUnitPhoto.changeColorOverPhotoColor = function() {
      var elem, _i, _len, _ref;
      _ref = $("li span.colors a[data-photos]");
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        elem = _ref[_i];
        this.preloadElemPhotos(elem);
      }
      return $("li span.colors a[data-photos]").hover(function() {
        return window.OnStageStockProductUnitPhoto.changePhotoColor(this, $($(this).parent().parent()));
      });
    };

    OnStageStockProductUnitPhoto.changeColorInPhotoSelect = function() {
      var elem, _i, _len, _ref;
      _ref = $("option[data-photos]");
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        elem = _ref[_i];
        this.preloadElemPhotos(elem);
      }
      return $("select[name=on_stage_colors]").change(function() {
        return $("select[name=on_stage_colors] option:selected").each(function() {
          return window.OnStageStockProductUnitPhoto.changePhotoColor(this, $("div.product_preview"));
        });
      });
    };

    OnStageStockProductUnitPhoto.changeOverPhotos = function() {
      var element, elements, photo_li, _i, _len, _results;
      elements = [$("li[data-photos]"), $("div.product_preview")];
      _results = [];
      for (_i = 0, _len = elements.length; _i < _len; _i++) {
        element = elements[_i];
        _results.push((function() {
          var _j, _len2, _results2;
          _results2 = [];
          for (_j = 0, _len2 = element.length; _j < _len2; _j++) {
            photo_li = element[_j];
            if (!($(photo_li).attr("data-photos") > 1)) continue;
            this.preloadElemPhotos(photo_li);
            _results2.push($(photo_li).hover((function() {
              $("img", this).attr("src", $(this).attr("data-photo-1"));
            }), function() {
              $("img", this).attr("src", $(this).attr("data-photo-0"));
            }));
          }
          return _results2;
        }).call(this));
      }
      return _results;
    };

    OnStageStockProductUnitPhoto.render = function() {
      this.changeOverPhotos();
      this.changeColorInPhotoSelect();
      return this.changeColorOverPhotoColor();
    };

    return OnStageStockProductUnitPhoto;

  })();

  $(document).ready(function() {
    return window.OnStageStockProductUnitPhoto.render();
  });

}).call(this);

