function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* Start font size handler */

var defSize = 1.0;
var minSize = 0.9;
var maxSize = 1.5;
var sizeVar = 0.1;
var sizeUnit = "em";
//writeCookie("resize", defSize, 10000);
writeCookie("resize", defSize, 10000);

// Set Body text size
function setSize(iSize) {
	if (!document.getElementById) return false;

	var bodyTag = document.getElementById("bloque_portada");

	if (bodyTag) {
		if (!bodyTag.style.fontSize) {
			if (!readCookie("resize")) {
				bodyTag.style.fontSize = defSize + sizeUnit;
			}
			else {
				bodyTag.style.fontSize = ((parseFloat(readCookie("resize"))) + sizeUnit);
			}
		}
		else {
			if (!readCookie("resize")) {
				bodyTag.style.fontSize = defSize + sizeUnit;
			}
			else {
				var newSize = parseFloat(iSize) + parseFloat(readCookie("resize"));
				if ((newSize <= maxSize) && (newSize >= minSize))
					bodyTag.style.fontSize = newSize + sizeUnit;
			}
		}
		cookieValue = bodyTag.style.fontSize;
		writeCookie("resize", cookieValue, 10000);
	}
}

// Attaches the onclick event to the correct ids to allow resizing
function resizeT(iClass, iDir) {
	var incLink = getElementsByClassName(iClass);

	for (var i=0; i < incLink.length; i++) {
		if (iDir == "decrease") {
			incLink[i].onclick = function() {
				setSize("-" + sizeVar + sizeUnit);
				return false;
			}
		}
		if (iDir == "increase") {
			incLink[i].onclick = function() {
				setSize(sizeVar + sizeUnit);
				return false;
			}
		}
	}
}

// add increase / decrease font event handlers
addLoadEvent( function(){ resizeT("fntIncrease", "increase") } );
addLoadEvent( function(){ resizeT("fntDecrease", "decrease") } );

// set base font size
addLoadEvent( function(){ setSize(defSize + sizeUnit) } );

// Writes cookie
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name, value, hours) {
	var expire = "";
	if(hours != null) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}

// Read Cookie
// alert( readCookie("myCookie") );
function readCookie(name){
	var cookieValue = "";
	var search = name + "=";

	if(document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);

	if (offset != -1) {
		offset += search.length;

	end = document.cookie.indexOf(";", offset);

	if (end == -1) end = document.cookie.length;
		cookieValue = unescape(document.cookie.substring(offset, end)) }
	}

	return cookieValue;
}


// Gets all elements with the specified class
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

/* End font size handler */

function makeArray(n){
	  this.length = n;
	  for (i=1;i<=n;i++){
	    this[i]=0;
	  }
	  return this;
}


