/////////////////////////////////////////////////////////////////////////
//      _
//    <' )_,  RealDecoy
//    (    ) 
//   ~~~~~~~~
//
//	SETTING PNG BACKGROUND IN CROSS BROWSERS
//
//	Description:
//	This script allows IE to find all obejcts with a PNG as a background images
//	and replaces it with the alpha filter in IE.  This does this by parsing the
//	entire DOM tree to find all objects with the 'png' class, reading the location
//	of the background image, setting the background image to nothing, then
//	assigning the background filter and including the location of the initial
//	PNG in the filter declaration.
//
//	This script assumes:
//		* All of the objects have the class set to 'png'
//		* There is otherwise proper CSS coding
//	
//	Works in:
//		* IE6
//		* IE5.5
//
//	It is also compatible with the following, but only because this script does
//	not run on these browsers since they can handle PNGs' alpha transparency.
//		* Firefox (Mac & PC)
//		* Opera 9 (Mac & PC)
//		* Safari
//
//	This script was inspired by "Opacity Displayer, Version 1.0" developed by Michael
//	Lovitt and owes thanks to the getElementsByClass function found on
//	http://www.dustindiaz.com/.
//
/////////////////////////////////////////////////////////////////////////

var classElements = new Array();
	
function getElementsByClass(searchClass) {
	var classElements = new Array();
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	
	//	Check for IE6 or less than IE6.  The method below is faster and
	//	more effecient, so it is worth it to have two methods to do this
	//	since the seconds methods is slower.
	if(document.getElementsByTagName('*').length) {
		tag = '*';
		var els = document.getElementsByTagName(tag);
		var elsLen = els.length;
		for (i = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements.push(els[i]);
			}
		}
		
	//	If we are using below IE6, which does not deal well with wildcards,
	//	we need to use the document.all array.
	} else {
		var j = 0;
		for(i = 0; i < document.all.length; i++) {
			if( pattern.test(document.all[i].className)) {
								
				//	Can't use .push here since IE5.5 does not support it
				classElements[j] = document.all[i];
				j++;
			}
		}
	}	
	return classElements;
}

function initPNGs() {
//	Check to see if we need to make a change since it only applies to IE5.5 & IE6.0
	
	/*	WHEN IE7 IS FINAL, NEED TO REVISE THIS SCRIPT SINCE IT SHOULD NOT BE CALLED BECAUSE
		IE7 CAN INTERPRET ALPHA TRANSPARENCY IN PNG.	*/
	if (browser.isIE55up && browser.isWin32) {

//	Get all of the objects with the 'png' class
		var pngObjects = getElementsByClass('png');
	
		for (var a = 0; a < pngObjects.length; a++) {
			
			//	Does the element have a background image?
			if(pngObjects[a].currentStyle['backgroundImage'] != "none") {
			
				//	Now, grab the background images of these objects.  This syntax only works
				//	in IE, but that is all we need to worry about since non-IE handles PNGs fine.
				var tempImgURL = stripImgUrl(pngObjects[a].currentStyle['backgroundImage'], "png");
			
				//	Now, we need to change the background to nothing, otherwise the PNG will still stay
				//	in the background and look bad.
				pngObjects[a].style.background = "";

				//	Now, we finally set the filter to the PNG
				pngObjects[a].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + tempImgURL + ".png', sizingMethod='scale')";
				
			//	If the image is within an <A> tag, do NOT assign a background to it.  If a background is
			//	assigned, it is complex to assign mouseover events to it if there is to be rollover events
			//	for this image.  It is best to use CSS to control the background image filter.
			} else if (pngObjects[a].tagName.toUpperCase() == "IMG") {

				//	Grab some datea from the IMG
				var tempImgHeight = pngObjects[a].height;
				var tempImgWidth = pngObjects[a].width;
				var tempBkgdImgUrl = stripImgUrl(pngObjects[a].src, "png");
				
				//	Set the image to become a transparent GIF
				pngObjects[a].src = "/images/blank.gif";	// Will need to be set for where there is a transparent pixel
				
				pngObjects[a].style.width = tempImgWidth;
				pngObjects[a].style.height = tempImgHeight;
				
				if(pngObjects[a].parentNode.tagName != "A") {
					pngObjects[a].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + tempBkgdImgUrl + ".png', sizingMethod='scale')";
				}
			}
		}
	}
}

//	Grab the URL of the PNG's location from the root directory, but leaving off
//	the extension.  The reason is the valued returned has stuff on the end, so simpler
//	to just chop it off.
function stripImgUrl(imgURL, extension) {
	var tempURL;

	//	Chop off "http://" and ".png"
	tempURL = imgURL.substring(imgURL.indexOf("://") + 3, imgURL.indexOf("." + extension));
	
	//	Chop off domain
	tempURL = tempURL.substr(tempURL.indexOf("/"));

	
	return tempURL;
}
		
