// BEGIN FONT SELECTOR FUNCTIONS

// -------------------------------------------------- //
//adjustable properties //


var fontresizingArray = [
							'mbBody',
							'mbBodyAlt',
							'mbBodyRed',
							'mbquote',
							'mbBodySmall',
							'mbLinks',
							'mbLinksSmall',
							'mbLinksLarge'
						];

var smallNum = -1;
//this should be the default
var mediumNum = 0;
var largeNum = 2;
var xlargeNum = 3;

var cookiename = 'fontsizeoffset'; //has to be numeric value for the cookie

// -------------------------------------------------- //
var smallName ="small" 
var mediumName ="medium"
var largeName ="large"
var xlargeName ="xlarge"

var rHistory = new Array();
var lastoffset = 0;
					
function LoadCookieOffset() 
{

	var fontCookie = new Cookie(cookiename);
	fontCookie.load(); 
	return fontCookie._value;

	if (!(fontsizeoffset))
		return 0;

	return 0;
}

function setFont(fontsetting) 
{
	var d = false;
//	if (confirm('show debug messages?')) d = true	
	
	var i, font_a, currentfontsize, newfontsize, font_a_collection;
 
	var newoffset = OffsetSelector( fontsetting );
	var cookieoffset = LoadCookieOffset();

	if (d)
	{
		alert('newoffset       : ' + newoffset + '\n' + 
		      'LoadCookieOffset: ' + LoadCookieOffset());
	}
	
	if (newoffset == cookieoffset)
		return;
	else 
		lastoffset = cookieoffset;

	rHistory[rHistory.length] = newoffset;
	
 
 	//if no elements have been found quit
	if ( (!(fontresizingArray)) || (fontresizingArray.length == 0) ) 
		return

		font_a_collection = getElementbyClass(fontresizingArray);

		//if nothing found quit
		if ( (!(font_a_collection)) || (font_a_collection.length == 0) ) 
			return

		for (i=0; i<font_a_collection.length; i++) 
		{
	 		font_a = font_a_collection[i];
	  
	  		currentfontsize = getCurrentFontsizeCSSValue(font_a);
	  
	  		newfontsize = (currentfontsize - lastoffset) + (newoffset);

			if (d)
			{
	  			alert('currentfontsize: ' + currentfontsize + '\n' + 
	  					'newoffset      : ' + newoffset + '\n' + 
						'newfontsize : ' + newfontsize + '\n' + 
						'lastoffset : ' + lastoffset + '\n\n' + 
						rHistory.join(','));
	  		}
			
	  		font_a.style.fontSize = newfontsize + "px";
	  
		}

	lastoffset = newoffset
	
    var fontCookie = new Cookie(cookiename, newoffset, '01/01/2015', '/', 'sylviabrowne.com');
	fontCookie.save();

	document.getElementById('fontsize_small').className = 'fontsize_small_rollover';
	document.getElementById('fontsize_medium').className = 'fontsize_medium_rollover';
	document.getElementById('fontsize_large').className = 'fontsize_large_rollover';
	document.getElementById('fontsize_xlarge').className = 'fontsize_xlarge_rollover';
	
	document.getElementById('fontsize_'+fontsetting).className = 'fontsize_'+fontsetting+'_active';
}


function getCurrentFontsizeCSSValue(elem) {

	var currentfontsize;
	
	if (document.all)	 {
	  //ie
	  currentfontsize = elem.currentStyle.fontSize;
	}  else  {
	  //mozilla  
	  currentfontsize = document.defaultView.getComputedStyle(elem, '').getPropertyValue('font-size');
	 }
	
	  currentfontsize = currentfontsize.replace('px','');
	  currentfontsize = parseInt( currentfontsize);
	  
	  return currentfontsize;
}

function OffsetSelector(fontsetting) 
{
    if (fontsetting == smallName) {
        return smallNum
    } else if (fontsetting == mediumName) {
        return mediumNum
    } else if (fontsetting == largeName) {
        return largeNum
    } else if (fontsetting == xlargeName) {
        return xlargeNum
	} else {
		return mediumNum
	}
} 


