﻿
var currentFontSize = 1;
var defaultFontSize = 13;

function changeFontSize(size) {
    var bodyTag = document.getElementsByTagName('body')[0];
    /*for (i = 0; i < p.length; i++) {
        
    }*/
    var found = bodyTag.style.fontSize;

    var fontSizeMultiplier = parseFloat(size) / parseFloat(currentFontSize);

    if (found.indexOf("em") != -1) {
        var currBodySize = parseFloat(bodyTag.style.fontSize.replace("em", ""));
        if (currBodySize) {
            var nuBodySize = currBodySize * fontSizeMultiplier;
            bodyTag.style.fontSize = nuBodySize.toString() + 'em';
        }
    } else if (found.indexOf("px") != -1) {
        var currBodySize = parseFloat(bodyTag.style.fontSize.replace("px", ""));
        if (currBodySize) {
            var nuBodySize = 0;
            if (fontSizeMultiplier > 1) {
                nuBodySize = Math.ceil(currBodySize * fontSizeMultiplier);
            } else {
                nuBodySize = Math.floor(currBodySize * fontSizeMultiplier);
            }
            
            bodyTag.style.fontSize = nuBodySize.toString() + 'px';
        }
    }
    
    if (!found) {
        var nuBodySize = defaultFontSize * fontSizeMultiplier;
        bodyTag.style.fontSize = nuBodySize.toString() + 'px';
    }
    


    var tags = document.getElementsByTagName('*');
    for (i = 0; i < tags.length; i++) {
        if (tags[i].style.fontSize && tags[i].tagName != "BODY") {
            var currTag = tags[i];
            var curr = parseFloat(currTag.style.fontSize.replace("px", ""));
            if (curr) {
                if (fontSizeMultiplier > 1) {
                    nu = Math.ceil(curr * fontSizeMultiplier);
                } else {
                    nu = Math.floor(curr * fontSizeMultiplier);
                }
                tags[i].style.fontSize = nu.toString() + 'px';
            }
        }
    }
    
    var curdate = new Date()
    curdate.setUTCMonth(curdate.getUTCMonth() + 1);
    
    //alert('textsizecookie='+size+'; expires='+ curdate.toGMTString() +'; path=/');
    document.cookie = 'textsizecookie=' + size + '; expires=' + curdate.toGMTString() + '; path=/';
    currentFontSize = size;
}


function setFontSize(){

    var size = readCookie("textsizecookie");
    
    if((size != null)&&(size!="")){
        changeFontSize(size);
    }
}

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;
}

