function addLoadEvent(func) {
   if (!window.__load_events) {
      var init = function () {
          // quit if this function has already been called
          if (arguments.callee.done) return;
      
          // flag this function so we don't do the same thing twice
          arguments.callee.done = true;
      
          // kill the timer
          if (window.__load_timer) {
              clearInterval(window.__load_timer);
              window.__load_timer = null;
          }
          
          // execute each function in the stack in the order they were added
          for (var i=0;i < window.__load_events.length;i++) {
              window.__load_events[i]();
          }
          window.__load_events = null;
      };
   
      // for Mozilla/Opera9
      if (document.addEventListener) {
          document.addEventListener("DOMContentLoaded", init, false);
      }
      
      // for Internet Explorer
      /*@cc_on @*/
      /*@if (@_win32)
          document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
          var 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
          window.__load_timer = setInterval(function() {
              if (/loaded|complete/.test(document.readyState)) {
                  init(); // call the onload handler
              }
          }, 10);
      }
      
      // for other browsers
      window.onload = init;
      
      // create event function stack
      window.__load_events = [];
   }
   
   // add function to event stack
   window.__load_events.push(func);
}

function addUnloadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

function suckerfish(type, tag, parentId) {
	if (window.attachEvent) {
		window.attachEvent("onload", function() {
			var sfEls = (parentId==null)?document.getElementsByTagName(tag):document.getElementById(parentId).getElementsByTagName(tag);
			type(sfEls);
		});
	}
}

sfHover = function(sfEls) {
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function menu() {
	if (document.getElementById) {
		var leftColumn = document.getElementById('leftColumn');
		var leftColumnContent = document.getElementById('leftColumnContent');
		var centerTools = document.getElementById('centerTools');
		var htmlCenter = centerTools.innerHTML;
		var htmlNavBar = document.getElementById('nav').innerHTML;
		if (leftColumn.style.display == 'block') {
			leftColumn.style.display = 'none';
			centerTools.innerHTML = htmlCenter.replace('Hide','Show');
		} else {
			leftColumn.style.display = 'block';
			leftColumn.style.width = '140px';
			centerTools.innerHTML = htmlCenter.replace('Show','Hide');
			leftColumnContent.innerHTML = '<ul id="menu">' + htmlNavBar + '</ul>';
		}
	}
}

function showmenus() {
	if (document.getElementById) {
		var menu2 = document.getElementById('menu2').innerHTML;
		document.getElementById('menu3').innerHTML = menu2.replace(/news/g,'articles');
		document.getElementById('menu5').innerHTML = menu2.replace(/news/g,'videos');
		document.getElementById('menu6').innerHTML = menu2.replace(/news/g,'books');
	}
}

function printpage() {
	var options = "scrollbars,resizable"; 
	var text = '<table cellpadding="10" cellspacing="0" border="0" width="100%"><tr><td valign="top"><em>Web address:</em> <strong>' + this.location.href.replace(/#/,'') + '</strong></td><td valign="top" align="right" id="printbutton"><input type=button value="Print this page" onClick="window.print()"></td></tr></table>';
		text += '<table cellpadding="10" cellspacing="0" border="0" width="100%"><tr><td valign="top"><img src="/images/logo-print.gif" width="275" height="90"></td><td valign="top">';
		text += '</td></tr></table>';
		text += '<div id="centerColumn"><div id="centerColumnContent">';
		text += document.getElementById('centerColumnContent').innerHTML;
		text += '</div></div>';
		text += '<div class="clear"></div>';
		text += '<div id="footer" style="background-color: #fff;">';
		text += document.getElementById('footer').innerHTML;
		text += '</div>';
		text = text.replace(/<scr\ipt[^>]*?>[.\s\S]*?<\/scr\ipt>/gi,'');
		text = text.replace(/<div id="first_ad_unit">[.\s\S]*?<hr><\/div>/gi,'');
		text = text.replace(/<div id="second_ad_unit">[.\s\S]*?<hr><\/div>/gi,'');
		text = text.replace(/<iframe[^>]*?>[.\s\S]*?<\/iframe>/gi,'');
		text = text.replace(/<p class="center">[.\s\S]*?<\/div>/gi,'</div>');
		text = text.replace(/<div id=first_ad_unit>[.\s\S]*?<hr>[\s]*?<\/div>/gi,'');
		text = text.replace(/<div id=second_ad_unit>[.\s\S]*?<hr>[\s]*?<\/div>/gi,'');
		text = text.replace(/<p class=center>[.\s\S]*?<\/div>/gi,'</div>');
		text = text.replace('<a href="/about.htm">About This Site</a> &nbsp;|&nbsp; <a href="/staff.htm">Editorial Staff</a> &nbsp;|&nbsp; <a href="/awards.htm">Awards &amp; Reviews</a> &nbsp;|&nbsp; <a href="/contribute/">Contribute News</a> &nbsp;|&nbsp; <a href="/advertise/">Advertise With Us</a> &nbsp;|&nbsp; <a href="/privacy.htm">Privacy Policy</a> &nbsp;|&nbsp; <a href="/terms.htm">Terms of Use</a></em>','');
		text = text.replace('<em>More on:</em>','<em>Keywords:</em>');
	var winprint = window.open("","",options);
		winprint.document.open();
		winprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		winprint.document.write('<html>');
		winprint.document.write('<head><scr'+'ipt>function stopError() { return true; }');
		winprint.document.write('window.onerror = stopError;');
		winprint.document.write('</scr'+'ipt>');
		winprint.document.write('<link href="/styles/print.css" type="text/css" rel="stylesheet" /><body>');
		winprint.document.write('<style type="text/css">@media print { #printbutton { display: none; } }</style></head>');
		winprint.document.write(text);
		winprint.document.write('</body></html>'); 
		winprint.document.close();
}

function emailpage() {
	var recipient = prompt("Recipient's email address:","");
	if (recipient == null) {
		alert("You must enter an email address.")
	} else {
		var subject = document.title;
		var url = document.location.href;
		var body = "Please see the following page ...\n\n" + "Title:   " + subject + "\n" + "Link:    " + url + "\n";
		var message = "mailto:" + recipient + "?subject=" + escape(subject) + "&body=" + escape(body);
		window.location = message;
		window.focus();
	}
}

function bookmark() {
	if (document.all)
		window.external.AddFavorite(this.location.href, document.title);
	else if (window.sidebar)
		window.sidebar.addPanel(document.title, this.location.href, "")
}

function size(arg) {
	var center = document.getElementById('centerColumnContent');
	var mid = document.getElementById('midColumnContent');
	if (arg == 'minus') {
		center.style.fontSize = '0.6em';
		mid.style.fontSize = '0.6em';
	} else if (arg == 'reset') {
		center.style.fontSize = '0.8em';
		mid.style.fontSize = '0.8em';
	} else if (arg == 'plus') {
		center.style.fontSize = '1em';
		mid.style.fontSize = '1em';
	}
}

function redirect(arg) {
	url = arg.replace(/^http[^?]*?\?url=/,'');
//	alert(url);
	location = url;
}

// Take content in a div with id="defer-xyz" and move it to a div with id="xyz"
function relocateDeferredContent() {
    var dc=document.getElementById("deferredContent");
    var divs=dc.getElementsByTagName("div");
    var replacements=new Array();
    for(var i=0;i<divs.length;i++){
        var deferredContent = divs[i];
        if (deferredContent.id.indexOf("defer-") == 0) {
            var placeHolder = document.getElementById(deferredContent.id.slice(6));
            replacements.push([deferredContent, placeHolder]);
        }
    }
    for(i=0;i<replacements.length;i++){
        replacements[i][0].parentNode.removeChild(replacements[i][0]);
        replacements[i][1].parentNode.replaceChild(replacements[i][0], replacements[i][1]);
        replacements[i][0].style.display = "block";
    }
    return true;
}

addLoadEvent(function() {
	suckerfish(sfHover, "LI", "nav");
});

addLoadEvent(function() {
	var excludedomains=["localhost", "127.0.0.1", "local.sciencedaily", "www.sciencedaily.com", "www.sciencedaily.net", "www.sciencedaily.org", "sciencedaily.com", "sciencedaily.net", "sciencedaily.org", "healthology.sciencedaily.com", "sciencedaily.healthology.com", "mediwire.sciencedaily.com", "sciencedaily.mediwire.com", "healthcare.careerbuilder.com", "sciencebiotech.careerbuilder.com", "engineering.careerbuilder.com", "informationtechnology.careerbuilder.com", "www.careerbuilder.com"];
	var excludedomains = excludedomains.join("|");
	rexcludedomains = new RegExp(excludedomains, "i");
	for (i=0; i<=(document.links.length-1); i++) {
		if (document.links[i].hostname.search(rexcludedomains)==-1 && document.links[i].href.indexOf("http:")!=-1) {
			document.links[i].target="_blank";
		}
		if (document.links[i].href.indexOf("/goto.php")!=-1) {
			document.links[i].onclick=new Function("redirect(this.href); return false;");
		}
	}
});
