(function() {
  var BRANDOPUS, COMMON, UTIL;
  $(function() {
    var $window;
    return $window = $(window);
  });
  COMMON = {
    fixedDivOffset: 97,
    currentQuote: 0,
    shortlistOpen: false,
    caseStudies: [],
    onShortlistCheckedUpdated: function() {
      var excludes, s_tags, str, tagstring, _i, _len;
      if ($('.shortlist-checkbox:checked').length === 0) {
        $('#btn-delete').hide();
        $('#btn-download').hide();
        $('#disabled-delete').show();
        $('#disabled-download').show();
      } else {
        $('#btn-delete').show();
        $('#btn-download').show();
        $('#disabled-delete').hide();
        $('#disabled-download').hide();
      }
      s_tags = [];
      excludes = "";
      $('.tags a').each(function() {
        var slug;
        slug = $(this).attr("data-slug");
        if ($.inArray(slug, s_tags) === -1) {
          if (slug !== void 0) {
            return s_tags.push(slug);
          }
        }
      });
      tagstring = "";
      $('.shortlist-checkbox').each(function() {
        return excludes += $(this).val() + "~";
      });
      for (_i = 0, _len = s_tags.length; _i < _len; _i++) {
        str = s_tags[_i];
        tagstring += str + "~";
      }
      return $.get('/shortlist/recommend/' + tagstring + '/' + excludes + '/', function(data) {
        return $('.recommendations').html(data);
      });
    },
    updateShortlistButtons: function() {
      return $('.link-add-shortlist').each(function() {
        var linkbutton, linkid;
        linkbutton = $(this);
        linkid = linkbutton.attr('case-study-id');
        linkbutton.show();
        return $('.shortlist-checkbox').each(function() {
          if (linkid === $(this).attr('case-study-id')) {
            return linkbutton.hide();
          }
        });
      });
    },
    updateShortlist: function() {
      $('#shortlist-count').html('View shortlist (' + $('#insert-shortlist li').length + ')');
      $('#shortlisted').tinycarousel({
        axis: 'y',
        pager: true,
        controls: true,
        animation: true,
        display: 5,
        duration: 500
      });
      $('.shortlist-checkbox').each(function() {
        return $(this).change(function() {
          return COMMON.onShortlistCheckedUpdated();
        });
      });
      return COMMON.updateShortlistButtons();
    },
    bindShortlistButtons: function() {
      COMMON.updateShortlistButtons();
      $('.link-add-shortlist').each(function() {
        return $(this).click(function() {
          $.get('/shortlist/add/' + $(this).attr('case-study-id') + '/', function(data) {
            $('#insert-shortlist').html(data);
            return COMMON.updateShortlist();
          });
          return false;
        });
      });
      $('#btn-delete').each(function() {
        return $(this).click(function() {
          var ids;
          ids = '';
          $('.shortlist-checkbox:checked').each(function() {
            return ids += $(this).attr('case-study-id') + "-";
          });
          ids = ids.slice(0, -1);
          $.get('/shortlist/remove/' + ids + '/', function(data) {
            $('#insert-shortlist').html(data);
            return COMMON.updateShortlist();
          });
          return false;
        });
      });
      return $('#btn-download').each(function() {
        $(this).mouseover(function() {
          return $(this).focus();
        });
        return $(this).focus(function() {
          var ids;
          ids = '';
          $('.shortlist-checkbox:checked').each(function() {
            return ids += $(this).attr('case-study-id') + "-";
          });
          ids = ids.slice(0, -1);
          $(this).attr('href', '/download/' + ids + '/');
          return false;
        });
      });
    },
    initCaseStudyIndexAnimationsForMobile: function() {
      return $(".case-studies > li").each(function() {
        $(this).css({
          "height": "285px"
        });
        $(this).find('a').filter('.wrapper-link-dark').each(function() {
          $(this).removeClass('wrapper-link-dark');
          return $(this).css({
            "display": "block",
            "width": "100%",
            "height": "100%",
            "top": "0",
            "position": "relative",
            "height": "285px",
            "color": "white"
          });
        });
        return $(this).find('a').filter('.wrapper-link-light').each(function() {
          $(this).removeClass('wrapper-link-light');
          return $(this).css({
            "display": "block",
            "width": "100%",
            "height": "100%",
            "top": "0",
            "position": "relative",
            "height": "285px",
            "color": "black"
          });
        });
      });
    },
    initCaseStudyIndexAnimations: function() {
      return $(".case-studies > li").hover(function() {
        $(this).stop();
        $(this).animate({
          height: "285px"
        }, 280);
        $(this).find('a').filter('.wrapper-link-dark').each(function() {
          return $(this).addClass('wrapper-link-dark-over');
        });
        return $(this).find('a').filter('.wrapper-link-light').each(function() {
          return $(this).addClass('wrapper-link-light-over');
        });
      }, function() {
        $(this).stop();
        $(this).animate({
          height: "125px"
        }, 280);
        $(this).find('a').filter('.wrapper-link-dark').each(function() {
          return $(this).removeClass('wrapper-link-dark-over');
        });
        return $(this).find('a').filter('.wrapper-link-light').each(function() {
          return $(this).removeClass('wrapper-link-light-over');
        });
      });
    },
    cloneCaseStudy: function(cs) {},
    initScrollForMobile: function() {
      return COMMON.applyPositionsForMobile(true);
    },
    initScroll: function() {
      var $window;
      COMMON.applyParallax();
      $window = $(window);
      return $window.scroll(function() {
        return COMMON.applyParallax();
      });
    },
    initCaseStudyParallax: function(isMobile, isHome) {
      var cs_count;
      cs_count = 0;
      return $('[data-type="background"]').each(function() {
        var background_img_url, cs, dataOverflow, offsetpos, sprite_count;
        offsetpos = $(this).offset();
        $(this).css('visibility', 'visible');
        if (isMobile) {
          $(this).css('position', 'absolute');
        }
        background_img_url = $(this).attr('data-image') || "dummylink";
        background_img_url = background_img_url.substr(0, background_img_url.length - 4);
        if (isMobile) {
          background_img_url = background_img_url + "_mob.png";
        } else {
          background_img_url = background_img_url + ".png";
        }
        $(this).css({
          "background-image": background_img_url
        });
        cs = {
          obj: $(this).parent(),
          sprites: [],
          offsetY: parseInt($(this).attr('data-offsety')),
          speed: parseInt($(this).attr('data-speed')),
          overflow: 0,
          height: $(this).height(),
          topOffset: offsetpos.top
        };
        COMMON.caseStudies.push(cs);
        if (isHome && cs_count === 0) {
          cs.offsetY = cs.offsetY === 300 ? 600 : 0;
          cs.speed = 1;
        }
        ++cs_count;
        dataOverflow = 0;
        if (isHome && isMobile) {
          cs.offsetY -= 600;
        }
        sprite_count = 0;
        return $(this).find('img').each(function() {
          var $cdiv, $section, cheight, cs_height, csprite, dupCs, img_height, img_offset_x, img_url, img_width;
          img_url = $(this).attr('data-image') || "dummylink";
          img_url = img_url.substr(0, img_url.length - 4);
          if (isMobile) {
            img_url = img_url + "_mob.png";
          } else {
            img_url = img_url + ".png";
          }
          img_width = parseInt($(this).attr('width'));
          img_height = parseInt($(this).attr('height'));
          img_offset_x = 0;
          $(this).attr('src', img_url);
          if (isMobile) {
            $(this).css('position', 'absolute');
          }
          csprite = {
            obj: $(this),
            offsetX: img_offset_x,
            speed: parseInt($(this).attr('data-speed')),
            offsetY: parseInt($(this).attr('data-offsety')),
            stopPoint: parseInt($(this).attr('data-stopPoint')) || 9e99
          };
          if (isHome && isMobile) {
            if (sprite_count === 0) {
              csprite.offsetY -= 300;
            } else {
              if (cs_count === 1) {
                csprite.offsetY -= 600;
              } else if (sprite_count >= 2) {
                csprite.offsetY -= 300;
              }
            }
          }
          sprite_count++;
          dataOverflow = parseInt($(this).attr('data-overflow'));
          if (cs.overflow < dataOverflow) {
            cs.overflow = dataOverflow;
          }
          cs_height = 600;
          if (dataOverflow > 0) {
            $cdiv = $('<div />');
            $cdiv.addClass("case-study-overflow");
            cheight = cs_height + dataOverflow;
            $cdiv.css({
              "height": "" + cheight + "px"
            });
            csprite.obj.detach();
            $cdiv.append(csprite.obj);
            $section = $('<div />');
            $section.addClass('section-overflow');
            csprite.obj.wrap($section);
            $cdiv.insertBefore($("#other-projects").parent());
            $cdiv.click(function(event) {
              var $target;
              $(this).hide();
              $target = $(document.elementFromPoint(event.clientX, event.clientY));
              if ($target.get(0) === $('.link-add-shortlist').get(0)) {
                $target.click();
              }
              return $(this).show();
            });
            dupCs = {
              obj: $cdiv,
              sprites: [csprite],
              offsetY: cs.offsetY,
              speed: cs.speed,
              overflow: dataOverflow,
              height: cs.height + dataOverflow,
              topOffset: cs.topOffset
            };
            return COMMON.caseStudies.push(dupCs);
          } else {
            return cs.sprites.push(csprite);
          }
        });
      });
    },
    scaleCaseStudyForMobile: function() {
      var combined_height, cs, csprite, image_height, image_width, totalheight, _i, _j, _len, _len2, _ref, _ref2;
      combined_height = 0;
      _ref = COMMON.caseStudies;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        cs = _ref[_i];
        cs.offsetY /= 2;
        cs.height /= 2;
        cs.obj.css({
          "height": cs.height
        });
        cs.obj.find("article").each(function() {
          $(this).css({
            "top": "20px"
          });
          return $(this).find("p").each(function() {
            return $(this).css({
              "font-size": "12px"
            });
          });
        });
        _ref2 = cs.sprites;
        for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
          csprite = _ref2[_j];
          image_width = parseInt(csprite.obj.attr('width')) / 2;
          image_height = parseInt(csprite.obj.attr('height')) / 2;
          csprite.obj.attr('width', "" + image_width + "px");
          csprite.obj.attr('height', "" + image_height + "px");
        }
      }
      totalheight = COMMON.caseStudies.length * 175 + 140;
      return $("#case-studies").css({
        "height": "" + totalheight + "px"
      });
    },
    applyPositionsForMobile: function(isParallax) {
      var $window, cs, csprite, _i, _len, _ref, _results;
      $window = $(window);
      _ref = COMMON.caseStudies;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        cs = _ref[_i];
        cs.obj.find("article").each(function() {
          return $(this).css('right', '20px');
        });
        cs.obj.css('position', 'absolute');
        cs.obj.find(".cs-controls").each(function() {
          return $(this).remove();
        });
        cs.obj.css({
          "top": cs.offsetY
        });
        _results.push((function() {
          var _j, _len2, _ref2, _results2;
          _ref2 = cs.sprites;
          _results2 = [];
          for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
            csprite = _ref2[_j];
            _results2.push(csprite.obj.css({
              "top": -COMMON.fixedDivOffset
            }));
          }
          return _results2;
        })());
      }
      return _results;
    },
    applyParallax: function() {
      var $window, basey, cs, csprite, da, db, offset, top, y, _i, _len, _ref, _results;
      $window = $(window);
      _ref = COMMON.caseStudies;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        cs = _ref[_i];
        top = -$window.scrollTop() / cs.speed + cs.offsetY + COMMON.fixedDivOffset;
        cs.obj.css({
          "top": top
        });
        basey = $window.scrollTop() + $window.height();
        da = basey - cs.offsetY;
        db = $window.scrollTop() - cs.offsetY;
        if (da < 0 || db > cs.height) {
          continue;
        }
        _results.push((function() {
          var _j, _len2, _ref2, _results2;
          _ref2 = cs.sprites;
          _results2 = [];
          for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
            csprite = _ref2[_j];
            offset = $window.scrollTop() / csprite.speed;
            y = $window.scrollTop() - offset;
            y += csprite.offsetY - cs.offsetY;
            if ($window.scrollTop() > csprite.stopPoint) {
              offset = csprite.stopPoint / csprite.speed;
              y = csprite.stopPoint - offset;
              y += csprite.offsetY - cs.offsetY;
            }
            y -= COMMON.fixedDivOffset;
            _results2.push(csprite.obj.css({
              "position": "absolute",
              "top": y
            }));
          }
          return _results2;
        })());
      }
      return _results;
    },
    handleScroll: function() {
      return COMMON.applyParallax();
    },
    activateNewsletter: function() {
      return $('#submit-newsletter').click(function() {
        var newsletterData;
        newsletterData = 'csrfmiddlewaretoken=' + $('input[name="csrfmiddlewaretoken"]')[0].value + '&name=' + $('#id_name')[0].value + '&company=' + $('#id_company')[0].value + '&email=' + $('#id_email')[0].value;
        $('input[name="interests"]:checked').each(function() {
          return newsletterData = newsletterData + '&interests=' + $(this)[0].value;
        });
        $.ajax({
          type: "POST",
          url: "/newsletter/",
          data: newsletterData,
          success: function(data) {
            $(".nyroModalLink").html(data);
            return COMMON.activateNewsletter();
          }
        });
        return false;
      });
    }
  };
  BRANDOPUS = {
    common: {
      init: function() {
        return $.getScript("/static/scripts/kerning.min.js");
      },
      mobile: function() {
        $("#shortlist").remove();
        $("a").filter(".shortlist-link").each(function() {
          return $(this).remove();
        });
        $(".newsletter").each(function() {
          return $(this).hide();
        });
        return $(".main").each(function() {
          return $(this).css({
            "position": "absolute"
          });
        });
      },
      nonMobile: function() {
        COMMON.shortlistOpen = false;
        $.get('/shortlist/', function(data) {
          $('#insert-shortlist').html(data);
          return COMMON.updateShortlist();
        });
        COMMON.bindShortlistButtons();
        COMMON.updateShortlistButtons();
        COMMON.onShortlistCheckedUpdated();
        $('.shortlist-link').click(function() {
          $("#shortlist").slideDown("normal").addClass("active");
          $(".shortlist-link").fadeOut();
          $(".contact-info").animate({
            top: '-=20px'
          }, 500);
          return false;
        });
        return $('.shortlist-close').click(function() {
          $("#shortlist").slideUp("normal").removeClass("active");
          $(".shortlist-link").fadeIn();
          $(".contact-info").animate({
            top: '+=20px'
          }, 500);
          return false;
        });
      },
      finalize: function() {
        $.nmObj({
          callbacks: {
            afterShowCont: function() {
              return COMMON.activateNewsletter();
            }
          }
        });
        return $('a.newsletter').nm();
      }
    },
    "home": {
      mobile: function() {
        COMMON.initCaseStudyParallax(true, true);
        return COMMON.initScrollForMobile();
      },
      nonMobile: function() {
        COMMON.initCaseStudyParallax(false, true);
        return COMMON.initScroll();
      },
      init: function() {
        COMMON.fixedDivOffset = 97;
        $('.scrollPage').click(function() {
          var toPosition;
          if (this.hash) {
            toPosition = $(this.hash).offset().top;
            $("body, html").animate({
              scrollTop: toPosition - 97
            }, 800);
            return false;
          }
        });
        $('#home-quotes h2').each(function() {
          return $(this).hide();
        });
        $('#home-quotes h2:eq(' + COMMON.currentQuote + ')').show();
        $('.btn-quote').click(function() {
          $('#home-quotes h2').each(function() {
            return $(this).hide();
          });
          COMMON.currentQuote++;
          if (COMMON.currentQuote >= $('#home-quotes h2').length) {
            COMMON.currentQuote = 0;
          }
          $('#home-quotes h2:eq(' + COMMON.currentQuote + ')').show();
          return false;
        });
        return $('#other-projects-carousel').tinycarousel({
          pager: true,
          controls: true,
          animation: true,
          display: 4,
          duration: 500
        });
      }
    },
    "about-us": {
      init: function() {
        return $('#year-slider').elastislide({
          imageW: 998,
          minItems: 1
        });
      }
    },
    "case-studies": {
      init: function() {
        return $('.case-study-overflow').click(function() {
          return alert('blah');
        });
      },
      mobile: function() {
        /*
              init case studies overview for mobile
              */        $("#filters").remove();
        $('input[value=brands]').change(function() {
          if ($('input[value=brands]:checked').val()) {
            $('select[name=brands]').show();
            $('select[name=disciplines]').hide();
            $('select[name=brands]').val('all');
            $('#showing-type').html('Showing brands:');
            return $.get('/case-studies/brand/all', function(data) {
              $('.kwicks').html(data);
              COMMON.initCaseStudyIndexAnimationsForMobile();
              return COMMON.bindShortlistButtons();
            });
          }
        });
        $('input[value=disciplines]').change(function() {
          if ($('input[value=disciplines]:checked').val()) {
            $('select[name=disciplines]').show();
            $('select[name=brands]').hide();
            $('select[name=disciplines]').val('all');
            $('#showing-type').html('Showing disciplines:');
            return $.get('/case-studies/discipline/all', function(data) {
              $('.kwicks').html(data);
              COMMON.initCaseStudyIndexAnimationsForMobile();
              return COMMON.bindShortlistButtons();
            });
          }
        });
        $('select[name=brands]').change(function() {
          return $.get('/case-studies/brand/' + $(this).val(), function(data) {
            $('.kwicks').html(data);
            COMMON.initCaseStudyIndexAnimationsForMobile();
            return COMMON.bindShortlistButtons();
          });
        });
        $('select[name=disciplines]').change(function() {
          return $.get('/case-studies/discipline/' + $(this).val(), function(data) {
            $('.kwicks').html(data);
            COMMON.initCaseStudyIndexAnimationsForMobile();
            return COMMON.bindShortlistButtons();
          });
        });
        $('select[name=disciplines]').hide();
        $('select[name=brands]').show();
        $('#showing-type').html('Showing brands:');
        return $.get('/case-studies/brand/all', function(data) {
          $('.kwicks').html(data);
          COMMON.initCaseStudyIndexAnimationsForMobile();
          return COMMON.bindShortlistButtons();
        });
      },
      nonMobile: function() {
        $('input[value=brands]').change(function() {
          if ($('input[value=brands]:checked').val()) {
            $('select[name=brands]').show();
            $('select[name=disciplines]').hide();
            $('select[name=brands]').val('all');
            $('#showing-type').html('Showing brands:');
            return $.get('/case-studies/brand/all', function(data) {
              $('.kwicks').html(data);
              COMMON.initCaseStudyIndexAnimations();
              return COMMON.bindShortlistButtons();
            });
          }
        });
        $('input[value=disciplines]').change(function() {
          if ($('input[value=disciplines]:checked').val()) {
            $('select[name=disciplines]').show();
            $('select[name=brands]').hide();
            $('select[name=disciplines]').val('all');
            $('#showing-type').html('Showing disciplines:');
            return $.get('/case-studies/discipline/all', function(data) {
              $('.kwicks').html(data);
              COMMON.initCaseStudyIndexAnimations();
              return COMMON.bindShortlistButtons();
            });
          }
        });
        $('select[name=brands]').change(function() {
          return $.get('/case-studies/brand/' + $(this).val(), function(data) {
            $('.kwicks').html(data);
            COMMON.initCaseStudyIndexAnimations();
            return COMMON.bindShortlistButtons();
          });
        });
        $('select[name=disciplines]').change(function() {
          return $.get('/case-studies/discipline/' + $(this).val(), function(data) {
            $('.kwicks').html(data);
            COMMON.initCaseStudyIndexAnimations();
            return COMMON.bindShortlistButtons();
          });
        });
        $('select[name=disciplines]').hide();
        $('select[name=brands]').show();
        $('#showing-type').html('Showing brands:');
        return $.get('/case-studies/brand/all', function(data) {
          $('.kwicks').html(data);
          COMMON.initCaseStudyIndexAnimations();
          return COMMON.bindShortlistButtons();
        });
      }
    },
    "case-study": {
      init: function() {
        return $('#other-projects-carousel').tinycarousel({
          pager: true,
          controls: true,
          animation: true,
          display: 4,
          duration: 500
        });
      },
      nonMobile: function() {
        COMMON.initCaseStudyParallax(false, false);
        return COMMON.initScroll();
      },
      mobile: function() {
        COMMON.initCaseStudyParallax(true, false);
        return COMMON.initScrollForMobile();
      }
    },
    "press": {
      load_years: function(start, end) {
        return $.get('/press/year/' + start + '/' + end, function(data) {
          $('#press-index-content').append(data);
          return $('#press-index-more-button').click(function() {
            $(this).remove();
            return BRANDOPUS.press.load_years(start - 1, start - 1);
          });
        });
      },
      init: function() {
        var last_year, year;
        year = $('#press-index-content').attr('data-year');
        last_year = year - 1;
        return BRANDOPUS.press.load_years(last_year, year);
      }
    },
    "careers": {
      init: function() {
        $('#careers-text h2').each(function() {
          return $(this).hide();
        });
        $('#careers-text h2:eq(' + COMMON.currentQuote + ')').show();
        return $('#careers-text h2').click(function() {
          $('#careers-text h2').each(function() {
            return $(this).hide();
          });
          COMMON.currentQuote++;
          if (COMMON.currentQuote >= $('#careers-text h2').length) {
            COMMON.currentQuote = 0;
          }
          $('#careers-text h2:eq(' + COMMON.currentQuote + ')').show();
          return false;
        });
      }
    },
    "contact": {
      init: function() {
        var googleMap;
        googleMap = $('<iframe width="368" height="466" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?hl=en&amp;q=brandopus&amp;ie=UTF8&amp;t=m&amp;vpsrc=6&amp;cid=11618338680812103108&amp;ll=51.52004,-0.132909&amp;spn=0.012444,0.01575&amp;z=15&amp;iwloc=A&amp;output=embed" id="google-maps-iframe"></iframe>');
        return $("#google-map").prepend(googleMap);
      }
    }
  };
  UTIL = {
    fire: function(func, funcname, args) {
      var namespace;
      namespace = BRANDOPUS;
      funcname = (funcname === void 0 ? "init" : funcname);
      if (func !== "" && namespace[func] && typeof namespace[func][funcname] === "function") {
        return namespace[func][funcname](args);
      }
    },
    loadEvents: function() {
      var bodyId, mobileDevice;
      bodyId = document.body.id;
      mobileDevice = /iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase());
      UTIL.fire("common");
      if (!mobileDevice) {
        UTIL.fire("common", "nonMobile");
      } else {
        UTIL.fire("common", "mobile");
      }
      $.each(document.body.className.split(/\s+/), function(i, classnm) {
        UTIL.fire(classnm);
        UTIL.fire(classnm, bodyId);
        if (!mobileDevice) {
          return UTIL.fire(classnm, "nonMobile");
        } else {
          return UTIL.fire(classnm, "mobile");
        }
      });
      return UTIL.fire("common", "finalize");
    }
  };
  $(document).ready(UTIL.loadEvents);
}).call(this);

