var cookie_name = "PreferredFontSize";
var font_clicks = 0;
var today = new Date();

function changeFontSize(increment) {
	var pClass;
	var fontSz;
	var myRules;
	// add logic here to find out wich stylesheet number we have to use ([3] in admin, [0] in normal mode)
	MSIERules = (document.styleSheets[0].rules);
	myRules = MSIERules ? document.styleSheets[0].rules : document.styleSheets[0].cssRules;
	font_clicks = font_clicks + increment;
	if (font_clicks < 0) {
		font_clicks=0;
		increment=0;
	}
	for (i = 0; i < myRules.length; i++) {
		if (MSIERules) {
			// internet explorer
			selector = myRules.item(i).selectorText.toUpperCase();
			if (selector == '.INTRODUCTION' || selector == '.FIRSTHEADING' ||
				selector == '.SECONDHEADING' || selector == '.ARTICLECOLUMN' ||
				selector == '.INFOHEADER' || selector == '.INFOHEADERBOX' || selector == '.BACKTOTOP' ||
				selector == 'H1' ||	selector == 'H2' ||	selector == 'H3' ||	selector == 'H4' ||
				selector == 'H5' ||	selector == 'H6' ||	selector == 'H7' ||
				selector == '.ARTICLECOLUMNFULL') {
				if (myRules.item(i).style.fontSize) {
					fontSz = parseInt(myRules.item(i).style.fontSize.substr(0, myRules.item(i).style.fontSize.length-2)) + increment + 'px';
					if (document.styleSheets[0]) {
						document.styleSheets[0].rules.item(i).style.fontSize = fontSz;
					}
				}
			}
		}
		else {
			// rest of the browsers
			pClass = myRules.item(i);
			selector = myRules.item(i).selectorText.toUpperCase();
			if (selector == '.PATHLINE' || selector == '.FIRSTHEADING' ||
				selector == '.SECONDHEADING' || selector == '.ARTICLECOLUMN' ||
				selector == '.INFOHEADER' || selector == '.BACKTOTOP' ||
				selector == 'H1' ||	selector == 'H2' ||	selector == 'H3' ||	selector == 'H4' ||
				selector == 'H5' ||	selector == 'H6' ||	selector == 'H7' ||
				selector == '.ARTICLECOLUMNFULL') {
				
				if (myRules.item(i).style.fontSize) {
					fontSz = parseInt(pClass.style.fontSize.substr(0, pClass.style.fontSize.length-2)) + increment+'px';
					if (document.styleSheets[0]) {
						document.styleSheets[0].cssRules.item(i).style.fontSize = fontSz;
					}
				}
			}
		} 
	}
	// save the user preference as a cookie
	var nextyear=new Date();
	nextyear.setFullYear(nextyear.getFullYear()+1);
	SetCookie(cookie_name,escape(font_clicks),nextyear,"/");
}
 
function resetFontSize() {
   changeFontSize(-font_clicks);
}

function GetCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return GetCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}
	
function clock(pLanguage) {
	var t = new Date();
	var h = t.getHours();
	var m = t.getMinutes();
	var s = t.getSeconds();
	var units = new String("");
	var hours = t.getHours();
	var min = t.getMinutes();
	var sec = t.getSeconds();
	if (hours < 10)	{
		hours = "" + hours;
	}
	if (min < 10) {
		min = "0" + min;
	}
	if (sec < 10) {
		sec = "0" + sec;
	}
	if (pLanguage == "FR") {
		document.getElementById("clock_date").innerHTML = DateString(pLanguage);
        document.getElementById("clock_time").innerHTML = hours + ":" + min ;
        window.setTimeout("clock('FR')", 1000);
	}
	if (pLanguage == "NL") {
		document.getElementById("clock_date").innerHTML = DateString(pLanguage);
        document.getElementById("clock_time").innerHTML = hours + ":" + min;
		window.setTimeout("clock('NL')", 1000);
	}
	if (pLanguage == "EN") {
		document.getElementById("clock_date").innerHTML = DateString(pLanguage);
        document.getElementById("clock_time").innerHTML = hours + ":" + min;
		window.setTimeout("clock('EN')", 1000);
	}
}

function GetDayFR(nDay) {
	var Days = new Array("dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi");
	return Days[nDay];
}

function GetDayEN(nDay) {
	var Days = new Array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
	return Days[nDay];
}

function GetDayNL(nDay) {
	var Days = new Array("zondag","maandag","dinsdag","woensdag","donderdag","vrijdag","zaterdag");
	return Days[nDay];
}

function GetMonthFR(nMonth) {
	var Months = new Array("janvier","f&eacute;vrier","mars","avril","mai","juin","juillet","ao&ucirc;t","septembre","octobre","novembre","d&eacute;cembre");
	return Months[nMonth];
}

function GetMonthEN(nMonth) {
	var Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	return Months[nMonth];
}

function GetMonthNL(nMonth) {
	var Months = new Array("januari","februari","maart","april","mei","juni","juli","augustus","september","oktober","november","december");
	return Months[nMonth];
}

function DateString(pTaal) {
	if (pTaal == "NL") {
		var strDate = toProperCase(GetDayNL(today.getDay())) + " " + today.getDate();
		strDate += " " + GetMonthNL(today.getMonth()) + " " + today.getFullYear();
		return strDate
	}
	if (pTaal == "FR") {
		var strDate = toProperCase(GetDayFR(today.getDay())) + " " + today.getDate();
		strDate += " " + GetMonthFR(today.getMonth()) + " " + today.getFullYear();
		return strDate
	}
	if (pTaal == "EN") {
		var strDate = toProperCase(GetDayEN(today.getDay())) + " " + today.getDate();
		strDate += " " + GetMonthEN(today.getMonth()) + " " + today.getFullYear();
		return strDate
	}
}

function toProperCase(s) {
	return s.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}

function alternateRows() {
	el = document.getElementsByTagName("table");
	for (i = 0; i < el.length; i++) {
		if (el[i].className == "ezcmstables") {
			rows = el[i].getElementsByTagName("tr");
			for (j = 0; j < rows.length; j++) {
				rows[j].className = "TableRow" + (j % 2);
			}
		}
	}
}

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 

//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr');

//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function ts( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 6 ) sz = 6;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}

window.onload = function() {

	// get the user preference
	var cookie = GetCookie(cookie_name);
	if (cookie != null) {
	    //changeFontSize(parseInt(unescape(cookie)));
	}
	externalLinks();
	alternateRows();
}
