/* -----------------------------------------------------------------------
	Client:		AfricaTours Inc.
	Title:		Shared scripts
	Author:		Jason Garber, jason@sixtwothree.org
	Copyright:	AfricaTours Inc., http://www.africasafaris.com
	Created:	20 May 2006
------------------------------------------------------------------------- */


/*
addLoadEvent : Runs a specified function when DOM is loaded

@param {String} func : The name of the function to be called

Source : <http://simon.incutio.com/archive/2004/05/26/addLoadEvent>
*/
function addLoadEvent( func ) {
	var oldonload = window.onload;
	if ( typeof window.onload != "function" ) {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


/*
onDomReady : Runs a specified function when DOM is loaded

@param {String} func : The name of the function to be called
	You may also include parameters to be passed to the function e.g. onDomReady(myFunction(param1,param2));
	This can also be a function literal as in onDomReady(function() { more code to run on page load });

Source : brothercake's domFunction <http://www.brothercake.com/site/resources/scripts/domready/>
	and Dave Rolsky's DOM.Ready <http://www.openjsan.org/doc/a/au/autarch/DOM/Ready/0.14/lib/DOM/Ready.html>
*/
function onDomReady( func ) {
	var t = setInterval( function() {
		if ( typeof document.getElementsByTagName != "undefined" && typeof document.getElementById != "undefined" && ( document.getElementsByTagName( "body" )[0] != null || document.body != null ) ) {
			if ( typeof func == "function" ) {
				func(); clearInterval(t);
			}
		}
	}, 250 );
}


/*
activateActiveX : Activates ActiveX content for Internet Explorer 6.0+ to avoid requiring a user to click before using an object

Source : <http://sixtwothree.org/blog/archives/2006/05/20/activateactivex-11/>
*/
function activateActiveX() {
	if ( !document.getElementsByTagName || !document.body.outerHTML || !document.compatMode ) return false;
	var elems = new Array( "object", "applet" );
	for ( i = 0, j = elems.length; i < j; i++ ) {
		var objects = document.getElementsByTagName(elems[i]);
		for ( k = 0, l = objects.length; k < l; k++ ) {
			var params = "";
			for ( m = 0, n = objects[k].childNodes.length; m < n; m++ ) {
				params += objects[k].childNodes[m].outerHTML;
			}
			objects[k].outerHTML = objects[k].outerHTML.replace( "</" + elems[i].toUpperCase() + ">", params + "</" + elems[i].toUpperCase() + ">" );
		}
	}
}
onDomReady( activateActiveX );


/*
sfHover : Son of Suckerfish Dropdowns allows for list-based dropdown navigation for Internet Explorer

Source : <http://www.htmldog.com/articles/suckerfish/dropdowns/>
*/
function sfHover() {
	if ( !window.attachEvent ) return false;
	var sfEls = document.getElementById( "nav-primary" ).getElementsByTagName( "li" );
	for ( i = 0, j = sfEls.length; i < j; i++ ) {
		sfEls[i].onmouseover = function() {
			this.className += " sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className = this.className.replace( new RegExp( " sfhover\\b" ), "" );
		}
	}
}
addLoadEvent( sfHover );


/*
displayCitations : grab cite attribute for blockquotes and display as a right-aligned paragraph
*/
function displayCitations() {
	var content = document.getElementById( "content" );
	var bquotes = content.getElementsByTagName( "blockquote" );
	for ( i = 0, j = bquotes.length; i < j; i++ ) {
		if ( bquotes[i].getAttribute( "cite" ) ) {
			var cite = bquotes[i].getAttribute( "cite" );
			var para = document.createElement( "p" );
			para.className = "author";
			var text = document.createTextNode( cite );
			para.appendChild( text );
			bquotes[i].appendChild( para );
		}
	}
}
addLoadEvent( displayCitations );