var links = document.getElementsByTagName("A");

function SearchKeywords(e)
{
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	if(typeof(targ.src) != 'undefined'){
		if(targ.src.match(/img\.youtube\.com/i)){
			if((words = targ.parentNode.innerHTML.match(/<br>(.*)$/i)) ||
				(words = targ.parentNode.parentNode.innerHTML.match(/<br>\s+<a\shref="http:\/\/youtube\.com.*?>(.*?)<\/a>\s*$/))){
					keywords = words[1].replace(/[^\w\s]/g, '');
					keywords = keywords.replace(/\s+/g, '+');
					keywords = keywords.toLowerCase();
			}
		} // else if(targ.src.match(/.*\.flickr\.com.*/)){
		//	keywords = targ.title.replace(/[^\w\s]/g, '');
		//	keywords = keywords.replace(/\s+/g, '+');
		//	keywords = keywords.toLowerCase();
		//} 
	} else if(typeof(targ.href) != 'undefined'){
		if((words = targ.href.match(/www\.articlesfactory\.com.*\/(.*)\.html/i)) ||
			(words = targ.href.match(/www\.myarticlemall\.com.*\/(.*)\//i))) {
			keywords = words[1].replace(/-+/g, '+');
		} else if(targ.href.match(/www\.youtube\.com/i)){
			if((words = targ.innerHTML.match(/<br>(.*)$/i)) ||
				(words = targ.parentNode.innerHTML.match(/<br>\s+<a\shref="http:\/\/youtube\.com.*?>(.*?)<\/a>\s*$/))){
					keywords = words[1].replace(/[^\w\s]/g, '');
					keywords = keywords.replace(/\s+/g, '+');
					keywords = keywords.toLowerCase();
			}
		} else if(targ.parentNode.className == 'prodtitle'){ // Products links
			keywords = targ.innerHTML.replace(/[^\w\s]/g, '');
			keywords = keywords.replace(/\s+/g, '+');
			keywords = keywords.toLowerCase();
		}
	}
	
	if(typeof(keywords) != 'undefined'){
		var url = location.href.match(/(.*\/)/);
		url[1] += keywords;
		location.href = url[1];
	}
}

function changeLinks(){
	var host = location.host;
	for(i = 0; i < links.length; i++){
		if(links[i].href.toLowerCase().indexOf(host) != -1 || links[i][0] == '/') continue;
		links[i].onclick = SearchKeywords;
	}
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
  var anchor = anchors[i];
  if (anchor.getAttribute("href") &&
  anchor.getAttribute("rel") == "external")
  anchor.target = "_blank";
 }
}

function DoOnLoad()
{
	// if(document.s) document.s.q.focus();
	changeLinks();
    externalLinks();
}

