// javascript document
/**
* dd_belatedpng: adds ie6 support: png images for css background-image and html .
* author: drew diller
* email: drew.diller@gmail.com
* url: http://www.dillerdesign.com/experiment/dd_belatedpng/
* version: 0.0.8a
* licensed under the mit license: http://dillerdesign.com/experiment/dd_belatedpng/#license
*
* example usage:
* dd_belatedpng.fix('.png_bg'); // argument is a css selector
* dd_belatedpng.fixpng( somenode ); // argument is an htmldomelement
**/
/*
please read:
absolutely everything in this script is silly. i know this. ie's rendering of certain pixels doesn't make sense, so neither does this code!
*/
var dd_belatedpng = {
ns: 'dd_belatedpng',
imgsize: {},
delay: 10,
nodesfixed: 0,
createvmlnamespace: function () { /* enable vml */
if (document.namespaces && !document.namespaces[this.ns]) {
document.namespaces.add(this.ns, 'urn:schemas-microsoft-com:vml');
}
},
createvmlstylesheet: function () { /* style vml, enable behaviors */
/*
just in case lots of other developers have added
lots of other stylesheets using document.createstylesheet
and hit the 31-limit mark, let's not use that method!
further reading: http://msdn.microsoft.com/en-us/library/ms531194(vs.85).aspx
*/
var screenstylesheet, printstylesheet;
screenstylesheet = document.createelement('style');
screenstylesheet.setattribute('media', 'screen');
document.documentelement.firstchild.insertbefore(screenstylesheet, document.documentelement.firstchild.firstchild);
if (screenstylesheet.stylesheet) {
screenstylesheet = screenstylesheet.stylesheet;
screenstylesheet.addrule(this.ns + '\\:*', '{behavior:url(#default#vml)}');
screenstylesheet.addrule(this.ns + '\\:shape', 'position:absolute;');
screenstylesheet.addrule('img.' + this.ns + '_sizefinder', 'behavior:none; border:none; position:absolute; z-index:-1; top:-10000px; visibility:hidden;'); /* large negative top value for avoiding vertical scrollbars for large images, suggested by james o'brien, http://www.thanatopsic.org/hendrik/ */
this.screenstylesheet = screenstylesheet;
/* add a print-media stylesheet, for preventing vml artifacts from showing up in print (including preview). */
/* thanks to r閙i pr関ost for automating this! */
printstylesheet = document.createelement('style');
printstylesheet.setattribute('media', 'print');
document.documentelement.firstchild.insertbefore(printstylesheet, document.documentelement.firstchild.firstchild);
printstylesheet = printstylesheet.stylesheet;
printstylesheet.addrule(this.ns + '\\:*', '{display: none !important;}');
printstylesheet.addrule('img.' + this.ns + '_sizefinder', '{display: none !important;}');
}
},
readpropertychange: function () {
var el, display, v;
el = event.srcelement;
if (!el.vmlinitiated) {
return;
}
if (event.propertyname.search('background') != -1 || event.propertyname.search('border') != -1) {
dd_belatedpng.applyvml(el);
}
if (event.propertyname == 'style.display') {
display = (el.currentstyle.display == 'none') ? 'none' : 'block';
for (v in el.vml) {
if (el.vml.hasownproperty(v)) {
el.vml[v].shape.style.display = display;
}
}
}
if (event.propertyname.search('filter') != -1) {
dd_belatedpng.vmlopacity(el);
}
},
vmlopacity: function (el) {
if (el.currentstyle.filter.search('lpha') != -1) {
var trans = el.currentstyle.filter;
trans = parseint(trans.substring(trans.lastindexof('=')+1, trans.lastindexof(')')), 10)/100;
el.vml.color.shape.style.filter = el.currentstyle.filter; /* complete guesswork */
el.vml.image.fill.opacity = trans; /* complete guesswork */
}
},
handlepseudohover: function (el) {
settimeout(function () { /* wouldn't work as intended without settimeout */
dd_belatedpng.applyvml(el);
}, 1);
},
/**
* this is the method to use in a document.
* @param {string} selector - required - a css selector, such as '#doc .container'
**/
fix: function (selector) {
if (this.screenstylesheet) {
var selectors, i;
selectors = selector.split(','); /* multiple selectors supported, no need for multiple calls to this anymore */
for (i=0; i size.h) {
c.b = size.h;
}
el.vml.image.shape.style.clip = 'rect('+c.t+'px '+(c.r+fudge)+'px '+c.b+'px '+(c.l+fudge)+'px)';
}
else {
el.vml.image.shape.style.clip = 'rect('+dc.t+'px '+dc.r+'px '+dc.b+'px '+dc.l+'px)';
}
},
figurepercentage: function (bg, size, axis, position) {
var horizontal, fraction;
fraction = true;
horizontal = (axis == 'x');
switch(position) {
case 'left':
case 'top':
bg[axis] = 0;
break;
case 'center':
bg[axis] = 0.5;
break;
case 'right':
case 'bottom':
bg[axis] = 1;
break;
default:
if (position.search('%') != -1) {
bg[axis] = parseint(position, 10) / 100;
}
else {
fraction = false;
}
}
bg[axis] = math.ceil( fraction ? ( (size[horizontal?'w': 'h'] * bg[axis]) - (size[horizontal?'w': 'h'] * bg[axis]) ) : parseint(position, 10) );
if (bg[axis] % 2 === 0) {
bg[axis]++;
}
return bg[axis];
},
fixpng: function (el) {
el.style.behavior = 'none';
var lib, els, nodestr, v, e;
if (el.nodename == 'body' || el.nodename == 'td' || el.nodename == 'tr') { /* elements not supported yet */
return;
}
el.isimg = false;
if (el.nodename == 'img') {
if(el.src.tolowercase().search(/\.png$/) != -1) {
el.isimg = true;
el.style.visibility = 'hidden';
}
else {
return;
}
}
else if (el.currentstyle.backgroundimage.tolowercase().search('.png') == -1) {
return;
}
lib = dd_belatedpng;
el.vml = {color: {}, image: {}};
els = {shape: {}, fill: {}};
for (v in el.vml) {
if (el.vml.hasownproperty(v)) {
for (e in els) {
if (els.hasownproperty(e)) {
nodestr = lib.ns + ':' + e;
el.vml[v][e] = document.createelement(nodestr);
}
}
el.vml[v].shape.stroked = false;
el.vml[v].shape.appendchild(el.vml[v].fill);
el.parentnode.insertbefore(el.vml[v].shape, el);
}
}
el.vml.image.shape.fillcolor = 'none'; /* don't show blank white shapeangle when waiting for image to load. */
el.vml.image.fill.type = 'tile'; /* makes image show up. */
el.vml.color.fill.on = false; /* actually going to apply vml element's style.backgroundcolor, so hide the whiteness. */
lib.attachhandlers(el);
lib.givelayout(el);
lib.givelayout(el.offsetparent);
el.vmlinitiated = true;
lib.applyvml(el); /* render! */
}
};
try {
document.execcommand("backgroundimagecache", false, true); /* tredosoft multiple ie doesn't like this, so try{} it */
} catch(r) {}
dd_belatedpng.createvmlnamespace();
dd_belatedpng.createvmlstylesheet();