﻿

var DashboardObjects = new Array('myprofile','myworkbook','myadvice','mynews','mylearning','adminfinancial','adminworkbook','adminadvice','adminnews', 'administrator');


function initDashboard(location) { 

	if (!document.getElementById) 
		return;
	var isSafari = false;
	if(navigator.userAgent.indexOf("Safari") >0)
		isSafari = true;
	//get/setup the document location
	var docLocation;
	var marker = document.location.href.indexOf('?');
	docLocation = (marker > 0) ? './'+document.location.href.substring(marker) : './?section=index';
	//login / logout check
	marker = docLocation.indexOf('&log');
	docLocation = (marker > 0) ? docLocation.substring(0, marker) : docLocation;
	marker = document.location.href.indexOf('&log');
	var longDocLocation = (marker > 0) ? document.location.href.substring(0, marker) : document.location.href;
	marker = docLocation.indexOf('#');
	docLocation = (marker > 0) ? docLocation.substring(0, marker) : docLocation;
	marker = longDocLocation.indexOf('#');
	longDocLocation = (marker > 0) ? document.location.href.substring(0, marker) : document.location.href;

	//div's
	for (var id in DashboardObjects){
		if(objectExists('dash-'+DashboardObjects[id])) {
		var currentDivID=DashboardObjects[id];
		//alert (DashboardObjects[id]);
			var myElements = document.getElementById('dash-' + DashboardObjects[id]).childNodes;
			for (var index in myElements){
				//if(isSafari)
				if(myElements[index].tagName == 'H3') {
					myElements[index].firstChild.onclick = new Function("divToggleDashboard('"+DashboardObjects[id]+"')");
				}
			}
			/*
			
			if(myBody.className == DashboardObjects[id] || myBody.className == 'home')
 				document.getElementById('dash-'+DashboardObjects[id]).className = DashboardObjects[id];
 			else*/
 			
 			document.getElementById('dash-'+DashboardObjects[id]).className =  DashboardObjects[id];// +'-collapsed'
		}
	}
	//li's
	var childToggle = document.getElementsByTagName('li');
	var index = 1;
	for (var id in childToggle){
	
		    if(childToggle[id].className == 'expanded-collapsed' || childToggle[id].className == 'expanded-collapsed last' || childToggle[id].className == 'expanded' || childToggle[id].className == 'expanded last' || childToggle[id].className == 'last' || childToggle[id].className == '') {
			    var liLink = childToggle[id].firstChild;
    			if (liLink!=null)
    			{
    			
			    if (liLink.tagName=='A')
			        {
			        childToggle[id].id = 'li'+index;
	                // only apply to a tags
			        liLink.onclick = new Function("liToggleDashboard('li"+index+"')");
			        index++;
                    }
                                 }   			
		    }
	    
	}

	//find all the links and check to see if the link is this page's address
	var links = document.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) {
		if (links[i].href == docLocation || links[i].href == longDocLocation ) {
			links[i].className = 'current';
			//alert(links[i].href);
			var myElement = links[i].parentNode.parentNode.parentNode;	
			if(myElement.tagName == 'LI') {
				if(myElement.className == 'expanded-collapsed')
					myElement.className = 'expanded';
				else if(myElement.className == 'expanded-collapsed last')
					myElement.className = 'expanded last';
			}					
		}
	}
	
    //display current item in lhs custom menu if the current page is a child
            
    
    	
}

function objectExists(objectID) {
	return (document.getElementById(objectID) != null) ? true : false; 
}


function divToggleDashboard(expandID) {
var action;

	if (!document.getElementById) 
    	return;
    	
 	//get the class of the Object
 	if(objectExists('dash-'+expandID)) {
 		var myElement = document.getElementById('dash-'+expandID);
 		var myClass = myElement.className;
 		var dashPosition = myClass.indexOf('-');
 		if(dashPosition == -1) {
 			myElement.className = myClass +'-collapsed';
 			action="collapsing";
 		}
 		else {
 			myElement.className = myClass.substring(0, dashPosition);
 			action="expanding"; 
 		}
 		jsUpdateHeaderCookie(expandID,action)	
 	}
}



