/* Copyright (c) 2006-2007, Apple Inc. All rights reserved. */
/* this file (in conjunction with the webmail iframe on the static server home) will enable the link to webmail. */

function addService(serviceName, serviceURL){
	if (!document.getElementById || !serviceName) return;
	var oItem = document.getElementById(serviceName.toLowerCase()+'_button');
	if(oItem){
		var oLink = oItem.getElementsByTagName('a').item(0);
		oLink.setAttribute('enabled','true');
		oLink.className = '';
		oLink.setAttribute('href',serviceURL);
	}
}

/* Add the given groups to the homepage. Takes an array of dictionaries; each dictionary should have a "href" and a "title" key. */
function addGroups(groups) {
	// find the groups list element
	var groupsListElm = document.getElementById('groups_listing');
	if (!groupsListElm) return; // silent fail
	// hide the progress msg and disabled msg
	document.getElementById('wiki_progress').style.display = 'none';
	document.getElementById('groups_disabled_msg').style.display = 'none';
	// show the groups list element and empty its contents (just in case we get called twice)
	groupsListElm.innerHTML = '';
	groupsListElm.style.display = '';
	// iterate through the groups...
	for (var i = 0; i < groups.length; i++) {
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.href = groups[i]['href'];
		a.appendChild(document.createTextNode(groups[i]['title']));
		li.appendChild(a);
		groupsListElm.appendChild(li);
	}
}

function serverHomeLoad() {
	var buttons = ['webmail_button', 'groups_button', 'users_button'];
	for(var i=0, c=buttons.length; i<c; i++){
		var elm = document.getElementById(buttons[i]);
		if (elm && elm.getElementsByTagName('a')[0]){
			elm = elm.getElementsByTagName('a')[0];
			if(!elm.getAttribute('enabled')){
				elm.className = 'disabled';
				elm.removeAttribute('href');
			}
		}
	}
	// come back in 10 seconds (current timeout)
	setTimeout(function() {
		// if the progress message is still showing, hide it and show the "groups disabled" message
		var progressElm = document.getElementById('wiki_progress');
		if(!progressElm) return;
		if (progressElm.style.display != 'none') {
			progressElm.style.display = 'none';
			document.getElementById('groups_disabled_msg').style.display = '';
		}
	}, 10000);
}
window.onload = serverHomeLoad;
