function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+ escape(cookieValue) + ";expires="+expire.toGMTString();
 //document.cookie = "callMoodLabel=controller:team;expires="+expire.toGMTString();

 //alert(cookieValue);
}

function removeCookie(cookieName)
{
	//document.cookie = cookieName+'=null; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/';

}

function alertMood()
{
	//cHeight = parseInt(document.getElementById('gright').style.height);
	if (window.moodInc == null || window.moodInc == 1)
	{
		
		cHeight = 586;
		window.moodInc = 0;
	}
	else
	{
		cHeight = 585;
		window.moodInc =1;
	}
	//alert(cHeight);
	document.getElementById('gright').style.height = cHeight;
		
}

function alertMap()
{
	//cHeight = document.getElementById('gleft').style.height;
	if (window.mapInc == null || window.mapInc == 1)
	{
		cHeight = parseInt(self.innerHeight)-(47+10) +1;
		//document.getElementById('gleft').style.height = cHeight;
		window.mapInc = 0;
		//alert("inc");
	}
	else
	{
		cHeight = parseInt(self.innerHeight)-(47+10);
		//document.getElementById('gleft').style.height = cHeight;
		window.mapInc = 1;
		//alert("dec");
	}
	//alert(cHeight);
	document.getElementById('gleft').style.height = cHeight;
	
}

function setSafariMood(v1, value1)
{
	var cookieName = "setMoodVariable";
	var cookieValue = v1 +":"+value1;
	setCookie(cookieName, cookieValue);
	
	//window.status = cookieName + cookieValue;

	
}

function setSafariMap(v1, value1, v2, value2)
{
	var cookieName = "setMapVariable";
	var cookieValue = v1 +":"+value1+":"+v2+":"+value2;
	setCookie(cookieName, cookieValue);
	//alertMap();
}

function callSafariMood(cmd, p1, p2)
{
			
	var cookieName = "callMoodLabel";
	var cookieValue = p1 +":"+p2;
	setCookie(cookieName, cookieValue);
	alertMood();
	//alert(cmd + p1 + p2);
 
	
}

function callSafariMap(cmd, p1, p2, p3)
{
	var cookieName = "callMapLabel";
	var cookieValue = p1 +":"+p2;
	setCookie(cookieName, cookieValue);
	alertMap();
	
}

function checkIt(string)
{
	if (window.iamsafari == null)
	{
		var detect = navigator.userAgent.toLowerCase();
		place = detect.indexOf('safari');
		if (place == -1)
			window.iamsafari = false;
		else
			window.iamsafari = true;
	}
	return window.iamsafari;
}




function showAround()
{
	document.getElementById("acampus").style.visibility = "visible";
}

function hideAround()
{
	document.getElementById("acampus").style.visibility = "hidden";

}

function initCheck()
{

	if(checkIt('safari'))
	{

		document.getElementById('gleft').style.height = parseInt(self.innerHeight)-(47+10+10);

	document.getElementById('gcenterpane').style.width = parseInt(document.body.clientWidth)-(195+195);

	document.getElementById('icenterpane').style.width  = parseInt(document.body.clientWidth)-(195+195);
	document.getElementById('icenterpane').style.height = parseInt(self.innerHeight)-(91+10+10);


	document.getElementById('courseload').style.width  = parseInt(document.body.clientWidth)-(195+195);
	document.getElementById('courseload').style.height = parseInt(self.innerHeight)-(91+10+15 + 10);


	document.getElementById('gright').style.left = parseInt(document.body.clientWidth)-195;


	document.getElementById('headerline').style.width = parseInt(document.body.clientWidth)-(195+195+35);
	}
	else
	{

	document.getElementById('gleft').style.height = parseInt(document.body.clientHeight)-(47+10 +10);

	document.getElementById('gcenterpane').style.width = parseInt(document.body.clientWidth)-(195+195);

	document.getElementById('icenterpane').style.width  = parseInt(document.body.clientWidth)-(195+195);
	document.getElementById('icenterpane').style.height = parseInt(document.body.clientHeight)-(91+10+10);


	document.getElementById('courseload').style.width  = parseInt(document.body.clientWidth)-(195+195);
	document.getElementById('courseload').style.height = parseInt(document.body.clientHeight)-(91+10+10);


	document.getElementById('gright').style.left = parseInt(document.body.clientWidth)-195;


	document.getElementById('headerline').style.width = parseInt(document.body.clientWidth)-(195+195+35);
	}
}




