(function() {
  $(function() {
    var add_return, clear_returns, container, reset_form, set_largest_height, switch_image, switch_img, switch_time, _i, _len, _ref;
    switch_img = function(active_img, imageset) {
      var current, next, time;
      time = 500;
      current = $(active_img).last().parent().find('img').index($(active_img).last());
      next = current !== imageset.length - 1 ? current + 1 : 0;
      if ($(active_img).length > 1) {
        $(active_img).first().removeClass('active_img').css('opacity', 0);
      }
      $(active_img).stop().animate({
        'opacity': 0
      }, time, function() {
        return $(this).removeClass('active_img');
      });
      return $($(active_img).last().parent().children('img')[next]).animate({
        'opacity': 1
      }, time, function() {
        return $(this).addClass('active_img');
      });
    };
    if ($('body#home').length > 0 && $('header img').length > 1) {
      $('header img').css('opacity', 0);
      $('header img').first().css('opacity', 1).attr('class', 'active_img');
      switch_time = 5000;
      setInterval(function() {
        return switch_img($('.active_img'), $('header img'));
      }, switch_time);
    }
    reset_form = function(form) {
      $(form).children('input').not('#submit').val('');
      $(form).children('textarea').val('');
      return $('#captcha').find('input:checked').attr('checked', false).prev('label').find('img').removeClass('opaque');
    };
    clear_returns = function(el) {
      return $(el).children().remove();
    };
    add_return = function(el, state, msg) {
      return $(el).append('<p class="' + state + '">' + msg + '</p>');
    };
    $('#submit').click(function() {
      var form, post;
      form = $(this).parent('form');
      post = form.serializeArray();
      $.post(form.attr('action'), post, function(data) {
        var message, return_el, status;
        data = JSON.parse(data);
        return_el = '.return';
        status = data[0];
        message = data[1];
        clear_returns(return_el);
        if (status === 'success') {
          reset_form(form);
        }
        return add_return(return_el, status, message);
      });
      return false;
    });
    switch_image = function(element_container, position) {
      var src_new_image;
      element_container.find('.active').removeClass('active');
      src_new_image = $(element_container.find('.product_thumbnails').find('img')[position]).attr('src');
      element_container.find('.main_prod_image').attr('src', src_new_image);
      return $(element_container.find('ul').find('a')[position]).parent().addClass('active');
    };
    set_largest_height = function(container) {
      var high, item, _i, _len, _ref;
      high = 0;
      _ref = $(container).children();
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        item = _ref[_i];
        if ($(item).attr('height') > high) {
          high = $(item).attr('height');
        }
      }
      return $(container).parent().parent().find('.main_image_holder').css('height', parseInt(high) + 15);
    };
    if ($('.bigcartelProdImage').is('*')) {
      _ref = $('.imageset .product_thumbnails');
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        container = _ref[_i];
        set_largest_height(container);
      }
      return $('.imageset_switch li a').click(function() {
        var position, self;
        self = $(this);
        if (self.parent().hasClass('active')) {
          return false;
        }
        position = self.parent().parent().children('li').index(self.parent());
        switch_image(self.parent().parent().parent(), position);
        return false;
      });
    }
  });
}).call(this);

