// This files creates the dropdown menus
// City of Meridian
// written by Mike Tanner
// 5-5-2008

//--------------------------------------------------------------------------------------------------
// All material contained within this and associated downloaded pages is the property of 4thorder(TM) 
// Copyright © 2005.  All rights reserved.
//
// Author: Michael Falatine || Authors email: 4thorder@4thorder.us
//
// USAGE: You may use this script for commercial or personal use, however, the copyright is retained-
// by 4thorder (TM).
//
// For other free scripts visit: http://simplythebest.net/scripts/
//---------------------------------------------------------------------------------------------------

// ||||||||||||||||
// | Basic Set-up  |
// ||||||||||||||||

// BRANCH CONTROL SETTINGS [Value MUST be in quotes]:
	// Enable single branch opening ONLY
	// Options: ['yes'=one branch at a time, ''=all branches will open]
	// Note: ALL Values other than 'yes' allows user to open more than one branch at a time
	var oneBranch='no';

// EVENT TYPE SETTINGS [Value MUST be in quotes]:
	// Options:
	// 'mouseover'= branch expands when mouse is OVER <TH> element
	// 'click'=branch expands when <TH> element is clicked
	var handlerTYPE='click';

// TRANSPARENCY SETTINGS (%) [Value MUST NOT be in qoutes]:
	var TValue=90;
		// 100=100% visible, 0=invisible [MUST BE  a number between 0 and 100]
		// Can be decimal (example: 70.5) or integer (example: 71)
		// Does not work in Opera

// IMAGE PLACEMENT [Value MUST be in quotes]:
	// Options:
	// 'before'=images will be inserted BEFORE content within <TH> element
	// 'after'=images will be inserted AFTER content within <TH> element
	// ''=images will NOT be included within <TH> element
	var ImagePlacement='before';


// |||||||||||||||||||||||
// | Define Images Here  |
// |||||||||||||||||||||||
// [Value MUST be in quotes]
// All images MUST be located in the folder "dscript122"
// "dscript122" folder MUST be located in SAME directory as the webpage that you want the menu to be on
// Note: Some images have been provided.  You may use provided, your own, or none.
// Sample images provided can be viewed in the "StartHere.htm" file

// SET [EXPAND] IMAGE FILE NAME:

	var imagePLUS='arrow.gif';

// SET [COLLAPSE] IMAGE FILE NAME:
		
	var imageMINUS	='arrowdn.gif';
	
	
	//--------------------------------------------------------------------------------------------------
// All material contained within this document and associated downloaded pages 
// is the property of 4thorder(TM) unless otherwise noted
// Copyright © 2005.  All rights reserved.
//
// Author: Michael Falatine || Authors email: 4thorder@4thorder.us
//
// USAGE: You may use this script for commercial or personal use, however, the copyright is retained-
// by 4thorder (TM).
//
// For other free Scripts visit: http://www.4thorder.us/Scripts/
//---------------------------------------------------------------------------------------------------

//-----------------begin insertAdjacent code-----------------------------------------------------
// This portion written by Thor Larholm thor@jscript.dk
// Allows for insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// functionality in Netscape / Mozilla /Opera
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}
//----------------------end insertAdjacent code-------------------------------------------------------

// ::::::::::::::::
// :::: Styles :::
// ::::::::::::::::
function setSTYLES()
{
mainTABLEElement=document.getElementById("mainTable");
TDCol=mainTABLEElement.getElementsByTagName("TD");
for (s=0; s < TDCol.length; s++)
	{TDCol.item(s).style.verticalAlign='top';}
	
// Set Transparency level
if(navigator.appName == 'Microsoft Internet Explorer')
	{document.getElementById('menuSystem').style.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+TValue+")";}
else
	{document.getElementById('menuSystem').style.MozOpacity=1;
	 TValue=parseFloat(TValue/100-.001); // .001 is fix for moz opacity/image bug
	 document.getElementById('menuSystem').style.MozOpacity=TValue;}
	
// Collection used to determine if section has children
SUBTABLECol=mainTABLEElement.getElementsByTagName("TABLE");
for (s=0; s<SUBTABLECol.length; s++)
	{
	TDChildrenCol=SUBTABLECol.item(s).getElementsByTagName("TD");
	// If children then insert image depending on ECState
	if(TDChildrenCol.length>0)
		{
		THCol=SUBTABLECol.item(s).getElementsByTagName("TH");
		if(ImagePlacement=='before')
			{
			var str='<IMG border="0" src="\images\/'+imagePLUS+'" alt="Expand ALL">&nbsp;';
			THCol.item(0).insertAdjacentHTML("afterBegin", str);
			}
		else if(ImagePlacement=='after')
			{
			var str='&nbsp;<IMG border="0" src="images\/'+imagePLUS+'" alt="Expand ALL">';
			THCol.item(0).insertAdjacentHTML("beforeEnd", str);
			}
		else {}
		THCol.item(0).style.cursor="pointer";
		}
	}
}

