var pportalID = 'future';
var pupcomingID = 'calendar';

document.write('<script type="text/javascript" language="javascript" src="/assets/js/common.js"></script>');

/* initialize AJAX for the Portal Selection Links */
	function initPortalSelector(){	
		//var portalLinks = getElementsByClassName(document, "a", "portal_link");
		var portalLinks = getElementsByClass("portal_link",document.getElementById("portal-menu"),"a")
		// attach AJAX event to each portal link
		/*
		for(i=0; i<portalLinks.length; i++){
			var objLink = document.getElementById(portalLinks[i].id);	
			makeAJAXLink('portal',objLink);
		}
		*/
		//init the "future" portal as the default;
		portalID = getCookie("PORTAL");
		if (portalID == null){
			setCookie("PORTAL","future",1000,"/");
			portalID = 'future';
		} else {
			setCookie("PORTAL",portalID,1000,"/");
		}
		//init the "calendar" as the default;
		upcomingID = getCookie("upcoming");
		if (upcomingID == null){
			setCookie("upcoming","calendar",1000,"/");
			upcomingID = 'calendar';
		}
		/*
		updatePortalPod('connect','/index.cfm?event=ajax.dspPod','pod=viewlets/portals/portalConnect&portalID='+portalID);
		updatePortalPod('portal-content','/index.cfm?event=ajax.dspPod','pod=viewlets/portals/portalContent&portalID='+portalID);
		updatePortalPod('portal-spotlight','/index.cfm?event=ajax.dspPod','pod=viewlets/portals/portalSpotlight&portalID='+portalID);
		*/
		highlightTab("portal",portalID);
			
		var upcomingLinks = getElementsByClass("upcoming_link",document.getElementById("upcoming-menu"),"a")
		// attach AJAX event to each portal link
		for(j=0; j<upcomingLinks.length; j++){
			var objLink = document.getElementById(upcomingLinks[j].id);	
			makeAJAXLink('upcoming',objLink);
		}
		//updatePortalPod('upcoming-content','/index.cfm?event=ajax.dspPod','pod=viewlets/portals/portalUpcoming&portalID='+portalID+'&content='+upcomingID);
		updatePortalPod('upcoming-content','/ajax/dspPod','pod=viewlets/portals/portalUpcoming&portalID='+portalID+'&content='+upcomingID);
		highlightTab('upcoming',upcomingID);	
	}

	function makeAJAXLink(pname,objLink){
		var LinkID = objLink.id;
		var pvalue = LinkID.replace(pname+"_link_","");
		// load new content onClick
		objLink.onclick = function(){
			if (pname == 'portal'){
				// we're updating the whole page			
				// reset the global portalID var to the current selected portal
				portalID = pvalue;
				setCookie("portal",portalID,1000,"/");
				upcomingID = getCookie("upcoming");
				updatePortalPod('connect','/ajax/dspPod','pod=viewlets/portals/portalConnect&portalID='+portalID);
				updatePortalPod('portal-content','/ajax/dspPod','pod=viewlets/portals/portalContent&portalID='+portalID);
				updatePortalPod('portal-spotlight','/ajax/dspPod','pod=viewlets/portals/portalSpotlight&portalID='+portalID);
				updatePortalPod('upcoming-content','/ajax/dspPod','pod=viewlets/portals/portalUpcoming&portalID='+portalID+'&content='+upcomingID);
			} else {
				upcomingID = pvalue;
				setCookie("upcoming",upcomingID,1000,"/");
				portalID = getCookie("PORTAL");
				updatePortalPod('upcoming-content','/ajax/dspPod','pod=viewlets/portals/portalUpcoming&portalID='+portalID+'&content='+upcomingID);
			}
			
			//highlight selected menu tab
			highlightTab(pname,pvalue);
			
			// don't actually follow the link		  				
			return false;
		}
	}

/* load the appropriate portal content */
	function updatePortalPod(target,getURL,pars){
		var myAjax = new Ajax.Updater(target, getURL, {method: 'get', parameters: pars});	
	}

/* highlight selected menu tab */
	function highlightTab(pname,pvalue){
		var pLinks = getElementsByClassName(document.getElementById(pname+"-menu"), "a", pname+"_link");
		var pTabID = document.getElementById(pname+"_link_"+pvalue);		
		// reset class on all portal tabs
		for(i=0; i<pLinks.length; i++){		
			var pLink = document.getElementById(pLinks[i].id);
			pLink.className = pname+"_link";
		}
		// set class
		pTabID.className = pname+"_link selected";
	}
	
/* run the event on window load */
	addEvent(window,'load',initPortalSelector);