function squishyCheck()
{
	if (checkIt('safari'))
		h = parseInt(self.innerHeight);
	else
		h =  parseInt(document.body.clientHeight);
				
	
	if (h < 600)
	{
		
			if(checkIt('safari'))
			{
				
				callSafariMood("gotoAndPlay", "squishyMaster", "on");
			}
			else
			{
				window.document.mood.TGotoLabel('/squishyMaster','on');
			}
		

	}
	else
	{
		if(checkIt('safari'))
			{	
			
				callSafariMood("gotoAndPlay", "squishyMaster", "off");
			}
			else
			{
				window.document.mood.TGotoLabel('/squishyMaster','off');
			}
		//alert("OFF");	
	 }
}



function resizeCheck()
{
	initCheck();
	squishyCheck();
}


function openP(theurl, winname, features)
{
	window.open(theurl,winname,features);

}




function im_popup(ret1, ret2, space, av, fcs)
{
		// Need to escape the name values in case they have spaces in them (Firstname Lastname)
		ret1 = escape(ret1);
		ret2 = escape(ret2);
		space = escape(space);
		av = escape(av);
		rev ="11_02";
		
		var window_id = "popup_" + ret1 + ret2;
		// Need to remove percents that come from encoding spaces, for window_id and space
		window_id = window_id.replace(/%/g,"_");
		space = space.replace(/%/g,"_");
		
		// Open the window
		var ret_value = "/ce/imPopup.html?userReceived=" + ret2 + "&imPeer=" + ret1 + "&space=" + space + "&av=" + av + "&fcs=" + fcs +"&r="+ rev;
		var w = window.open(ret_value,window_id,'status=no,scrollbars=no,toolbar=no,alwaysRaised,menubar=no,directories=no,location=no,resizable=no,width=274,height=360');
		w.focus;
}




function switchMood(orbit, teamToken)
{
   
	var pos = -1;
	
	if(checkIt('safari'))
   	{
		//do nothing since otherwise safari does not work
	}
	else
	{
		// Retrieve the cookies that have been set.
		var allCookies = document.cookie;
	
	
		// Determine if the internalAuthorityCookie has been set.  If the cookie has not 
		// been set, switch the mood orb as indicated by the orbit param that has been passed 
		// in.  If the cookie has been set, always set the mood orb to faculty.
		pos = allCookies.indexOf("internalAuthorityCookie");
	}



	if(pos == -1)
	{
		if(checkIt('safari'))
		{
			
			if(teamToken != null && teamToken != "" && orbit == "team")
			{
				//window.status = teamToken;
				setSafariMood("ttoken", teamToken);
				callSafariMood("gotoAndPlay", "controller", orbit);
			}
			else
				callSafariMood("gotoAndPlay", "controller", orbit);
			
		}
		else
		{
		   window.document.mood.TGotoLabel('/controller',orbit);
		}
	}
	else
	{
		window.document.mood.TGotoLabel('/controller',"faculty");
	}

}


function setPrintPage(cpage)
{
	var ppage = cpage + "&discView=expand&paper=1";

	var allItems = window.top.document.getElementsByTagName("link");
	var linkEl = allItems[0];
	var hrefAt = linkEl.setAttribute('href', ppage);


}



