var idNames = new Array('top','articles');
var theRules = {
		'#top h3' : function(el){
			el.onclick = function(){
				if (Element.hasClassName(this, 'invisible')) {
					new Effect.BlindDown('top-body');
					Element.removeClassName(this, 'invisible');
					setCookie(this.parentNode.id, '', 365);
				} else {
					new Effect.BlindUp('top-body');
					Element.addClassName(this, 'invisible');
					setCookie(this.parentNode.id, 'invisible', 365);
				}
			}		
		},
		'#articles h3' : function(el){
			el.onclick = function(){
				if (Element.hasClassName(this, 'invisible')) {
					new Effect.BlindDown('articles-body');
					Element.removeClassName(this, 'invisible');
					setCookie(this.parentNode.id, '', 365);
				} else {
					new Effect.BlindUp('articles-body');
					Element.addClassName(this, 'invisible');
					setCookie(this.parentNode.id, 'invisible', 365);
			}
		}		
	}
};

Behaviour.register(theRules);
Behaviour.addLoadEvent(do_onload);

function do_onload() {
		
		// Id names of all the "boxes"
		boxIds = $("top","articles");	
		
		for (i = 0; i < boxIds.length; i++) {
			if (boxIds[i]) {
				cookieValue = readCookie(boxIds[i].id);
				if (cookieValue == 'invisible') {
					var h3 = boxIds[i].getElementsByTagName('h3');
					Element.addClassName(h3[0], 'invisible');
					var kids = boxIds[i].childNodes;
					for (j = 1; j < kids.length; j++) {
						if (kids[j].id) {
							Element.hide(kids[j]);
						}
					}
				}
			}		
		}	
	}
	
	function setCookie(name,value,days) {
	 if (days) {
		 var date = new Date();
		 date.setTime(date.getTime()+(days*24*60*60*1000));
		 var expires = ";expires="+date.toGMTString();
	 } else {
		 expires = "";
	 }
	 document.cookie = name+"="+value+expires+";path=/";
	}
		
	function readCookie(name) {
	 var needle = name + "=";
	 var cookieArray = document.cookie.split(';');
	 for(var i=0;i < cookieArray.length;i++) {
		 var pair = cookieArray[i];
		 while (pair.charAt(0)==' ') {
			 pair = pair.substring(1, pair.length);
		 }
		 if (pair.indexOf(needle) == 0) {
			 return pair.substring(needle.length, pair.length);
		 }
	 }
	 return null;
	}