/*
Javascript for creating a collapsible list of items.
Author: Glen Fingerholz - City of Saint Paul
Date: 9/20/2007
===================================================
To add a budget meeting/topic, stay within the Data[##} area;
DO NOT TOUCH anything below the line "function UpdateFrame(htmlCode)"
===================================================
*/

var expandChar = "+";
var colapseChar = "-";
var defaultSortOption = "date";
var expandedTextColor = "black";
var colapsedTextColor = "black";
var MainMenuItems = new Array();
var Data = new Array();

Data[0] = new Array('January','County','January 2','County assessor establishes property values');

Data[1] = new Array('March','County','March','2009 Property Tax statements delivered');

Data[2] = new Array('April','County','April','County Assessor "Open Book" sessions. <a href="http://www.co.ramsey.mn.us/prr/Assessor/index.htm" target="_blank">More info</a>');

Data[3] = new Array('May - July','City','','May: City departments start 2010 budget planning');

Data[4] = new Array('August','Mayor','August 11','<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=380" target="_blank">Mayor`s City Budget Proposal</a>');

Data[5] = new Array('August','Council','August 12','<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=382" target="_blank">Council Budget review begins</a>');

Data[6] = new Array('August','OFS','August 12','<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=382" target="_blank">Financial Services 2010 Budget overview</a>');

Data[7] = new Array('August','CAO','August 19','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1413" target="_blank">City Attorney: Budget presentation</a>');

Data[8] = new Array('August','Fire','August 19','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1413" target="_blank">Fire: Budget presentation</a>');

Data[9] = new Array('August','OFS','August 19','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1413" target="_blank">OFS: 5-year Forecast</a>');

Data[10] = new Array('August','OTC','August 26','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1421" target="_blank">Office of Technology: Budget presentation</a>');

Data[11] = new Array('August','DSI','August 26','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1421" target="_blank">Safety & Inspections: Budget presentation</a>');

Data[12] = new Array('August','OFS','August 26','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1421" target="_blank">Financial Svcs: Capital STAR, levy limits</a>');

Data[13] = new Array('September','Police','September 2','<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=393" target="_blank">Police overview</a>');

Data[14] = new Array('September','County','September 2', '<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=393" target="_blank">County Property tax trends</a>');

Data[15] = new Array('September','Parks & Rec','September 2', '<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=393" target="_blank">Parks & Rec overview</a>');

Data[16] = new Array('September','Public Works','September 2','<a href="http://stpaul.granicus.com/MediaPlayer.php?publish_id=393" target="_blank">ROW Maintenance Fund report</a>');

Data[17] = new Array('September','Council','September 9','Deadline: maximum 2010 City tax levy');

Data[18] = new Array('October','Public Hearing','October 7','Ratify 2009 Right-of-Way ROW rates');

Data[19] = new Array('October','Public Hearing','October 28','2010 Budget Hearing-Chambers');

/*
Data[0] = new Array('January','County','January 2','County assessor establishes property values');
*/

Data[20] = new Array('November','Public Hearing','November 4','Proposed 2010 ROW rates hearing-Chambers');

Data[21] = new Array('November', 'County', 'November 15', 'mid-Nov: Truth in Taxation notices are sent');

Data[22] = new Array('December','Public Hearing','December 2','Truth-in-Taxation Hearing-Chambers');

Data[23] = new Array('September','Public Works','September 9','<a href="http://stpaul.granicus.com/MediaPlayer.php?view_id=38&clip_id=1436" target="_blank">PW Budget Presentation</a>');
/*  
================
NOTES
=All entries above this comment will display in list; copy the template code below for new events.  

Data[_##_] = new Array('__month__ ','____topic:e.g. DSI___','__month date__','<a href="http://stpaul.granicus.com/____can use either clip_id or publish_id___" target="_blank">___e.g. Safety & Inspections (DSI) overview__</a>');

=Entries do NOT have to be chronological, but MAKE SURE that each Data[##] line is numbered sequentially or the list won't populate correctly

=Future events can be included in list above; just include the topic & leave the <a href.../a> code out - Add that when meeting takes place. ex: Dec 2.  This is also how to post an event that doesn't have a video link (e.g. deadlines)

=Use these standard topics to sort correctly:
CAO
City
Council
County
DSI
Fire
HRA
Human Resources
Library
Mayor
OFS
OTC
Parks & Rec
Police
Public Hearing
Public Works
RCVA
Risk Management
Sustainability
Water (SPRWS)
==========================
*/

function UpdateFrame(htmlCode)
{
	//update the frame html
	document.getElementById('frame').innerHTML = htmlCode;
}

