/*
 * DIRECTSCIENCE.INFO MAIN JAVASCRIPT
 *
 * COPYRIGHT BOYANG ZHAO
 *
 * Revision Date: January 19, 2007 20:47
 * Revision: 7
 */

/*variables*/
var designwidth = 800;

/*Preload Images*/
var imagelist = new Array('/images/sidetransbo.gif','/images/sidetransbol.gif','/images/searchb2.gif','/images/navsub.gif','/images/logingo.gif','/images/loginx.gif');
for(i=0;i<imagelist.length;i++){
	var image = new Image();
	image.src = imagelist[i];
}
	
/*Break Out of Frames*/
if(window.top != window.self)
window.top.location = window.location;

//events
function addEventHandler(object,type,handler){
	if(document.addEventListener){
		object.addEventListener(type,handler,false);
	} else if(document.attachEvent){
		object.attachEvent('on'+type,handler);
	}
}

function removeEventHandler(object,type,handler){
	if(document.removeEventListener){
		object.removeEventListener(type,handler,false);
	} else if(document.detachEvent){
		object.detachEvent('on'+type,handler);
	}
}

addEventHandler(window,'load',function(){
	setTimeout(navsubt,500);
	addEvents();
	windowadjust();
});

addEventHandler(window,'resize',windowadjust);

