// ----------------------------------------------------------
// Global Variables
// ----------------------------------------------------------

var rotators = new Array();  // array to hold multiple rotating banners on single page, can have maximum 10 rotators
var firstAds = new Array(); // used to read in from cookie which ad we are up to
var speed = 10000;          // sets the speed of rotation, always set to 7.2 seconds
var rLength = 0;          // length of rotators

var dom = (document.getElementById); // the following three are for testing whether these objects exist in the
var ns  = (document.layers&&!dom);  // particular browser the user has
var ie  = (document.all&&!dom);    //

var subStr = ""; // used to remember which nav objects are unfolded
var noColor = ""; // used simply as an empty string for restyleelement

function initialiseUI() {

   // left navigation
   if (getCookie("leftNav")!=null) { subStr = getCookie("leftNav"); }
   if (subStr) {
	  var splitStr = subStr.split('-');
      for (var i=0; i<splitStr.length; i++) {
         displaySubNodes(splitStr[i],false);
      }
   }
   
   
   // banner ad
   if (rotators) { setTimeout('rotate()',speed); }
}

// -----------------------------
// FUNCTION TO keep dropdown consistency throughout site as you move
// Author: Michael Kelly

// not used for Endex rotators and leftNav not used either so
// if used add onUnload="rememberUI();" to <body> tag
function rememberUI() {

   // left navigation
   setCookie("leftNav",subStr);
   
   // banner ad
/*
   if (rotators) {
      var rlen = rotators.length;
      var firstAdStr = "";
      for (var m=0;m<rlen;m++) { firstAdStr = firstAdStr + ',' + rotators[m].currentAd; }
      firstAdStr = firstAdStr.substring(1);
      setCookie("bannerAd",firstAdStr);
   }
*/
}


// ----------------------------------------------------------
// AdvertObject object and methods
// ----------------------------------------------------------

// OBJECT - AdvertObject representing the img, id and url of each ad
// Author Mike Kelly
function AdvertObject(adID, imgFile, imgLink, type) {

   // properties
   this.advertID  = adID;
   this.imageFile = imgFile;
   this.imageLink = imgLink;
   this.mediaType = type;
   this.HTMLcode  = "";
   this.flashVars = "";
   this.flashVarsLoc = "";
   this.flashVarsRem = ""; // NOTE: this has been shortened to have no suffix for use as various file types, gif, txt, swf
   this.remoteAccess = true;
   
   // methods
   this.getHTML = getHTML;

}

// -----------------------------
// METHOD/AdvertObject - getHTML, to sort out the html code for the object
// Author Mike Kelly
function getHTML(obj,w,h) {
   var retCode = "";
   var objNum = obj.substring(3);
   var idName = "banner" + objNum;
   if (this.mediaType=="flash") {
      retCode='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
	  retCode+='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"';
	  retCode+='WIDTH="'+w+'" HEIGHT="'+h+'" id="flashBanner" ALIGN="">';
	  retCode+='<PARAM NAME=movie VALUE="'+this.imageFile+'">';
	  retCode+='<PARAM NAME=loop VALUE=true>';
	  retCode+='<PARAM NAME=quality VALUE=high>';
	  retCode+='<EMBED src="dynamicText.swf" loop=false quality=high WIDTH="'+w+'"';
	  retCode+='HEIGHT="'+h+'" NAME="flashBanner" ALIGN="" TYPE="application/x-shockwave-flash"';
	  retCode+='PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>';
	  retCode+='</OBJECT>';
	  if (this.flashVarsRem) { retCode='<img src="'+this.flashVarsRem+'.gif" onerror="checkConnection('+objNum+','+this.advertID+')" height=1 width=1>'+retCode; }
   } else {
	  if (typeof this.imageLink!="undefined") {
	      retCode='<a href="javascript:goToLink('+objNum+');">';
	  }
	  retCode+='<img name="'+idName+'" id="'+idName+'" ';
	  retCode+='onmouseover="window.status=rotators['+objNum+'].adverts[rotators['+objNum+'].currentAd].imageLink; return true" ';
	  retCode+='onmouseout="window.status=\'\'; return true" src="'+this.imageFile+'" height="'+h+'" width="'+w+'" border="0">';
	  if (typeof objNum != "undefined") {
	  	 retCode+='</a>';
	  }
   }
   this.HTMLcode = retCode;
}

