// Script Copyright Steve Gibson 2008
// licenced for use only on Heatons.net and its staging server

var URLs = [
 ["/index.html", 1],                      // 0  ie url, placeholder no, URL index number
 ["/stationery/officestationery.html", 1],                 // 1
 ["/stationery/officestationery.html", 2],                 // 2
 ["/stationery/savemoney.html", 1],            // 3
 ["/stationery/savemoney.html", 2,],           // 4
 ["/stationery/ilovestationery.html", 1,],     // 5
 ["/stationery/ilovestationery.html", 2,],     // 6
 ["/stationery/nextdaydelivery.html", 1,],               // 7
 ["/stationery/nextdaydelivery.html", 2,],               // 8
 ["/stationery/facilitymanagement.html", 1,],         // 9
 ["/stationery/facilitymanagement.html", 2,],         // 10
 ["/stationery/bestpractices.html", 1,],  // 11
 ["/stationery/bestpractices.html", 2,],  // 12
 ["/stationery/officecatalogue.html", 1,],        // 13
 ["/stationery/officecatalogue.html", 2],          // 14
 ["/print/officeprint.html", 1],                 // 15
 ["/print/officeprint.html", 2],                 // 16
 ["/print/savemoneyonprint.html", 1],            // 17
 ["/print/savemoneyonprint.html", 2,],           // 18
 ["/print/iloveprint.html", 1,],     // 19
 ["/print/iloveprint.html", 2,],     // 20
 ["/print/fastprintdelivery.html", 1,],               // 21
 ["/print/fastprintdelivery.html", 2,],               // 22
 ["/print/greatprintdesign.html", 1,],         // 23
 ["/print/greatprintdesign.html", 2,],         // 24
 ["/print/printthatsells.html", 1,],  // 25
 ["/print/printthatsells.html", 2,],  // 26 
 ["/furniture/ergonomicfurniture.html", 1],                 // 27
 ["/furniture/ergonomicfurniture.html", 2],                 // 28
 ["/furniture/ecofurniture.html", 1],            // 29
 ["/furniture/ecofurniture.html", 2,],           // 30
 ["/furniture/executivefurniture.html", 1,],     // 31
 ["/furniture/executivefurniture.html", 2,],     // 31
 ["/furniture/fitoutrefurbfurniture.html", 1,],               // 33
 ["/furniture/fitoutrefurbfurniture.html", 2,],               // 34
 ["/furniture/freecad.html", 1,],         // 35
 ["/furniture/freecad.html", 2,],         // 36
 ["/furniture/furniturerange.html", 1,],  // 37
 ["/furniture/furniturerange.html", 2,],  // 38 
 ["/furniture/ilovefurniture.html", 1,],  // 39
 ["/furniture/ilovefurniture.html", 2,],  // 40 
 ["/furniture/officedesking.html", 1,],  // 41
 ["/furniture/officedesking.html", 2,],  // 42 
 ["/furniture/officefurniture.html", 1,],  // 43
 ["/furniture/officefurniture.html", 2,],  // 44 
 ["/furniture/officereception.html", 1,],  // 45
 ["/furniture/officereception.html", 2,],  // 46
 ["/furniture/officeseating.html", 1,],  // 47
 ["/furniture/officeseating.html", 2,],  // 48
 ["/furniture/refurbishmentfitout.html", 1,],  // 49
 ["/furniture/refurbishmentfitout.html", 2,],  // 50 
 ["/furniture/savemoneyonfurniture.html", 1,],  // 51
 ["/furniture/savemoneyonfurniture.html", 2,], // 52
 ["/furniture/boardroommeetingroom.html", 1,],  // 53
 ["/furniture/boardroommeetingroom.html", 2,],  // 54 
 ["/furniture/breakoutareas.html", 1,],  // 55
 ["/furniture/breakoutareas.html", 2,], // 56 
 ["/gifts/ecofriendlygifts.html", 1,],  // 57
 ["/gifts/ecofriendlygifts.html", 2,],  // 58 
 ["/gifts/giftsthatsell.html", 1,],  // 59
 ["/gifts/giftsthatsell.html", 2,],  // 60 
 ["/gifts/ilovegifts.html", 1,],  // 61
 ["/gifts/ilovegifts.html", 2,],  // 62 
 ["/gifts/personalisedgifts.html", 1,],  // 63
 ["/gifts/personalisedgifts.html", 2,],  // 64
 ["/gifts/promotionalgifts.html", 1,],  // 65
 ["/gifts/promotionalgifts.html", 2,],  // 66
 ["/gifts/savemoneyongifts.html", 1,],  // 67
 ["/gifts/savemoneyongifts.html", 2,],  // 68 
 ["/heatonsteam.html", 1,],  // 69
 ["/heatonsteam.html", 2,],  // 70
 ["/onestopofficesupplies.html", 1,], // 71
 ["/onestopofficesupplies.html", 2,], // 72
 ["/provensavings.html", 1,],  // 73
 ["/provensavings.html", 2,],  // 74
 ["/psweloveahappyoffice.html", 1,],  // 75
 ["/psweloveahappyoffice.html", 2,], // 76 
 ["/index.html", 2], // 77
 ["/", 1], // 78
 ["/", 2] // 79
];

