/**
 * Set cookie variables
 */
var Cookie = {
	set: function(name, value, daysToExpire) {
    	var expire = '';
    	if (daysToExpire != undefined) {
      		var d = new Date();
      		d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      		expire = '; expires=' + d.toGMTString();
    	}
    	return document.cookie = escape(name)+"="+escape(value || '') + expire+"; path=/";
    	
  	},
  	get: function(name) {
    	var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    	return (cookie ? unescape(cookie[2]) : null);
  	},
  	erase: function(name) {
    	var cookie = Cookie.get(name) || true;
    	Cookie.set(name, '', -1);
    	return cookie;
  	},
  	accept: function() {
    	if (typeof navigator.cookieEnabled == 'boolean') {
      		return navigator.cookieEnabled;
    	}
    	Cookie.set('_test', '1');
    	return (Cookie.erase('_test') == '1');
	}
}; 


/**
 * Increase the size of the text and store factor in cookie
 */
function increaseText() {
	var textsizeAddition = Cookie.get('TEXTSIZE');
	if(textsizeAddition == ''){
		textsizeAddition = 0;
	}
	
	/* var texts = $$('td.middle'); */
	var texts = $$('div.fontdiv');
	texts.each(function(thisText){
		var allTextelems = thisText.descendants();
		
		/* update each element */
		allTextelems.each(function(textelem){
			var currentSize = textelem.getStyle('fontSize'); 
			
			var newSize = parseInt(currentSize.sub(/px/, '')) + 1;
			textelem.setStyle({
				fontSize: newSize + 'px'
				
			});
		});
	});
	var updateCookie = parseInt(textsizeAddition) + parseInt(1);
	Cookie.set('TEXTSIZE', updateCookie);
}

/**
 * Decrease the size of the text and store factor in cookie
 */
function decreaseText() {
	var textsizeAddition = Cookie.get('TEXTSIZE');
	if(textsizeAddition == ''){
		textsizeAddition = 0;
	}
	var texts = $$('div.fontdiv');
	texts.each(function(thisText){
		var allTextelems = thisText.descendants();
		/* update each element */
		allTextelems.each(function(textelem){
			var currentSize = textelem.getStyle('fontSize'); 
			var newSize = parseInt(currentSize.sub(/px/, '')) - 1;
			textelem.setStyle({
				fontSize: newSize + 'px'
			});
		});
	});
	var updateCookie = parseInt(textsizeAddition) - parseInt(1);
	Cookie.set('TEXTSIZE', updateCookie);
}

/**
 * Prints the content of the page
 */
function printContent() {
	if($$('div.fontdiv').length > 0){
		var pageContent = $$('div.fontdiv').first().innerHTML;
		pageContent = pageContent.gsub(/(display|DISPLAY):\s*none/, 'display: block');
		var stylesheets = $$('link');
		var popupWin = window.open("", "newwin");
		// Add title
		popupWin.document.write('<html><head><title>Pagina - Printversie</title>');
		// Add stylesheets
		stylesheets.each(function(stylesheet){
			if(stylesheet.media == 'print'){
				popupWin.document.write('<link href="'+stylesheet.href+'" rel="stylesheet" type="text/css" media="screen, print"/>');
			}
		});
		popupWin.document.write('</head>');
		
		// Add body and content
		popupWin.document.write('<body>'+pageContent+'</body></html>');
		popupWin.document.close();
		popupWin.print();
	}
}
