
/*
 Replacement for windows.onload.
 This fires after the page content (HTML), except images and other binary content, is loaded.
 Window’s onload event fires after all page content is loaded. 
 That includes images and other binary content.
*/
/* http://www.thefutureoftheweb.com/blog/adddomloadevent
 * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 * Special thanks to Dan Webb's domready.js Prototype extension
 * and Simon Willison's addLoadEvent
 *
 * For more info, see:
 * http://www.thefutureoftheweb.com/blog/adddomloadevent
 * http://dean.edwards.name/weblog/2006/06/again/
 * http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 * 
 *
 * To use: call addDOMLoadEvent one or more times with functions, ie:
 *
 *    function something() {
 *       // do something
 *    }
 *    addDOMLoadEvent(something);
 *
 *    addDOMLoadEvent(function() {
 *        // do other stuff
 *    });
 *
 */

addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;
            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already run, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }
        load_events.push(func);
    }
})();

//-------------------------------------------------------------------
function scrollPut() {
	setCookie("scrollPos",scrollIdY());
}
// Gets the "scrollPos" cookie and sets scrollTop
function scrollGet() {
	/* must use body onload or addDOMLoadEvent) instead of windows.onload
	for IE to set ScrollTop correctly (it must be done after HTML is loaded)
	*/
	var objDiv;
	var iCookie=Number(getCookie("scrollPos"));
	if (document.getElementById("content_main")) {
		var objDiv = document.getElementById("content_main");
		if (objDiv.scrollTop) {
			objDiv.scrollTop = iCookie;
		}
	}
/*
	don't retain scroll position cookie, it is set by an "external" link
	the scroll position shouldn't be restored with normal navigation
*/
	eraseCookie("scrollPos");
}

// determines current body Y scroll position in IE & FF
// http://www.west-wind.com/WebLog/posts/4607.aspx
function scrollBodyY() {
	var scrollTop = document.body.scrollTop;
	if (scrollTop == 0) {
	    if (window.pageYOffset)
	        scrollTop = window.pageYOffset;
	    else
	        scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return scrollTop;
}

// determines current id Y scroll position in IE & FF
function scrollIdY() {
	var scrollTop = document.getElementById("content_main").scrollTop;
	return scrollTop;
}
//====================================================================
function extLink(e) {
	scrollPut();
}

addDOMLoadEvent(externalLinks);
/*
 use window.onload instead of dDOMLoadEvent(scrollGet)
 to get IE to restore scroll correctly
*/
window.onload=scrollGet;

/* 	---------------------------------------------------------------------------
Flexible Javascript Events
http://ejohn.org/projects/flexible-javascript-events/

This library consists of two functions: addEvent and removeEvent. To use them, call it using these methods:

addEvent( object, eventType, function );

The 'object' parameter should be the object upon which you want the event to be called.
The 'eventType' parameter should hold the name of the event, for example: 'click', 'mouseover', 'load', 'submit', etc. More can be found here.
The 'function' parameter should be a reference to a function that you wish to have called whenever the event fires. One parameter will be passed to 'function' - the event object.

Some examples of addEvent in use:

addEvent( document.getElementById('foo'), 'click', doSomething );
addEvent( obj, 'mouseover', function(){ alert('hello!'); } );

removeEvent( object, eventType, function );

removeEvent is virtually identical to addEvent, with the obvious difference: Instead of the function being added to the event handler, it is removed instead. All of the above code and parameters applies to this function.

*/
	
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}
/* 	---------------------------------------------------------------------------
	XHTML 1.0 Strict recommendations of the W3C no longer include the 
 	target attribute of the <a> tag.
 	Replacement that is compliant from Kevin Yank at
	http://www.sitepoint.com/article/standards-compliant-world/

Modified to handle either rel="external" links which open a new window
and rel="internal" links which do not open a new window but return to 
the scroll position of the originating page.

Used addDOMLoadEvent instead of windows.onload
*/

function externalLinks() {
	var anchors = document.getElementsByTagName("a");
	if (!document.getElementsByTagName) return;
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href")) {
			sAnchorRel=anchor.getAttribute("rel");
			switch(sAnchorRel) {
				// same window but return to scroll position
				case "internal":
					addEvent( anchor, 'click', extLink );
					break;
				case "external":
					anchor.target = "_blank";
					break;
				default:
					break;
			}
		}
	}
}
/* 	---------------------------------------------------------------------------
	from phpBB functions to disable select-text script (IE4+, NS6+)
*/

