$(function() {

  // Quickly add some target blank links
  $('#um_community a').attr('target', '_blank');

  // Start the cycle
  cycle = $('.row1 > .col1');

  if (cycle.children().length) {
    cycle.append('<div class="pager" />');
    cycleImgCont = $('.images', cycle);
    cyclePagerCont = $('.pager', cycle);

    moreInfoButton = '<div class="more_info_button"><div style="position: relative;">\
                  <a href="#"><img src="/2011_05_refresh/images/carousel_learn_more.png" /></a>\
                </div></div>';
    
    cycleImgCont.children().each(function() {
      var that = $(this);
      moreInfoElem = $('.more_information', that);

      if (moreInfoElem.length > 0) {
        that.append(moreInfoButton);
        $('.more_info_button > div > a', that).click(function(e) {
          e.preventDefault();
          $('.more_information', that).fadeIn();
        });
      }
    });

    cycleImgCont.cycle({
      before: function(curr, next, o) {
        $('.more_information').fadeOut();
        toggleMoreInformation(curr, o.$cont, 'hide');
      },
      after: function(curr, next, o) {
        toggleMoreInformation(next, o.$cont, 'show');
      },
      pager: cyclePagerCont,
      pagerAnchorBuilder: function(i, elem) {
        return '<a href="#"></a>';
      },
      pause: 1,
      timeout: 8000
    });
  }

  function toggleMoreInformation(elem, cont, action) {
    elem = $(elem);
    moreInformation = $('.more_info_button > div > a', elem);
    if (moreInformation.length > 0) {
      moreInformation.animate({
        top: (action === 'hide' ? '25px' : 0)
      }, 300);
    }
  }

  // Create and format the Discover MD/Campus Photos box
  (function() {
    hlCont = $('#highlighted_container');
    hlCont.wrapInner('<div id="highlighted_container_wrap" />')
    hlContWrap = $('#highlighted_container_wrap');

    var headerStates = {
      discover_md_container: '/2011_05_refresh/images/front_page/discover_md/header_discover_md.png',
      campus_photos: '/2011_05_refresh/images/front_page/discover_md/header_campus_photos.png'
    }

    hlContWrap.before('\
      <img src="' + headerStates.discover_md_container + '" id="highlighted_container_header_toggle" usemap="#highlighted_container_header"/>\
      <map id="highlighted_container_header" name="highlighted_container_header">\
        <area shape="rect" coords="11,11,185,22" href="#discover_md_container">\
        <area shape="rect" coords="216,11,326,22" href="#campus_photos">\
      </map>\
    ');

    var preloadImage = document.createElement('img');
    preloadImage.src = headerStates.campus_photos;

    $('#highlighted_container_header area').click(function(e) {
      e.preventDefault(e);

      link = $(this).attr('href');
      if ($(link).is(':visible')) {
        return;
      }

      $('#highlighted_container_header_toggle').attr('src', headerStates[link.substr(1, link.length)]);
      hlContWrap.children().hide();

      $(link).show();

      if ($.browser.msie && $.browser.version.substr(0,1) < 8){
        $('#um_community').hide().show();
      }

    });
  }());

  // Flickr loading for campus photos
  $('#campus_photos')
    .prepend('<a title="Prev" class="ui-corner-all" id="campus_photos_prev"><span class="ui-icon ui-icon-circle-triangle-w">Prev</span></a>')
    .append('<a title="Next" class="ui-corner-all" id="campus_photos_next"><span class="ui-icon ui-icon-circle-triangle-e">Next</span></a>');

  $('#campus_photos_images')
    .cycle({
      timeout: 0,
      speed: 500,
      fit: true,
      prev: $('#campus_photos_prev'),
      next: $('#campus_photos_next')
  });
});

