/* *************************************************************
 *  externalTarget v1.0 - June 18, 2005
 *	Copyright 2005 - Steven Moberg - http://www.mobergmedia.com
 *
 *  DESCRIPTION:
 *		makes all external links open in a new browser window
 *
 *  USAGE:
 *		Run in SSL mode
 *			externalTarget.runWhenSSL = false; 
 *			(default == false)	
 *
 *		Ignore Domains
 *			externalTarget.ignoreDomain('myDomain.com');
 *			externalTarget.ignoreDomain('myDomain.net');
 *			externalTarget.ignoreDomain('www.myOtherDomain.com');
 *			(default == the current domain)
 *
 *		Ignore Partial Hrefs
 *			externalTarget.ignoreHref('mobergmedia.com/promo');
 *			externalTarget.ignoreHref('mobergmedia.com/login');
 *
 *		Allow Protocols
 *			externalTarget.allowProtocol('https');
 *			externalTarget.allowProtocol('ftp:');
 *			(defaul == 'http:')
 *
 *     	Enable debugging	
 *			externalTarget.debug = true;
 *			(default == false)
 *			
 *		Specify External Link Style
 *			externalTarget.className = 'external';
 *			(default == '')
 *
 *			<style type="text/css">
 *			a.external { color:green; font-size:12pt; }
 *			</style>
 ************************************************************* */


externalTarget = { 
	domains : [], 
	hrefs : [],
	protocols : ["http:"],
	className : '',
	runWhenSSL : false,
	debug : false,  
	ignoreDomain : function(domain){ 
		var thisDomain = domain.toLowerCase();
		this.domains.push(thisDomain);
		if(!/^www./.test(thisDomain))
			this.domains.push('www.'+thisDomain);				
	},	
	ignoreHref : function(href){ 
		this.hrefs.push(href.toLowerCase()); 
	},
	allowProtocol : function(protocol){
		if(!/\:$/.test(protocol))
			protocol+=":";
		this.protocols.push(protocol.toLowerCase());
	},									   
	init : function(){
		this.ignoreDomain(document.domain); // ignore current domain
		if(!this.runWhenSSL && /^https:/.test(document.URL.toLowerCase()))
			return false; // if NOT runOnSSL && SSL return
		
		var debug = "~~ EXTERNAL TARGET SETTINGS ~~";
		for(var i=0;i < this.domains.length; i++) 
			debug += '\nignore-DOMAIN\t: '+this.domains[i];
		for(var i=0;i < this.hrefs.length; i++) 
			debug += '\nignore-HREF\t: '+this.hrefs[i];
		for(var i=0;i < this.protocols.length; i++) 
			debug += '\nallow-PROTOCOL\t: '+this.protocols[i];
		debug += "\n\n~~ EXTERNAL TARGET RESULTS ~~";
		
	    for(var i = 0; i < document.links.length; i++) {
			if(document.links[i].hostname.length == 0 || document.links[i].target.length > 0)
				debug += '\nIGNORE-target\t: '+document.links[i].href;
			else if(this.domains.indexOf(document.links[i].hostname) > -1)
				debug += '\nIGNORE-domain\t: '+document.links[i].href;
			else if(this.hrefs.contains(document.links[i].href) > -1)
				debug += '\nIGNORE-href\t: '+document.links[i].href;
			else if(this.protocols.indexOf(document.links[i].protocol) == -1)
			    debug += '\nIGNORE-protocol\t: '+document.links[i].href;
			else {
				document.links[i].target = '_blank';
				if(this.className.length) document.links[i].className += " "+this.className;
		   		debug += '\nEXTERNAL-TARGET\t: '+document.links[i].href;
			}			
		}
		if(this.debug == true)
			alert(debug);
		debug = null;
			
		if(typeof this.onload == 'function')
			return this.onload();
	}	
}

if (window.addEventListener)
    window.addEventListener('load', function(){ externalTarget.init(); }, false);
else if (window.attachEvent)
    window.attachEvent("onload", function(){ externalTarget.init(); });
else {
	externalTarget.onload = window.onload;
	window.onload = function(){ externalTarget.init(); };	
}

if(typeof String.prototype.trim == 'undefined'){
	String.prototype.trim = function(){ return this.replace(/(^\s+)|\s+$/g,"") }
}
if(typeof Array.prototype.push == 'undefined'){
	String.prototype.push = function(val){ this[this.length] = val; return this.length-1; }
}
if(typeof Array.prototype.indexOf == 'undefined'){
	Array.prototype.indexOf =  function(val,nocase){
		nocase = (nocase == true)?true:false;
		if(nocase) val = val.toLowerCase();
		for(var i=0; i<this.length;i++)
			if(this[i] == val || (nocase && this[i].toLowerCase() == val)) return i;
		return -1;
	}
}
if(typeof Array.prototype.contains == 'undefined'){
	Array.prototype.contains =  function(val,nocase){
		var flags = (nocase == true)?"gi":"g";
		var regObj = null;
		for(var i=0; i<this.length;i++){
			if(new RegExp(this[i], flags).test(val)) { return i };
		}
		return -1;
	}
}