/*******************************************************************************\
|	Name:		Portable Network Graphics Transform								|
|																				|
|	Filename:	png_transform.js												|
|	Version:	1.1.0															|		
|	Author:		Eric van Blokland												|
|	Project:	Shared library													|
|																				|
|	Copyright:	2006, Footsteps													|
|																				|
|	Summary:	Replaces png images with a DirectX Transform filter in Internet |
|				Explorer 5.5 and 6.x											|
|				Feel free to use this library, if you have suggestions for		|
|				possible additions, please contact us at:						|
|																				|
|				http://www.footsteps.nl											|
|																				|
\*******************************************************************************/

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );

function png_transform(sElementTypes,scale) {	
	if(is_ie5_5 || is_ie6) {	
		aImg=Array();
		aImg=window.document.getElementsByTagName('img');
		for(i=0;i<aImg.length;i++) {
			var sSrc=aImg[i].src.toLowerCase();
			if(sSrc.indexOf('.png') ==aImg[i].src.length-4 ) {
				oImg=aImg[i];				
				oImg.runtimeStyle.visibility='hidden';
				oSpan=document.createElement('span');				
				if(oImg.height) {
					oSpan.runtimeStyle.height=oImg.height+'px';
					oSpan.runtimeStyle.width=oImg.width+'px';
				}
				else if(oImg.currentStyle.height) {
					oSpan.runtimeStyle.height=oImg.currentStyle.height;
					oSpan.runtimeStyle.width=oImg.currentStyle.width;
				}
				if(!oImg.height && !oImg.currentStyle.height) {
					oImg.onresize=png_transform_this;
				}
				else {
					oSpan.runtimeStyle.display='inline-block';
					oSpan.runtimeStyle.position='static';
					oImg.runtimeStyle.display='none';
					oSpan.runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+oImg.src+"')";					
					oImg.insertAdjacentElement('beforeBegin', oSpan);
				}
								
			}
		}	
		aElementTypes=sElementTypes.split(';');
		for(x=0;x<aElementTypes.length;x++) {
			aElements=Array();
			aElements=window.document.getElementsByTagName(aElementTypes[x]);
			a=0;
			for(i=0;i<aElements.length;i++) {			
				a=i;
				if(aElements[i].currentStyle.backgroundImage && aElements[i].currentStyle.backgroundImage!='none') {
					var sSrc=aElements[i].currentStyle.backgroundImage.toLowerCase();										
					if(sSrc.indexOf('.png')>0) {
						oElement=aElements[i];													
						oElement.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+oElement.currentStyle.backgroundImage.substring(5,oElement.currentStyle.backgroundImage.length-2)+"'"+(scale ? ",sizingMethod='crop'" : "")+")";																	
						oElement.runtimeStyle.backgroundImage='none';	
					}
				}
			}
		}
	}
}
function png_transform_byid(id,scale) {
	if(is_ie5_5 || is_ie6) {
		oElement=document.getElementById(id);
		if(oElement && oElement.currentStyle.backgroundImage) {
			var sSrc=oElement.currentStyle.backgroundImage.toLowerCase();
			if(sSrc.indexOf('.png)') == oElement.currentStyle.backgroundImage.length-5 ) {			
				oElement.currentStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+oElement.currentStyle.backgroundImage.substring(4,oElement.currentStyle.backgroundImage.length-1)+"'"+(scale ? ",sizingMethod='scale'" : "")+")";					
				oElement.currentStyle.backgroundImage='';					
			}
		}
	}
}
function png_transform_byclassname(sElementTypes,sClassNames,scale) {
	if(is_ie5_5 || is_ie6) {
		aElementTypes=sElementTypes.split(';');
		for(x=0;x<aElementTypes.length;x++) {
			aElements=Array();
			aElements=window.document.getElementsByTagName(aElementTypes[x]);
			for(i=0;i<aElements.length;i++) {
				aClassNames=sClassNames.split(';');
				for(b=0;b<aClassNames.length;b++) {
					if(aElements[i].className==aClassNames[b] && aElements[i].currentStyle.backgroundImage) {
						var sSrc=aElements[i].currentStyle.backgroundImage.toLowerCase();
						if(sSrc.indexOf('.png)') == aElements[i].currentStyle.backgroundImage.length-5 ) {
							oElement=aElements[i];												
							oElement.currentStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+get_base_href()+oElement.currentStyle.backgroundImage.substring(4,oElement.currentStyle.backgroundImage.length-1)+"'"+(scale ? ",sizingMethod='scale'" : "")+")";					
							oElement.currentStyle.backgroundImage='';	
						}
					}					
				}
			}
		}
	}
}
function png_transform_this(oTarget,force) {
	oTarget.onresize=null;
	if(is_ie5_5 || is_ie6) {
		if(oTarget.src) {
			var sSrc=oTarget.src.toLowerCase();
			if(sSrc.indexOf('.png') ==oTarget.src.length-4 || force ) {
				oImg=oTarget;				
				oImg.style.visibility='hidden';
				oSpan=document.createElement('span');	
				if(oImg.height) {
					oSpan.style.height=oImg.height+'px';
					oSpan.style.width=oImg.width+'px';
				}
				else if(oImg.style.height) {
					oSpan.style.height=oImg.style.height;
					oSpan.style.width=oImg.style.width;
				}
				if(!oImg.height && !oImg.style.height) {			
					oImg.onresize=png_transform_oTarget;
				}
				else {					
					oSpan.style.display='inline-block';
					oSpan.style.position='relative';
					oImg.style.display='none';
					oSpan.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+oImg.src+"')";
					oSpan.className=oImg.className;
					//oImg.insertAdjacentElement('beforeBegin', oSpan);
					oImg.parentNode.replaceChild(oSpan,oImg);
				}
			}
		}
		else if(oTarget.currentStyle.backgroundImage) {
			var sSrc=oTarget.currentStyle.backgroundImage.toLowerCase();
			if(sSrc.indexOf('.png)') == oTarget.currentStyle.backgroundImage.length-5 ) {			
				oTarget.currentStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+get_base_href()+oElement.currentStyle.backgroundImage.substring(4,oElement.currentStyle.backgroundImage.length-1)+"'"+(scale ? ",sizingMethod='scale'" : "")+")";					
				oTarget.currentStyle.backgroundImage='';					
			}
		}
	}	
}
