// register event handlers
$(document).ready(function() {
  // front page slideshow
  $('.box').corner("20px");
  $('.news').corner("20px");
  $('.messages').corner("15px");
  $('.image-frame').corner("15px");

  startWidth = $('#gallery-image a img').width()+21;
  startHeight = $('#gallery-image a img').height()+21;
  if (startWidth==21)  startWidth = 221;
  if (startHeight==21) startHeight = 221;

  $('#gallery-image').hide();  
  $('#gallery-slideshow').show().cycle({ 
    fx:    'fade', 
    before: function (curr, next, opts) { 
      $('#gallery-slideshow img').css({ opacity: 1 });
      $('#gallery-caption').fadeTo(1, 0.01);      
      text = ($('img', this).attr('title'));
      setTimeout("$('#gallery-caption').text('"+ text +"');", 100);
    },
    after: function () { $('#gallery-caption').fadeTo(500, 1); },
    cssFirst: { 'left' : new Function("return -( "+startWidth+" - $('#gallery-slideshow').width() )/2;"), 
                 'top'  : new Function("return -( "+startHeight+" - $('#gallery-slideshow').height() )/2;") },
    cssBefore: { 'left' : function () { return -( $(this).width() - $('#gallery-slideshow').width() )/2; }, 
                 'top'  : function () { return  -( $(this).height() - $('#gallery-slideshow').height() )/2; } },
    pause:  5
  });

  // news headlines
  var headCount = 5;
  var headInterval;
  var oldHeadline = 0;
  var currHeadline = 0;
  var headlines = new Array(); // an array of jQuery objects

  function headRotate() {
    currHeadline = (oldHeadline + 1) % headCount;
    headlines[oldHeadline].animate({top: "-40px"}, "slow", function() {
      $(this).css({ top: '45px'});
    });
    headlines[currHeadline].show().animate({top: "5px" }, "slow");
    oldHeadline = currHeadline;
  }

  if ($("#headings")) {

    for (var i = 0; i < headCount; i++) {
      headlines[i] = $("div.listings-item:eq("+i+")");
    }

    headlines[currHeadline].css({ top: '5px'});

    headInterval = setInterval(headRotate, 5000);
    $('#scrollup').hover(function() {
      clearInterval(headInterval);
    }, function() {
      headInterval = setInterval(headRotate, 5000);
      headRotate();
    });
  }

  // center photo gallery images
  if ($(".images")) {
    $(".images .image img").each(function (intIndex) {
      $(this).css({ 'left': ( $('.image-frame:first').width() - ($(this).width() + 6) )/2,
                    'top': ( $('.image-frame:first').height() - ($(this).height() + 10) )/2 
                  });
    });

    // register modal display
    $(".images li a").each(function (intIndex) {
      $(this).bind("click", function (e) {
        e.preventDefault();
        var title = '';
        if ($('img', this).size()) {
          title = $('img', this).attr("title");
        } else {
          title = $(this).text();
        }
        var content = '<div class="modalImage"><a href="'+$(this).attr("href")+'">'+
                  '<img src="/files/'+$(this).attr("full")+'" /></a></div>'+
                  '<div class="modalCaption">'+title+'</center>';
        $.modal(content);
        if ($('.modalImage img').height > 500) {
          $('.modalImage img').width *= 500/$('.modalImage img').height;
          $('.modalImage img').height = 500;
        }
      });
    });
  }  
});
