/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
jQuery.timer = function (interval, callback)
{
var interval = interval || 100;

if (!callback)
  return false;

_timer = function (interval, callback) {
  this.stop = function () {
    clearInterval(self.id);
  };
  
  this.internalCallback = function () {
    callback(self);
  };
  
  this.reset = function (val) {
    if (self.id)
      clearInterval(self.id);
    
    var val = val || 100;
    this.id = setInterval(this.internalCallback, val);
  };
  
  this.interval = interval;
  this.id = setInterval(this.internalCallback, this.interval);
  
  var self = this;
};

return new _timer(interval, callback);
};

function openPDFWin(url) {
  window.open(url, '', 'resizable');
}

$(document).ready(function() {
  $("a[href$=.pdf]").click(function(e) { openPDFWin($(this).attr("href")); e.preventDefault(); });

  equalHeight($(".eq"));
  $(".image_thumb ul li:first").addClass("active");
  $(".image_thumb ul li:last").addClass("last");

  var timer = $.timer(4000, function() {
    var next = ($(".image_thumb ul li.active").next().size() > 0) ? $(".image_thumb ul li.active").next() : $(".image_thumb ul li:first");
    doClick(next, false);
  });

  $(".image_thumb ul li").click(function(){ doClick(this, timer); }).hover(function() { //Hover effects on list-item
      $(this).addClass('hover'); //Add class "hover" on hover
      }, function() {
      $(this).removeClass('hover'); //Remove class "hover" on hover out
  });
  
  if($("a[rel^=lightbox]").size() > 0) { $("a[rel^=lightbox]").lightbox(); }
});

function doClick(elem, timer) {
  if ($(elem).is(".active")) {
    return false;
  } else {
    $(".main_image div").hide().html('<a href="' + $(elem).attr("data-url") + '">' + $(elem).find('p').html() + '</a>').fadeIn("slow");
  }

  $(".image_thumb ul li").removeClass('active');
  $(elem).addClass('active');
  if (timer !== false) { timer.stop(); }
  return false;
}

function equalHeight(group) {
  var tallest = 0;
  group.each(function() {
    var thisHeight = $(this).height();
    if(thisHeight > tallest) {
      tallest = thisHeight;
    }
  });
  group.height(tallest);
}

