/* -------------------------------------------------------  
log clicks
Michael Leikam leikam@yahoo.com 
mod: 2006,2007,2008

@todo:
	* check to see if there's already an onclick defined for a link before overwriting it
	* check for https and send to a redirect URL that's also https so that the user doesn't lose his lock
------------------------------------------------------- */

/* much leaner implementation of add_redirects*/
window.clicklog = new Object();
window.clicklog.enabled = false; // master switch
window.clicklog.enabled = true; // master switch
// config variables
window.clicklog.container = null; 
window.clicklog.highlightModifiedLinks = false;
window.clicklog.highlightColor = "#ffffaa";
window.clicklog.rd_url = null;  
window.clicklog.rd_url = 'http://www.cherokeeuniforms.com/rd'; // the rd script url


/* ------------------------------------------------------- functions */

function addRDlinks(){
	if (clicklog.enabled && clicklog.rd_url){
		var container = document;
		if(clicklog.container){
			var x = document.getElementById(clicklog.container)
			if(x){ container = x 
			}
		}
		var links = container.getElementsByTagName('a');
		for(var i=0;i<links.length;i++){ 
			if(clicklog.highlightModifiedLinks){ links[i].style.backgroundColor=clicklog.highlightColor; 
			}
			links[i].onclick = rewrite;
		}
	}
}
// intercept the click and rewrite the url as it's followed
// but never do mailto: links
function rewrite(){
	var ok = true
	var href= this.href
	if(	href.substring(0,7)=='mailto:'){ ok = false }
	if( href.substring(0,11)=='javascript:'){ ok = false }
	if( href.substring(0,11)=='JAVASCRIPT:'){ ok = false }
	if(ok){
		var to2 = escape(href)
		var fr2 = escape(document.location)
		this.href = clicklog.rd_url+'?from='+fr2+'&to='+to2; 
	}
}

function addEvent(obj, evType, fn, useCapture){
	// see http://www.scottandrew.com/weblog/articles/cbs-events
	// fails quietly on IE5/MAC
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
//	} else {alert("Handler could not be attached");
	}
}

if(document.getElementById && document.getElementsByTagName){
	addEvent(window,"load",addRDlinks,false)
}