// ----------------------------------------------------------
// AdvertRotator object and methods
// ----------------------------------------------------------

// -----------------------------
// OBJECT - AdvertRotator representing the entire rotator and those AdvertObjects added to it
// Author Mike Kelly
function AdvertRotator(id,w,h) {

   // properties
   this.rotatorID = "div" + id;
   this.adWidth = w;
   this.adHeight = h;
   this.currentAd = 0;
   this.adverts = new Array();
   this.type = "image";
   
   // methods
   this.addAdvert = addAdvert;
   
}

// -----------------------------
// METHOD/AdvertRotator - addAdvert to add and AdvertObject to the AdvertRotator
// Author Mike Kelly
function addAdvert(x) { // addAdvert(imgFile, imgLink, type [, flashUrl]) is the actual arguments required

   if (addAdvert.arguments) { var adArgs = addAdvert.arguments; }
   if (adArgs[0]) { var imgFile = adArgs[0]; }
   if (adArgs[1]) { var imgLink = adArgs[1]; }
   if (adArgs[2]) { var type = adArgs[2]; }
   if (adArgs[3]) { var flashUrl = adArgs[3]; }

   if (addAdvert.arguments) {
      var nextID = this.adverts.length;
      this.adverts[nextID] = new AdvertObject(nextID,imgFile,imgLink,type);
      this.adverts[nextID].getHTML(this.rotatorID,this.adWidth,this.adHeight);
   
      // preload images
      if (type=="image") { preloadImage(imgFile); }
   
      // setup dynamic flash
      if (type=="flash") {
         if (flashUrl) { this.adverts[nextID].flashVarsRem = flashUrl.substring(0,(flashUrl.length-4)); }
		 this.adverts[nextID].flashVarsLoc = imgFile.substring(0,(imgFile.length-4));
      }
   }
   
}

// ----------------------------------------------------------
// Rotation functions
// ----------------------------------------------------------

// -----------------------------
// FUNCTION - prepare the page for rotators
// Author Mike Kelly
function initRotators() {

   var badCookie = getCookie("bannerAd");
   if (rotators) {
      if (badCookie!=null) { firstAds = badCookie.split(','); }
      else { for(var i=0;i<rotators.length;i++) { firstAds[i] = 0; } }
   }

}

// -----------------------------
// FUNCTION - write out the html code for the rotator
// Author Mike Kelly
function writeRotator(obj) {
   
   var b4 = "";
   var afta = "";
   var rotObject = rotators[obj];
   if (firstAds[obj]) { var currAd = firstAds[obj]; } else { currAd = 0; firstAds[obj] = 0; }
   
   // check if banner rotator has only images or other media types
   for (var i=0;i<rotObject.adverts.length;i++) {
      if (rotObject.adverts[i].mediaType!="image") { rotObject.type = "multi"; i = rotObject.adverts.length; }
   }
   
   // make allowances for only image or multi
   if (rotObject.type=="multi") {
      if (ie||dom) {
	     b4 = '<div id="div'+obj+'" style="width:'+rotObject.adWidth+'px;height:'+rotObject.adHeight+'px">';
		 afta = '</div>';
	  } else if (ns) {
	     
	  }
   }
   document.writeln(b4);
   document.writeln(rotObject.adverts[currAd].HTMLcode);
   document.writeln(afta);
   rotObject.currentAd = currAd;   
}

// -----------------------------
// FUNCTION - rotate to change the images, urls, etc
// Author Mike Kelly
function rotate() {

   var adsLength = 0;
   var currentAdvert = 0;

   for (var n=0; n<rotators.length; n++) {
      adsLength = rotators[n].adverts.length;
	  currentAdvert = rotators[n].currentAd;
	  if (currentAdvert>=(adsLength-1)) { changeAd(n,0); }
	  else { currentAdvert++; changeAd(n,currentAdvert); }
   }
   setTimeout('rotate()',speed);
   
}

