var excludes = 'img,script,p';
var replacementText = [];

// Octal representations from http://www.pjb.com.au/comp/diacritics.html
// Spanish
replacementText['cliente']		 		= [ 'miembro'];
replacementText['de la compra'] 		= [ ''];
replacementText['lista de la '] 		= [ 'lista '];
replacementText[' de productos'] 		= [ ''];
replacementText['Productos: '] 			= [ "Art\355culos: "];
replacementText['Mostrar producto'] 	= [ 'Mostrar'];
replacementText['Mostrar categor\355a'] 	= [ 'Mostrar'];
replacementText[' producto '] 			= [ " art\355culo "];

// French
replacementText['client']		 		= [ 'membre'];
replacementText['d\'achats'] 			= [ ''];
replacementText[' de produit'] 		= [ ''];
replacementText['Produits: '] 			= [ 'Articles: '];
replacementText['Afficher le produit'] 	= [ 'Afficher'];
replacementText['Afficher la cat\351gorie'] 	= [ 'Afficher'];
replacementText[' produit '] 			= [ " article "];


// English
replacementText['This product is no longer available.'] = [ ''];
replacementText['customer']		 		= [ 'member'];
replacementText['shopping list'] 		= [ 'list'];
replacementText['Shopping list'] 		= [ 'List'];
replacementText['Product search'] 		= [ 'Search'];
replacementText['Display product'] 		= [ 'Display'];
replacementText['Display category'] 	= [ 'Display'];
replacementText['Products:'] 			= [ 'Items:'];
replacementText['products'] 			= [ 'items'];
replacementText[' product '] 			= [ ' item '];

addLoadListener(initEvents);

function findReplacementText(content) {
	var replaceTxt = content;
	var match = false;
	
	for (var oldText in replacementText) {
	//	alert(oldText);
	//	alert(replacementText[oldText]);
	
		if (!(replaceTxt.indexOf(oldText) == -1)) {
			match = true;
			var regEx	= new RegExp(oldText, "g");
			replaceTxt = replaceTxt.replace(regEx, replacementText[oldText]);
		}

	}

	if (match) {
		return replaceTxt;
	}
	else {
		return false;
	}
}

function replaceText(node) {
	if ( (excludes + ',').indexOf(node.tagName.toLowerCase() + ',') === -1) {
		if (node.value) {
			if (!(node.value.indexOf("shopping list") == -1)) {
				var replaceTxt = node.value.replace('shopping list', 'list');
				node.value = replaceTxt;
			}
			if (!(node.value.indexOf("Shopping list") == -1)) {
				var replaceTxt = node.value.replace('Shopping list', 'List');
				node.value = replaceTxt;
			}
		// 	if (!(node.value.indexOf(" de la compra") == -1)) {
// 				var regEx	= new RegExp(" de la compra", "g");
// 
// 				var replaceTxt = node.value.replace(regEx, '');
// 				node.value = replaceTxt;
// 			}
		}
		
		if (node.innerHTML) {	
			var newText =	findReplacementText(node.innerHTML);
			if (newText) {
				//alert(newText);
				node.innerHTML	= newText;
				return node;
			}
			
			// exceptions
		 	if ( !(node.innerHTML.indexOf('()') == -1) ) {
 				match = true;
 				node.innerHTML = node.innerHTML.replace(/\(\)/g, '');
 			}
			var tagName	= node.tagName.toLowerCase()
			if (! (tagName.indexOf('th') == -1) ) {
				if (! (node.innerHTML.indexOf('Producto') == -1) ) {
					match = true;
					node.innerHTML = node.innerHTML.replace(/Producto/g, "Art\355culo");
				}
				if (! (node.innerHTML.indexOf('Product') == -1) ) {
					match = true;
					node.innerHTML = node.innerHTML.replace(/Product/g, "Item");
				}
			}
			
			
			
		}
	}
	return node;
}

function walkTheDOM(node) {
    if (node.tagName) {
		node = replaceText(node);
		if (node) {
  			node = node.firstChild;
  		}
  	 while (node) {
      	  walkTheDOM(node);
       	 node = node.nextSibling;
    	}
    }
}

function initEvents() {
  attachEventListener(document.body, "load", walkTheDOM(document.body), false);
  return true;
}


function attachEventListener(target, eventType, functionRef, capture)
{
  // standards-based
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  // IE
  else if (typeof target.attachEvent != "undefined")
  {
    var functionString = eventType + functionRef;
    target["e" + functionString] = functionRef;
    
    target[functionString] = function(event)
    {
      if(typeof event == "undefined"){event = window.event}; target["e" + functionString](event);
    };
    
    target.attachEvent("on" + eventType, target[functionString]);
  }
  // older browsers
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}