// $Id: banner.js 107900 2010-07-12 07:01:25Z smaram $
/**
  A simple javascript object to make banners rotate.
  Usage:
   1. Define the object.
      var unb = new UnwiredRotatingBanner('homepage_banner', 'homepage_banner_image', 5000);
      
   2. Add an image or two
   
		unb.add(
		   'Unwired business', 
		   '/images/homepage/banner_unwiredbusiness_june09.jpg', 
		   'http://www.unwiredbusiness.com.au/');
		   
		unb.add(
		   'Monthly Plan plans and pricing', 
		   '/images/homepage/banner_starterkit_june09.jpg', 
		   'http://www2.unwired.com.au/what-is-unwired/plans-pricing/monthly-plans');
		   
   3. Start the rotation:
      unb.start();
*/ 

/**
 * UnwiredRotatingBanner class
 *
 * @param string anchorId
 *  The id of the element in the page which is the link tag, the href of this
 *  will also rotate along with the image.
 * @param string imageId
 *  The id of the image for which the src will rotate.
 */
function UnwiredRotatingBanner(anchorId, imageId, time) 
{
   this.list      = new Array();
   this.anchorId  = anchorId;
   this.imageId   = imageId;
   this.image     = null;
   this.anchor    = null;
   this.time      = time;
   this.interval  = null;
   this.position  = 0;
   
   
   /**
    * Adds a new image to the rotation queue
    * 
    * @param string strAlt
    *   Textual title of the image, this will be the image alt tag.
    * @param string strSrc
    *   The src URI/URL for the image
    * @param string strHref
    *   The link to go to when the image is clicked.
    * @param string strTitle
    *   Textual title of the image, this will be the image title tag.
    */
   this.add = function(strAlt, strSrc, strHref, strTitle)
   {
      var anonObj = { alt: strAlt, src: strSrc, href: strHref, cache: null, title: strTitle };
      
      if (document.images)
      {
         anonObj.cache = new Image();
         anonObj.cache.src = strSrc;
      }
      
      this.list.push(anonObj);      
   }
   
   
   /**
    * This checks to see if the elements belonging to the given ids exist yet.
    *
    * @return boolean
    */
   this.checkElements = function()
   {
      if (this.image == null || this.anchor == null)
      {
         var image = document.getElementById(this.imageId);
         var anchor = document.getElementById(this.anchorId);
         
         if (typeof(image) == 'undefined' || typeof(anchor) == 'undefined')
         {
            return false;
         }
         
         this.image = image;
         this.anchor = anchor;
      }
      
      return true;
   }
   
   
   /**
    * Rotates the banner once, will not rotate if the elements given by the 
    * passed in ids don't exist yet.
    */
   this.rotate = function()
   {
      if (!this.checkElements())
      {
         return;
      }
      
      var banner = this.list[this.position];
      
      this.image.src    = banner.src;
      this.image.alt    = banner.alt;
      if (banner.title != null) 
      {
         this.image.title  = banner.title;      
      }
      this.anchor.href  = banner.href;
      
      this.position ++;
      
      if (this.position == this.list.length)
      {
         this.position = 0;
      } 
   }
   
   
   /**
    * Starts the rotation sequence
    */
   this.start = function()
   {
      var me = this;
      this.interval = window.setInterval(function(){me.rotate()}, this.time);
   }
   
   
   /**
    * Stops the rotation sequence
    */
   this.stop = function()
   {
      clearInterval(this.interval);
   }
}


