/* http://www.quirksmode.org/js/cookies.html */

function createCookie( name, value, days ) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie( name ) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie( name ) {
	createCookie(name,"",-1);
}

/*** Modules ***/

function module_swapDisplay( element )
{
	var moduleContent    = $( element + 'modcontent' );
	if( moduleContent.getStyle( 'display' ) == 'none' ) {
		Effect.BlindDown( moduleContent, { duration: 0.4 } );
		createCookie( element + 'mod', 'open');
	}
	else {
		Effect.BlindUp( moduleContent, { duration: 0.4 } );
		createCookie( element + 'mod', 'closed');
	}
}

function checkModules() {
	var modules = ['cactusnews', 'costaricanews', 'culture', 'cal', 'forum', 'weather', 'products', 'chat'];

	for ( i in modules ) {
		if (readCookie(modules[i] + 'mod') == null) {
			createCookie( modules[i] + 'mod', 'closed');
		}
		if (readCookie(modules[i] + 'mod') == 'open') {
			$( modules[i] + 'modcontent' ).show();
		}
		/*if (readCookie(modules[i] + 'mod') == 'closed') {
			$( modules[i] + 'modcontent' ).hide();
		}*/
	}
}

/*** Menus ***/

function menu_swapDisplay ( event ) {
	if( this.next().getStyle( 'display' ) == 'none' ) {
		this.next().setStyle({height: 'auto'});
		Effect.BlindDown( this.next(), { duration: 0.4 } );
	}
	else {
		Effect.BlindUp( this.next(), { duration: 0.4 } );
	}
}

function configureMenus() {
	var headers = $$('div#maincol div.csc-default div.csc-header');
	for(var i=0; i<headers.length; i++) {
		var header = headers[i];
		if (headers[i].down('h4') != undefined) {
			var list = headers[i].next('ul.csc-menu');
			if (list != undefined) {
				list.hide();
				headers[i].observe('click', menu_swapDisplay);
				headers[i].setStyle({cursor: 'pointer'});
			}
		}
	}
}

/*** onLoad ***/

if (window.addEventListener) {
	window.addEventListener('load', checkModules, false);
	window.addEventListener('load', configureMenus, false);
}
else if (window.attachEvent) {
	window.attachEvent ('onload', checkModules);
	window.attachEvent ('onload', configureMenus);
}