// :::::::::::::::::::::::::::
// :::: Global Functions :::
// :::::::::::::::::::::::::::
window.onload=InitializePage;

function InitializePage()
{
    try
    {
    

	    hideALL(); 
    	
	    //alert('InitializePage ' + document.cookie);
    	
	    loadPreviousSessionValues();
	    setSTYLES(); 
    	
	    attachEventhandlers();
    }
    catch(e)
    {
    
    }
    
	
	
}

// Attach event handlers to all images within container
function attachEventhandlers()
{

    try
    {


        mainTABLEElement=document.getElementById("mainTable");
        TABLECol=mainTABLEElement.getElementsByTagName("TABLE");

        if (TABLECol!=null)
	        {for (l=0; l<TABLECol.length; l++)
		        {
		        THCol=TABLECol.item(l).getElementsByTagName("TH");
		        THCol.item(0).setAttribute('id',l);
		        if(handlerTYPE=='mouseover')
			        {THCol.item(0).onmouseover=eHandler;
			         document.getElementsByTagName("BODY").item(0).onclick=eHandler;}
		        else if(handlerTYPE=='click')
			        {document.getElementsByTagName("BODY").item(0).onclick=eHandler;}
		        }
	        }
    	
    }
    catch(e)
    {}
	
	
}

function hideALL()
{
    try
    {
	    mainTABLEElement=document.getElementById("mainTable");
	    TABLECol=mainTABLEElement.getElementsByTagName("TABLE");
	    for (a=0; a < TABLECol.length; a++)
	    {
		    IMGCol=TABLECol.item(a).getElementsByTagName("IMG");
		    if (IMGCol.item(0)!=null)
		    {
				    IMGCol.item(0).setAttribute('src','images/'+imagePLUS);
		    }
    		
		    THCol=TABLECol.item(a).getElementsByTagName("TH");
		    for (b=0; b<THCol.length; b++)
		    {
			    THCol.item(b).setAttribute("ECState",0);
		    }
    			
		    TRCol=TABLECol.item(a).getElementsByTagName("TR");
    		
		    for (c=1; c<TRCol.length; c++)
		    {
			    //alert(TRCol.item(c));
			    TRCol.item(c).style.display='none';
		    }
	    }
	 }
	 catch(e)
	 {}
}

function showSingle()
{
    try
    {
        mainTABLEElement=document.getElementById("mainTable");
        TABLECol=mainTABLEElement.getElementsByTagName("TABLE");

        if (TABLECol!=null)
	        {
	        for (z=0; z < TABLECol.length; z++)
		        {
		        TRCol=TABLECol.item(z).getElementsByTagName("TR");
		        THCol=TABLECol.item(z).getElementsByTagName("TH");
		        IMGCol=TABLECol.item(z).getElementsByTagName("IMG");

		        // Grab ECState and expand or collapse branch
		        State=THCol.item(0).getAttribute("ECState");
		        if(State==0)
			        {
			        // ECState is COLLAPSED (+) (0)
			        if (IMGCol.item(0)!=null){IMGCol.item(0).setAttribute('src','images/'+imagePLUS);}
			        for (l=1; l<TRCol.length;l++)
				        {
				        TRCol.item(l).style.display='none';
				        }
			        }
		        else	 if(State==1)
			        {
			        // ECState is EXPANDED (-) (1)
			        if (IMGCol.item(0)!=null){IMGCol.item(0).setAttribute('src','images/'+imageMINUS);}
				        for (m=0; m<TRCol.length; m++)
				        {// Browser compatibility code
				        if(navigator.appName == 'Microsoft Internet Explorer')
					        {
					        if(navigator.userAgent.indexOf('Opera') != -1)
						        {TRCol.item(m).style.display='table-row';}
					        else	{TRCol.item(m).style.display='block'; }
					        }
				        else	{TRCol.item(m).style.display='table-row';}
				        }
			        }
		        }
	        }
	}
	catch(e)
	{}
}

// ::::::::::::::::::::::::::
// :::: Event Handlers ::::
// ::::::::::::::::::::::::::
var targ;
var previousTargID

function eHandler(e)
{
    try
    {
	    // Browser compatibility code
	    if (!e) var e = window.event;
	    if (e.target) targ = e.target;
	    else if (e.srcElement) targ = e.srcElement;
	    if (targ.nodeType == 3) // defeat Safari bug
		    targ=targ.parentNode;
    	
	    THElement=findTH(targ);
    	
    	
    	
	    if(THElement!=null)
	    {
    		 
    		
		    if(THElement.id!=previousTargID && oneBranch=='yes' )
		    {
				    hideALL();
		    }
    		
		    if(handlerTYPE=='mouseover')
		    {
			    hideALL();
		    }		
		    previousTargID=THElement.id
    		
		    // Toggle ECState
		    State=THElement.getAttribute("ECState");
    	
		    if(State==0)
		    {
				    THElement.setAttribute("ECState",1);
		    }
		    else
		    {
			    THElement.setAttribute("ECState",0);
		    }
    		
		    //alert(document.cookie);
		    document.cookie = "last_selection_" + THElement.title + "=" + THElement.getAttribute("ECState") + "; path=/"
		    //alert(document.cookie);
    	
		    showSingle();
	    }
	    else
	    {
		    // Remarking out since this was collapsing the menu anytime you clicked on the browser
		    //hideALL();
	    }
	    //alert(THElement.title)
	    //alert(THElement.getAttribute("ECState"));
	    //alert(THElement.id);
    	
    	
    	
    	
	    //alert(getCookie("last_selection_" + THElement.id))
	    //alert(getCookie("last_selection__" + THElement.id))
    	
	    //alert(document.cookie);
    }
    catch(e)
    {}
}


