
//To use this library, you have put in the html header tags with the following code 
//<script type="text/javascript" src="camaleon.js"></script>


var cookieName 		= "referrer";
var cookieValueToMatch	= "adwords";

var imageFind 		= new Array("title.jpg"); //Image to find
var imageReplace	= new Array("01604-864-333.jpg"); //Image to replace

var textFind		= new Array(); // Text to Find
var textReplace		= new Array(); //Text to Replace

function getUrlParameter(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ){
    	return "";
	}
	else{
	    return results[1];
	}
}

function compareCookie(){
	var oneYearExpiry		= 31536000000;
	var theDate 			= new Date();
	var oneYearLater 		= new Date( theDate.getTime() + oneYearExpiry );
	var expiryDate 			= oneYearLater.toGMTString();
	var urlParameterValue 	= getUrlParameter(cookieName);
	var cookie				= getCookie(cookieName);

	if(urlParameterValue) {
		setCookie( cookieName, urlParameterValue, expiryDate);
		if(urlParameterValue == cookieValueToMatch){
			replaceContent();
		}
	} else {
		if(cookie == cookieValueToMatch) {
			replaceContent();
		}
	}
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
    		c_start	= c_start + c_name.length+1;
    		c_end	= document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function setCookie(c_name,value,expiredays){

	document.cookie	= c_name+ "=" +escape(value)+";expires="+expiredays+";path=/;";
}

window.onload = function() {
	compareCookie();
}

function replaceContent(){
	img = document.getElementsByTagName("img")
	camaleonImageReplace(img);
	camaleonTextReplace();
}

function camaleonImageReplace(nodes){
	try{
		if(imageFind.length){
			for(var i = 0; i < nodes.length; i++){
				node		= nodes[i].src;
				nodePart	= node.split("/");
				imageSrc	= nodePart.pop()
				imagePath	= nodePart.join("/");
				for(var j = 0; j <= imageFind.length; j++ ){
					if( imageFind[j] == imageSrc ){
						nodes[i].src = imagePath +"/"+imageReplace[j];
						break;
					}
				}
			}
		}
	}catch(err){

	}
}
function camaleonTextReplace(){
	try{
		if(texFind.length){
			for(var j = 0; j <= textFind.length-1; j++){
				body 		= document.body
				nodeText	= body.innerHTML;
				matchStr 	= nodeText.indexOf(textFind[j]);
				if(matchStr > -1){
					newText 			= replaceTxt(nodeText,textFind[j],textReplace[j]);
					body.innerHTML 	= newText
				}
			}
		}	
	}catch(err){

	}
}

function replaceTxt(texto,s1,s2){
	return texto.split(s1).join(s2);
}




