/*
em.js
By Eiki Martinson for eikimartinson.com
June 2008

Resizes line-height and margins to maintain vertical rhythm across p's
containing images. Requires the height attribute to be set for all
images (as it should be anyway!).

Needs textresizedetector.js, by Lawrence Carvalho
*/

window.onload = function() 
{
    gridcookie();
}

window.onunload = function() 
{
    griduncookie();
}

var vertgridbox = 0;

function onFontResize(e,args) {
/*    var msg = "\nThe base font size in pixels: " + args[0].iBase;
    msg +="\nThe current font size in pixels: " + args[0].iSize;
    msg += "\nThe change in pixels from the last size:" + args[0].iDelta;
    alert(msg);*/
}
//function to call once TextResizeDetector has init'd
TextResizeDetector.USER_INIT_FUNC = vertical;

function vertical()
{
    var iBase = TextResizeDetector.addEventListener(onFontResize,null);
    var baselineems = 1.5; //in ems
    var baselinepx  = iBase*baselineems;  //in px
    var thumbborder = 2;   //in px
    var paras = document.getElementsByTagName('p');
    for(i=0;i<paras.length;i++) {    
	var imgs = paras[i].getElementsByTagName('img');
        if(imgs.length) {
	    var max = 0;
	    for(j=0;j<imgs.length;j++) {
		// find tallest - thumbs have a 2px border additionally
		if(imgs[j].className == 'thumb') {
		    if(imgs[j].height + thumbborder*2 > max) {
			max = imgs[j].height + thumbborder*2;
		    }
		}
		else {
		    if(imgs[j].height > max) {
			max = imgs[j].height;
		    }
		}
	    }
	    // set line-height and margins appropriately now that we know the maximum height
	    if(max % baselinepx == 0) {
		max++;
	    }
	    else {
		if(max % baselinepx == baselinepx - 1) {
		    max = max + 2;
		}
	    }
	    var lheight = Math.ceil(max/baselinepx)*baselineems;
	    var style = 'line-height: ' + lheight + 'em;';
	    paras[i].style.cssText = style;
	    paras[i].setAttribute('style',style);	   
	}
    }
}


function vertgrid(check)
{
    var body = document.getElementsByTagName('body');
    if(check) {	
	var style = "background-image: url(/testrule.png);";
	vertgridbox = 1;
    }
    else {
	var style = "background-image: none;";
	vertgridbox = 0;
    }
    body[0].style.cssText = style;
    body[0].setAttribute('style',style);
}


function gridcookie() {
    var vertgridcheckbox = document.getElementById('vertgridcheckbox');
    var cookie = readCookie("grid");
    if(cookie == 1) {
	vertgrid(1);
	vertgridcheckbox.checked = true;
    }
    else {
	if(vertgridcheckbox.checked) {
	    vertgrid(1);
	    vertgridcheckbox.checked = true;
	}
	else {
	    vertgrid(0);
	    vertgridcheckbox.checked = false;
	}
    }
}

function griduncookie() {
    if(vertgridbox) {
	createCookie("grid", 1, 365);
    }
    else {
	clearCookie("grid");
    }
}


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


function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function clearCookie(name)
{
    var date = new Date ( ); 
    date.setTime(date.getTime() - 1); //one second in the past
    document.cookie = name + "=0; expires=" + date.toGMTString()+"; path=/";
}
