$(document).ready(function() {
  
  $("span.mailto").each(function(){
    exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
    match = exp.exec($(this).text());
    addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
    emaillink = match[2] ? match[2] : addr;
    subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
     $(this).after('<a href="mailto:'+addr+subject+'">'+ emaillink + '</a>');
    $(this).remove();
  });
  
  // Superfish
  $('ul.sf-menu').superfish({
    speed: 'fast',
    autoArrows: false,
    dropShadows: false
  });
  
  // Move background header images
  image1Container = $('#header');
  image2Container = $('#header-background');
  image1LeftPosition = 0;
  image2LeftPosition = 0;
  setInterval('moveBackgrounds()', 80);
  
  // News and events tab
  $(".news-content li").click(function(){
    window.location=$(this).find("a").attr("href"); return false;
  });

  $("ul#news-list").carouFredSel({
      direction: "up",
      width: 280,
      height: 100,
      items: {
        visible: 2,
        width: 280,
        height: 40
      },
      scroll: {
        easing: "linear",
        pauseOnHover: true,
        items: 1
      },
      auto: {
        pauseDuration: 4000,
        delay: 2000
      }
  });

  $("ul#event-list").carouFredSel({
      direction: "up",
      width: 280,
      height: 100,
      items: {
        visible: 2,
        width: 280,
        height: 40
      },
      scroll: {
        easing: "linear",
        pauseOnHover: true,
        items: 1
      },
      auto: {
        pauseDuration: 4000,
        delay: 2000
      }
  });
  
  // Site Search Form
  var site_search = $('#site_search_form input[name="q"]')
  var initial_search_value = 'Search'
  
  site_search.click(function() {
    $(this).select();
  });
  
  site_search.focus(function() {
    if ($(this).val() == initial_search_value) {
      $(this).val('');
    }
  });
  
  site_search.blur(function() {
    if ($(this).val() == '') {
      $(this).val(initial_search_value);
    }
  });
  site_search.blur();
  
  $('#site_search_form img.submit').click(function() {
    $(this).closest('form').submit();
  });
  
  $('#site_search_form').submit(function(event) {
    if ($(this).find('input[name="q"]').val() == initial_search_value) {
      event.preventDefault;
      $(this).find('input[name="q"]').focus();
      return false;
    }
  });
  
  // Image Cycle
  $('.image-cycle .images').cycle();
  
  // Alumni form
  $("#refer-friend>p:nth-child(2n+2)").addClass("last");
  
  
  // Email Clicks Tracker
  $('a[href ^="mailto:"]').click(function(){
    _gaq.push(['_trackEvent', 'Email', 'Click', $(this).attr('href')]);
  });
  
  
});

function moveBackgrounds() {
  image1LeftPosition = image1LeftPosition + 2;
  if (image1LeftPosition >= 3000) {image1LeftPosition = 0}
  image2LeftPosition = image2LeftPosition - 5;
  if (image2LeftPosition <= -3000) {image2LeftPosition = 0}
  image1Container.css('background-position', image1LeftPosition + 'px 0');
  image2Container.css('background-position', image2LeftPosition + 'px 0');
  //console.log('Image positions: ' + image1LeftPosition + ', ' + image2LeftPosition);
}


