/*
All code below was originally written by Eric Costello, but has been modified
for use in PikiePikie by Steve Pike.
You are free to use, modify and distribute it. Please leave this 
comment block intact so that people can find the original unmodified
version at: http://www.glish.com/css/blogger/

Also see:
http://www.schwa.com
http://www.glish.com
*/

var default_fs = getStyleByTag('div','fontSize');
var default_ff = 'Verdana, Arial, Helvetica, sans-serif'

// read the cookie to get the proper font family, set to default Verdana if a first timer
var ff = readCookie('fontFamily');

// read the cookie to get the proper font size, set to default if a first timer
var fs = parseInt(readCookie('fontSize'));
if (!fs>0)
	fs = default_fs
	//fs=0;

var body_fs, h1_fs, h2_fs, h3_fs, h4_fs, h5_fs, h6_fs;

if (fs>0) {
	setSizes();

	document.writeln('<style>');
	document.writeln('body{font-size:'+body_fs+'px;}');
	document.writeln('h1{font-size:'+h1_fs+'px;}');
	document.writeln('h2{font-size:'+h2_fs+'px;}');
	document.writeln('h3{font-size:'+h3_fs+'px;}');
	document.writeln('h4{font-size:'+h4_fs+'px;}');
	document.writeln('h5{font-size:'+h5_fs+'px;}');
	document.writeln('h6{font-size:'+h6_fs+'px;}');
	document.writeln('<\/style>');
}

if (ff!='') {
	document.writeln('<style>');
	document.writeln('body{font-family:'+ff+';}');
	document.writeln('<\/style>');
}

// called from body onload
function init() {
	setSizes()
	changeType();
}

// this function is called from the A+ button
function increaseSize() {
	if (fs<1) {
		fs=11;
	}
	fs+=1;
	setSizes();
	changeType();
}

// this function is called from the A- button
function decreaseSize() {
	if (fs>0) {
		if (body_fs>1) {
			fs-=1;
			setSizes();
			changeType();
		}
	}
}


// cookie functions modified from code found at Alexei Kourbatov's javascripter.net/faq/
function clearCookie(cookieName) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() - 3600000*24*3000);
	document.cookie = cookieName + "=''" + ";expires="+expire.toGMTString();
}

// cookie functions modified from code found at Alexei Kourbatov's javascripter.net/faq/
function setCookie(cookieName,cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*3000);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
	
function readCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

// thanks to randomwalks.com for this code
function targetLinks(boNew) {
	if (boNew) 
		where = "_blank";
	else
		where = "_self";
	for (var i=0; i<=(document.links.length-1); i++) {
		document.links[i].target = where;
	}
}

// These 2 setstyle functions were modified from code by Steven Champeon found at
// http://developer.apple.com/internet/_javascript/styles.html

// setStyleByTag: given an element type, style property and value
// args:
//  e - element type or id
//  p - property
//  v - value
function setStyleByTag(e, p, v) {
	var elements = document.getElementsByTagName(e);
	for(var i = 0; i < elements.length; i++) {
		elements.item(i).style[p] = v;
	}
}

function getStyleByTag(e, p) {
	var elements = document.getElementsByTagName(e);
	if (elements.length)
		return elements.item(0).style[p];
	return ''
}

// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
// 
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

function setDefaults() {
	//fs = default_fs
	fs = 11
	ff = default_ff
	setSizes();
	changeType();
	clearCookie('fontSize');
	clearCookie('fontFamily');
}
	
// this changes the sizes of the various elements relative to the fs
function setSizes() {
	if (fs>0) {
		body_fs = fs
		h1_fs = body_fs+6;
		h2_fs = body_fs+5;
		h3_fs = body_fs+4;
		h4_fs = body_fs+3;
		h5_fs = body_fs+2;
		h6_fs = body_fs+1;
	}
}

function changeType() {
	if (!document.getElementsByTagName) {return false;} // unclean! unclean!
	// because NS6 seems to freak out on abs. positionined divs
	// when you change a style property of the body, we have to
	// set the fontFamily and fontSize on div elements and not the body
	if (ff!='') {
		setStyleByTag('div','fontFamily',ff);
		setCookie('fontFamily',ff);
	}

	if (fs>0) {
		setStyleByTag('div','fontSize',body_fs+'px');
		setStyleByTag('h1','fontSize',h1_fs+'px');
		setStyleByTag('h2','fontSize',h2_fs+'px');
		setStyleByTag('h3','fontSize',h3_fs+'px');
		setStyleByTag('h4','fontSize',h4_fs+'px');
		setStyleByTag('h5','fontSize',h5_fs+'px');
		setStyleByTag('h6','fontSize',h6_fs+'px');
		setCookie('fontSize',fs);
	}
}
