/*

	Filename: global.js

	Author: J Christopher Garcia
	
	Description: Global JavaScript file for entire site. Defines default window.onLoad function. Additional scripts should add to, not replace this function.

*/


document.domain = getDomainRoot( document.domain );

window.onload = appendEvent( openExternalLinksInNewWindow, window.onload );


function getDomainRoot( domain ) {

	var pattern = new RegExp( '([^\.]+\.[^\.]+)$', 'i' );

	var matches = domain.match( pattern );
		
	if ( matches.length == 2 ) {
	
		return matches[1];
		
	}
	
	return false;
	
} // end getDomainRoot


// function openExternalLinksInNewWindow( $links ) {
function openExternalLinksInNewWindow( node ) {
	/*
	
		This function causes all links that point to resources that are not hosted from the domain (or subdomain) that served this page to open in a new window.
		
		i.e:	<a href="yoursite.com/something"> : opens in the current window
				<a href="othersite.com/resource"> : opens in a new window
		
	
	*/
	
	if ( node == undefined ) { node = document.body; }
	
	links = node.getElementsByTagName('a');
	
	var regex = new RegExp( document.domain + '\/?$', 'i' );
		
	for ( i = 0; i < links.length; i++ ) {
	
		result = links[i].hostname.match( regex );
								
		if ( result == null ) {
		
			links[i].target = "_blank";
			
			/*
			
			NOTE: The following function works as well, and is "more correct" in that it does not rely on the target attribute (which has been depreciated) of the LINK object. I'm setting the target attribute only because Safari displays "Open ... in a new window" in the status bar when a user mouses over the link.
						
			$links[$i].onclick = function () {
			
				window.open( this.href );
				this.blur();
				return false;
			
			}
			
			*/
			
			
			if ( links[i].title != '' ) {
				
				links[i].title = links[i].title + ' (Opens in a new window)';
			
			}
			
			else {
			
				links[i].title =  'Link to ' + links[i].hostname + ' opens in a new window.';
			
			}
		
		}
		
	}
		
} // end openExternalLinksInNewWindow



// function: isParentOrSelf taken from s5 (http://meyerweb.com/s5)

/*
function isParentOrSelf( element, id ) {
	if ( element == null || element.nodeName=='body' ) return false;
	else if ( element.id == id ) return true;
	else return isParentOrSelf( element.parentNode, id );
}
*/

/*
function getChildElement( $targetElement, $parent ) {

	if ( $parent.childNodes != null ) {
	
		var $i;
	
		for ( $i = 0; $i < $parent.childNodes.length; $i++ ) {
		
			var $child = $parent.childNodes[$i];
			
			if ( ( $child.nodeType ==  1 ) && ( $child.tagName.toLowerCase() == $targetElement.toLowerCase() ) ) {
			
				return $child;
		
			}
		
			else if ( $child.childNodes != null ) {
		
				return getChildElement( $targetElement, $child );
		
			}
			
			else { return false; }
	
		}
		
	}
	
	return false;

} // end getChildElement
*/


function removeNodeById( nodeId ) {

	var node = document.getElementById( nodeId );

	if ( node && node.parentNode != null ) {
						
		return node.parentNode.removeChild( node );
		
	}
	
	return false;
	
} //  end removeNodeById


function getAnchorFromHref( href ) {

	return href.slice( href.indexOf('#') + 1 );
	
} // end getAnchorFromHref



function appendToClassName( addition, original ) {
	
	if ( original != null ) {
					
		return original + ' ' + addition;
					
	}
					
	else {
					
		return addition;
					
	}
	
} // end appendToClassName



function removeFromClassName( token, className ) {

	var pattern = new RegExp( '(\s?' + token + ')', 'i' );
	
	if ( matches = className.match( pattern ) ) {
						
		return className.replace( matches[0], "" );
					
	}
	
	return className;

} // end removeFromClassName



function loadImageIntoCache( url ) {
	
	img = new Image();
	img.src = url;
	
} // end loadImageIntoCache


function appendEvent( newEvent, oldEvent ) {
		
	if ( oldEvent && ( typeof( oldEvent ) == 'function' ) ) {
			
		return function() { oldEvent(); newEvent(); }
	
	}
	
	return newEvent;

} // end appendEvent


function sifrSetup() {

	if ( typeof sIFR == "function" ){
	
		// This is the preferred "named argument" syntax
		sIFR.replaceElement(named({sSelector:"body h1", sFlashSrc:"/swf/vag_rounded_bold.swf", sColor:"#333366", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", nPaddingTop:0, nPaddingBottom:0, sFlashVars:"textalign=left&offsetTop=0"}));
	
	}

}


window.onload = appendEvent( sifrSetup, window.onload );


/*

// -------------------- LOCAL NAV CODE --------------- //

function appendToFileName( $modifier, $url ) {
	
	var $upToExtension = $url.slice( 0, $url.lastIndexOf( '.' ) );
	
	var $extension = $url.slice( $url.lastIndexOf( '.' ), $url.length );
		
	return $upToExtension + $modifier + $extension;

} // end appendFileName



function removeFromFileName( $modifier, $url ) {

	var $upToModifier = $url.slice( 0, $url.lastIndexOf( $modifier ) );
	
	var $extension = $url.slice( $url.lastIndexOf( '.' ), $url.length );
		
	return $upToModifier + $extension;

} // end appendFileName


function intLocalNav() {
	
	var $localNav;
	
	if ( $localNav = document.getElementById('localNav') ) {
		
		for ( $i = document.links.length - 1; $i >= 0; $i-- ) {
						
			if ( isParentOrSelf( document.links[$i], 'localNav' ) ) {
			
				var $link = document.links[$i];
				
				// preload hover state image...
				var $image = getChildElement('img', $link );
				loadImageIntoCache( appendToFileName( '-hover', $image.src ) );			
				
				$link.onmouseover = function() { var $image = getChildElement('img', this ); if ( $image.src.indexOf('-hover') == -1 ) { $image.src = appendToFileName( '-hover', $image.src ); } }
				$link.onfocus = function() { var $image = getChildElement('img', this ); if ( $image.src.indexOf('-hover') == -1 ) { $image.src = appendToFileName( '-hover', $image.src ); } }
				
				$link.onmouseout = function() {  var $image = getChildElement('img', this ); if ( $image.src.indexOf('-hover') != -1 ) { $image.src = removeFromFileName( '-hover',$image.src ); } }
				$link.onblur = function() {  var $image = getChildElement('img', this ); if ( $image.src.indexOf('-hover') != -1 ) { $image.src = removeFromFileName( '-hover',$image.src ); } }
				
			
			}
		
		}
		
	}
	

} // end intLocalNav

window.onload = appendEvent( intLocalNav, window.onload );


// -------------------- END: LOCAL NAV CODE --------------- //

*/