var txtZoomFactors = new Array(75,82,90,100,115,135,165,200);
var nZoomFactors = 8; // Xinha messes with Array.length(), so be explicit
var txtZoomDefault = 2;
var txtZoom=txtZoomDefault;

// Adjust stylesheet as document loads
// (This script needs to be loaded after the stylesheet)

var tz_cookie=readCookie('txtZoom');
if (tz_cookie!=null) {
	txtZoom = Math.floor(tz_cookie);
	if (txtZoom<0) txtZoom=0;
	if (txtZoom>=nZoomFactors) txtZoom=nZoomFactors-1;
	// Document hasn't loaded yet, so we can't use setTextZoom(txtZoom)
	// Instead, change the stylesheet declaration
	rules=document.styleSheets[0].cssRules;
	for (var i in rules) {
		if (rules[i].selectorText=='body' && rules[i].style.fontSize) {
			rules[i].style.fontSize=txtZoomFactors[txtZoom]+'%';
			break;
		}
	}
}


function setTextZoom(z) {
	switch (z) {
		case -1:
			txtZoom--;
			if (txtZoom<0) txtZoom=0;
			break;
		case 0:
			txtZoom=txtZoomDefault;
			break;
		case 1:
			txtZoom++;
			if (txtZoom>=nZoomFactors) txtZoom=nZoomFactors-1;
			break;
	}
	document.body.style.fontSize=txtZoomFactors[txtZoom]+'%';
	with (document.getElementById("tz_sm")) {
		className = (txtZoom==0) ? "greylink" : "";
		title = (txtZoom==0) ? "" : "smaller";
	}
	with (document.getElementById("tz_rs")) {
		className = (txtZoom==txtZoomDefault) ? "greylink" : "";
		title = (txtZoom==txtZoomDefault) ? "" : "reset";
	}
	with (document.getElementById("tz_lg")) {
		className = (txtZoom==nZoomFactors-1) ? "greylink" : "";
		title = (txtZoom==nZoomFactors-1) ? "" : "larger";
	}
	if (txtZoom!=txtZoomDefault) {
		eraseCookie('txtZoom');
		createCookie('txtZoom',txtZoom,32*86400); // keep cookie for a month or so
	}
	else {
		eraseCookie('txtZoom');
	}
	return false;
}
	

function insertTextResizer() {
	document.write('<div class="topblock">');
	document.write('<p>Adjust text size: ');

	var t_smaller = (txtZoom>0) ? 'title="smaller"' : 'class="greylink"';
	document.write('<strong><a id="tz_sm" '+t_smaller+' href="javascript:" ');
	document.write('onclick="return setTextZoom(-1);">');
	document.write('<small>A</small></a></strong>&nbsp;');

	var t_reset = (txtZoom!=txtZoomDefault) ? 'title="reset"' : 'class="greylink"';
	document.write('<strong><a id="tz_rs" '+t_reset+' href="javascript:" ');
	document.write('onclick="return setTextZoom(0);">');
	document.write('A</a></strong>&nbsp;');
	
	var t_larger = (txtZoom<nZoomFactors-1) ? 'title="larger"' : 'class="greylink"';
	document.write('<strong><a id="tz_lg" '+t_larger+' href="javascript:" ');
	document.write('onclick="return setTextZoom(1);">');
	document.write('<big>A</big></a></strong>');
	
	document.writeln('</div>');
	
	// Add frame breakout button
	if (top.location != location) {
		document.write('<div id="framebreak"><img src="/style/b19.gif" alt="" title="Show only this frame" ');
		document.write('onmouseover="return fb_ani_on()" onmouseout="return fb_ani_off()" ');
		document.writeln('onclick="top.location.href=window.location.href" width="19" height="19" /></div>');
	}
}

var fb_state = 8;
var fb_bg_pos = new Array( 'top left',    'top center',    'top right',
                           'center left', 'center center', 'center right',
                           'bottom left', 'bottom center', 'bottom right' );
var fb_bg_t = false;

function fb_ani_on() {
	fb_set_state(0);
	fb_bg_t = setInterval(fb_ani,50);
	return true;
}

function fb_ani_off() {
	clearInterval(fb_bg_t);
	fb_set_state(8);
	return true;
}

function 	fb_set_state(s) {
  fb_state = s;
  if (s>8) s=8;
  document.getElementById('framebreak').style.backgroundPosition = fb_bg_pos[s];
}

function fb_ani() {
	fb_set_state((fb_state + 1) % 20);
}



function createCookie(name,value,lifetime_in_secs) {
	var expires = "";
	if (lifetime_in_secs) {
		var date = new Date();
		date.setTime(date.getTime()+lifetime_in_secs*1000);
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	name += "=";
	var cookie_list = document.cookie.split(";");
	for(var i=0;i < cookie_list.length;i++) {
		var cookie = cookie_list[i].replace(/^\s+|\s+$/,'');
		if (cookie.indexOf(name)==0) return cookie.substring(name.length,cookie.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-86400);
}
