//this function allows IE6 to use pseudo css :hover on elements other than <a>


sfHover1 = function() {

	if (document.all&&document.getElementById) 
	{
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) 
		{
			sfEls[i].onmouseover=function() 
			{
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() 
			{
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}



/*
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


function hideObj(obj)
{
	obj.style.visibility='hidden';
}

function showAndAlign(obj, alignObj)
{
	obj.style.top = findPosY(alignObj);
	obj.style.left = findPosX(alignObj);
	obj.style.visibility='visible';
}

function showAroundCampus()
{

		
	if(document.getElementById("around_campus").do_open == true)
	{
        document.getElementById("around_campus").style.display = "block";
        document.getElementById("around_campus").visibility = "visible";
	
        document.getElementById("around_button").className="buttonOver";
        //document.getElementById("around_icon_img").setAttribute("src", "/global_nav/d_icon.gif");
        //document.getElementById("around_icon").innerHTML = "<img  src='/global_nav/d_icon.gif'/>";
        //document.getElementById("around_campus").style.display = "block";

        if (document.getElementById('MenuFrameHelper') != null) {
            el = document.getElementById('MenuFrameHelper');
            menuEl = document.getElementById('around_campus');

            el.style.left = findPosX(menuEl) - 150;
            //el.style.zIndex = menuEl.style.zIndex + 1;
            el.style.display = "block";
            //alert("left=" + el.style.left + ", zIndex=" + el.style.zIndex + ", display=" + el.style.display);
        }
	}
}

function hideAroundCampus()
{
    if (document.getElementById('MenuFrameHelper') != null) {
        el = document.getElementById('MenuFrameHelper');
        el.style.display = "none";
    }

	document.getElementById("around_campus").style.display = "none";
	document.getElementById("around_campus").visibility = "hidden";
	
	document.getElementById("around_button").className="buttonOut";
	//document.getElementById("around_icon_img").setAttribute("src", "/global_nav/d_open.gif");
	//document.getElementById("around_icon").innerHTML = "<img  src='/global_nav/d_open.gif'/>";
	//document.getElementById("around_campus").style.display = "none";
	
	document.getElementById("around_campus").do_open = false;
}
*/
/*
function toggleMenu()
{
	
	var menuStatus = document.getElementById("around_campus").visibility;
	

	if (menuStatus == null || menuStatus == "hidden" )
	{
		showAroundCampus();
		
		
		
		
	}
	else if(menuStatus == "visible")
	{
		 hideAroundCampus();
		
	}
}
*/
/*
function dontHideYet(obj)
{
	/*if (!e) e = window.event;
	if (e.target) var trg = e.target;
	else if (e.srcElement) var trg = e.srcElement;
	if (trg != obj) return // Mouseout on links, ignore event
	// Mouseout took place on dropdown
	// Handle event
	if(obj.id == "around_area")
		document.getElementById("around_campus").the_timeout = setTimeout("hideAroundCampus();",200);
}
*//*
function removeTimeOut(obj)
{
	if (!e) e = window.event;
	if (e.target) var trg = e.target;
	else if (e.srcElement) var trg = e.srcElement;
	if (trg != obj) return // Mouseout on links, ignore event
	// Mouseout took place on dropdown
	// Handle event
	if(obj.id == "around_area")
	{
	clearTimeout(document.getElementById("around_campus").the_timeout);
	showAroundCampus();
	}

}
*//*
function showFirstTime(obj)
{
	if (!e) e = window.event;
	if (e.target) var trg = e.target;
	else if (e.srcElement) var trg = e.srcElement;
	if (trg != obj) return // Mouseout on links, ignore event
	// Mouseout took place on dropdown
	// Handle event
	clearTimeout(document.getElementById("around_campus").the_timeout);
	document.getElementById("around_campus").do_open = true;
	showAroundCampus();
	
}
	*/
/*
function showSelected(row){

	
	row.className="around_campus_s"
	row.style.color = "#fff";
	row.style.cursor = "pointer";
	

	
}

function showUnselected(row){

	
	row.style.backgroundcolor = "#cccccc";
	row.style.color = "#003366";
	
}
*/
var newWin = null;
function openWin(url, w, h) {
	var resizable = ",resizable=no";
	if(openWin.arguments.length > 3) {
		resizable = ",resizable=yes,scrollbars=yes";
	}
	var props = "width=" + w + ",height=" + h + ",toolbar=no,menubar=no,personalbar=no,statusbar=yes"+resizable;
	if(newWin && !newWin.closed) {
		newWin.focus();
	}
	
	newWin = window.open(url, 'newWin', props);
}

function newwindow(url) { 
	winName = window.open(url,'title', 'width=790,height=536,resizable=yes, scrollbars=yes'); 
	winName.focus();
} 

// Session timeout related functions.
var the_timer = null;
var the_interval = null;
var timerRunning = false;
var currentTime;
var warned;
var connectedFns = [];
var myHome = "myellis";

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

function okToStartTimer() {

	var startTimer = false;

	// We don't want to run the timer on the login page or from the global nav
	// in course pages.  We run the timer inside the iframe on course pages 
	// (i.e., the timer is invoked from the /CMS/course/index template).
	if (location.href.indexOf("login.jsp") < 0 && location.href.indexOf("course_frame.jsp") < 0) {
		startTimer = true;
	}
	
	return startTimer;
}

function InitializeTimer() {
    // Set the length of the timer, in seconds.  The durationInMinutes is the 
    // value specified in the config.jide_session_timeout config setting.
    if(okToStartTimer()) {
	    startTimer();
    }
}

function clearTheTimer() {
    if (the_timer != null) {
        clearTimeout(the_timer);
        the_timer = null;
    }    
}

function clearTheInterval(intervalVal) {

    if (the_interval != null) {
        clearInterval(the_interval);
        the_interval = null;
    }
    
}

function startTimer() {
	clearTheTimer();
    clearTheInterval();
	
    // Check the session every 5 minutes.
    the_interval = setInterval("checkSession()", 300000);
    timerRunning = true;
}

function showWarningMessage() {

    clearTheTimer();
        
    if (!warned) {          
		showSessionGoingToExpireMsg();
		warned = true;
    }  
    
    // Wait 60 seconds and then run the function to check the session before 
    // timing out.  
    the_timer = setTimeout("checkSessionBeforeAutoLogout()", 60000);
}

function checkSession() { 
	dojo.xhrGet( { 
	  // The following URL hits the checkSessionAccess.jsp to check if the user's
	  // session has had any activity within the last 30 minutes.
	  url: "/" + myHome + "/checkSessionAccess.jsp?param=checkSession", 
	  handleAs: "text",
	
	  timeout: 5000, // Time in milliseconds
	
	  // The LOAD function will be called on a successful response.
	  load: function(response, ioArgs) { 
	    // If there hasn't been any session activity in the last 30 minutes, 
	    // clear the interval for the page and show the session timeout warning
	    // message.
	    if (response != null && "true" == response.trim()) {
	       clearTheInterval();
	       showWarningMessage();
	    } 
	    return response; 
	  },
	
	  // The ERROR function will be called in an error case.
	  error: function(response, ioArgs) { 
	    console.error("HTTP status code: ", ioArgs.xhr.status); 
	    return response; 
	    }
	  });
}
      
function checkSessionBeforeAutoLogout() {
	dojo.xhrGet( { 
       // The following URL hits the checkSessionAccess.jsp to check if the user's
       // session has had any activity within the last 30 minutes.
	   url: "/" + myHome + "/checkSessionAccess.jsp?param=checkSession", 
	   handleAs: "text",
	
	   timeout: 5000, // Time in milliseconds
	
	   // The LOAD function will be called on a successful response.
	   load: function(response, ioArgs) { 
	     // If we havent had any session activity, we log out the user.
	     if (response != null && "true" == response.trim()) {
            logOutUser();
	     } else {
	        // If the user has taken some action, then we need clear the timer
	        // on the page and renew the session from this page.
	        clearTheTimer();
	        renewSession();
	     }
	     return response; 
	   },
	
	   // The ERROR function will be called in an error case.
	   error: function(response, ioArgs) { 
	     console.error("HTTP status code: ", ioArgs.xhr.status); 
	     return response; 
	     }
	   });
}

function logOutUser() {

    clearTheTimer();
    clearTheInterval();

    var schemeTermIdx = 0; 

    if (location.protocol.charAt(4) == "s") {   
        schemeTermIdx = 8; 
    } else {
        schemeTermIdx = 7; 
    }  
     
    var oldHref = location.href.substring(schemeTermIdx);

    var urlTermIdx = oldHref.indexOf("/");
    
    var host = location.href.substring(0,schemeTermIdx + urlTermIdx);
    
    document.location.replace(host + '/cms/action/portlets.LogOutAction')
    
}

function logMeOut() {

    dojo.xhrGet( { 
       // The following URL hits the checkSessionAccess.jsp to check if the user's
       // session has had any activity within the last 30 minutes.
       url: "/" + myHome + "/checkSessionAccess.jsp?param=logout", 
       handleAs: "text",
    
       timeout: 5000, // Time in milliseconds
    
       // The LOAD function will be called on a successful response.
       load: function(response, ioArgs) { 
         // If we havent had any session activity, we log out the user.
         if (response != null && "true" == response.trim()) {
            logOutUser();
         } 
         return response; 
       },
    
       // The ERROR function will be called in an error case.
       error: function(response, ioArgs) { 
         console.error("HTTP status code: ", ioArgs.xhr.status); 
         return response; 
         }
       });
}

function renewSession() {

	dojo.xhrGet( { 
       // The following URL hits the checkSessionAccess.jsp to get the user's
       // EDGE user id.
	   url: "/" + myHome + "/checkSessionAccess.jsp?param=checkUser", 
	   handleAs: "text",
	
	   timeout: 5000, // Time in milliseconds
	
	   // The LOAD function will be called on a successful response.
	   load: function(response, ioArgs) { 
	     // If the user id is 1, then the session has timed out (likely on some 
	     // other page), so we need to log the user out.
	     if (response != null && "1" == response.trim()) {
	         logOutUser();
	     } else {     
	         // If the user id is not a guest user id, then we clear the timer
	         // and the interval on the page, ping the server to renew the 
	         // session from the page, re-initialize the timer, and hide the 
	         // session timeout warning message.
		     clearTheTimer();
		     clearTheInterval();
		
		     // pingServer();
			 warned=false;
			 InitializeTimer();
			 resetFocus();
			 releaseFocus();
			 hideSessionGoingToExpireMsg();
	     }
	     return response; 
	   },
	
	   // The ERROR function will be called in an error case.
	   error: function(response, ioArgs) { 
	     console.error("HTTP status code: ", ioArgs.xhr.status); 
	     return response; 
	     }
	   });
}

function pingServer() {
	dojo.xhrGet( { 
	   // The following URL hits the pingServer.jsp so that the session is 
	   // renewed and the session.accessTime session attribute can be updated
	   // to show that there's been some user initiated session activity within 
	   // the last 30 minutes.
	   url: "/" + myHome + "/pingServer.jsp", 
	   handleAs: "text",
	
	   timeout: 5000, // Time in milliseconds
	
	   // The LOAD function will be called on a successful response.
	   load: function(response, ioArgs) { 
	       return response; 
	   },
	
	   // The ERROR function will be called in an error case.
	   error: function(response, ioArgs) { 
	     console.error("HTTP status code: ", ioArgs.xhr.status); 
	     return response; 
	     }
	   });
}

function hideSessionGoingToExpireMsg() {
	var overlay = document.getElementById('session_warning');
	var warnMsg = document.getElementById('warning_message');
	overlay.style.display="none";
	warnMsg.style.display="none";
}

function showSessionGoingToExpireMsg() {

	var box_w = document.getElementById('session_warning');
	var box_x = document.getElementById('warning_message');
	var renew_link = document.getElementById('renew_link');
	
	persistFocus();
	
	box_w.style.display = "block";
	box_x.style.display = "block";

	var pagesize = new getWarnPageSize();
	var scrollPos = new getWarnScrollPos();
	var ua = navigator.userAgent;
	
	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';} 
	/*else {box_w.style.width = pagesize.width-20+'px';}*/ // scrollbars removed! Hurray!
	box_w.style.height = pagesize.height+scrollPos.scrollY+'px';
	window.onscroll = maintWarnPos;
	window.onresize = maintWarnPos;
	maintWarnPos();
	renew_link.focus();
}

function posWarningToCenter(elem) {
	var scrollPos = new getWarnScrollPos();
	var pageSize = new getWarnPageSize();
	var emSize = new getWarnElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;	
	elem.style.left = x+'px';
	elem.style.top = y+'px';	
}

function getWarnScrollPos() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

function getWarnPageSize() {
	var docElem = document.documentElement
	this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
	this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

function getWarnElementSize(elem) {
	this.width = elem.offsetWidth ||  elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}

function maintWarnPos() {
	
	var sessionwarn = document.getElementById('session_warning');
	var warningmessage = document.getElementById('warning_message');
	var pagesize = new getWarnPageSize();
	var scrollPos = new getWarnScrollPos();
	var ua = navigator.userAgent;

	if(ua.indexOf("MSIE ") != -1) {sessionwarn.style.width = pagesize.width+'px';} 
	/*else {box_w.style.width = pagesize.width-20+'px';}*/

	if(ua.indexOf("Opera/9") != -1) {sessionwarn.style.height = document.body.scrollHeight+'px';}
	else {sessionwarn.style.height = pagesize.height+scrollPos.scrollY+'px';}
	
	// alternative 1
	//box_w.style.height = document.body.scrollHeight+50+'px';	
	
	posWarnToCenter(warningmessage);
	
}

function posWarnToCenter(elem) {
	var scrollPos = new getWarnScrollPos();
	var pageSize = new getWarnPageSize();
	var emSize = new getWarnElementSize(elem);
	var x = Math.round(pageSize.width/2) - (emSize.width /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSize.height /2) + scrollPos.scrollY;	
	elem.style.left = x+'px';
	elem.style.top = y+'px';	
}

// The original leftOffset value for the menu that will be shifted. 
var baseMenuOffset = 0;

function showMenu(menuName, hOffset) {

	var menu = document.getElementById(menuName);
    
	// Set the display to block, but don't show it until we've applied the horizontal offset.
	menu.style.display = 'block';
	baseMenuOffset = menu.offsetLeft;
	
	// Set the new offset if necessary.
	if (hOffset != 0) {
    	var newOffset = menu.offsetLeft + hOffset;
    	menu.style.left = newOffset+'px';
	}
	
    // Now make the menu visible.	
	menu.style.visibility = 'visible';
}

function hideMenu(menuName) {

	var menu = document.getElementById(menuName);

	menu.style.visibility = 'hidden';
	
    // Set the left style for the menu back to the originall value.  Not sure why, but the browser
    // seems to add an extra 10px when it renders the menu.  So, we just account for it here as
    // well.
	menu.style.left= baseMenuOffset-10+'px';
	
	menu.style.display = 'none';
}

var priorFocusObj = null;

function persistFocus() {
	var txtAreaObjArray = document.body.getElementsByTagName("textarea");
	var txtAreaObj = null;
	
	if (txtAreaObjArray.length > 0) {
		txtAreaObj = txtAreaObjArray[0];
		priorFocusObj = txtAreaObj;
	}
		
}

function resetFocus() {
	if (priorFocusObj != null) {
		priorFocusObj.focus();
	}
}

function releaseFocus() {
	priorFocusObj = null;
}