function OffsetNameSelector(offset) 
{
    if (offset == smallNum) {
        return smallName
    } else if (offset == mediumNum) {
        return mediumName
    } else if (offset == largeNum) {
        return largeName
    } else if (offset == xlargeNum) {
        return xlargeName
	} else {
		return mediumName
	}
} 

function getElementbyClass(classnameArray){
 var inc=0;
 var classnamecollection = new Array();
 var alltags=document.all? document.all : document.getElementsByTagName("*");
 
 var classnameArrayStr = classnameArray.join("|") + "|";
 
 for (i=0; i<alltags.length; i++){
   if (alltags[i].className.length>0)
   { 
	if ( classnameArrayStr.indexOf(alltags[i].className + "|")>=0 )
    {
     classnamecollection[inc++]=alltags[i];
	}
   }
 }

 return classnamecollection;

} 

function getElementbyClass1(classname){
 var inc=0;
 var classnamecollection = new Array();
 var alltags=document.all? document.all : document.getElementsByTagName("*");
 for (i=0; i<alltags.length; i++){
   if (alltags[i].className==classname)
     classnamecollection[inc++]=alltags[i];
 }

 return classnamecollection;

} 

// END FONT SELECTOR FUNCTIONS



// HELPER COOKIE FUNCTIONS

Cookie = function(name, value, expiration, path, domain, secure) {
    this._name = name;
    this._value = value;
	this._expiration = (expiration) ? new Date(expiration) : null;
    this._path = (path) ? path : null;
    this._domain = (domain) ? domain : null;
    this._secure = (secure) ? secure : false;
}

Cookie.prototype.save = function() {
    var nameValuePairs = new Array();
    for (var prop in this) {
        if (prop.charAt(0) != "_" && typeof this[prop] != "function") {
			nameValuePairs.push(escape(this[prop]));
        }
    }

    var cookie = this._name + "=" + this._value;
    if (this._expiration) {
        cookie += "; expires=" + this._expiration.toGMTString();
    }
    if (this._path) {
        cookie += "; path=" + this._path;
    }
    if (this._domain) {
        cookie += "; domain=" + this._domain;
    }
    if (this._secure) {
        cookie += "; secure";
    }

    // Write the cookie
    document.cookie = cookie;
}


Cookie.prototype.load = function() {
    var allCookies = document.cookie;
    if (allCookies != "" && allCookies.indexOf(this._name) != -1) {
        var startPos = allCookies.indexOf(this._name) + (this._name.length + 1);
        var endPos = (allCookies.indexOf(";", startPos) != -1) ? allCookies.indexOf(";", startPos) : allCookies.length;
        var cookie = allCookies.substring(startPos, endPos);
        this._value = unescape(cookie);

        return true;
    }
    else {
        return false;
    }
}


// END HELPER COOKIE FUNCTIONS


function renderReadability() {
	document.write('<table width=100><tr>');
	document.write('<td id="fontsize_small" class="fontsize_small_rollover"><a href="#" onclick="setFont(\'small\'); return false;"></a></td>');
	document.write('<td id="fontsize_medium" class="fontsize_medium_rollover"><a href="#" onclick="setFont(\'medium\'); return false;"></a></td>');
	document.write('<td id="fontsize_large" class="fontsize_large_rollover"><a href="#" onclick="setFont(\'large\'); return false;"></a></td>');
	document.write('<td id="fontsize_xlarge" class="fontsize_xlarge_rollover"><a href="#" onclick="setFont(\'xlarge\'); return false;"></a></td>');
	document.write('</tr></table>');

	var fontsetting = OffsetNameSelector(LoadCookieOffset());

   if (fontsetting) 
   { 
    document.getElementById('fontsize_'+fontsetting).className = 'fontsize_'+fontsetting+'_active'; 
   } else {
    document.getElementById('fontsize_'+'medium').className = 'fontsize_medium_active';       
   }      

}