function view(cpage, title, scope, report, nodeId, teamToken) {

	window.top.document.title = title;
	setPrintPage(cpage);
	if(teamToken != null && teamToken != "" && scope == "t")
	{
		if(checkIt('safari'))
			{
				//setSafariMood("ttoken", teamToken);
			}
			else
			{
				window.document.mood.SetVariable('ttoken', teamToken);
			}
	}

	showCourseLoad();

	if(checkIt('safari'))
	{
		setSafariMap("incoming", nodeId, "fullswitch", "n");
		callSafariMap("gotoAndPlay", "dispatcher", "newPosition");
	}
	else
	{
		window.document.map.SetVariable('incoming',nodeId);
		window.document.map.SetVariable('fullswitch','n');
		window.document.map.TGotoLabel('/dispatcher','newPosition');
	}

	


	var header = '';
	header += '<div class="orbitStamp" style="background-color: #CCE2FD;">';
	header += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="190" height="35">';
	
	//var report = "";

	if (areWeInQuietMode())
	{
		header = '<div class="orbitStamp" style="background-color: #CCE2FD;">';
		header +=  '</div><DIV class="classHeading" style="background-color: #CCE2FD;"><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis" style="background-color: #CCE2FD;">...</span></H1><div id="headerline" class="headerLine" style="border-top: #9999CC 1px solid;"></div></DIV>';
		document.getElementById('gcenterpane').innerHTML = header;
		switchMood('class');
	} else if (scope == 'c')
	{
		header += '<param name="movie" value="/ce/animated/c_report.swf"><param name="quality" value="high"><param name="wmode" value="opaque">';
		header +=  '<embed src="/ce/animated/c_report.swf?" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
		header +=  '</div><DIV class="classHeading" style="background-color: #CCE2FD;"><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis" style="background-color: #CCE2FD;">...</span></H1><div id="headerline" class="headerLine" style="border-top: #9999CC 1px solid;"></div></DIV>';
		document.getElementById('gcenterpane').innerHTML = header;
		switchMood('class');
	} else if (scope == 't')
	{
		header += '<param name="movie" value="/ce/animated/t_report.swf"><param name="quality" value="high"><param name="wmode" value="opaque">';
		header +=  '<embed src="/ce/animated/t_report.swf?" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
		header +=  '</div><DIV class="classHeading" style="background-color: #CCE2FD;"><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis" style="background-color: #CCE2FD;">...</span></H1><div id="headerline" class="headerLine" style="border-top: #9999CC 1px solid;"></div></DIV>';
		document.getElementById('gcenterpane').innerHTML = header;
		switchMood('team', teamToken);
	} else if (scope == 'f')
	{
		header += '<param name="movie" value="/ce/animated/f_report.swf"><param name="quality" value="high"><param name="wmode" value="opaque">';
		header +=  '<embed src="/ce/animated/f_report.swf?" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
		header +=  '</div><DIV class="classHeading" style="background-color: #CCE2FD;"><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis" style="background-color: #CCE2FD;">...</span></H1><div id="headerline" class="headerLine" style="border-top: #9999CC 1px solid;"></div></DIV>';
		document.getElementById('gcenterpane').innerHTML = header;
		switchMood('faculty');
	} else
	{
		header += '<param name="movie" value="/ce/animated/c_report.swf?LName=finalState"><param name="quality" value="high"><param name="wmode" value="opaque">';
		header +=  '<embed src="/ce/animated/c_report.swf?LName=finalState" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
		header +=  '</div><DIV class="classHeading" style="background-color: #CCE2FD;"><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis" style="background-color: #CCE2FD;">...</span></H1><div id="headerline" class="headerLine" style="border-top: #9999CC 1px solid;"></div></DIV>';
		document.getElementById('gcenterpane').innerHTML = header;
		switchMood('class');
	}
	
	manipulatePageTitle();
	setDiscussionTextAreaColumn();
	
	if(checkIt('safari'))
	{
		centerIframe.location.href=cpage;
	}
	else
	{
	var center = '';
	//center += '<DIV class="discussionIndicator" id="sampleBar"></DIV>';
	center += '<iframe id="centerIframe" name="centerIframe" src="' + cpage + '" width="100%"  height="100%" scrolling="auto" marginwidth="0" marginheight="0" align="left" frameborder="0"></iframe>';
	document.getElementById('icenterpane').innerHTML = center;
	}


	//centerIframe.location.href=cpage;
	//window.document.all.centerIframe.style.scrolling="no";

	//window.scrollTo(0,0);
	//drawDiscInd("sampleBar",newposts, totalposts, true);
	//alert("center");
	//window.scrollTo(0,0);

	
	
}

