1 // IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
\r 
   2 // (c) 2008-2009 Angus Turnbull http://www.twinhelix.com
\r 
   4 // This is licensed under the GNU LGPL, version 2.1 or later.
\r 
   5 // For details, see: http://creativecommons.org/licenses/LGPL/2.1/
\r 
   7 var IEPNGFix = window.IEPNGFix || {};
\r 
   9 IEPNGFix.tileBG = function(elm, pngSrc, ready) {
\r 
  10         // Params: A reference to a DOM element, the PNG src file pathname, and a
\r 
  11         // hidden "ready-to-run" passed when called back after image preloading.
\r 
  13         var data = this.data[elm.uniqueID],
\r 
  14                 elmW = Math.max(elm.clientWidth, elm.scrollWidth),
\r 
  15                 elmH = Math.max(elm.clientHeight, elm.scrollHeight),
\r 
  16                 bgX = elm.currentStyle.backgroundPositionX,
\r 
  17                 bgY = elm.currentStyle.backgroundPositionY,
\r 
  18                 bgR = elm.currentStyle.backgroundRepeat;
\r 
  20         // Cache of DIVs created per element, and image preloader/data.
\r 
  30         var tiles = data.tiles,
\r 
  31                 pngW = tiles.img.width,
\r 
  32                 pngH = tiles.img.height;
\r 
  35                 if (!ready && pngSrc != tiles.src) {
\r 
  36                         // New image? Preload it with a callback to detect dimensions.
\r 
  37                         tiles.img.onload = function() {
\r 
  39                                 IEPNGFix.tileBG(elm, pngSrc, 1);
\r 
  41                         return tiles.img.src = pngSrc;
\r 
  45                 if (tiles.src) ready = 1;
\r 
  50         if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
\r 
  51                 bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
\r 
  55         // Convert English and percentage positions to pixels.
\r 
  66         x = pos[bgX] || bgX;
\r 
  67         y = pos[bgY] || bgY;
\r 
  68         if (pc = x.match(/(\d+)%/)) {
\r 
  69                 x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
\r 
  71         if (pc = y.match(/(\d+)%/)) {
\r 
  72                 y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
\r 
  77         // Handle backgroundRepeat.
\r 
  78         var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
\r 
  79                 repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
\r 
  82                 if (x > 0) x -= pngW;
\r 
  86                 if (y > 0) y -= pngH;
\r 
  90         this.hook.enabled = 0;
\r 
  91         if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
\r 
  92                 elm.style.position = 'relative';
\r 
  96                 maxX = repeatX ? elmW : x + 0.1,
\r 
  98                 maxY = repeatY ? elmH : y + 0.1,
\r 
 102         if (pngW && pngH) {
\r 
 103                 for (xPos = x; xPos < maxX; xPos += pngW) {
\r 
 104                         for (yPos = y; yPos < maxY; yPos += pngH) {
\r 
 106                                 if (!tiles.cache[count]) {
\r 
 107                                         tiles.cache[count] = document.createElement('div');
\r 
 110                                 var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
\r 
 111                                         clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
\r 
 112                                 d = tiles.cache[count];
\r 
 114                                 s.behavior = 'none';
\r 
 115                                 s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
\r 
 116                                 s.top = yPos + 'px';
\r 
 117                                 s.width = clipR + 'px';
\r 
 118                                 s.height = clipB + 'px';
\r 
 120                                         (yPos < 0 ? 0 - yPos : 0) + 'px,' +
\r 
 123                                         (xPos < 0 ? 0 - xPos : 0) + 'px)';
\r 
 124                                 s.display = 'block';
\r 
 126                                         s.position = 'absolute';
\r 
 128                                         if (elm.firstChild) {
\r 
 129                                                 elm.insertBefore(d, elm.firstChild);
\r 
 131                                                 elm.appendChild(d);
\r 
 134                                 this.fix(d, pngSrc, 0);
\r 
 139         while (count < tiles.cache.length) {
\r 
 140                 this.fix(tiles.cache[count], '', 0);
\r 
 141                 tiles.cache[count++].style.display = 'none';
\r 
 144         this.hook.enabled = 1;
\r 
 146         // Cache so updates are infrequent.
\r 
 157 IEPNGFix.update = function() {
\r 
 158         // Update all PNG backgrounds.
\r 
 159         for (var i in IEPNGFix.data) {
\r 
 160                 var t = IEPNGFix.data[i].tiles;
\r 
 161                 if (t && t.elm && t.src) {
\r 
 162                         IEPNGFix.tileBG(t.elm, t.src);
\r 
 166 IEPNGFix.update.timer = 0;
\r 
 168 if (window.attachEvent && !window.opera) {
\r 
 169         window.attachEvent('onresize', function() {
\r 
 170                 clearTimeout(IEPNGFix.update.timer);
\r 
 171                 IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
\r