function windowadjust(){
	if(window.innerWidth){
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if(document.documentElement.clientWidth){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if(document.body.clientWidth){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
}

//offset of element
function offsetElmnt(id,type) {
	elmnt = document.getElementById(id);
	if (elmnt.offsetParent){
		eleft = elmnt.offsetLeft
		etop = elmnt.offsetTop
		while(elmnt.offsetParent != null){
			elmnt = elmnt.offsetParent;
			eleft += elmnt.offsetLeft;
			etop += elmnt.offsetTop;
		}
	}
	
	var tmp = type == 'left' ? eleft : etop;
	return tmp;
}

/*DS-LOGIN Navsub*/
function navsubt(){
for(var i=0;i<11;i++)
	setTimeout('navsub('+i+')',120*i);
}
function navsub(b){
	var navsuby = document.getElementById('navsub');
	navsuby.onclick = loginc;
	navsuby.onmouseover = logino;
	navsuby.onmouseout = logine;
	navsuby.style.filter='alpha(opacity='+b*10+')';
	navsuby.style.opacity=''+b/10+'';
	
	var navsubxy = document.getElementById('navsub_loginx');
	navsubxy.onmouseover = function(){this.style.cursor='pointer';};
	navsubxy.onclick = navsubbe;
	
	if(document.getElementById('navsubimg')){
	var navsubimg = document.getElementById('navsubimg');
	navsubimg.onmouseover=function(){this.src='/images/logingo.gif';};
	navsubimg.onmouseout=function(){this.src='/images/loging.gif';};
	}
}

/*DS-M1 Nav*/
function navo(){
	this.style.borderColor='#86A7B7'
}
function nave(b){
	this.style.borderColor='#E5E6E7 #D9DFE3 #D7DEE2 #D9DFE3';
}

/*DS-C1 Sidebar*/
function csideo(){
	this.style.background='url(/images/sidetransbo.gif) top left repeat-x';
	this.style.cursor='pointer';
	this.firstChild.style.background='url(/images/sidetransbol.gif) center left repeat-y';
}
function csidee(d){
	var cside = 'cside'+d;
	var cslink = 'l'+d;
	this.style.background='none';
	this.firstChild.style.background='none';
}
function csidec(){
	location = this.firstChild.href;
}

/*DS-C1 MAIN TAB*/
function cmain2tabo(){
	this.style.background='url(/images/cmaintransg_tab.gif) repeat-x';
}
function cmain2tabe(){
	this.style.background='#e6e6e6';
}

/*DS-M0SR*/
function tasks(){
	document.getElementById('main0sr_tab').style.zIndex='-1';
	displayelmnt('main0sr0','show');
}
function taskse(){
	document.getElementById('main0sr_tab').style.zIndex='0';
	displayelmnt('main0sr0','hide');
}

addEventHandler(window,'load',tasksadjust);
taskadjustck = false;

function tasksadjust(){
	if(document.getElementById('main0sr_tab')){
		document.getElementById('main0sr_tab').style.top=offsetElmnt('main0sr','top')+'px';
		document.getElementById('main0sr0').style.top=offsetElmnt('main0sr','top')+'px';
	}
	if(!taskadjustck){
		taskadjustck = true;
		addEventHandler(window,'resize',tasksadjust);
	}
}

/*Add Events*/
function addEvents(){
	//(traditional event registration is used to prevent incompatibility)
	//forms
	formaddevent();
	
	//search
	document.getElementById('searchk').onfocus=function(){
		if(this.value == 'Enter search terms')
		this.value='';
		this.style.color='#000000';
		formaddevent('focus');
	};
	
	document.getElementById('searchk').onblur=function(){
		if(this.value == '')
		this.value='Enter search terms';
		this.style.color='#cccccc';
		formaddevent('blur');
	};
	
	document.getElementById('searchimg').onmouseover=function(){this.src='/images/searchb2.gif';};
	document.getElementById('searchimg').onmouseout=function(){this.src='/images/searchb.gif';};
	
	//nav
	var text;
	var nav = document.getElementById('nav');
	for(i=0;i<nav.childNodes.length;i++){
		if(nav.childNodes[i].nodeName != '#text'){
			var addee = nav.childNodes[i].firstChild;
			addee.onmouseover = navo;
			addee.onmouseout = nave;
		}
	}
	
	//cside
	var cside = document.getElementById('cside');
	var csidepattern = /^cside\d{1}\w/;
	var a = 0;
	var csidearray = new Array();
	for(i=0;i<cside.childNodes.length;i++){
		if(cside.childNodes[i].nodeName != '#text'){
			if(cside.childNodes[i].className.search(csidepattern)!=-1){
				if(a == 3) a = 0;
				a++;
				if(a == 2){
					b = csidearray.length;
					csidearray[b] = i;
				}
			}
		}
	}
	for(i=0;i<csidearray.length;i++){
		var csideby = cside.childNodes[csidearray[i]];
		for(c=0;c<csideby.childNodes.length;c++){
			if(csideby.childNodes[c].nodeName != '#text'){
				var csidecy = csideby.childNodes[c]
				for(d=0;d<csidecy.childNodes.length;d++){
					if(csidecy.childNodes[d].nodeName != '#text'){
						if(csidecy.childNodes[d].className != 'noevents'){
							var addee = csidecy.childNodes[d];
							addee.onmouseover = csideo;
							addee.onmouseout = csidee;
							addee.onclick = csidec;
						}
					}
				}
			}
		}
	}
	
	//cmain2_t
	if(document.getElementById('cmain2_t')){
		var cmain2_t = document.getElementById('cmain2_t');
		for(i=0;i<cmain2_t.childNodes.length;i++){
			if(cmain2_t.childNodes[i].nodeName != '#text'){
				if(cmain2_t.childNodes[i].className != 'cmain2_tabf'){
					var addee = cmain2_t.childNodes[i];
					addee.onmouseover = cmain2tabo;
					addee.onmouseout = cmain2tabe;
				}
			}
		}
	}
}

function formaddevent(type){
	if(type == 'focus'){
		if(keyshortcut){
			document.onkeydown = '';
			document.onkeyup = '';
		}
	} else if(type == 'blur'){
		if(keyshortcut){
			document.onkeydown = keydownf;
			document.onkeyup = keyupf;
		}
	} else {
		var forminputf = document.getElementsByTagName('input');
		for(i=0;i<forminputf.length;i++){
			if(forminputf[i].type=='text' || forminputf[i].type=='password'){
				forminputf[i].onfocus=function(){
					if(keyshortcut){
						document.onkeydown = '';
						document.onkeyup = '';
					}
				};
				forminputf[i].onblur=function(){
					if(keyshortcut){
						document.onkeydown = keydownf;
						document.onkeyup = keyupf;
					}
				};
			}
		}
		var forminputf = document.getElementsByTagName('textarea');
		for(i=0;i<forminputf.length;i++){
			forminputf[i].onfocus=function(){
				if(keyshortcut){
					document.onkeydown = '';
					document.onkeyup = '';
				}
			};
			forminputf[i].onblur=function(){
				if(keyshortcut){
					document.onkeydown = keydownf;
					document.onkeyup = keyupf;
				}
			};
		}
	}
}

/*Keyboard Shortcuts*/
if(keyshortcut){
	document.onkeydown = keydownf;
	document.onkeyup = keyupf;
}

function getkey(e){
	var key;
	if(!e) e = window.event;
	if(e.which) key = e.which;
	else if(e.keyCode) key = event.keyCode;
	
	return key;
}

//onkeydown - when typing will not affected
function keydownf(e){
	keyd = getkey(e);
	if(keyd == 71){
		document.onkeydown = gokeydownf;
		document.onkeyup = keyupf;
	} else {
		switch(keyd){
			case 81:
				document.onkeyup = function(){
					document.getElementById('searchk').value=''; 
					document.getElementById('searchk').focus();
				}
			break;
			case 76:
				var navsubbk = document.getElementById('navsubb').style.zIndex;
				if(navsubbk == '-1' || navsubbk == '0' || navsubbk == ''){
					loginc();
					document.onkeyup = function(){
						document.getElementById('userk').value='';
						document.getElementById('userk').focus();
					}
				} else if(navsubbk == '1') {
					navsubbe();
				}
			break;
			default:
				if (article){
					switch(keyd){
						case 69:document.location.href=document.getElementById('edit').href; break;
						case 86:document.location.href=document.getElementById('previous_versions').href; break;
						case 67:document.location.href=document.getElementById('collaborate').href; break;
						case 68:document.location.href=document.getElementById('discuss').href; break;
						case 65:document.location.href=document.getElementById('about').href; break;
						case 84:
							var tasktabk = document.getElementById('main0sr_tab').style.zIndex;
							if(tasktabk == '0' || tasktabk == ''){
								tasks();
								document.onkeydown = tkeydownf;
								document.onkeyup = keyupf;
							} else if(tasktabk == '-1'){
								taskse();
							}
						break;
						default:document.onkeydown = '';document.onkeyup = keyrestore;
						
					}
				} else if(!article){
					switch(keyd){
						case 66:document.location.href=document.getElementById('back').href; break;
						default:document.onkeydown = '';document.onkeyup = keyrestore;
					}
				}
			break;
		}
	}
}

function gokeydownf(e){
	keyd = getkey(e);
	switch(keyd){
		case 70: document.location.href="/fields/"; break;
		case 82: document.location.href="/references/"; break;
		case 83: document.location.href="/resources/"; break;
		case 80: document.location.href="/protocols/"; break;
		case 79: document.location.href="/programs/"; break;
		case 72: document.location.href="/index.php"; break;
		default: document.onkeydown = keydownf;
	}
}
		
function tkeydownf(e){
	keyd = getkey(e);
	switch(keyd){
		case 80: document.location.href=document.getElementById('print').href; break;
		case 83: document.location.href=document.getElementById('send').href; break;
		case 68: document.location.href=document.getElementById('pdf').href; break;
		case 66: document.location.href=document.getElementById('bookmark').href; break;
		case 76: document.location.href=document.getElementById('delicious').href; break;
		default: document.onkeydown = keydownf;
	}
}

function keyupf(e){
	keyu = getkey(e);
	keyrestore();
}

function keyrestore(){
	document.onkeydown = keydownf;
	document.onkeyup = keyupf;
}

/*Bookmark*/
function bookmark(url,title){
	if(window.external){
		window.external.AddFavorite(url,title);
	} else if(window.sidebar){
		window.sidebar.addPanel(title, url, '');
	}
	/* else if(window.opera && window.print){
		return true;
	}*/
}

//ajax class
function ajax(){
	this.xmlhttp = null;
	this.url = null;
	this.method = null;
	this.postdata = null;
	this.async = true;
	this.contentType = null;
	this.responseFormat = null;
	this.handleResponse = null;
	this.handleError = null;
	
	this.createPostdata = function(query){
		for(var i=0;i<query.length;i++){
			if(i==0)
			this.postdata = query[i];
			else 
			this.postdata += '&'+query[i];
		}
		this.contentType = 'application/x-www-form-urlencoded';
	};
	
	this.initialize = function(){
		if(!this.xmlhttp){
			try {
				this.xmlhttp = new XMLHttpRequest();
			}
			catch(err) {
				try {
					this.xmlhttp = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch(err) {
					try {
						this.xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch(err){
						return false;
					}
				}
			}
		}
	return this.xmlhttp;
	};
	
	this.startxmlhttp = function(){
		if(!this.initialize()){
			alert('Error creating XMLHttpRequest object!');
			return;
		}
		this.xmlhttp.open(this.method,this.url,this.async);
		var self = this;
		this.xmlhttp.onreadystatechange = function(){
			try {
				var response = null;
				if(self.xmlhttp.readyState == 4){
					if(self.responseFormat == 'text'){
						response = self.xmlhttp.responseText;
					} else if(self.responseFormat == 'xml'){
						response = self.xmlhttp.responseXML;
					}
					if(self.xmlhttp.status >= 200 && self.xmlhttp.status < 300){
						self.handleResponse(response);
					} else {
						self.handleError(response);
					}
				}
			} catch(e){
			}
		};
		if(this.contentType)
			this.xmlhttp.setRequestHeader('Content-Type', this.contentType);
		try {
		this.xmlhttp.send(this.postdata);
		} catch(e) {
		}
	};
	
	this.getxmlhttp = function(url,handle,method,query,format){
		if(format == undefined || format == '') format = 'text';
		if(method == undefined || method == '') method = 'GET';
		if(query == undefined || query == '') query = null;
		else this.createPostdata(query);
		this.url = url;
		this.handleResponse = handle;
		this.responseFormat = format;
		this.method = method;
		this.startxmlhttp();
	};
}

//setting ajax variable
var ajax = new ajax();
		
//foldout tree list class
function tree(){
	this.status = 'close';
	this.refold = null;
	this.fold = function(elementid){
		if(this.refold){
			displayelmnt(elementid,'show');
		} else {
			if(this.status=='open'){
				displayelmnt(elementid,'hide');
				this.status = 'close';
			} else if(this.status=='close'){
				displayelmnt(elementid,'show');
				this.status = 'open';
			}
		}
	};
}

//check for article existence
function checkarticle(articletitle,handle){
	if(articletitle.length > 0){
		var query = "access=allow&title="+articletitle;
		query = query.split("&");
		var url="/dsmain/processes/checkarticle.php";
		ajax.getxmlhttp(url,handle,'POST',query);
	} else {
		handle('initial');
	}
}

//new window default settings
function nw(type,url,source,width,height){
	if(type == null){
		width=800;
		height=500;
		menubar=1;
	} else if(type == 'small'){
		width=400;
		height=200;
		menubar=0;
	}
	window.open(url, source, 'width='+width+', height='+height+', status=0, toolbar=0, location=0, menubar='+menubar+', directories=0, resizable=1, scrollbars=1');
}

//hide and show element
function displayelmnt(elemntid,type){
	if(type == 'show'){ var a = 'visible'; var b = 'block'; }
	else if(type == 'hide'){ var a = 'hidden'; b = 'none'; }
	document.getElementById(elemntid).style.visibility=a;
	document.getElementById(elemntid).style.display=b;
}

//create element
function createElement(a){
  if (document.createElement){
	  return document.createElement(a);
  } else if(document.createElementNS) {
	  return document.createElementNS("http://www.w3.org/1999/xhtml",a);
  }
}

/*get field select option*/
function getSelectValue(id){
	selectpointer = document.getElementById(id);
	for(i=0;i<selectpointer.options.length;i++){
		if(selectpointer.options[i].selected)
		return selectpointer.options[i].value;
	}
}

//include scripts within javascript
function include(scriptname){
	var head = document.getElementsByTagName('head')[0];
	var incjs = document.createElement('script');
	incjs.setAttribute('type','text/javascript');
	incjs.setAttribute('src','/js/'+scriptname+'.js');
	head.appendChild(incjs);
}

addedinclude = new Array();
function include_once(scriptname){
	if(!in_array(scriptname,addedinclude)){
		addedinclude[addedinclude.length] = scriptname;
		include(scriptname);
	}
}

//search array
function in_array(needle,haystack){
	for(var i=0;i<haystack.length;i++){
		if(needle.toLowerCase() == haystack[i].toLowerCase()){
			return true;
		}
	}
	return false;
}
		
//article modes
modesj = new tree();
modesj.status = 'open';

function amodes(){
	modesj.fold('amodesbar0');
	if(modesj.status == 'close'){
		displayelmnt('admodesbar1','show');
	} else {
		displayelmnt('admodesbar1','hide');
	}
}