function jsUpdateHeaderCookie(expandID,action){

//alert(expandID + action);
	if (document.cookie.indexOf('selectedheaders=')==-1)
 	{
 	document.cookie='selectedheaders=none';
 	}
 	
 	var CookieSelectedNodesList='';
 	cookies=document.cookie.split(';');
 	for(var i=0;i< cookies.length;i++)
 	{
  	    if (cookies[i].indexOf('selectedheaders=')!=-1)
 	    {
 	    CookieSelectedNodesList=cookies[i].substring(cookies[i].indexOf('=')+1);
 	    }
   	}
 	
 	switch(action) {
 			case 'expanding':
 			jsAddNodetoHeaderList(expandID,CookieSelectedNodesList);				
 				break;
 			case 'collapsing': 
 			jsRemoveNodeFromHeaderList(expandID,CookieSelectedNodesList);				
 			    break;
 	} 	

}

function liToggleDashboard(expandID) {
var action;

    if (!document.getElementById) 
    	return;	
 	//get the class of the Object
 
 	if(objectExists(expandID)) {
 		var myElement = document.getElementById(expandID);
 		var myClass = myElement.className;
 		switch(myClass) {
 			case 'expanded': 				myElement.className = 'expanded-collapsed';
 			                                action="collapsing";
 											break;
 			case 'expanded last': 			myElement.className = 'expanded-collapsed last';
 			                              action="collapsing";
 											break;
 			case 'expanded-collapsed': 		myElement.className = 'expanded';
 			                                action="expanding";
 											break;
 			case 'expanded-collapsed last': myElement.className = 'expanded last';
 			                                 action="expanding";
 											break;
 		} 		
 	}

    
 	// update the cookie that stores the id of all expanded nodes
 	if (document.cookie.indexOf('selectednodes=')==-1)
 	{
 	document.cookie='selectednodes=none';
 	}
 	
 	var CookieSelectedNodesList='';
 	cookies=document.cookie.split(';');
 	for(var i=0; i< cookies.length;i++)
 	{
  	    if (cookies[i].indexOf('selectednodes=')!=-1)
 	    {
 	    CookieSelectedNodesList=cookies[i].substring(cookies[i].indexOf('=')+1);
 	    }
   	}
 	
 	switch(action) {
 			case 'expanding':
 			jsAddNodetoList(expandID,CookieSelectedNodesList);				
 				break;
 			case 'collapsing': 
 			jsRemoveNodeFromList(expandID,CookieSelectedNodesList);				
 			    break;
 	} 		


}

function jsAddNodetoList(NewValue,List)
// if the supplied value does not already exist in the list then add it
{
    if(List.indexOf(NewValue)==-1)
    {
       if ((List.indexOf('none')==0)|| (List.indexOf('undefined')==0))
       {document.cookie='selectednodes='+  NewValue;}
       else
       {document.cookie='selectednodes='+ List + ',' + NewValue;}
 	}   
}

function jsAddNodetoHeaderList(NewValue,List)
// if the supplied value does not already exist in the list then add it
{

    if(List.indexOf(NewValue)==-1)
    {
       if ((List.indexOf('none')==0)|| (List.indexOf('undefined')==0))
       {document.cookie='selectedheaders='+  NewValue;}
       else
       {document.cookie='selectedheaders='+ List + ',' + NewValue;}
 	}   
}

function jsRemoveNodeFromList(NewValue,List)
{
//split the comma delimited List
//rebuild the List without the selected item
var selectedids=List.split(',');
var updatedList;
var firstitemflag;
firstitemflag=='y';
    for(var i=0;i< selectedids.length;i++)
    {
        if ((selectedids[i]!=NewValue) && (selectedids[i]!='undefined') )
        {
            if (firstitemflag=='y')
            {
            updatedList=updatedList + selectedids[i];
            firstitemflag=='n'
            }
            else
            {
            updatedList=updatedList + ',' + selectedids[i];
            }
        }
    }
 	document.cookie='selectednodes=' + updatedList;
}