var Campaigns = [
 "HomeQuote",            // 0 (Campaign index number 0)
 "HomeAd",        // 1
 "StationeryQuote",    // 2
 "StationeryAd",        // 3
 "PrintQuote",    // 4
 "PrintAd",        // 5
 "FurnitureQuote",    // 6
 "FurnitureAd",        // 7
 "GiftQuote",    // 8
 "GiftAd"        // 9

];

var Matches = [
 [0, 0],  // ie url index number = campaign index number
 [1, 2],  // /stationery.html 2 = StationeryQuote
 [2, 3],  // /stationery.html 3 = StationeryAd
 [3, 2],
 [4, 3],
 [5, 2],
 [6, 3],
 [7, 2],
 [8, 3],
 [9, 2],
 [10, 3],
 [11, 2],
 [12, 3],
 [13, 2],
 [14, 3],
 [15, 4],  // /print 1 = PrintQuote
 [16, 5],  // /print 2 = PrintAd
 [17, 4],
 [18, 5],
 [19, 4],
 [20, 5],
 [21, 4],
 [22, 5],
 [13, 4],
 [24, 5],
 [25, 4],
 [26, 5],
 [27, 6],  // /furniture 1 = Quote
 [28, 7],  // /furniture 2 = Ad
 [29, 6],
 [30, 7],
 [31, 6],
 [32, 7],
 [33, 6],
 [34, 7],
 [35, 6],
 [36, 7],
 [37, 6],
 [38, 7],
 [39, 6],
 [40, 7],
 [41, 6],
 [42, 7],
 [43, 6],
 [44, 7],
 [45, 6],
 [46, 7],
 [47, 6],
 [48, 7],
 [49, 6],
 [50, 7],
 [51, 6],
 [52, 7],
 [53, 6],
 [54, 7],
 [55, 6],
 [56, 7],
 [57, 8],  // /gifts 1 = Quote
 [58, 9],  // /gifts 1 = Ad
 [59, 8],
 [60, 9],
 [61, 8],
 [62, 9],
 [63, 8],
 [64, 9],
 [65, 8],
 [66, 9],
 [67, 8],
 [68, 9],
 [69, 0],  // /home pages 1 = Quote
 [70, 1],  // /home pages 1 = Ad
 [71, 0],
 [72, 1],
 [73, 0],
 [74, 1],
 [75, 0],
 [76, 1],
 [77, 1],
 [78, 0],
 [79, 1]
];

// ONLY EDIT THIS CONFIGURATION AREA BELOW

