//
// outboundlink.js
//

OutboundLink = function()
{
	this.els = [];
	this.use_new_class = true;
	this.additional_classname = "outbound";
	this.use_filter_IMG = true;
	this.use_new_window = true;
	//
	this.onLoad(this,"init");
}
OutboundLink.prototype.limitById = function(id)
{
	this._id = id;
}
OutboundLink.prototype.limitByClassName = function(name)
{
	this._classname = name;
}
OutboundLink.prototype.useNewClassName - function(b)
{
	this.use_new_class = b;
}
OutboundLink.prototype.additionalClassName = function(name)
{
	this.additional_classname = name;
}
OutboundLink.prototype.useFilterIMG = function(b)
{
	this.use_filter_IMG = b;
}
OutboundLink.prototype.useNewWindow = function(b)
{
	this.use_new_window = b;
}
OutboundLink.prototype.onLoad = function(scope, func)
{
	if(window.addEventListener)
		window.addEventListener("load", function(e){ scope[func](e); }, false );
	else if(window.attachEvent)
		window.attachEvent("onload", function(e){ scope[func](e); });
}
OutboundLink.prototype.init = function()
{
	if(this._classname)
		this.els = this.getElementsByClassName(this._classname);
	else if(this._id)
		this.els.push(document.getElementById(this._id));
	else
		this.els.push(document.body);
	//
	var e = this.els;
	for(i=0,len=e.length; i<len; i++)
	{
		var a = e[i].getElementsByTagName("a");
		if(this.use_filter_IMG) a = this.filterIMG(a);
		//
		for(j=0,len2=a.length; j<len2; j++)
		{
			var el = a[j], href = el.href;
			if(!!(href.match(/^https?:.+/)))
			{
				if(this.use_new_class) this.setNewClass(el);
				if(this.use_new_window) this.setNewWindow(el);
				/*
				if(this.use_new_class)
				{
					//console.log(this.isIE());
					if(!this.isIE())
					{
						el.setAttribute("class",this.additional_classname);
					} else {
						el.setAttribute("className",this.additional_classname);
					}
				}
				if(this.use_new_window)
					el.setAttribute("onclick","window.open(this.href); return false;");
					*/
			}
		}
	}
}
OutboundLink.prototype.setNewClass = function(_el)
{
	if(!this.isIE())
	{
		_el.setAttribute("class",this.additional_classname);
	} else {
		_el.setAttribute("className",this.additional_classname);
	}
}
OutboundLink.prototype.setNewWindow = function(_el)
{
	if(!this.isIE())
	{
		_el.setAttribute("onclick","window.open(this.href); return false;");
	} else {
		_el.setAttribute("onclick",function(){ window.open(_el.href); return false;});
	}
}
OutboundLink.prototype.isIE = function()
{
	//console.log(navigator.userAgent.indexOf("MSIE")!=-1);
	if(navigator.userAgent.indexOf("MSIE")!=-1)
	{
		return true;
	} else {
		return false;
	}
}	
OutboundLink.prototype.filterIMG = function(nodelist)
{
	var e=[];
	for(var i=0,len=nodelist.length; i<len; i++)
	{
		var n = nodelist[i], tn = ""+n.firstChild.tagName;
		if(!(tn.match(/img/i))) e.push(n);
	}
	return e;
}
OutboundLink.prototype.getElementsByClassName = function(_classname)
{
	var e = [];
	var a = document.getElementsByTagName("*");
	for(var i=0,len=a.length; i<len; i++)
	{
		var n = a[i].className;
		if(n&&n==_classname) e.push(a[i]);
	}
	return e;
}