var read1 = '0';
var read2 = '0';
var read3 = '0';
var read4 = '0';
var read5 = '0';

// Marks a description as being read
function read(foo) {
	if (foo === 'read1') read1 = '1';
	if (foo === 'read2') read2 = '1';
	if (foo === 'read3') read3 = '1';
	if (foo === 'read4') read4 = '1';
	if (foo === 'read5') read5 = '1';
}

// Checks to see if a description has been read
function readCond(foo,bar) {
	if (foo === 'read1') { if (read1 !== '1') {	read1 = '1'; popup(bar,'600','450'); } }
	if (foo === 'read2') { if (read2 !== '1') {	read2 = '1'; popup(bar,'600','450'); } }
	if (foo === 'read3') { if (read3 !== '1') {	read3 = '1'; popup(bar,'600','450'); } }
	if (foo === 'read4') { if (read4 !== '1') {	read4 = '1'; popup(bar,'600','450'); } }
	if (foo === 'read5') { if (read5 !== '1') {	read5 = '1'; popup(bar,'600','450'); } }
}


// Toggles the tabbed box on the home page
function tabby(foo) {
	objs = new Array();
	objs = document.getElementsByTagName("*");
	for (i=0 ; i<objs.length ; i++) {
		if (objs[i].className === 'active') objs[i].className = 'inactive';
		if (objs[i].className === 'texts') objs[i].style.visibility = 'hidden';
		if (objs[i].className === 'texts') objs[i].style.position = 'absolute';
	}
	document.getElementById('tab'+foo).className = 'active';
	document.getElementById('tabText'+foo).style.visibility = 'visible';
	document.getElementById('tabText'+foo).style.position = 'static';
}

// Form activation script
function activate(brillig) {
	document.getElementById('config').name = "config";
	document.getElementById('config').value = brillig;
}

// Open a new window with specific dimensions
function popup(file,wide,high) {
	window.open(file,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,copyhistory=no,width='+wide+',height='+high)
	}

// Page setup scripts
function setup() {
	// Makes all external links open in new windows
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
	
	// Sets up all objects affect by the show/hide script
	var els = document.getElementsByTagName('*');
	for (var i=0;i<els.length;i++) {
		if (els[i].className == 'hidden') { toggle(els[i].id,0); }
		if (els[i].className == 'toggle') els[i].style.display = 'block';
	}
	setTimeout('opensesame()',20);
}

// Show/hide scripts
function toggle(id,duration,step_duration){
	var el = document.getElementById(id);
	if (!el || !el.style) return;
	if (duration === undefined) duration = 500;
	if (!step_duration) step_duration = 10;
	var steps = Math.max(Math.ceil(duration/step_duration),1);
	if (el.offsetHeight == 0) { //show it
		morph_height(el,0,el.naturalHeight,duration,steps,0);
	} else { //hide it
		el.naturalHeight = el.naturalHeight || el.offsetHeight;
		el.style.overflow = 'hidden';
		morph_height(el,el.offsetHeight,0,duration,steps,0);
	}
}
function morph_height(el,from,to,duration,steps,step) {
	var x = step/steps;
	var y = Math.sin((x-0.5)*Math.PI);
	var z = (y+1)*0.5;
	var h = from + (to-from)*z;
	if (step == steps) h = to;
	el.style.height = h+'px';
	if (h == to) return;
	var t = duration/steps;
	if (el.style.height === '0px') el.style.position = 'static';
	setTimeout(function(){morph_height(el,from,to,duration,steps,step+1)},t);
}
function opensesame() {
	var p = window.location + "";
	var q = p.indexOf('.org');
	p = p.substring(q+5);
	q = p.indexOf('/');
	p = p.substring(0,q);
	if (p === 'About-AIHSP') toggle('submenu1',0);
	if (p === 'Membership') toggle('submenu2',0);
	if (p === 'News' || p ==='lists') toggle('submenu3',0);
}

window.onload = setup;