// Rotating ad banner core object
//

// Add & adjust all properties passed at instantiation etc.

    promo = new Array(); // global array of Promotion objects

// Promotion object
function Promotion()
{
    // Properties & default values
    // Overwritten when Promotion object is instantiated
    this.imageHeight = 10;
    this.imageWidth  = 10;
    this.baseName    = 'NOT_SET_';
    this.imageType   = '';     // to be .gif or .jpg, but all images must be the same!
    this.imagePath   = '';     // NB must have the trailing '/'
    this.selectedBannerName  = '';     // Banner name of the selected ad

    // Set fully-qualified path if necessary
    // NB Default is a relative path
    this.imagePathPrefix = ''; 

    this.imageClass  = '';     // CSS style classname for IMG image tag if required
    this.linkClass   = '';     // CSS style classname for A link tag if required
    this.target      = '_top'; // default link target

    this.weighting = new Array ();    // ad weightings
    this.adURL = new Array ();        // link URL's
    this.bannerName = new Array ();   // banner tracking names
    this.adAltText = new Array();     // banner ALT & TITLE attributes

    this.display = getBanner; // output banner HTML
    this.getAdIndex = getAdIndex; // Get a random banner number
    this.getBannerName = getBannerName; // return the banner name when one is set 
}

// Produce banner ad HTML
// Passed string  (mainPath, optional fully qualified part of image URL, for test rig)
//                default with no parameter is imagePathPrefix property
//        integer (mode, optional test mode flag, 0 or not sent=normal, 1=test mode)
//        integer (testDay, optional testing override to force image for specified day, 0=Sunday)
//                default with no parameter is current day
//        integer (testAd, optional testing override to force the exact banner to display)
//                default with no parameter is random
// Returns HTML string
function getBanner(mainPath, mode, testDay, testAd)
{
    var sHTML = '';
    var imagePrefix = '';
    var isScript = false; // default setting. Set to true if URL is inline script
    var sScript = '';    // parsed script to be added to link onclick event
    var sOnClick = '';   // onclick event routine

    var isTestMode = false;
    if (parseInt(mode) == 1) 
    {
      isTestMode = true;
    }
    
    // Set image path prefix as required
    if (!mainPath) 
    {
      imagePrefix = this.imagePathPrefix;
    }
    else imagePrefix = mainPath;

    if (isNaN(parseInt(testDay)) )
    {
       var theDate = new Date();
       var day = theDate.getDay();
    }
    else
    {
      var day = parseInt(testDay);
      if (day < 0 || day > 6) alert ('Warning - test day value ' +testDay +' out of range 0 - 6')
    }

    if (isNaN(parseInt(testAd)) )
    {
       var ad = this.getAdIndex(day);
    }
    else
    {
       var ad = parseInt(testAd);
       if (ad < 1 || ad > this.weighting[day].length) alert ('Warning - banner ad no. ' +ad +' out of range with weighting 1 - ' +this.weighting[day].length);
    }

    this.selectedBannerName = this.bannerName[ad];
    
    imageName = this.baseName +ad +this.imageType;

    // Does this ad use inline script instead of a URL?
    if (this.adURL[ad].indexOf('script:') > -1 )
    {
      isScript = true;
      sScript = this.adURL[ad].substr(this.adURL[ad].indexOf('javascript:')+11);
    }
    else isScript = false;

/*
    // Try Google Urchin direct tracking call for ad display
    // Urchin may not have loaded
		try {
      __utmSetVar('Adimpression/' +this.bannerName[ad]);
		}
		catch (errVar) { }
						
    // Set Coremetrics query string tag
    var cmTag = 'cm_sp=' +this.bannerName[ad];
    // Set start or continuation of query string
    if (this.adURL[ad].indexOf('?') > -1)
    {
      cmTag = '&' +cmTag;
    }
    else // including inline script, so this is added to dummy URL
    {
      cmTag = '?' +cmTag;
    }
*/

    if (isTestMode)
    {
      this.target = '_bannertest';// override to open ad URL a new window in test mode to ease testing
    }

  //var sOnClick = "trackerRequest('op=click', 'bn=" +this.bannerName[ad] +"');__utmSetVar('Adclick/" +this.bannerName[ad] +"');";
    var sOnClick = ''; 
    var sHTML = '<a href="';
    
    if (isScript)
    {
      sOnClick = sScript +';' +sOnClick ; // do vital stuff first in case there's a tracking problem
      if (!isTestMode)
      {
        sHTML = sHTML + location.pathname +'"'; // create dummy URL for CM tagging, no target
      }
      else sHTML = sHTML + 'javascript:void(0);//Test mode"';// test rig dummy URL with no target 
    }
    else
    {
      sHTML = sHTML +this.adURL[ad] +'" target="' +this.target +'" ';
    }

    sHTML = sHTML +'onclick="' +sOnClick +'" class="' +this.linkClass +'" title="'
      +this.adAltText[ad] +'"><img src="' +imagePrefix +this.imagePath +imageName
      +'" height="' +this.imageHeight +'" width="' +this.imageWidth
      +'" border="0" alt="' +this.adAltText[ad] +'" title="' +this.adAltText[ad]
      +'" class="' +this.imageClass +'"></a>';

    if (isNaN(mode) || parseInt(mode) == 0)
    {
      document.write (sHTML);// output to browser immediately
      return;
    }
    else return sHTML;// return HTML for display elsewhere
}

// Get random banner number
// Passed integer (day of the week, 0 = Sun)
// Returns integer; banner ad to display
function getAdIndex(theDay)
{
    var randNo = (Math.random());
    var weightSum = 0.0;
    for (index=0; weightSum <= randNo; index+=1)
    {
        weightSum += this.weighting[theDay][index]/100;
    }

    // set failsafe default
    if (index > this.weighting[theDay].length || index > this.bannerName.length-1 )
    {
        // attempt failsafe to the lower of weighting or banner entries
        index = (this.weighting[theDay].length > this.bannerName.length-1) ? this.bannerName.length-1 : this.weighting[theDay].length;
    }
    return (index);
}

// Get name of banner if one has now been set - banner is set by main display() call
// Passed void
// Returns string, banner ad name
function getBannerName()
{
  return this.selectedBannerName;
}

///////////////