/*
function disableselect(e) {	return false }
function reEnable() { return true }
function clickIE() 
{
	if (document.all) { (message);return false;	}
}
function clickNS(e) 
{
	if (document.layers||(document.getElementById&&!document.all)) 
	{
		if (e.which==2||e.which==3) { (message);return false; }
	}
}

*///Disable select-text script (IE4+, NS6+)
//if IE4+
/*
document.onselectstart=new Function ("return false")
*/
//if NS6
/*
if (window.sidebar)
{
	document.onmousedown=disableselect
	document.onclick=reEnable
}
//Disable right click script
var message="";
if	(document.layers)
	{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")

*/
/* 	---------------------------------------------------------------------------
	Cookie set, get, erase
*/
function setCookie(c_name,value,expiredays)	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name){
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    return unescape(document.cookie.substring(c_start,c_end));
	} 
}
return "";
}
function eraseCookie(c_name){
setCookie(c_name,"",-1);
}

/* ---------------------------------------------------------------
Generic Function to swap graphics
http://jehiah.cz/archive/simple-swap
<img id="refnext_img" src="./images/arrow_next.jpg" oversrc="./images/tmp.jpg" alt="" />
oversrc is graphic to be swapped in onmouseover
--------------------------------------------------------------- */

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};

addDOMLoadEvent(PreSimpleSwapOnload);
addDOMLoadEvent(SimpleSwapSetup);

/*
window.onload = function(){PreSimpleSwapOnload(); ArrowsRefSetup(); SimpleSwapSetup()}
*/
function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}
function SimpleSwap(el,which){
	el.src=el.getAttribute(which||"origsrc");
}
/* --------------------------------------------------------------- 
Function to simplify links for Back and Next Arrows

<div id="arrows" refback="" refnext="<?php echo($host_dir) ?>ascendedmasters1.php">
<?php include("./includes/arrows.php"); ?>

refback is Back link, refnext is Next link
if either is blank a blank image is substituted for original
--------------------------------------------------------------- */

addDOMLoadEvent(ArrowsRefSetup);

function ArrowsRefSetup () {
	var ref=document.getElementById("arrows");
    var refnext = ref.getAttribute("refnext");
	var next=refnext.toString();
	if (next=="") {
		var img=document.getElementById("refnext_img")
		document.getElementById("refnext").removeChild(img);
		var anc=document.getElementById("refnext")
		document.getElementById("arrow_next").removeChild(anc);
		document.getElementById("arrow_next").appendChild(img);
		document.getElementById("refnext_img").setAttribute("src", "images/arrow_blank.gif");
		document.getElementById("refnext_img").setAttribute("oversrc", "images/arrow_blank.gif");
		document.getElementById("refnext_img").setAttribute("origsrc", "images/arrow_blank.gif");
	}
	else {
		document.getElementById("refnext").setAttribute("href", next);
		var target=document.getElementById("arrows").getAttribute("tgtnext");
		if (target !== null) {
			  document.getElementById("refnext").setAttribute("target", target);
			  }
	}
    var refback = ref.getAttribute("refback");
	var back=refback.toString();
	if (back=="") {
		var img=document.getElementById("refback_img")
		document.getElementById("refback").removeChild(img);
		var anc=document.getElementById("refback")
		document.getElementById("arrow_back").removeChild(anc);
		document.getElementById("arrow_back").appendChild(img);
		document.getElementById("refback_img").setAttribute("src", "images/arrow_blank.gif");
		document.getElementById("refback_img").setAttribute("oversrc", "images/arrow_blank.gif");
		document.getElementById("refback_img").setAttribute("origsrc", "images/arrow_blank.gif");
	}
	else {
		document.getElementById("refback").setAttribute("href", back);
	}

}

/* --------------------------------------------------------------- 
Function to get hostname and use local instead of internet hostname
if local host is used

SUPERSEEDED by PHP code
--------------------------------------------------------------- */
/*
addDOMLoadEvent(checkHost);
*/

// check for lnxsrv as host
/*
function checkHost () {
	sNet="www.motherandlanello.com";
	sLocal="lnxsrv/motherandlanello"
	// if lnxsrv replace all internet hostnames in document
	if (location.hostname.match("lnxsrv") !== null) {
		var aEls = document.getElementsByTagName('a');
		for (var i = 0, aEl; aEl = aEls[i]; i++) {
			aEl.href = aEl.href.replace(sNet, sLocal);
		}
		var imgEls = document.getElementsByTagName('img');
		for (var i = 0, imgEl; imgEl = imgEls[i]; i++) {
			imgEl.src = imgEl.src.replace(sNet, sLocal);
		}	
	}
}
*/

	