var rollovers = {};

var createRollovers = function()
{

  var initRollover = function (imageId)
  {
    rollovers[imageId] = {off:new Image(), over:new Image()};
    rollovers[imageId].off.src = $(imageId).src;
    rollovers[imageId].over.src = $(imageId).src.replace('-off', '-over');
    
    Event.observe($(imageId), 'mouseover', function () {$(imageId).src = rollovers[imageId].over.src;});
    Event.observe($(imageId), 'mouseout', function () {$(imageId).src = rollovers[imageId].off.src;});
  };

  for (var x=0; x<arguments.length; x++) {
	  initRollover(arguments[x]);
  }
  
};

var initialiseThumbnailRollover = function (thumbnail)
{
  Event.observe(thumbnail, 'mouseover', function(){
    return new Effect.Morph(thumbnail, {
      beforeStart: function(ev){$(ev.element).absolutize();},
      style: {
        width: '188px',
        height: '128px',
        top: '25px',
        left: '-5px'
      },
      duration: 0.05
    });
  });
  Event.observe(thumbnail, 'mouseout', function(){
    return new Effect.Morph(thumbnail, {
      afterFinish: function(ev){$(ev.element).relativize();},
      style: {
        width: '178px',
        height: '118px',
        top: '30px',
        left: '0px'
      },
      duration: 0.05
    });
  });
};

Event.observe(window, 'load', function(){
  createRollovers('bannerContactPhone', 'bannerContactEmail');
});

(function ($J) {
  
  $J(function () {

    $J('#Slideshow').tabs({
      show: function (event, ui) {
        $J('.pages a:first-child', $J(ui.panel)).trigger('click');
      }
    });
    
    $J('#compare a').bind('click', function (event) {
      event.preventDefault();
      $J('#tabNav a[href=' + $J(this).attr('href') + ']').trigger('click');
      return false;
    });
    
    $J('.slideshow').each(function (i) {

      var Slideshow = this;
      
      $J('.pause', Slideshow).show();

      $J('.images', Slideshow).cycle({
        fx: 'fade',
        next: $J('.next', Slideshow),
        prev: $J('.previous', Slideshow),
        after: !ie6 ? function () { $J('.caption', Slideshow).text(this.title); } : null,
        pager: $J('.pages', Slideshow),
        slideExpr: 'img',
        fastOnEvent: 100,
        timeout: autoplay ? 4000 : 0
      });
      
      if (ie6) {
        $J('.caption', Slideshow).hide();
      }
      
      if (autoplay) {
      
        $J('.pause', Slideshow).bind('click', function (event) {
          event.preventDefault();
          $J('.images', Slideshow).cycle('pause');
          $J('.pause', Slideshow).hide();
          $J('.play', Slideshow).show();
          return false;
        });
      
        $J('.play', Slideshow).bind('click', function (event) {
          event.preventDefault();
          $J('.images', Slideshow).cycle('resume');
          $J('.play', Slideshow).hide();
          $J('.pause', Slideshow).show();
          return false;
        });
      
      } else {
        $J('.pause, .play', Slideshow).hide();
      }

    });

  });

})(jQuery);