function jsRemoveNodeFromHeaderList(NewValue,List)
{
//split the comma delimited List
//rebuild the List without the selected item
var selectedids=List.split(',');
var updatedList;
updatedList='';
var firstitemflag;
firstitemflag='y';
    for(var i=0;i < selectedids.length;i++)
    {
        if ((selectedids[i]!=NewValue)&& (selectedids[i]!=null))
        {
            if (firstitemflag=='y')
            {
            //alert('FI true adding = ' + selectedids[i]);
            updatedList=updatedList + selectedids[i];
            firstitemflag='n'
            }
            else
            {
          //alert('fi='+firstitemflag+'adding = ' + selectedids[i]);
            updatedList=updatedList + ',' + selectedids[i];
            }
        }
    }
 	document.cookie='selectedheaders=' + updatedList;
}
function jsInitialiseHeaders(){
// by default all left hand nav items should be expanded
var HeaderList="";
var FirstElementFlag='y';
if (document.cookie.indexOf('selectedheaders=')==-1)  	//write the cookie value to set all headers to expanded by default
 	{
 	    for (var element in DashboardObjects){
 	        if (FirstElementFlag=='y')
 	        {
 	        //alert('first');
            HeaderList=HeaderList  + DashboardObjects[element]  ; 
             FirstElementFlag='n';   
 	        }
 	        else
 	        {
 	          HeaderList=HeaderList + ',' + DashboardObjects[element]  ; 
 	         
 	        } 
 	    } 
 	   document.cookie='selectedheaders=' + HeaderList ;
 	} // end if
 	
 	
}


function setCustomMenuState()
{
  	jsInitialiseHeaders();
    var CookieSelectedNodesList='';
    var CookieSelectedHeadersList='';
 	cookies=document.cookie.split(';');
 	var ThisElement;
    var selectedNodes;
  	

  	// nodes
 	for(var i=0;i < cookies.length;i++)
 	{
  	    if (cookies[i].indexOf('selectednodes=')!=-1)
 	    {
 	    CookieSelectedNodesList=cookies[i].substring(cookies[i].indexOf('=')+1);
 	    }
   	}

   	selectedNodes=CookieSelectedNodesList.split(',');
    for(var i=0;i < selectedNodes.length;i++)
    {
        if((selectedNodes[i]!='')&&(selectedNodes[i]!='undefined')&&(selectedNodes[i]!='none'))
        {
        
        ThisElement= document.getElementById(selectedNodes[i]);
        ThisElement.className='expanded';
        
        }
    }
 //headers
    //because we do not know which controls will be on this page until run time look for all elements with dash- as part of their id and initialise them to be collapsed then read the items from the cookie that should be expanded
    var headers=document.getElementsByTagName('div');
    for (var i=0;i < headers.length;i++)
    {
        if (headers[i]!=null)
        {
            if (headers[i].id.indexOf('dash-')!=-1) // if its a dashboard div
                {
                headers[i].className=headers[i].id.replace('dash-','') + '-collapsed';
                }
        }
    }
    
    // now expand headers that are in the expanded cookie list
    for(var i=0;i < cookies.length;i++)
 	{
  	    if (cookies[i].indexOf('selectedheaders=')!=-1)
 	    {
 	    CookieSelectedHeadersList=cookies[i].substring(cookies[i].indexOf('=')+1);
 	    }
 	    
   	}
   	selectedHeaders=CookieSelectedHeadersList.split(',');
    for(var i=0;i < selectedHeaders.length;i++)
    {
        if((selectedHeaders[i]!='')&&(selectedHeaders[i]!='undefined'))
        {
       
        ThisElement= document.getElementById('dash-' + selectedHeaders[i]);
      // alert(selectedHeaders[i]);
        if (ThisElement!=null )
            {
             
            ThisElement.className=selectedHeaders[i];
            }
        }
    }
 
 
 

}



addLoadEvent(function() {
    setTimeout(initDashboard, 2);
   setTimeout(setCustomMenuState,5);
   
});

