//get or set active StyleSheet
function activeStyleSheet(title) {
	//Shorthand for document.getElementsByTagName
	function _tags(i,o) { 
		o=o==undefined?document:o;
		return o.getElementsByTagName(i);
	}
	var undefined, i, ret, nret=0,
		links=_tags('link');
	for(i=0; i<links.length; i++) {
		ret=links[i].getAttribute('title');
		if(/stylesheet$/.test(links[i].getAttribute("rel")) && !!ret) {
			if (title==undefined && !links[i].disabled)
				return ret;
			else 
				links[i].disabled = !(ret==title);
		}
	}
}

//get or set cookie
function Cookie(name, value) {
	var undefined;
	if (value==undefined) {
		var i,tmp,cookies=document.cookie.split('; ');
		for (i=0; i<cookies.length; i++)
			if ((tmp=cookies[i].split('='))[0]==name)
				return unescape(tmp[1]);
		return null;
	} 
	var expiry=(new Date());
	expiry.setTime(expiry.getTime()+31557600000);
	var tmp=document.cookie=name+'='+escape(value)+'; expires='+expiry.toGMTString()+'; path=/';
	return value;
}

//should be 'setPersistentStylesheet'
function setActiveStyleSheet(title) {
	//Eh?  Weird, but it seems to fix IE problem.
	activeStyleSheet('none');
	//called on font-size change
	activeStyleSheet(title);
	for (var i=1; i<=5; i++)
		document.images['safetytxt'+i].src = "/images/txtslices/txt-"+ title +"_0"+i+".gif";
	Cookie("style", title);
	if (Cookie("style")!==title)
		window.location.search='style='+title;
}

//should be 'styleSheetBootstrap'
function callCookie() {
	//called on page load to make font size changes persistent
	var style=Cookie('style');
	if (!!style) setActiveStyleSheet(style);
	return true;
}


/* Bookmark function */
function bookmark() { 
var pagename = "STAVZOR (valproic acid)"; 

if (document.all) window.external.AddFavorite(bookmarkurl,pagename);
else alert('Press CTRL-D (Command-D on a Mac) to add a bookmark to:\n"'+bookmarkurl+'".');
} 


/* Print function */
(function () {
	var mac=/mac/.test(navigator.userAgent);
	if (!!window.print) {
		window.print = function () { alert(window.print.message); }
		window.print.message = 
			'To print this page press '+(mac?'Command':'Control')+'-P. For best results, make sure your browser is set to print background images.'
	}
	window.printpage=window.print;
})();


/* Rollover Box Popup Scripts */
var
	trail={
		xOff: 5,
		duration: 0,
		height: 170,
		visible:false
	},
	cache={},
	Ajax = function (method, url, params, success, failure) {
		var http = !!window.XMLHttpRequest?
			(new XMLHttpRequest()):
			(new ActiveXObject("Microsoft.XMLHTTP")),
			qsa = !!params?(function() {
				var i,t=[];
				for (i in params) t.push(escape(i)+'='+escape(t));
				return t.join('&');
			})():'',
			post=!!method?method.toLowerCase()!='get':false,
			uri=url+(!post&&qsa.length?('?'+qsa):'');
		
		http.open(method,uri);
		http.onreadystatechange = function () {
			if (http.readyState!=4) return;
			if (http.status != 200 && !!failure) return failure(http);
			if (!!success) return success(http);
		};
		http.send(post?qsa:null);
	},
	Observe = function (o,e,h) {
		if (!!o.attachEvent) 
			return o.attachEvent('on'+e,function () { h.apply(o,[window.event]); });
		return o.addEventListener(e,h,false);
	}


function sndReq(term) {
	if (!cache[term])
		Ajax('get','/shared/glossary/includes/' + term + '.php',{},function (http) {
			handleResponse(cache[term]=http.responseText);
		});
	else
		handleResponse(cache[term]);
}

function handleResponse(response) {
    document.getElementById("trailinsides").innerHTML = response;
}

function gettrailobj(){
	if (document.getElementById)
		return document.getElementById("trailimageid")
	else if (document.all)
		return document.all.trailimagid
}

function showtrail(term){
	newHTML = '<div id="trailinsides" style="padding:10px; background-color: #FFF; border: 1px solid #98005D; font-size:12px;"><p>Loading definition...</p></div>';
	gettrailobj().innerHTML = newHTML;
	sndReq(term);
	gettrailobj().style.visibility="visible";
	trail.visible=true;
}


function hidetrail(){
	var to=gettrailobj().style;
	to.visibility="hidden";
	to.left="-500px";
	trail.visible=false;
}

function followmouse(e){
	if (!trail.visible) return;
	var 
		to=gettrailobj(),
		cnt=document.getElementById('trailinsides'),
		html=document.getElementsByTagName('html')[0],
		scroll={
			top: Math.max(html.scrollTop,document.body.scrollTop),
			left: Math.max(html.scrollLeft,document.body.scrollLeft)
		},
		mouse=!!document.all?{
			x:window.event.clientX,
			y:window.event.clientY
		}:{
			x:e.pageX-scroll.left,
			y:e.pageY-scroll.top
		},
		portal={
			width:html.clientWidth,
			height:html.clientHeight
		},
		pos={ 
			x:(mouse.x>portal.width/2?(-trail.xOff-to.offsetWidth):trail.xOff)+mouse.x, 
			y:Math.max(scroll.top,Math.min(scroll.top+mouse.y-cnt.offsetHeight/2,portal.height-cnt.offsetHeight+scroll.top))
		};
	to.style.left=pos.x+"px";
	to.style.top=pos.y+"px";
}

Observe(window,'load',function () {
	var trailObject = document.createElement('div');
	trailObject.id='trailimageid';
	document.body.appendChild(trailObject);
});

Observe(document,'mousemove',followmouse);