(function() {
  var activateCompareLinkForTwoEdus, addEducationToCookie, contains, initializeFavouritesPage, isFavourite, removeIdFromCookie, renderSmallStarsAndFavouritesBlock, switchElements, writeFavouritesCookie, writeIdsToCompareLink;
  addEducationToCookie = function(id) {
    var cookieValue, newValue;
    cookieValue = $.cookie('favourites');
    if (cookieValue !== null) {
      if (cookieValue.match(id) !== null) {
        return false;
      }
      if (cookieValue.split('_').length >= 25) {
        alert('Your favourites can contain no more than 25 courses');
        return false;
      }
    }
    newValue = cookieValue === null || cookieValue === '' ? id : cookieValue + '_' + id;
    writeFavouritesCookie(newValue);
    return true;
  };
  removeIdFromCookie = function(id) {
    var cookieValue, newVal, old_id, old_ids, _i, _len;
    cookieValue = $.cookie('favourites');
    if (cookieValue !== null) {
      old_ids = cookieValue.split('_');
      newVal = "";
      for (_i = 0, _len = old_ids.length; _i < _len; _i++) {
        old_id = old_ids[_i];
        if (old_id !== id) {
          if (newVal === "") {
            newVal = old_id;
          } else {
            newVal = newVal + '_' + old_id;
          }
        }
      }
      return writeFavouritesCookie(newVal);
    }
  };
  writeFavouritesCookie = function(newValue) {
    return $.cookie('favourites', newValue, {
      expires: 28,
      path: '/'
    });
  };
  isFavourite = function(id) {
    var cookieValue;
    cookieValue = $.cookie('favourites');
    if (!(cookieValue != null)) {
      return false;
    }
    if (cookieValue.match(id) === null) {
      return false;
    } else {
      return true;
    }
  };
  renderSmallStarsAndFavouritesBlock = function(href) {
    $('a[id^=fav_]').each(function() {
      var id;
      id = $(this).attr('id').split('_')[1];
      if (isFavourite(id)) {
        $(this).attr('class', 'smallRemoveFromFavAction');
        return $(this).attr('title', 'Remove this study programme from your favourites');
      } else {
        $(this).attr('class', 'smallAddToFavAction');
        return $(this).attr('title', 'Add this study programme to your favourites');
      }
    });
    return $('#favourites').load(href);
  };
  switchElements = function(el1, el2) {
    el1.clone(true).insertAfter(el2);
    el2.insertAfter(el1);
    return el1.remove();
  };
  writeIdsToCompareLink = function() {
    var url, url_prefix, value;
    value = '';
    $('#rightColumn div[id^=nonCompEdu_]').each(function() {
      var id;
      id = $(this).attr('id').split('_')[1];
      if (value) {
        return value = value + '_' + id;
      } else {
        return value = id;
      }
    });
    url = $('a[rel=comparePage]').attr('href');
    url_prefix = url.split('&')[0] + '&' + url.split('&')[1].split('=')[0] + '=';
    return $('a[rel=comparePage]').attr('href', url_prefix + value);
  };
  activateCompareLinkForTwoEdus = function() {
    if ($('#rightColumn div[id^=nonCompEdu_]').length > 1) {
      return $('a[rel=comparePage]').removeClass('hidden');
    } else {
      return $('a[rel=comparePage]').addClass('hidden');
    }
  };
  initializeFavouritesPage = function() {
    var ids;
    if (window.location.href.match('withCompares') != null) {
      ids = window.location.href.split('/').pop();
      $('a.compareNav').each(function() {
        if (contains(ids, $(this).attr('id'))) {
          return $(this).click();
        }
      });
    }
    return activateCompareLinkForTwoEdus();
  };
  contains = function(str, arg) {
    var _ref;
    return (str != null ? (_ref = str.match(arg)) != null ? _ref.length : void 0 : void 0) > 0;
  };
  $(function() {
    renderSmallStarsAndFavouritesBlock('/favourites/list');
    $('div[id^=nonCompEdu_]').each(function() {
      var id;
      id = $(this).attr('id').split('_')[1];
      return $('<a class="compareNav" id=' + id + '></a>').attr('title', 'add').appendTo(this).click(function() {
        if ($('div[rel=' + id + ']').length === 1) {
          $('#' + id).attr('title', 'add');
          switchElements($('#' + id).parent(), $('div[rel=' + id + ']').attr('rel', ''));
        } else {
          $('#' + id).attr('title', 'remove');
          switchElements($('#rightColumn .placeHolder:first').attr('rel', id), $('#' + id).parent());
        }
        writeIdsToCompareLink();
        return activateCompareLinkForTwoEdus();
      });
    });
    initializeFavouritesPage();
    $('a[rel=addFavourite]').live('click', function(event) {
      var id;
      event.preventDefault();
      id = $(this).attr('id').split('_')[1];
      if (addEducationToCookie(id)) {
        return renderSmallStarsAndFavouritesBlock($(this).attr('href'));
      }
    });
    $('a[rel=removeFavourite]').live('click', function(event) {
      var id;
      event.preventDefault();
      id = $(this).attr('id').split('_')[1];
      removeIdFromCookie(id);
      return renderSmallStarsAndFavouritesBlock($(this).attr('href'));
    });
    $('a[rel=resetFavourites]').live('click', function(event) {
      event.preventDefault();
      writeFavouritesCookie(null);
      return renderSmallStarsAndFavouritesBlock($(this).attr('href'));
    });
    $('a[rel=toggleSmallFavourite]').live('click', function(event) {
      var id;
      event.preventDefault();
      id = $(this).attr('id').split('_')[1];
      if ($(this).attr('class') === 'smallAddToFavAction') {
        addEducationToCookie(id);
      } else {
        removeIdFromCookie(id);
      }
      return renderSmallStarsAndFavouritesBlock($(this).attr('href'));
    });
    $('a[rel=toggleLargeFavourite]').live('click', function(event) {
      var id;
      event.preventDefault();
      id = $(this).attr('id').split('_')[1];
      if ($(this).attr('class') === 'largeAddToFavAction') {
        if (addEducationToCookie(id)) {
          $(this).attr('class', 'largeRemoveFromFavAction');
          return $(this).attr('title', 'Remove this study programme from your favourites');
        }
      } else {
        removeIdFromCookie(id);
        $(this).attr('class', 'largeAddToFavAction');
        return $(this).attr('title', 'Add this study programme to your favourites');
      }
    });
    $('div.details').each(function() {
      var block, tabs, wrapper;
      block = $(this);
      wrapper = $('<div>').addClass('tabs').appendTo(block);
      tabs = $('<ol>').addClass('tabs').appendTo(wrapper);
      $('div.tab', block).each(function() {
        var section, title;
        section = $(this).appendTo(wrapper);
        title = $('h2', section).remove().text();
        return $('<li>').append($('<h2>').text(title)).appendTo(tabs).click(function(event) {
          event.preventDefault();
          $('div.tab', block).removeClass('active');
          $('li', tabs).removeClass('active');
          section.addClass('active');
          return $(this).addClass('active');
        });
      });
      $('div.tab:first', block).addClass('active');
      return $('li:first', block).addClass('active');
    });
    $('div.comparison').each(function() {
      $('div.tab', this).each(function() {
        var tab;
        tab = $(this);
        $('div.info', tab).addClass('hidden');
        return $('div.title', tab).click(function() {
          $('div.info', tab).toggleClass('hidden');
          return $('div.title', tab).toggleClass('active');
        });
      });
      $('div.tab:first div.info', this).toggleClass('hidden');
      return $('div.tab:first div.title', this).toggleClass('active');
    });
    $('#datepicker').datepicker({
      dateFormat: 'M d, yy',
      altField: "#alternate",
      altFormat: "yy-mm-dd"
    });
    $('div.help, img.altpopup').easyTooltip();
    $('div.filter select').dropdown();
    $('div.filter input.dropdown').addClass('refresher');
    $('#search .refresher').bind('change', function(event) {
      return $('#search').submit();
    });
    $('#search input.refresher').bind('keypress', function(event) {
      if (event.keyCode === 13) {
        return $('#search').submit();
      }
    });
    $('#homeSearch input.refresher').bind('keypress', function(event) {
      if (event.keyCode === 13) {
        return $('#search').submit();
      }
    });
    $('#homeSearch .refresher').bind('change', function(event) {
      return $('#homeSearch').submit();
    });
    $('#homeSearch input.refresher').bind('keypress', function(event) {
      if (event.keyCode === 13) {
        return $('#homeSearch').submit();
      }
    });
    return $('#copyFavouritesToCookie').click(function() {
      return writeFavouritesCookie($(this).attr('rel'));
    });
  });
}).call(this);

