home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 July / PCpro_2004_07.ISO / docs / amset / printer-friendly.cfm_files / thing_003.js < prev    next >
Encoding:
Text File  |  2003-12-21  |  3.3 KB  |  121 lines

  1. init( "Flip.init()" );
  2. var IMAGE_OFF_SUFFIX = "_off";
  3. var IMAGE_ON_SUFFIX = "_on";
  4.  
  5. /********************************************************************************
  6.  *
  7.  *    thing.flip.js
  8.  *    
  9.  *    usage : 
  10.  *        Change OFF_SUFFIX and ON_SUFFIX to match image names.
  11.  *        
  12.  *        For each image which is to be flipped, document.onload should call
  13.  *            flip.myFlipName = new Flip( myImageName, myImageSource )
  14.  *        To turn image on, call 
  15.  *            flip.myFlipName.on()
  16.  *        To turn image off, call
  17.  *            flip.myFlipName.off()
  18.  *
  19.  ********************************************************************************/
  20.  
  21. var flip = new Array();
  22. function Flip( imgName, imgSrc, doc ){
  23.     this.docImg = getDocImg(imgName, doc);
  24.     this.imgOff = new Image( this.docImg.width, this.docImg.height );
  25.     this.imgOffsrc = this.imgOff.src = this.docImg.src;
  26.     
  27.     this.imgOn = new Image ( this.docImg.width, this.docImg.height );
  28.     this.imgOnsrc = this.imgOn.src = ( imgSrc != null ) ? imgSrc : this.docImg.src.replace( IMAGE_OFF_SUFFIX, IMAGE_ON_SUFFIX );
  29. }
  30.  
  31. Flip.prototype.on = function(force){
  32.     if( force ){
  33.         this.imgOn.src = "";
  34.         this.docImg.src = this.imgOnsrc;
  35.     }
  36.     else if( this.imgOn.src ){
  37.             this.docImg.src = this.imgOn.src;
  38.     }
  39.     if ( this.status )
  40.         window.status = this.status;
  41.     return true;
  42. }
  43.  
  44. Flip.prototype.off = function(force){
  45.     if( force ){
  46.         this.imgOff.src = "";
  47.         this.docImg.src = this.imgOffsrc;
  48.     }
  49.     else if( this.imgOff.src ){
  50.         this.docImg.src = this.imgOff.src;
  51.     }
  52.     window.status = "";
  53.     return true;
  54. }
  55. Flip.prototype.killFlip = function( newSrc ){
  56.     var fixedImgSrc;
  57.     if ( newSrc == "on" )
  58.         fixedImgSrc = this.imgOn.src;
  59.     else if ( newSrc == "off" )
  60.         fixedImgSrc = this.imgOff.src;
  61.     else
  62.         fixedImgSrc = newSrc;
  63.     this.imgOff.src = this.imgOn.src = this.docImg.src = fixedImgSrc;
  64. }
  65. Flip.prototype.resurect = function(){
  66.     this.imgOff.src = this.imgOffsrc;
  67.     this.imgOn.src = this.imgOnsrc;
  68.     this.docImg.src = this.imgOffsrc;
  69. }
  70. //  function getDocImg( name )
  71. //  - searches recursively through document.layers for the named image
  72. //  - returns the named element in document.images
  73. function getDocImg(name, d){
  74.     d = ( d == null ) ? document : d; //set d to be the document if empty
  75.     var img = d.images[name];
  76.     if (img) return img; //found it
  77.     
  78.     if ( ! document.layers ) return null; //in ie, we die here
  79.     
  80.     for ( var i=0; i < d.layers.length; i++ ) 
  81.         if ( d.layers[i].id ){
  82.             img = getDocImg( name, d.layers[i].document );  //recursive call
  83.                 if (img) return img; //found it
  84.         }
  85.     return null; //did not find it
  86. }
  87.  
  88. //  function mouseover(name)
  89. //    - turn on flip[name] if it exists
  90. function mouseover(name){
  91.     if (flip[name])
  92.         flip[name].on();
  93. }
  94.  
  95. //  function mouseover(name)
  96. //    - turn off flip[name] if it exists
  97. function mouseout(name){
  98.     if (flip[name])
  99.         flip[name].off();
  100. }
  101.  
  102. // function getAllImages( d )
  103. //  - searches through document for images
  104. //  - recurses through layers
  105.  
  106. Flip.init = function( d ){
  107.     if( d == null ) d = document;
  108.     for ( var i = 0; i < d.images.length; i++ ){
  109.         var src = d.images[i].src;
  110.         if ( d.images[i].name ){
  111.             if ( src.indexOf( IMAGE_OFF_SUFFIX ) != -1 ){
  112.                 flip[ d.images[i].name ] = new Flip( d.images[i].name, null, d );
  113.             }
  114.         }
  115.     }
  116.     if ( !document.layers ) return;
  117.     for ( var i=0; i < d.layers.length; i++ ) 
  118.         Flip.init( d.layers[i].document );  //recursive call
  119. }
  120.  
  121.