/*
 * SICI Templates
 * Copyright (C) 2005-2007
 *     Amministrazione Provinciale di Pesaro e Urbino -- Italy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * The GNU General Public License can be found at
 * http://www.gnu.org/copyleft/gpl.html.
 * A copy is found in the textfile GPL.txt
 *
 * Font Resize Script
 *
 */

rsz = null;

function frszInit(sId) {
    if(!sId) sId = 'tmpl.sici.body';
    if(!rsz) rsz = new FontResizer(document.getElementById(sId));
    return rsz;
}

function frszInc(sId) {
    frszInit(sId);
    rsz.increase();
    return false;
}

function frszDec(sId) {
    frszInit(sId);
    rsz.decrease();
    return false;
}

function frszRes(sId) {
    frszInit(sId);
    rsz.reset();
    return false;
}

function FontResizer(domNode) {
    this.cookieName = "frszCurSizeIndex";
    this.domNode = domNode;
    this.curSizeIndex = -1

    if(!this.domNode) return;

    this.classList = Array('frsz-S', 'frsz-M', 'frsz-L', 'frsz-XL', 'frsz-XXL');
    this.curSizeIndex = this.loadSize();
    //this.curSizeIndex = 1;
    this.doUpdate();
}

FontResizer.prototype.increase = function () {
    if(this.curSizeIndex > -1 
       && this.curSizeIndex < this.classList.length - 1) {
	this.curSizeIndex++;
	this.saveSize();
	this.doUpdate();
    }
}

FontResizer.prototype.decrease = function () {
    if(this.curSizeIndex > 0) {
	this.curSizeIndex--;
	this.saveSize();
	this.doUpdate();
    }
}

FontResizer.prototype.reset = function () {
    if(this.curSizeIndex >= 0) {
	this.curSizeIndex = 1
	this.saveSize();
	this.doUpdate();
    }
}

FontResizer.prototype.saveSize = function () {
    var name = this.cookieName;
    var value = this.curSizeIndex;
    /** @see http://www.quirksmode.org/js/cookies.html */
    var date = new Date();
    date.setTime(date.getTime()+(1*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = name+"="+value+expires+"; path=/";
}

FontResizer.prototype.loadSize = function () {
    var name = this.cookieName;
    var retval = 1;
    /** @see http://www.quirksmode.org/js/cookies.html */
    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) {
	    retval = parseInt(c.substring(nameEQ.length,c.length));
	    if(retval == null) {
		retval = 1;
	    }
	    break;
	}
    }
    return retval;
}

FontResizer.prototype.doUpdate = function () {
    this.domNode.className = this.classList[this.curSizeIndex];
}



