﻿// source: http://snippets.dzone.com/posts/show/26
Math.randomMax = function(maxVal, asFloat) {
    var val = Math.random() * maxVal;
    return asFloat ? val : Math.round(val);
}

// source: http://ragulan.co.uk/articles/view/30
// create a custom JavaScript object on the fly to handle the  banner rotation

header_image_object =
  {
      // index tracks the current image that is being displayed,  
      // starts at 1 because image 0 is the default image (array index's start from 0) 
      index: 1,

      stop: false,
      href_id: '',
      img_id: '',

      // an array of images for the header 
      header_images: [],
      header_alt: [],
      header_href: [],

      // function to increase the index value, and reset it if we are at the end of the array 
      inc_index: function() {
          this.index++;

          if (this.index == this.header_images.length) {
              this.index = 0;
          }
      },

      // getting function to return the current index 
      get_index: function() { return this.index; },

      add: function(url, alt, img) {
          var next = this.header_images.length;
          this.header_images[next] = img;
          this.header_alt[next] = alt;
          this.header_href[next] = url;
      },

      start: function() {
          this.index = Math.randomMax(this.header_images.length);
          var obj = $(header_image_object.href_id);
          var img = $(header_image_object.img_id);

          // change the image
          img.attr("src", header_image_object.header_images[header_image_object.index]);
          img.attr("alt", header_image_object.header_alt[header_image_object.index]);
          img.attr("title", header_image_object.header_alt[header_image_object.index]);
          obj.attr("href", header_image_object.header_href[header_image_object.index]);

          obj.mouseover(function() { header_image_object.stop = true; });
          obj.mouseout(function() { header_image_object.stop = false; });

          // increase the index
          header_image_object.inc_index();
      },

      // function  to fade out, swap the image, increase the index and fade in the image 
      swap: function() {
          // first fade the image out
          if (!header_image_object.stop) {
              $(header_image_object.img_id).fadeOut(1000, function() {
                  var obj = $(header_image_object.href_id);
                  var img = $(header_image_object.img_id);

                  // change the image
                  img.attr("src", header_image_object.header_images[header_image_object.index]);
                  img.attr("alt", header_image_object.header_alt[header_image_object.index]);
                  img.attr("title", header_image_object.header_alt[header_image_object.index]);
                  obj.attr("href", header_image_object.header_href[header_image_object.index]);

                  // fade the image back in 
                  img.fadeIn(1000);

                  // increase the index
                  header_image_object.inc_index();
              });
          }
      }
  };

// swap the image ever 5 seconds
setInterval(header_image_object.swap, 6000);

$(document).ready(function() {

    $('A[rel="external"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });

    $('A[rel="no-follow"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
});
