$(document).ready(function() {
  var body_id = $('body').attr('id');
  $('div#container').addClass('jquery');
  
  $("#navigation ul").elastic_underline({ fx: "easeInQuad", speed: 600, click: function(e, li) {
    $('#navigation ul li.back').animate({ width: 0 }, 100, 'easeOutQuad', function() {
      document.location.href = $(li).children('a')[0].href;
    });    
    e.preventDefault();
  } });
  
  $('#logo a').wrapInner('<span></span>');
  $('#address dt.col').each(function() {
    $(this).text($(this).text().charAt(0) + '.');
  });
  
  function onBefore() {
    $('#caption').html('');
    if($('#viewport').hasClass('hidden')) { $('#viewport').cycle('pause'); }
  }
  
  function onAfter() { 
    $('#caption').html('<strong>' + this.alt + '</strong> ' + this.title);
    if($('#viewport').hasClass('hidden')) { $('#viewport').cycle('pause'); }
  }
  
  $('#viewport').each(function() {
    $(this).cycle({ fx: 'fade', speed: 1200, timeout: 3000, easing: 'easeInOutQuad', delay: 1000, sync: 0, before: onBefore, after: onAfter, random: $(this).hasClass('random') });
    var firstImg = $(this).children('img')[0];
    if(firstImg) {
      $('#caption').html('<strong>' + firstImg.alt + '</strong> ' + firstImg.title);
    }
  });
  
  $('#images').hover(
    function() {
      var vp = $('#viewport');
      vp.cycle('pause');
      vp.slideUp("slow", function() { vp.addClass('hidden'); });
    },
    function() {
      var vp = $('#viewport');
      vp.slideDown("slow", function() {
        vp.removeClass('hidden');
        vp.cycle('resume');
      });
    }
  );
  
  $('.markup a[href$=.pdf], .markup a[href$=.jpg], .markup a[href$=.gif], .markup a[href$=.png]').click(function(e) {
    openWin(this.href, 'external', 960, 540);
    e.preventDefault();
  });	
});

function openWin(uri, name, width, height) {
	var n = window.open(uri, name, 
		'width='+width+',height='+height
		+',left='+(screen.width -width )/2
		+',top=' +(screen.height-height)/2
		+',status=0,menubar=0,toolbar=0,scrollbars=1,resizable=1'
	);
	n.focus();
}

(function($) {
$.fn.elastic_underline = function(o) {
    o = $.extend({ fx: "linear", speed: 500, click: function(){}, linum: 0 }, o || {});
    return this.each(function() {
      var me = $(this);
			var noop = function() { }
      var back = $('<li class="back"><div class="left"></div><div class="bottom"></div><div class="corner"></div></li>').appendTo(me);
      var li = $("li", this);
			var curr = $("li.current", this)[0];
      
      li.not(".back").hover(function() { move(this); }, noop);
      $(this).hover(noop, function() { move(curr); });
      li.click(function(e) { setCurr(this); return o.click.apply(this, [e, this]); });

      setTimeout(function() { move(curr); }, 200);
      
      function setCurr(el) { back.css({ "right": "40px", "width": $(el).width() + "px", "height": 24 + "px", "top": el.offsetTop + "px" }); curr = el; };
      function move(el) { back.stop().animate({ width: $(el).width(), right: 40, height: 24, top: el.offsetTop }, o.speed, o.fx) };      
    });
};})(jQuery);
