/**
  * This function works as browser-switch, ensuring that the
  * pngfix-functionality only is available for IE 6.x or lower.
  * If it is called inside those browsers, it will call
  * addPNGFix() three times to search the current document for
  * png-graphics. Wherever such graphics are used, the specific
  * CSS-Style "behavior" for IE will be added.
  * 
  * @param domain - the top-level domain needed to link the .htc
  */
function browserCheckPNGFix(domain) {
	var browser  = navigator.userAgent;
	var position = parseInt(browser.indexOf('MSIE'));
	var urlToHtc = domain + '/pngfix/iepngfix.htc';
	var version  = 0;

	if (position == -1) {
		return false;	
	} else {
		version = parseInt(browser.substr(position+5,1));
	}

	if(!window.opera && document.all && version <= 6) {
		 addPNGFix(urlToHtc, "img");
		 addPNGFix(urlToHtc, "div");
		 addPNGFix(urlToHtc, "input");
	}
}

/**
 * Using jQuery, this function gets an array of all elements of
 * a specific tag from the current Page. The style-property
 * "behavior" will be automatically set for all of those
 * elements using png's.
 *  
 * @param url - complete link to .htc file
 * @param tagName - name of the HMTL-tag to search page for
 */
function addPNGFix(url, tagName) {
	var bgImg = "none";
	var urlToHtc = url;
	
	$(tagName).each(function() {
		if (!$(this).hasClass ("pngfix")) {
			if(tagName == "div") {
				bgImg = $(this).css("background-image");
			} else if(tagName == "img" || tagName == "input") {
				if($(this).attr("src") != undefined) {
					bgImg = $(this).attr("src");
				}
			}
			
			if(bgImg.indexOf(".png") > 0) {
				if ($(this).css("behavior") == "") {
					$(this).css("behavior", "url(" + urlToHtc + ")");
				}
			}
		}
	});
}