function fullSwitch(nodeId)
{

	if(nodeId !== null && nodeId != "" && nodeId != "undefined")
		{
			if(checkIt('safari'))
			{
				//setSafariMap("incoming", nodeId, "fullswitch", "y");
				//callSafariMap("gotoAndPlay", "dispatcher", "newPosition");
			}
			else
			{
			//alert(nodeId);
				window.document.map.SetVariable('incoming',nodeId);
				window.document.map.SetVariable('fullswitch',"y");
				window.document.map.TGotoLabel('/dispatcher','newPosition');
			}
		}
}


function fullSafariSwitch(nodeId)
{
	//alert(nodeId);
	if(nodeId !== null && nodeId != "" && nodeId != "undefined")
		{
			if(checkIt('safari'))
			{
				setSafariMap("incoming", nodeId, "fullswitch", "y");
				callSafariMap("gotoAndPlay", "dispatcher", "newPosition");
			}
		}
}


	
	
function areWeInQuietMode()
{
    var query = window.top.location.href;
	var pos = query.indexOf("static.jsp");
	if (pos == -1) return false;
	return true;
}

function replaceEntities(str) {

    var ents = new Array(4);
    ents[0] = "\&#39;";
    ents[1] = "\&amp;";
    ents[2] = "\&lt;";
    ents[3] = "\&quot;";

    var subs = new Array(4);
    subs[0] = "\'";
    subs[1] = "\&";
    subs[2] = "\<";
    subs[3] = "\"";
    
    for (var i = 0; i < 4; i++) {
        var re = new RegExp(ents[i], "g");
        str = str.replace(re, subs[i]);
    }

    return str;
        
}

function switchCenter(cpage, title, scope, report, nodeId, teamToken) {

        var newTitle = replaceEntities(title);

        window.top.document.title = newTitle;

		setPrintPage(cpage);
	
	
		if(teamToken != null && teamToken != "" && scope == "t")
		{
			if(checkIt('safari'))
			{
					//setSafariMood("ttoken", teamToken);
			}
			else
			{
				window.document.mood.SetVariable('ttoken', teamToken);
			}
		}
	
		
		showCourseLoad();
	
	
		if(nodeId !== null && nodeId != "" && nodeId != "undefined")
		{
			//alert(nodeId);
			if(checkIt('safari'))
			{
					setSafariMap("incoming", nodeId, "fullswitch", "n");
					callSafariMap("gotoAndPlay", "dispatcher", "newPosition");
			}
			else
			{
				window.document.map.SetVariable('incoming',nodeId);
				window.document.map.SetVariable('fullswitch','n');
				window.document.map.TGotoLabel('/dispatcher','newPosition');
			}
		}
		
		var header = '';
		header += '<div class="orbitStamp">';
		header += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="190" height="35">';
		
		
		if (areWeInQuietMode())
		{
			header = '<div class="orbitStamp">';
			header +=  '</div>';
			header += '<DIV class=classHeading><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis">...</span></H1>';
			header += '<div id="headerline" class="headerLine"></div></div>';
			document.getElementById('gcenterpane').innerHTML = header;
			switchMood('class');
		} else if (scope == 'c')
		{
			header += '<param name="wmode" value="opaque"><param name="movie" value="/ce/animated/c_animate.swf?LName=finalState"><param name="quality" value="high">';
			header +=  '<embed src="/ce/animated/c_animate.swf?LName=finalState" wmode="opaque" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
			header +=  '</div>';
			header += '<DIV class=classHeading><H1 class="coursePageTitle"><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis">...</span></H1>';
			header += '<div id="headerline" class="headerLine"></div></div>';
			document.getElementById('gcenterpane').innerHTML = header;
			switchMood('class');
		} else if (scope == 't')
		{
			header += '<param name="wmode" value="opaque"><param name="movie" value="/ce/animated/t_animate.swf"><param name="quality" value="high">';
			header +=  '<embed src="/ce/animated/t_animate.swf?" wmode="opaque" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
			header +=  '</div><DIV class=classHeading><H1 class=coursePageTitle><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis">...</span></H1><div id="headerline" class="headerLine"></div></DIV>';
			document.getElementById('gcenterpane').innerHTML = header;
			switchMood('team', teamToken);
		} else if (scope == 'f')
		{
			header += '<param name="wmode" value="opaque"><param name="movie" value="/ce/animated/f_animate.swf"><param name="quality" value="high">';
			header +=  '<embed src="/ce/animated/f_animate.swf?" wmode="opaque" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
			header +=  '</div><DIV class=classHeading><H1 class=coursePageTitle><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis">...</span></H1><div id="headerline" class="headerLine"></div></DIV>';
			document.getElementById('gcenterpane').innerHTML = header;
			switchMood('faculty');
		} else
		{
			header += '<param name="wmode" value="opaque"><param name="movie" value="/ce/animated/c_animate.swf?LName=finalState"><param name="quality" value="high">';
			header +=  '<embed src="/ce/animated/c_animate.swf?LName=finalState" wmode="opaque" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="190" height="35"></embed></object>';
			header +=  '</div><DIV class=classHeading><H1 class=coursePageTitle><span id="pageTitle">' + title + '</span><span id="ellipsis" class="ellipsis">...</span></H1><div  id="headerline" class="headerLine"></div></DIV>';
			document.getElementById('gcenterpane').innerHTML = header;
			switchMood('class');
		}
		
		manipulatePageTitle();
		setDiscussionTextAreaColumn();
		
		
		
	
		if(checkIt('safari'))
		{
			centerIframe.location.href=cpage;
		}
		else
		{
		var center = '';
		//center += '<DIV class="discussionIndicator" id="sampleBar"></DIV>';
		center += '<iframe id="centerIframe" name="centerIframe" src="' + cpage + '" width="100%"  height="100%" scrolling="auto" marginwidth="0" marginheight="0" align="left" frameborder="0"></iframe>';
		document.getElementById('icenterpane').innerHTML = center;
		}
		//window.document.all.centerIframe.style.scrolling="no";
	
		//window.scrollTo(0,0);
		//drawDiscInd("sampleBar",newposts, totalposts, true);
		//alert("center");
		//window.scrollTo(0,0);
		//alert("client width: " + document.body.clientWidth + "\nclientHeight: " + document.body.clientHeight);
	
}



