/**
* Setup replacement
*/
function imageReplace() {
	replaceItems = new Array("h1","h2","h3","h6");
//	urlImageTool = "";
//	if( location.href.substring(7,12) == "local" || location.href.substring(7,10)=="dev" || location.href.substring(7,10)=="web" ){
		urlImageTool = "http://www.mylaps.com/";
//	}
	
	// List 
	imageRelations = new Array();
	imageRelations["h1"] = "textimage/headerDark/";
	
	imageRelations["h2"] = "textimage/headerDark/";
	imageRelations["h2_blockText"] = "textimage/headerDarkBold/";
	imageRelations["h2_blockContent"] = "textimage/headerInvert/";
	
	imageRelations["h3"] = "textimage/shadowBoxBigDark/";
	//imageRelations["h4"] = "textimage/headerDark/";
	imageRelations["h5"] = "textimage/headerDark/";
	imageRelations["h6"] = "textimage/headerDark/";
}

/**
* Replacement logic
*/
imageReplace.prototype = {
	/**
	* Start the replacement
	*/
	execute: function() {
	
		for(var i = 0; i < replaceItems.length; i++) {
			
			$(replaceItems[i]).each(function(){
				
				// when there is already an image, we don't have to create an other image
				if( $(this).children("img").length )
					return;
				
				// when it has a link, put the image in the link
				if( $(this).children("a").length ){
		
					var text = $(this).children("a").html();
					$(this).children("a").html('<img src="'+ imageUrl(text, $(this) )+'" alt="'+text+'" />');
					
				}
				else{
					var text = $(this).html();
					$(this).html('<img src="'+imageUrl(text, $(this) )+'" alt="'+text+'" />');
					
				}
			});
		}
	},
	
	/**
	* Checks if there is already an image in the header element
	*/
	haveChildImage: function(headerObject) {
		if(headerObject.childNodes.length > 0) {
			for(var i = 0; i < headerObject.childNodes.length; i++) {
				var nodeName = headerObject.childNodes[i].nodeName;
				if(nodeName.toLowerCase() == "img") {
					return true;
				}
			}
		}
		return false;
	}
}

/**
* Returns url for imageLocation
*/
function imageUrl(content, headerObject) {
	var headerObjectName = headerObject.get(0).nodeName;
	return urlImageTool + getImageType(headerObject) + headerObjectName.toLowerCase() + "/" + TestEncoding(content) + ".gif";
}

/**
* Returns the url of the current header element
*/
function getImageType(headerObject) {
	var header = headerObject.get(0).nodeName;
	
	if(typeof(imageRelations[header.toLowerCase() + "_" + headerObject.parent().attr('class')]) != "undefined" && imageRelations[header.toLowerCase() + "_" + headerObject.parent().attr('class')] != null) {
		return imageRelations[header.toLowerCase() + "_" + headerObject.parent().attr('class')];
	}
	
	return imageRelations[header.toLowerCase()];
}

/**
* Get parent classname to select "special" images
*/
function getParentCssClassName(headerParentNode) {
	return headerParentNode.className;
}


/**
* This event is starting after the page is loaded
*/
function imageReplaceLoader() {
	var replaceLoaderObject = new imageReplace();
	replaceLoaderObject.execute();
}

/**
* Start the replacement on the onload of a page
*/
if(typeof(window.attachEvent) != "undefined" && window.attachEvent != null) {
	window.attachEvent("onload", imageReplaceLoader);
} else {
	window.onload = imageReplaceLoader;
}

/**
* Encodes the image name
*/
function TestEncoding( inputString ){                   
  var encodeString=encodeURIComponent(inputString);
  return encodeURIComponent(encodeString);
}
