/*
 * SEO link handler
 * requires Loader.js
 */

// assign an event handler to all the elements specified by selector
function handleLinksPrototype(selector, handler, func) {
	var el = $$(selector);
	if(el.length) {
		el.each(function(e) {
			Element.observe(e, handler, func);
		});
	}	
}
function handleLinks(classN, handler, func) {
	var att=false,add=false,els,i,l,a,t;
	if("attachEvent" in window) {
		att = true;
	} else if("addEventListener" in window) {
		add = true;
	}		
	
	function onmouseover(t) {
		return function(e) {
			t.call(this, e);
			func.call(this, e);
		};
	}
	
	if("getElementsByClassName" in document) {
		els = document.getElementsByClassName(classN);			
	} else {
		els = document.getElementsByTagName("a");	
		for(i=0,l=els.length;i<l,a=els[i];i++){
			if(a.className.indexOf(classN) == -1) {
				els.splice(i,1);
			}		
		}
	}
	for(i=0,l=els.length;i<l,a=els[i];i++){
		if(att) {
			a.attachEvent("on"+handler, func);
		} else if(add) {
			a.addEventListener(handler, func, true);
		} else {
			if(t = a["on"+handler]) {
				a["on"+handler] = onmouseover(t);
			} else {
				a["on"+handler] = func;
			}
		}
	}
	els = null;
}

/* 
 * link handler functions
 */

// selector = sel. of the element containing the links
function  handleATagOverId(selector, idFilter) {
	if(!idFilter) {
		idFilter = identity;
	}
	var handler = function() {
		var el = this;
		if(window === el) {
			el = this.event.srcElement;
		}
		if(el.href && el.href.length > 0) return;
		el.href = idFilter(el.id);
	};
	var childHandler = function() {
		var el = this;
		if(window === el) {
			el = this.event.srcElement;
		}
		el = el.parentNode;
		// unscramble yo
		if (el.id) {
			if(el.href && el.href.length > 0) return;
			el.href = idFilter(el.id);
		}
	}
	//handleLinks(selector + " .scrambledLinkId", "mouseover", handler);
	handleLinks('scrambledLinkId', "mouseover", handler);
	//: handle img in a tags for ie
	handleImageInATagsForIE('scrambledLinkId', 'mouseover', childHandler);
}

//: handle img in a tags for ie
function handleImageInATagsForIE(css, event, handler)
{
	function onmouseover(t) {
		return function(e) {
			t.call(this, e);
			func.call(this, e);
		};
	}
	
	var str ="";
	var els = document.getElementsByClassName(css);
	for(i=0;i<els.length;i++){
		for(j=0;j<els[i].childNodes.length;j++){
			var child=els[i].childNodes[j];
			if (child.nodeName == 'IMG') {
				if("attachEvent" in window) {
					child.attachEvent("on"+event, handler);
				} else if("addEventListener" in window) {
					child.addEventListener(event, handler, true);
				} else {
					if(t = child["on"+event]) {
						child["on"+event] = onmouseover(t);
					} else {
						child["on"+event] = handler;
					}
				}
			}
			str += els[i].childNodes[j].nodeName + " ";
		}
	}
}

/*
 * attribute filters come here
 */
function identity(f) {
	return f;
}

function decodeId_1(id) {
	id = id.slice(1); // L
	id = id.replace(/:/g,'/');
	id = id.replace(/\.\.\._/g,":");
	id = id.replace(/^([a-zA-Z0-9.]+)\/\/(.*)/,'$1://$2');
	
	return id;
}


/*
 * activate mouseover stuff onload
 */
//document.observe('dom:loaded', function() {
Loader.addOnDOMload(function() {
	// there has to be one call for each $type used in php
	// for now, all links use the id attr
	/*handleATagOverId("#nav");
	handleATagOverId("#topBrands");
	handleATagOverId("#nav_vert");*/	
	handleATagOverId('body', decodeId_1);
});
Loader.addOnDOMload(function(){
	if(!window.brandTextIsThere) return;
	var b=document.getElementById('brandText');
	var a=document.getElementById('brandTextPlaceholder');
	if(a && b) {
		a.parentNode.replaceChild(b, a);		
		b.style.display = 'block';	
	}
});

