home *** CD-ROM | disk | FTP | other *** search
/ Total Gamer 2005 January / TG_CD.ISO / Html / layers.js < prev    next >
Text File  |  2002-04-22  |  3KB  |  146 lines

  1. msie = navigator.appVersion.indexOf("MSIE") != -1;
  2.  
  3. function layerSupport() {
  4.     return (document.all || document.layers);
  5. }
  6.  
  7. layers = new Array();
  8. layerX = new Array();
  9. layerY = new Array();
  10. cursorX = 0;
  11. cursorY = 0;
  12.  
  13. function getViewWidth() {
  14.   if (msie)
  15.     return document.body.clientWidth;
  16.   else
  17.     return window.innerWidth;
  18. }
  19.  
  20. function getViewHeight() {
  21.   if (msie)
  22.     return document.body.clientHeight;
  23.   else
  24.     return window.innerHeight;
  25. }
  26.  
  27. function getViewXOffset() {
  28.   if (msie)
  29.     return document.body.scrollLeft;
  30.   else
  31.     return window.pageXOffset;
  32. }
  33.  
  34. function getViewYOffset() {
  35.   if (msie)
  36.     return document.body.scrollTop;
  37.   else
  38.     return window.pageYOffset;
  39. }
  40.  
  41. function getLayerX(i) {
  42.     return layerX[i];
  43. }
  44.  
  45. function getLayerY(i) {
  46.     return layerY[i];
  47. }
  48.  
  49. function moveLayer(i, x, y) {
  50.   layerX[i] = x;
  51.   layerY[i] = y;
  52.   if (msie) {
  53.     layers[i].style.pixelLeft = x;
  54.     layers[i].style.pixelTop = y;
  55.   }
  56.   else {
  57.     layers[i].left = x;
  58.     layers[i].top = y;
  59.   }
  60. }
  61.  
  62.  
  63. function setVisible(i, show) {
  64.    if (msie)
  65.       layers[i].style.visible = show ? "show" : "hidden";
  66.    else
  67.       layers[i].visibility = show;
  68. }
  69.  
  70.  
  71.  
  72. function outOfBounds(config, x, y, w, h) {
  73.   var result = 0;
  74.   
  75.   // note that fudge factor isn't used for bottom
  76.   if (x < getViewXOffset() - config.xFudge)
  77.     result |= 1; // Left
  78.   else if (x + w >= getViewXOffset() + getViewWidth() + config.xFudge)
  79.     result |= 2; // Right
  80.   
  81.   if (y + h >= getViewYOffset() + getViewHeight())
  82.     result |= 4; // Bottom
  83.   else if (y < getViewYOffset() - config.yFudge)
  84.     result |= 8; // Top
  85.     
  86.   return result;
  87. }
  88.  
  89. function random(bound) {
  90.   return Math.floor(Math.random() * bound);
  91. }
  92.  
  93. function randomX(config) {
  94.   return getViewXOffset() + random(getViewWidth() - config.imageWidth);
  95. }
  96.  
  97. function randomY(config) {
  98.   return getViewYOffset() - config.yFudge + random(getViewHeight() + config.yFudge - config.imageHeight);
  99. }
  100.  
  101. function setVisible(i, show) {
  102.    if (msie)
  103.       layers[i].style.visibility = show ? "" : "hidden";
  104.    else
  105.       layers[i].visibility = show ? "show" : "hide";
  106. }
  107.  
  108. function writeImage(image, name, x, y) {
  109.     layerX[name] = x;
  110.     layerY[name] = y;
  111.     if (msie) {
  112.       document.writeln('<div id="' + name + '" style="position:absolute;left:' + x + ';top:' + y + ';"><img src="' + image + '"></div>');
  113.       layers[name] = document.all[name];
  114.     }
  115.     else {
  116.       document.writeln('<layer id="' + name + '" left=' + x + ' top=' + y + '><img src="' + image + '"></layer>');
  117.       layers[name] = document.layers[name];
  118.     }
  119. }
  120.  
  121. function writeImages(config) {
  122.   for (var i = 0; i < config.imageCount; i++) {
  123.     var startX = randomX(config);
  124.     var startY = config.startOnScreen ? randomY(config) : -config.imageHeight;
  125.     var name = config.prefix + i;
  126.     writeImage(config.image, name, startX, startY);
  127.   }
  128. }
  129.  
  130. function cursorXY(e) {
  131.   cursorX = msie ? (getViewXOffset() + event.clientX) : e.pageX;
  132.   cursorY = msie ? (getViewYOffset() + event.clientY) : e.pageY;
  133. }
  134.  
  135. function captureXY() {
  136.   if (!msie) { document.captureEvents(Event.MOUSEMOVE); }
  137.   document.onmousemove = cursorXY;
  138. }
  139.  
  140. function config() {
  141.   this.xFudge = 0;
  142.   this.yFudge = 0;
  143.   this.updateInterval = 50;
  144.   this.startOnScreen = true;
  145.   this.imageCount = 1;
  146. }