// -----------------------------
// FUNCTION - to actually change the image and update the current ad value
// Author Mike Kelly
function changeAd(rotID,adID) {

   var rotObject = rotators[rotID];
   if (rotObject.type=="multi") {
      if (dom) { document.getElementById(rotObject.rotatorID).innerHTML = rotObject.adverts[adID].HTMLcode; }
      if (ie) { document.all[rotObject.rotatorID].innerHTML = rotObject.adverts[adID].HTMLcode; } 
      if (ns) { document.layers[rotObject.rotatorID].document.write(rotObject.adverts[adID].HTMLcode); document.layers[rotObject.rotatorID].document.close(); }
	  if (rotObject.adverts[adID].mediaType=="flash") { setTimeout('updateFlash('+rotID+','+adID+')',200); }
   } else {
      if (document.images) {
	     var bannerImg = eval("document.images.banner"+rotID);
         bannerImg.src = rotObject.adverts[adID].imageFile;
	  }
   }

   rotators[rotID].currentAd = adID;
}

// ----------------------------------------------------------
// Dynamic flash functions
// ----------------------------------------------------------

// -----------------------------
// FUNCTION - checkConnection, simply trys to pull an image from the same location as dynamic txt files to see if they're up
// Author Mike Kelly
function updateFlash(rotID,adID) {
   var adObj = rotators[rotID].adverts[adID];
   if (adObj.remoteAccess && adObj.flashVarsRem) { setTextVar(adObj.flashVarsRem+".txt"); }
   //else if(adObj.flashVars) { setTextVar("cookie"); setTimeout('loadCookie(adObject)',500); }
   else if (adObj.flashVarsLoc) { setTextVar(adObj.flashVarsLoc+".txt"); }
}

// -----------------------------
// FUNCTION - loadCookie, if the remote txt file is unavailable, the next best data should be in a cookie. this uploads each var 1 by 1
// Author Mike Kelly
/*function loadCookie(adObject) {
   
}*/

// -----------------------------
// FUNCTION - checkConnection, simply trys to pull an image from the same location as dynamic txt files to see if they're up
// Author Mike Kelly
function checkConnection(rotID,adID) { rotators[rotID].adverts[adID].remoteAccess=false; }

// -----------------------------
// FUNCTION - setTextVar, changes text variables within the flash banners
// Author Mike Kelly
function setTextVar(val) { window.document.flashBanner.setVariable("varLoc",val); }

// -----------------------------
// FUNCTION - acceptVars, is the standard function called by the flash movie to send the variables back for placement in a cookie
// Author Mike Kelly
function acceptVars(vars) { return vars; }

// ----------------------------------------------------------
// Extra functions used
// ----------------------------------------------------------

// -----------------------------
// FUNCTION - to go to the link of the displayed banner
// Author Mike Kelly
function goToLink(obj) {
   var currObject = eval(rotators[obj].adverts[rotators[obj].currentAd]);
//   window.open(currObject.imageLink);
	location.href=currObject.imageLink;
}

// -----------------------------
// FUNCTION - to preload all banner images
// Author Mike Kelly
function preloadImage(s) {
   var pImg = new Image();
   pImg.src = s;
}

// FUNCTIONS for setting and getting from cookies
// Author Mike Kelly
function setCookie(name, value) {
   var exp = new Date();                            // make new date object
   exp.setTime(exp.getTime() + (60 * 60 * 3));     // set it 3 hrs ahead
   document.cookie = name + "=" + escape(value) + "; path=/; expires=" + exp.toGMTString();
}

function getCookie(name) {
	var cname = name + "=";               
	var dc = document.cookie;
	    if (dc.length > 0) {              
	    var begin = dc.indexOf(cname);       
	        if (begin != -1) {           
	        begin += cname.length;       
	        var end = dc.indexOf(";", begin);
	            if (end == -1) end = dc.length;
				var rtnStr = unescape(dc.substring(begin, end));
	            return rtnStr;
	        }
	    }
	return null;
}

function deleteCookie(name, value) {
   var exp = new Date();                            // make new date object
   exp.setTime(exp.getTime() - (60 * 60 * 3));     // set it 3 hrs behind
   document.cookie = name + "=" + escape(value) + "; path=/; expires=" + exp.toGMTString();
}