var Images = [
{index:0, // IndexQuote
 ImageSelection: [			  
                  		  {src:"http://www.heatons.net/images/quotes/GeneralDelivery.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Fast Delivery Stationery"},
  				  {src:"http://www.heatons.net/images/quotes/GeneralPrices.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Best Price Stationery"},
				  {src:"http://www.heatons.net/images/quotes/GeneralService.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Office Stationery"}],
 ImageDisplay: "5"},

{index:1, // IndexAd
 ImageSelection: [
				  {src:"http://www.heatons.net/images/banners/banner03.gif", href:"http://www.masternaut.co.uk/more-info.aspx", width:190, height:838, alt:"Vehicle Tracking"},
				  {src:"http://www.heatons.net/images/banners/banner05.gif", href:"http://www.heatons.net/downloads/Heatons%20Diary%202010.pdf", width:190, height:838, alt:"Are You Ready For 2010?"}],
 ImageDisplay: "random"},

{index:2, // StaioneryQuote
 ImageSelection: [			  
                  		  {src:"http://www.heatons.net/images/quotes/StationeryDelivery.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Fast Delivery Stationery"},
  				  {src:"http://www.heatons.net/images/quotes/StationeryPrices.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Best Price Stationery"},
				  {src:"http://www.heatons.net/images/quotes/StationeryService.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Office Stationery"}],
 ImageDisplay: "5"},

{index:3, // StationeryAd
 ImageSelection: [
				  {src:"http://www.heatons.net/images/banners/banner03.gif", href:"http://www.masternaut.co.uk/more-info.aspx", width:190, height:838, alt:"Vehicle Tracking"},
				  {src:"http://www.heatons.net/images/banners/banner05.gif", href:"http://www.heatons.net/downloads/Heatons%20Diary%202010.pdf", width:190, height:838, alt:"Are You Ready For 2010?"}],
 ImageDisplay: "random"},
 
 {index:4, // PrintQuote
 ImageSelection: [			  
                  		  {src:"http://www.heatons.net/images/quotes/PrintDelivery.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Fast Delivery Stationery"},
  				  {src:"http://www.heatons.net/images/quotes/PrintPrices.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Best Price Stationery"},
				  {src:"http://www.heatons.net/images/quotes/PrintService.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Office Stationery"}],
 ImageDisplay: "5"},

{index:5, // PrintAd
 ImageSelection: [
				  {src:"http://www.heatons.net/images/banners/banner05.gif", href:"http://www.heatons.net/downloads/Heatons%20Diary%202010.pdf", width:190, height:838, alt:"Are You Ready For 2010?"},
				  {src:"http://www.heatons.net/images/banners/banner02.gif",href:"http://www.heatons.net/downloads/banner02.pdf", width:190, height:838, alt:"Free iPod Shuffle"}],	
 ImageDisplay: "random"}, 
 
  {index:6, // FurnitureQuote
 ImageSelection: [			  
                  		  {src:"http://www.heatons.net/images/quotes/GeneralDelivery.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Fast Delivery Stationery"},
  				  {src:"http://www.heatons.net/images/quotes/GeneralPrices.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Best Price Stationery"},
				  {src:"http://www.heatons.net/images/quotes/GeneralService.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Office Stationery"}],
 ImageDisplay: "5"},

{index:7, // FurnitureAd
 ImageSelection: [
				  {src:"http://www.heatons.net/images/banners/banner03.gif", href:"http://www.masternaut.co.uk/more-info.aspx", width:190, height:838, alt:"Vehicle Tracking"}],
 ImageDisplay: "random"},
 
{index:8, // GiftQuote
 ImageSelection: [			  
                  		  {src:"http://www.heatons.net/images/quotes/GeneralDelivery.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Fast Delivery Stationery"},
  				  {src:"http://www.heatons.net/images/quotes/GeneralPrices.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Best Price Stationery"},
				  {src:"http://www.heatons.net/images/quotes/GeneralService.gif", href:"http://www.heatons.net/officecontacts/contact.html", width:190, height:125, alt:"Office Stationery"}],
 ImageDisplay: "5"},

{index:9, // GiftAd
 ImageSelection: [		  
				  {src:"http://www.heatons.net/images/banners/banner05.gif", href:"http://www.heatons.net/downloads/Heatons%20Diary%202010.pdf", width:190, height:838, alt:"Are You Ready For 2010?"},
				  {src:"http://www.heatons.net/images/banners/banner02.gif",href:"http://www.heatons.net/downloads/banner02.pdf", width:190, height:838, alt:"Free iPod Shuffle"}],	
 ImageDisplay: "random"}
 ];			   


// EDITABLE CONFIGURATION END

