home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 August / Australian PC User - August 2003 (CD2).iso / tutorial / macromed / files / flash / menu_files / globalnav.js < prev    next >
Encoding:
JavaScript  |  2003-03-12  |  5.0 KB  |  197 lines

  1. /* 
  2.     Copyright (c) 2002-2003 Macromedia, Inc. 
  3.     globalnav scripts    
  4.     Language: JavaScript 1.2
  5. */
  6.  
  7. var calculatedOffset;
  8.  
  9. function getElementOffset(element) {
  10.     calculatedOffset = 0;
  11.     if (element != null)    {
  12.         elementOffset = calculateOffset_Y(element);
  13.     }
  14.     return elementOffset;
  15. }
  16.  
  17. function calculateOffset_Y(element) {
  18.     if (element != null)    {
  19.         calculatedOffset += element.offsetTop;
  20.         if (element.offsetParent) {
  21.             if ((element.offsetParent == document.body) || 
  22.                     (element.offsetParent.tagName == ('HTML'||'BODY'))) {    
  23.                 return calculatedOffset;
  24.             } else {
  25.                 return calculateOffset_Y(element.offsetParent);
  26.             }
  27.         } else {
  28.             return calculatedOffset;
  29.         }
  30.     }
  31. }
  32.  
  33. function isOverlapped(element,minOffset) {
  34.     var overlapped;
  35.     if (element && minOffset) {
  36.         var offset = getElementOffset(element);
  37.         overlapped = ((offset < minOffset) ? true : false);
  38.     }
  39.     return overlapped;
  40. }
  41.  
  42. function checkIsFlash(element) {
  43.     var isFlash;
  44.     var type = element.type.toLowerCase();
  45.     var codebase = element.getAttribute('codebase');            
  46.     if ((type.indexOf('flash') != -1) || 
  47.             (codebase.indexOf('swflash.cab') != -1)) {
  48.         isFlash = true;
  49.     } else {
  50.         isFlash = false;
  51.     }
  52.     return isFlash;
  53. }
  54.  
  55. function setVisibilityOnOverlap(elements,minOffset) {
  56.     if (elements && minOffset) {
  57.         for (var i=0; i < elements.length; i++) {
  58.             var currentElement = elements[i];
  59.             if ((checkIsFlash(currentElement) == false) && 
  60.                     (isOverlapped(currentElement,minOffset) == true)) {
  61.                 currentElement.style.visibility = 'hidden';
  62.             } else {
  63.                 currentElement.style.visibility = 'visible';
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70. function setFormVisibilityOnOverlap(minOffset) {
  71.     if (minOffset != null) {
  72.         for (var i=0; i < document.forms.length; i++) {
  73.             var currentForm = document.forms[i];
  74.             for (var j=0; j < currentForm.elements.length; j++) {
  75.                 var currentElement = currentForm.elements[j];
  76.                 if (isOverlapped(currentElement,minOffset) == true) {
  77.                     if (currentElement.type.indexOf('select') != -1) {
  78.                         currentElement.style.visibility = 'hidden';
  79.                     } else {
  80.                         currentElement.disabled = true;
  81.                     }
  82.                 } else {
  83.                     currentElement.style.visibility = 'visible';
  84.                     currentElement.disabled = false;
  85.                 }
  86.             } 
  87.         }
  88.     }
  89. }
  90.  
  91. function checkElementOverlap(minOffset) {
  92.     var docObjects = document.getElementsByTagName('object');
  93.     setVisibilityOnOverlap(docObjects,minOffset);
  94.     setVisibilityOnOverlap(document.embeds,minOffset);
  95.     setFormVisibilityOnOverlap(minOffset);
  96. }
  97.  
  98. function setSWFHeight(h) {
  99.     if (this.swf_object != null) {
  100.         this.swf_object.setAttribute('height',h);
  101.     }    
  102.     if (this.swf_embed != null) {
  103.         this.swf_embed.setAttribute('height',h);
  104.     }
  105. }
  106.  
  107. function expandNav(h) {
  108.     this.checkElements(h);
  109.     this.myDIV.style.height = h+'px';
  110.     this.setSWFHeight(h);
  111. }
  112.  
  113. function colapseNav(h) {
  114.     this.setSWFHeight(h)
  115.     this.myDIV.style.height = h+'px';
  116.     this.checkElements(h);
  117. }
  118.  
  119. function setHeight(h,overlap) {
  120.     if (h != null) {
  121.         if ((overlap == null) || (overlap == true)) {
  122.             (this.currentHeight < h) ? this.expandNav(h) : this.colapseNav(h);
  123.         } 
  124.         else if (overlap == false) {    
  125.             positionContent(h);
  126.             this.setSWFHeight(h);
  127.         }
  128.         this.currentHeight = h;
  129.     }
  130. }
  131.  
  132. GlobalNav = function(swf,height) {
  133.     /* properties */
  134.     this.swf = swf;
  135.     this.swf_object = document.getElementById('mmglobalnav_object');
  136.     this.swf_embed = document.getElementById('mmglobalnav_embed');
  137.     this.myDIV = document.getElementById('globalnav');
  138.     this.currentHeight = (height ? height : this.swf_object.height);
  139.     /* methods */
  140.     this.checkElements = checkElementOverlap;
  141.     this.setSWFHeight = setSWFHeight;
  142.     this.setHeight = setHeight;
  143.     this.colapseNav = colapseNav;
  144.     this.expandNav = expandNav;
  145. }
  146.  
  147. // called by globalnav.swf 
  148. // sets the mediapref cookie
  149. function setFlash(flash_version_string) {
  150.     var pixelName = 'flash_pixel';
  151.     if (document[pixelName] && flash_version_string) {
  152.         document[pixelName].src = '/set_media_pref?'+flash_version_string;
  153.     }
  154. }
  155.  
  156. //
  157. var mmglobalnav;
  158. //
  159. function initGlobalNav(swf,h) {
  160.     if (document.getElementById('mmglobalnav_object') || 
  161.             document.getElementById('mmglobalnav_embed')) {
  162.         mmglobalnav = new GlobalNav(swf,h);
  163.         if (h == null) h = mmglobalnav.currentHeight;
  164.         window.setTimeout('mmglobalnav.setHeight('+h+',false)',200);
  165.     }
  166. }
  167.  
  168. function positionContent(y) {
  169.     if (y != null) {
  170.         var contentBox = document.getElementById('contentWrapper');
  171.         contentBox.style.top = y + 'px';
  172.     }
  173. }
  174.  
  175. function adjustForGlobalMessaging() {
  176.     if (document.getElementById('globalmessaging') && 
  177.             document.getElementById('globalmessage')) {
  178.         var message_gif = document.getElementById('globalmessage');
  179.         var y = 64 + message_gif.height + 8; // globalnav gifs + message gif + margin
  180.         positionContent(y);
  181.     }
  182. }
  183.  
  184. function defineFunctions() {
  185.     if (!document.getElementById) {
  186.         if (document.layers) document.getElementById = function() { return; }
  187.         if (document.all) document.getElementById = function(str) { return document.all[str]; }
  188.     }
  189. }
  190.  
  191. function initPage() {
  192.     defineFunctions();
  193.     initGlobalNav('globalnav.swf',64);// Flash version of globalnav only 
  194.     adjustForGlobalMessaging();                // GIF version of globalnav only
  195. }
  196.  
  197. /* END */