// ***********************************************************************************************
// This function loads the previous stored cookie value and uncollpases the menus that were clicked on
// Written by Mike Tanner (tannerm@meridiancity.org)
// 5-4-2008
// ****************************************************************************************************
function loadPreviousSessionValues()
{
    try
    {
	    //alert('loadPreviousSessionValues');
    	
	    var menuCount = getCookie("menuCount");
    	
	    //alert('Total Menus ' + menuCount);
    	
	    var a_all_cookies = document.cookie.split( ';' );
	    var a_temp_cookie = '';
	    var cookie_name = '';
	    var cookie_value = '';
	    var menu_id='';
	    var splitter='';
	    var b_cookie_found = false; // set boolean t/f default f
	    var mainTABLEElement='';
	    var TABLECol='';
	    var c=0;
	    var a=0;
	    var THCol='';
	    var TRCol='';
    	
	    //alert(document.cookie);
    	
	    for ( i = 0; i < a_all_cookies.length; i++ )
	    {
		    // now we'll split apart each name=value pair
		    a_temp_cookie = a_all_cookies[i].split( '=' );
    		
    		
		    // and trim left/right whitespace while we're at it
		    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    		
    		
		    if ( cookie_name.substring(0, 15) == "last_selection_" )
		    {
			    b_cookie_found = true;
			    // we need to handle case where cookie has no value but exists (no = sign, that is):
			    if ( a_temp_cookie.length > 1 )
			    {
				    //splitter =cookie_name.split('last_selection_');
				    cookie_name = cookie_name.replace('last_selection_','');

				    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
    				
    				
				    mainTABLEElement=document.getElementById("mainTable");
				    TABLECol=mainTABLEElement.getElementsByTagName("TABLE");
				    for (a=0; a < TABLECol.length; a++)
				    {
					    // Get Header Columns colletion
					    THCol=TABLECol.item(a).getElementsByTagName("TH");
    										
					    // Get table row collection
					    TRCol=TABLECol.item(a).getElementsByTagName("TR");
    					
					    IMGCol=TABLECol.item(a).getElementsByTagName("IMG");
    					
					    //TRCol=TABLECol.item(a).getElementById(cookie_name);
    					
					    if (THCol.item(0).title == cookie_name)
					    {
						    if (cookie_value == "1")
						    {
							    //alert(IMGCol.length);
							    if (IMGCol.item(0)!=null)
							    {
									    alert('changed image');
									    IMGCol.item(0).setAttribute('src','images/'+imageMINUS);
							    }
							    THCol.item(0).setAttribute("ECState",1);
    							
						    }
    									
    						
						    // Short Circuit
						    a = TABLECol.length + 1;
    					
					    }
    					
				    }
			    }
		    }
	    }
    	
    	
	    showSingle();
	    return true;
    }
 catch(e)
 {}

}




function getCookie( check_name ) {
    try
    {
	    // first we'll split this cookie up into name/value pairs
	    // note: document.cookie only returns name=value, not the other components
	    var a_all_cookies = document.cookie.split( ';' );
	    var a_temp_cookie = '';
	    var cookie_name = '';
	    var cookie_value = '';
	    var b_cookie_found = false; // set boolean t/f default f
    	
	    for ( i = 0; i < a_all_cookies.length; i++ )
	    {
		    // now we'll split apart each name=value pair
		    a_temp_cookie = a_all_cookies[i].split( '=' );
    		
    		
		    // and trim left/right whitespace while we're at it
		    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    	
		    // if the extracted name matches passed check_name
		    if ( cookie_name == check_name )
		    {
			    b_cookie_found = true;
			    // we need to handle case where cookie has no value but exists (no = sign, that is):
			    if ( a_temp_cookie.length > 1 )
			    {
				    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			    }
			    // note that in cases where cookie is initialized but no value, null is returned
			    return cookie_value;
			    break;
		    }
		    a_temp_cookie = null;
		    cookie_name = '';
	    }
	    if ( !b_cookie_found )
	    {
		    return null;
	    }
	}
	catch(e)
	{}
}

function findTH(t)
{
   try
   {
    	
	    //alert(t.title);
    	
      if (t.tagName == "TH")
  	    {return t;}
      else if
  	    (t.tagName == "BODY")
  	    {return null;}
      else
      {return findTH(t.parentNode);}
   }
   catch(e)
   {}
}