function RandomImage(url, placeholderId, num)
{
  if(!url || !url.length)
    return;
  if(!placeholderId || !placeholderId.length)
    return;
  if(!num)
    num = 1;

  this.parent  = document.getElementById(placeholderId);
  this.images  = [];
  this.timeout = 0;

  if(!this.parent) return;

  if((this.parent.style.position != "absolute") && (this.parent.style.position != "relative"))
	this.parent.style.position = "relative";

  while(this.parent.firstChild)
    this.parent.removeChild(this.parent.firstChild);

  var a = document.createElement("a");
  a.target = "_blank";
  this.parent.appendChild(a);

  // Index of current URL?
  var i = 0;
  var l = URLs.length;

  for(; i < l; i++)
  {
    var address_ = URLs[i][0];
	if(address_.charAt(0) == "/")
	{
	  // relative path:
	  address_ = window.location.protocol + "//" + window.location.host + address_;
	}

    if((address_ == url) && (URLs[i][1] == num))
      break;
  }

  if(i == l) return;

  // Index of associated campaign?
  var j = 0;
  var m = Matches.length;

  for(; j < m; j++)
    if(Matches[j][0] == i)
      break;

  if(j == m) return;

  j = Matches[j][1];

  // Image info: 
  var k = 0;
  var g = Images.length;

  for(; k < g; k++)
    if(Images[k].index == j)
      break;

  if(k == g) return;

  // Images[k] is the target image info object.
  var obj = Images[k];

  // Parse ImageDisplay option:
  this.timeout = (obj.ImageDisplay == "random") ? 0 : (1000 * parseInt(obj.ImageDisplay));

  // What images to add:
  for(var c = 0; c < obj.ImageSelection.length; c++)
  {
    var imageObj = obj.ImageSelection[c];

    this.addImage(imageObj.src, imageObj.href, imageObj.width, imageObj.height, imageObj.alt);
  }

  this.current  = this.timeout ? 0 : Math.floor(Math.random() * obj.ImageSelection.length);
  this.firstRun = true;

  this.run();

  return this;
}

RandomImage.prototype = {

  addImage: function(src, href, width, height, alt)
  {
    var l = this.images.length;

    var imag = this.images[l] = new Object;
    imag.src = src;

    var img = imag.obj = document.createElement("img");

    img.src    = src;
	img.width  = width;
	img.height = height;
    img.id     = "random_image_" + this.parent.id + "_" + String(l);
	img.alttxt = alt;
	img.url    = href;

    var is = img.style;

	is.zIndex   = 1;
	is.position = "absolute"
	is.top      = "0px";
	is.left     = "0px";
    is.border   = "0";
    is.opacity  = 0;
    is.filter   = "alpha(opacity=0)";

    this.parent.firstChild.appendChild(img);

    return l;
  },

  run: function()
  {
    this._run();
  },

  _delegate: function(obj, func, param)
  {
    param = param || null;
    return function() { func.call(obj, param); };
  },

  _run: function()
  {
    var cur = this.current;

    if(!this.firstRun)
    {
      var img = this.images[cur];

      this._fade(img.obj.id, 0, 50, 10, true);
    }

    var l = this.images.length, cur = this.current = this.timeout ? (((cur == l - 1) || this.firstRun) ? 0 : (cur + 1)) : cur;

    var img = this.images[cur];
    this._fade(img.obj.id, 100, 50, 10, true);

    if(this.timeout)
    {
	  this.firstRun = false;
	  setTimeout(this._delegate(this, this._run), this.timeout + 500);
    }
  },

  _fade: function(id, destOp, rate, delta, affectParent)
  {
    var obj = document.getElementById(id);

    if(obj.timer)
	{
		clearTimeout(obj.timer);
	}

    var curOp = obj.filters ? obj.filters.alpha.opacity : (obj.style.opacity * 100.0);
    var direction = (curOp <= destOp) ? 1 : -1;

	if((curOp == 0) && (destOp == 100))
	{
		obj.style.display = "block";
	}

	if(affectParent)
	{
		if((curOp >= 50) && (destOp == 100))
		{
			if(obj.alttxt)
			{
				obj.title = obj.alttxt || "";
				obj.parentNode.title = obj.alttxt || "";
			}
			else
			{
				obj.removeAttribute("title");
				obj.parentNode.removeAttribute("title");
			}

			if(obj.url)
			{
				obj.parentNode.href = obj.url || "";
			}
			else
			{
				obj.parentNode.removeAttribute("href");
			}
		}
	}

    delta  = Math.min(direction * (destOp - curOp), delta);
    curOp += direction * delta;

	if((curOp == 0) && (destOp == 0))
	{
		obj.style.display = "none";
	}

    if(obj.filters)
	{
		obj.filters.alpha.opacity = curOp;
	}
    else
	{
		obj.style.opacity = curOp / 100.0;
	}

    if(curOp != destOp)
	{
		obj.timer = setTimeout(function() { RandomImage.prototype._fade(id, destOp, rate, delta, affectParent); }, rate);
	}
  }
};