/*******************************************
 accfontsize.js -- v1.1
 (c)2009 Eric Berkson. All Rights Reserved.
*******************************************/

var accFontSize = {
	cookiename: '_accfs',
	multiples: new Array(.9,1,1.1,1.2,1.3),
	setCookie: function(name,value,expiredays) {
		this.expiredate=new Date();
		this.expiredate.setDate(this.expiredate.getDate()+expiredays);
		document.cookie=name+"="+escape(value)+((expiredays===null) ? "" : ";expires="+this.expiredate.toGMTString())+";path=/";
	},
	getCookie: function(name) {
		if(document.cookie.length>0) {
			this.cstart=document.cookie.indexOf(name+"=");
			if(this.cstart!=-1) {
				this.cstart=this.cstart+name.length+1;
				this.cend=document.cookie.indexOf(";",this.cstart);
				if(this.cend==-1) {
					this.cend=document.cookie.length;
				}
				return unescape(document.cookie.substr(this.cstart,this.cend));
			}
		}
		return null;
	},
	destroyCookie: function(name) {
		this.expiredate=new Date();
		this.expiredate.setDate(this.expiredate.getDate()-1);
		document.cookie=name+";expires="+this.expiredate.toGMTString();
	},
	cookieCheck: function() {
		this.int=Math.round(Math.random()*100);
		accFontSize.setCookie(accFontSize.cookiename,this.int,null);
		if(parseInt(accFontSize.getCookie(accFontSize.cookiename))===this.int) {
			return true;
		}
		return false;
	},
	cssGetDefaults: function() {
		this.oElement=document.body, this.oFontSize;
		if(window.getComputedStyle) {
			this.oFontSize=window.getComputedStyle(this.oElement,null).fontSize;
		}
		else if(this.oElement.currentStyle) {
			this.oFontSize=this.oElement.currentStyle.fontSize;
		}
		this.fs=new Array();
		this.fs[0]=parseFloat(this.oFontSize);
		this.fs[1]=this.fs[0].toString();
		this.fs[1]=this.oFontSize.slice(this.fs[1].length);
		return this.fs;
	},
	prepHTML: function() {
		document.getElementById('accFontSize').innerHTML='<span style="font-size: 80%;"><a href="javascript: accFontSize.decrement();">A</a></span><span style="font-size: 100%;"><a href="javascript: accFontSize.reset();">A</a></span><span style="font-size: 120%;"><a href="javascript: accFontSize.increment();">A</a></span>';
	},		
	onload: function() {
		if(accFontSize.getCookie(accFontSize.cookiename)===null) {
			if(accFontSize.cookieCheck()===true) {
				this.defaults=accFontSize.cssGetDefaults();
				accFontSize.setCookie(accFontSize.cookiename,this.defaults[0]+";"+this.defaults[1]+";"+1,null);
				accFontSize.prepHTML();
			}
		}
		else {
			this.defaults=accFontSize.cssGetDefaults();
			this.fs=accFontSize.getCookie(accFontSize.cookiename);
			this.fs=this.fs.split(';');
			document.body.style.fontSize=this.defaults[0]*this.fs[2]+this.defaults[1];
			accFontSize.prepHTML();
		}
	},
	increment: function() {
		this.fs=accFontSize.getCookie(accFontSize.cookiename);
		this.fs=this.fs.split(';');
		for(this.pos=0;this.pos<accFontSize.multiples.length-1;this.pos++) {
			if(this.fs[2]==accFontSize.multiples[this.pos]) {
				break;
			}
		}
		if(this.pos<accFontSize.multiples.length-1) {
			this.pos+=1;
			document.body.style.fontSize=this.fs[0]*accFontSize.multiples[this.pos]+this.fs[1];
			accFontSize.setCookie(accFontSize.cookiename,this.fs[0]+';'+this.fs[1]+';'+accFontSize.multiples[this.pos],null);
		}
	},
	reset: function() {
		this.fs=accFontSize.getCookie(accFontSize.cookiename);
		this.fs=this.fs.split(';');
		document.body.style.fontSize=this.fs[0]+this.fs[1];
		accFontSize.setCookie(accFontSize.cookiename,this.fs[0]+';'+this.fs[1]+';'+1,null);
	},
	decrement: function() {
		this.fs=accFontSize.getCookie(accFontSize.cookiename);
		this.fs=this.fs.split(';');
		for(this.pos=0;this.pos<accFontSize.multiples.length-1;this.pos++) {
			if(this.fs[2]==accFontSize.multiples[this.pos]) {
				break;
			}
		}
		if(this.pos>0) {
			this.pos-=1;
			document.body.style.fontSize=this.fs[0]*accFontSize.multiples[this.pos]+this.fs[1];
			accFontSize.setCookie(accFontSize.cookiename,this.fs[0]+';'+this.fs[1]+';'+accFontSize.multiples[this.pos],null);
		}
	},
	init: function() {
		document.onload=accFontSize.onload();
	}
};