home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / javascript / banner.js next >
Text File  |  2005-02-21  |  10KB  |  255 lines

  1. // ----------------------------------------------------------
  2. // Global Variables
  3. // ----------------------------------------------------------
  4.  
  5. var rotators = new Array();  // array to hold multiple rotating banners on single page, can have maximum 10 rotators
  6. var firstAds = new Array(); // used to read in from cookie which ad we are up to
  7. var speed = 15000;          // sets the speed of rotation, always set to 10 seconds
  8. var rLength = 0;          // length of rotators
  9.  
  10. var dom = (document.getElementById); // the following three are for testing whether these objects exist in the
  11. var ns  = (document.layers&&!dom);  // particular browser the user has
  12. var ie  = (document.all&&!dom);    //
  13.  
  14. // ----------------------------------------------------------
  15. // AdvertObject object and methods
  16. // ----------------------------------------------------------
  17.  
  18. // OBJECT - AdvertObject representing the img, id and url of each ad
  19. // Author Mike Kelly
  20. function AdvertObject(adID, imgFile, imgLink, type) {
  21.  
  22.    // properties
  23.    this.advertID  = adID;
  24.    this.imageFile = imgFile;
  25.    this.imageLink = imgLink;
  26.    this.mediaType = type;
  27.    this.HTMLcode  = "";
  28.    this.flashVars = "";
  29.    this.flashVarsLoc = "";
  30.    this.flashVarsRem = ""; // NOTE: this has been shortened to have no suffix for use as various file types, gif, txt, swf
  31.    this.remoteAccess = true;
  32.    
  33.    // methods
  34.    this.getHTML = getHTML;
  35.  
  36. }
  37.  
  38. // -----------------------------
  39. // METHOD/AdvertObject - getHTML, to sort out the html code for the object
  40. // Author Mike Kelly
  41. function getHTML(obj,w,h) {
  42.    var retCode = "";
  43.    var objNum = obj.substring(3);
  44.    var idName = "banner" + objNum;
  45.    if (this.mediaType=="flash") {
  46.       retCode='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"';
  47.       retCode+='codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"';
  48.       retCode+='WIDTH="'+w+'" HEIGHT="'+h+'" id="flashBanner" ALIGN="">';
  49.       retCode+='<PARAM NAME=movie VALUE="'+this.imageFile+'">';
  50.       retCode+='<PARAM NAME=loop VALUE=true>';
  51.       retCode+='<PARAM NAME=quality VALUE=high>';
  52.       retCode+='<EMBED src="dynamicText.swf" loop=false quality=high WIDTH="'+w+'"';
  53.       retCode+='HEIGHT="'+h+'" NAME="flashBanner" ALIGN="" TYPE="application/x-shockwave-flash"';
  54.       retCode+='PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>';
  55.       retCode+='</OBJECT>';
  56.       if (this.flashVarsRem) { retCode='<img src="'+this.flashVarsRem+'.gif" onerror="checkConnection('+objNum+','+this.advertID+')" height=1 width=1>'+retCode; }
  57.    } else {
  58.       retCode='<a href="javascript:goToLink('+objNum+');"><img name="'+idName+'" id="'+idName+'" ';
  59.       retCode+='onmouseover="window.status=rotators['+objNum+'].adverts[rotators['+objNum+'].currentAd].imageLink; return true" ';
  60.       retCode+='onmouseout="window.status=\'\'; return true" src="'+this.imageFile+'" height="'+h+'" width="'+w+'" border="0"></a>';
  61.    }
  62.    this.HTMLcode = retCode;
  63. }
  64.  
  65. // ----------------------------------------------------------
  66. // AdvertRotator object and methods
  67. // ----------------------------------------------------------
  68.  
  69. // -----------------------------
  70. // OBJECT - AdvertRotator representing the entire rotator and those AdvertObjects added to it
  71. // Author Mike Kelly
  72. function AdvertRotator(id,w,h) {
  73.  
  74.    // properties
  75.    this.rotatorID = "div" + id;
  76.    this.adWidth = w;
  77.    this.adHeight = h;
  78.    this.currentAd = 0;
  79.    this.adverts = new Array();
  80.    this.type = "image";
  81.    
  82.    // methods
  83.    this.addAdvert = addAdvert;
  84.    
  85. }
  86.  
  87. // -----------------------------
  88. // METHOD/AdvertRotator - addAdvert to add and AdvertObject to the AdvertRotator
  89. // Author Mike Kelly
  90. function addAdvert(x) { // addAdvert(imgFile, imgLink, type [, flashUrl]) is the actual arguments required
  91.  
  92.    if (addAdvert.arguments) { var adArgs = addAdvert.arguments; }
  93.    if (adArgs[0]) { var imgFile = adArgs[0]; }
  94.    if (adArgs[1]) { var imgLink = adArgs[1]; }
  95.    if (adArgs[2]) { var type = adArgs[2]; }
  96.    if (adArgs[3]) { var flashUrl = adArgs[3]; }
  97.  
  98.    if (addAdvert.arguments) {
  99.       var nextID = this.adverts.length;
  100.       this.adverts[nextID] = new AdvertObject(nextID,imgFile,imgLink,type);
  101.       this.adverts[nextID].getHTML(this.rotatorID,this.adWidth,this.adHeight);
  102.    
  103.       // preload images
  104.       if (type=="image") { preloadImage(imgFile); }
  105.    
  106.       // setup dynamic flash
  107.       if (type=="flash") {
  108.          if (flashUrl) { this.adverts[nextID].flashVarsRem = flashUrl.substring(0,(flashUrl.length-4)); }
  109.          this.adverts[nextID].flashVarsLoc = imgFile.substring(0,(imgFile.length-4));
  110.       }
  111.    }
  112.    
  113. }
  114.  
  115. // ----------------------------------------------------------
  116. // Rotation functions
  117. // ----------------------------------------------------------
  118.  
  119. // -----------------------------
  120. // FUNCTION - prepare the page for rotators
  121. // Author Mike Kelly
  122. function initRotators() {
  123.  
  124.    var badCookie = getCookie("bannerAd");
  125.    if (rotators) {
  126.       if (badCookie!=null) { firstAds = badCookie.split(','); }
  127.       else { for(var i=0;i<rotators.length;i++) { firstAds[i] = 0; } }
  128.    }
  129.  
  130. }
  131.  
  132. // -----------------------------
  133. // FUNCTION - write out the html code for the rotator
  134. // Author Mike Kelly
  135. function writeRotator(obj) {
  136.    
  137.    var b4 = "";
  138.    var afta = "";
  139.    var rotObject = rotators[obj];
  140.    if (firstAds[obj]) { var currAd = firstAds[obj]; } else { currAd = 0; firstAds[obj] = 0; }
  141.    
  142.    // check if banner rotator has only images or other media types
  143.    for (var i=0;i<rotObject.adverts.length;i++) {
  144.       if (rotObject.adverts[i].mediaType!="image") { rotObject.type = "multi"; i = rotObject.adverts.length; }
  145.    }
  146.    
  147.    // make allowances for only image or multi
  148.    if (rotObject.type=="multi") {
  149.       if (ie||dom) {
  150.          b4 = '<div id="div'+obj+'" style="width:'+rotObject.adWidth+'px;height:'+rotObject.adHeight+'px">';
  151.          afta = '</div>';
  152.       } else if (ns) {
  153.          
  154.       }
  155.    }
  156.    document.writeln(b4);
  157.    document.writeln(rotObject.adverts[currAd].HTMLcode);
  158.    document.writeln(afta);
  159.    rotObject.currentAd = currAd;   
  160. }
  161.  
  162. // -----------------------------
  163. // FUNCTION - rotate to change the images, urls, etc
  164. // Author Mike Kelly
  165. function rotate() {
  166.  
  167.    var adsLength = 0;
  168.    var currentAdvert = 0;
  169.  
  170.    for (var n=0; n<rotators.length; n++) {
  171.       adsLength = rotators[n].adverts.length;
  172.       currentAdvert = rotators[n].currentAd;
  173.       if (currentAdvert>=(adsLength-1)) { changeAd(n,0); }
  174.       else { currentAdvert++; changeAd(n,currentAdvert); }
  175.    }
  176.    setTimeout('rotate()',speed);
  177.    
  178. }
  179.  
  180. // -----------------------------
  181. // FUNCTION - to actually change the image and update the current ad value
  182. // Author Mike Kelly
  183. function changeAd(rotID,adID) {
  184.  
  185.    var rotObject = rotators[rotID];
  186.    if (rotObject.type=="multi") {
  187.       if (dom) { document.getElementById(rotObject.rotatorID).innerHTML = rotObject.adverts[adID].HTMLcode; }
  188.       if (ie) { document.all[rotObject.rotatorID].innerHTML = rotObject.adverts[adID].HTMLcode; } 
  189.       if (ns) { document.layers[rotObject.rotatorID].document.write(rotObject.adverts[adID].HTMLcode); document.layers[rotObject.rotatorID].document.close(); }
  190.       if (rotObject.adverts[adID].mediaType=="flash") { setTimeout('updateFlash('+rotID+','+adID+')',200); }
  191.    } else {
  192.       if (document.images) {
  193.          var bannerImg = eval("document.images.banner"+rotID);
  194.          bannerImg.src = rotObject.adverts[adID].imageFile;
  195.       }
  196.    }
  197.  
  198.    rotators[rotID].currentAd = adID;
  199. }
  200.  
  201. // ----------------------------------------------------------
  202. // Dynamic flash functions
  203. // ----------------------------------------------------------
  204.  
  205. // -----------------------------
  206. // FUNCTION - checkConnection, simply trys to pull an image from the same location as dynamic txt files to see if they're up
  207. // Author Mike Kelly
  208. function updateFlash(rotID,adID) {
  209.    var adObj = rotators[rotID].adverts[adID];
  210.    if (adObj.remoteAccess && adObj.flashVarsRem) { setTextVar(adObj.flashVarsRem+".txt"); }
  211.    //else if(adObj.flashVars) { setTextVar("cookie"); setTimeout('loadCookie(adObject)',500); }
  212.    else if (adObj.flashVarsLoc) { setTextVar(adObj.flashVarsLoc+".txt"); }
  213. }
  214.  
  215. // -----------------------------
  216. // 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
  217. // Author Mike Kelly
  218. /*function loadCookie(adObject) {
  219.    
  220. }*/
  221.  
  222. // -----------------------------
  223. // FUNCTION - checkConnection, simply trys to pull an image from the same location as dynamic txt files to see if they're up
  224. // Author Mike Kelly
  225. function checkConnection(rotID,adID) { rotators[rotID].adverts[adID].remoteAccess=false; }
  226.  
  227. // -----------------------------
  228. // FUNCTION - setTextVar, changes text variables within the flash banners
  229. // Author Mike Kelly
  230. function setTextVar(val) { window.document.flashBanner.setVariable("varLoc",val); }
  231.  
  232. // -----------------------------
  233. // FUNCTION - acceptVars, is the standard function called by the flash movie to send the variables back for placement in a cookie
  234. // Author Mike Kelly
  235. function acceptVars(vars) { return vars; }
  236.  
  237. // ----------------------------------------------------------
  238. // Extra functions used
  239. // ----------------------------------------------------------
  240.  
  241. // -----------------------------
  242. // FUNCTION - to go to the link of the displayed banner
  243. // Author Mike Kelly
  244. function goToLink(obj) {
  245.    var currObject = eval(rotators[obj].adverts[rotators[obj].currentAd]);
  246.    window.open(currObject.imageLink);
  247. }
  248.  
  249. // -----------------------------
  250. // FUNCTION - to preload all banner images
  251. // Author Mike Kelly
  252. function preloadImage(s) {
  253.    var pImg = new Image();
  254.    pImg.src = s;
  255. }