home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2006 May
/
PCWMAY06.iso
/
javascript
/
banner.js
next >
Wrap
Text File
|
2005-02-21
|
10KB
|
255 lines
// ----------------------------------------------------------
// 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 = 15000; // sets the speed of rotation, always set to 10 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); //
// ----------------------------------------------------------
// 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 {
retCode='<a href="javascript:goToLink('+objNum+');"><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"></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);
}
// -----------------------------
// FUNCTION - to preload all banner images
// Author Mike Kelly
function preloadImage(s) {
var pImg = new Image();
pImg.src = s;
}