function hideCourseLoad()
{
	// Check to see how much time has passed since the loading screen has been up
	var now = new Date();
	var elapsed = (now.getTime() - document.getElementById('courseload').time);
	var left = 1500 - elapsed;
	
	// If we haven't seen the course loading screen for the given minimum, try again in a bit
	if (left > 0)
	{
		setTimeout('hideCourseLoad();', left);
	}
	else
	{
		// Hide the course loading screen
		document.getElementById('courseload').style.visibility = "hidden";	
		document.getElementById('icenterpane').style.visibility = "visible";
	}
}

function showCourseLoad()
{
	// Set a flag to know when someone put up the loading screen
    var now = new Date();
    var millis = now.getTime();
	document.getElementById('courseload').time = millis;

	// Put up the course loading screen	
	document.getElementById('courseload').style.visibility = "visible";	
	document.getElementById('icenterpane').style.visibility = "hidden";
}



function manipulatePageTitle()
{
 	var titlewidth = document.getElementById('pageTitle').offsetWidth;	
	var centerwidth = document.getElementById('gcenterpane').offsetWidth;
	var availabletitlewidth = parseInt(centerwidth) - 192;
    if( titlewidth > availabletitlewidth)
     { 
       document.getElementById('ellipsis').style.visibility = "visible";	
     }
    else 
     {
       document.getElementById('ellipsis').style.visibility = "hidden"; 
     }
 }
//this function calculates the cols for the discussion textarea; 
//width of the discussion dialog box;
//a flag to determine if the action buttons need to be on separate lines
this.col = 0; 
this.messageFormWidth = 0;
this.verysmall = 0;
function setDiscussionTextAreaColumn()
{
 	var centerwidth = document.getElementById('gcenterpane').offsetWidth;
	var ratio = 475/40; 
	if (centerwidth > 510)
	  {  
	    //15 left margin + 20 scroll
	    col = 50;
	    messageFormWidth = 475;
	  }
	else if(centerwidth < 355)
	{
	  col = 28;
	  messageFormWidth = 310;
	}
	else 
	{
	  col = Math.round(centerwidth/ratio);   
	  messageFormWidth = centerwidth - 55;
	}

	if(messageFormWidth < 455)
	  verysmall = 1;
	else
	  verysmall = 0; 	
} 