
/*

	THIS CODE IS COMPILED FROM DIFFERENT PNG HACKS I'VE FOUND ALL OVER THE WEB.
	I DON'T OWN IT, ONLY ORGANIZED IT AND COMMENTED IT, IT'S PRETTY MUCH COMMUNITY CODE AT THIS POINT.

*/


/*
	THIS FUNCTION WILL FIND ALL THE IMG AND INPUT IMG FILES IN THE SOURCE
	IT THEN CHECKS IF THE "PNGFIX" ATTRIBUTE IS SET TO "TRUE", AND IF SO IT WILL FIX THE PNGS.
	
	TO FIX THE PNGS, IT SETS CSS PROPERTIES, AND THEN CREATES A DIV TO OVERLAY THE PNG WITH THOSE 
	CSS PROPERTIES AND THE SOURCE OF THE IMAGE IT IS OVERLAPPING.  THE TRICK HERE IS THAT THE 
	DIV CAN DO ALPHA RENDERING USING MICROSOFT'S ALPHAIMAGELOADER OBJECT
*/
function pngfix_start(){
	// quit if not IE browser
	if(!document.all || window.opera) return;
	// get IE version, if below 5.5 or at 7, quit since it's not supported or native.
	var version = parseFloat(navigator.appVersion.match(/MSIE (\d.*)/)[1]);
	if( (version < 5.5) || (version >= 7) ) return;
	// create a style document on the body of the page if one does not exist.
	if(document.styleSheets.length==0)
		document.body.appendChild(document.createElement("style"));
	// add the alpha rendering, etc rules to the .pngfix class in the first CSS stylesheet
	document.styleSheets[document.styleSheets.length-1].addRule(".pngfix",'position:absolute;z-index:expression(this.nextSibling.currentStyle.zIndex);top:expression(this.nextSibling.offsetTop);left:expression(this.nextSibling.offsetLeft);height:expression(this.nextSibling.offsetHeight);width:expression(this.nextSibling.offsetWidth);filter:expression("progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+this.nextSibling.src.replace(/\'/g,"%27")+"\')");');
	// create variables
	var fixme = [];
	var i,c,e,f;
	// get all image tags, add them to the fixme[] array
	var imgs = document.getElementsByTagName("img");
	for(i=0; i<imgs.length; i++){
		fixme[fixme.length] = imgs[i];
	}
	// get all inputs of type img, add them to the fixme[] array
	var inputs = document.getElementsByTagName("input");
	for(i=0; i<inputs.length; i++){
		if(inputs[i].type && inputs[i].type.match(/^image$/i)){
			fixme[fixme.length] = inputs[i];
		}
	}
	// loop through the fixme[] array, if they ahve pngfix attribute, fix them
	for(i=0; i<fixme.length; i++){
		if((fixme[i].getAttribute("pngfix"))=="true") pngfix(fixme[i]);
	}
}




/*
	THIS IS CALLED BY THE PNGFIX_START() FUNCTION, AND WILL BE CALLED 
	FOR EVERY IMG THAT NEEDS TO BE FIXED (HAS THE PNGFIX=TRUE ATTRIBUTE).
	
	THIS FUNCTION ACTUALLY CREATES THE DIV AND SETS THE CSS CLASS WITH THE 
	PROPERTIES TO FIX THE PNG.  YOU CAN CALL THIS ONE DIRECTLY FROM YOUR OWN JS IF YOU WANT TOO
	AS LONG AS PNGFIX_START() WAS CALLED ONCE BEFORE SINCE IT CREATES THE CSS PROPERTIES THIS FUNCTION USES.
*/
function pngfix(obj){
		// set filter type to alpha for this object
		obj.style.filter = "alpha(opacity=0)";
		// create a div on the document
		var tmp = document.createElement("div");
		// set class of the div we created to the pngfix class
		tmp.className="pngfix";
		// set position of the object to relative (in case it's not already)
		obj.style.position = "relative";
		// insert the div before the png image. The pngfix class will then take all properties from the png.
		// basically the div overlaps the png and does the alpha rendering on itself with the image's source.
		obj.parentNode.insertBefore(tmp,obj);
		// clear out temporary div we created
		tmp = null;
}



// START THE PNGFIX_START() FUNCTION TO BEGIN FIXING ALL THE PNG IMAGE FILES!
pngfix_start();