function DisplayMainMenu()
{
	//make sure we have some data
	if(Data.length <=0)
		return;
	
	//collect the sort option
	var sBox = document.getElementById('sortorder');
	var sortOption = sBox.options[sBox.selectedIndex].value;
	
	//is the sort option blank, if so default it
	if(sortOption == '')
		sortOption = defaultSortOption;
	
	//start the frame inner html
	var html = "<table width=\"90%\" ><tr><td class=\"cal\">";
	
	//clear the main menu items array
	MainMenuItems.length = 0;
	
	//determine which sort option we using
	switch(sortOption)
	{
		case "date":
			//loop through the data array
			for(i=0;i<Data.length;i++)
			{
				//if the value is not equal to null
				if(Data[i] != null)
				{
					//declare a variable, using for determining groups
					var boolHasItem =  false;
					
					//loo through the main menu items
					for(x=0;x<MainMenuItems.length;x++)
					{	
						//if the data equals the main menu item group it
						if(Data[i][0] == MainMenuItems[x])
						{
							//set the value
							boolHasItem = true;
							//break out of loop
							break;
						}
					}
					
					//check for grouping
					if(!boolHasItem)
						MainMenuItems[i] = Data[i][0];
				}
			}
			break;
		case "presentation":
			for(i=0;i<Data.length;i++)
			{
				if(Data[i] != null)
				{
					var boolHasItem =  false;
					for(x=0;x<MainMenuItems.length;x++)
					{
						if(Data[i][1] == MainMenuItems[x])
						{
							boolHasItem = true;
							break;
						}
					}
					
					if(!boolHasItem)
						MainMenuItems[i] = Data[i][1];
				}
			}
// try the following line to get the presentation view to sort alpha
//			MainMenuItems.sort();
			break;
		default:
			break;	
	}
	
	// if(sortOption == "presentation")
		// MainMenuItems.sort();
		
	for(i=0;i<MainMenuItems.length;i++)
	{
		if(MainMenuItems[i] != null)
		{
			
		html += "<span id='s"+i+"' class='menulabel'>[<a style='cursor:hand' id='am" + i + "' onClick=\"SectionChange('m" + i +"')\">+</a>] " + MainMenuItems[i] + "</strong></span>";
		html += "<div class='submenuitems' id='m" + i + "'>";
		html += "</div>";
		}
	}
	
	UpdateFrame(html);
	
	for(key in MainMenuItems)
		document.getElementById('m' + key).style.visibility = "hidden";
}

function SectionChange(section)
{
	
	if(document.getElementById(section).style.visibility == "hidden")
	{
		//clear any text
		document.getElementById(section).innerHTML = null;
		
		//get the section id number
		var ix = section.substr(1,section.length);
		
		//set the color of the expanded section
		document.getElementById('s'+ix).style.color = expandedTextColor;
		document.getElementById('s'+ix).style.fontWeight = "bold";
		
		//create a variable to be used for the text of the sub menu section
		var tex = '';
		
		//collect the sort option
		var sBox = document.getElementById('sortorder');
		var sortOption = sBox.options[sBox.selectedIndex].value.replace(/^\s+|\s+$/g, '');	
		var pos = 0;
		if(sortOption == "presentation")
				pos = 1;	
				
		tex += "<ul>";
		if(pos==1 && Data[ix][2] != "")
			tex += "<li>" + Data[ix][2] + " - " + Data[ix][3] + "</li>";
		else
			tex += "<li>" + Data[ix][3] + "</li>";

		//get the main menu name
		var itemNameA = document.getElementById('s'+ix).innerHTML.split("]");
		
		//trim any spaces from it.
		var itemName = itemNameA[1].replace(/^\s+|\s+$/g, '');
		
		//IE carries over the closing strong tag for some reason, Look for it and strip it if found
		if(itemName.substr(itemName.length - 9, itemName.length) == "</STRONG>")
			itemName = itemName.substr(0, itemName.length -9);
		
		for(x=0;x<Data.length;x++)
		{
			//make sure we don't add the existing sub menu items
			if(x!=ix)
			{
				//match the name so that we can group the sub menu items
				var dif = 0
				var l = Data[x][pos].length
				for(i=0;i<l;i++)
				{
					if(Data[x][pos].replace('&','&amp;').charAt(i) != itemName.charAt(i))
						dif += 1;
				}
				
				//DEBUG LINE
				//alert("Difference in " + Data[x][pos] + " and " + itemName + " is " + dif);
				
				
				if(dif == 0)
				{				
					//add the items of this item to the submenus				
					if(pos==1 && Data[x][2] != "")
						tex += "<li>" + Data[x][2] + " - " + Data[x][3] + "</li>";
					else
						tex += "<li>" + Data[x][3] + "</li>";
				}
			}
		}
		
		tex += "</ul><br/>";
		
		//set the text of the sub menu item section
		document.getElementById(section).innerHTML = tex;
		
		//set it to visible
		document.getElementById(section).style.visibility = "visible";
		
		//swith the expander char to the colapse char
		document.getElementById('a' + section).innerHTML = colapseChar;
	}
	else if(document.getElementById(section).style.visibility == "visible")
	{
		//beccause of the differences in browswers we need to run 2 sets of code here.
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			//clear any text in the sub menu item section
			var bhtml = "<div class='submenuitems' id='"+section+"'>";
			bhtml += "</div>";
			
			document.getElementById(section).innerText = '';
			document.getElementById(section).outerHTML = bhtml;	
		}
		else
		{
			document.getElementById(section).innerHTML = '';
		}
		
		//hide it
		document.getElementById(section).style.visibility = "hidden";
		
		//switch the colapse char with the expand char
		document.getElementById('a' + section).innerHTML = expandChar;
		
		//get the section id number
		var ix = section.substr(1,section.length);
		
		//set the color of the expanded item to red
		document.getElementById('s'+ix).style.color = colapsedTextColor;
		document.getElementById('s'+ix).style.fontWeight = "normal";
		
	}
	else
	{
		//an element was not originally set to visible or hidden
		alert("What now?");
	}
}

function Sort()
{
	//clear the frame inner html
	document.getElementById('frame').innerHTML = null;
	
	//The called function determines the sort order. 
	DisplayMainMenu();
}
