/*_______________________________________________________________________
 | 
 | Module owner  : Digital sun productions (http://www.digsun.com)
 | Created (by)  : Pär Nordlund
 |               : 2006-06-19
 | Modified (by) : ______ ___________ 
 |               : xxxx-xx-xx
 | 
 | Description   : Menusystem.
 |
 | License       : This file is created and owned by the Digital sun productions network
 |                 and may not be disseminated outside the organisation without persmission.
 |                 Contact sales@digsun.com for questions regarding licence.
 | 
  ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/


/*_______________________________________________________________________
 | Design structure
  ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
	<div id="ds_menu_container">
		<div id="GROUP">
			<div>Headline!</div>
			<div>Submenu</div>
		</div>
	</div>

	All submenues can include unlimited number of GROUP's or other html elements
	and have unique css properties.
	
	ie.
	<div id="ds_menu_container">
		<div id="GROUP">
			<div>Headline!</div>
			<div>
				<h1>Sub menu</h1>
				Text <a href="">Link</a><br><br>
				
				<div id="GROUP">
					<div>Headline 1.1</div>
					<div>Submenu </div>
				</div>
				<div id="GROUP" style="position: absolite; right: 0px; top: 0px">
					<div>Headline 1.2</div>
					<div>Submenu</div>
				</div>
				<img src="image.gif">
			</div>
		</div>
	</div>
*/


/*_______________________________________________________________________
 | Objects, variables.
  ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	var ds_active_nodes;				// Holds the string with active node.
	var ds_sub				= Array();
	var ds_headline			= Array();
	var ds_gindex			= 0;
	var ds_hide_unactive	= true;		// Used to close GROUP's that are not in the heirarki.
	var ds_change_css_class	= true;	// Change between ds_menu_headline_on/off when active.		

/*_______________________________________________________________________
 | Functions
  ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/

	/* Check node type and return true/false
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function ds_nodeType(node, ntype) {
  	
		if (node.nodeName.toUpperCase() == ntype) return true;
		return false;
	}

	/* Change background color if node isnt active.
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	 
	function ds_menu_update_class(node, mouseon) {
	
		if (node.active == true) {
			if (mouseon)
				node.className = "ds_menu_headline_hover_active";
			else
				node.className = "ds_menu_headline_on";
		  return;
		}
		if (mouseon)
			node.className = "ds_menu_headline_on";	
		else
			node.className = "ds_menu_headline_off";
		
		return;
	  
	}

	/* Return all childs in node by node type, such as div/text and so on.
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function ds_getChildrenByType(node, nType) {
	  	
		var result = Array();
		for (var a = 0; a < node.childNodes.length; a++) {
			if (node.childNodes[a].nodeName == nType) result[result.length] = node.childNodes[a];
		}
		return result;
	}

	/* Hide all nodes which are not in the index/hierarki of the current node.
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function ds_hideAll(index) {
		var hide;
	 	var res = index.split(".");
	 	for (var a = 0; a < ds_gindex; a++) {
	 	  	hide = true;
	 	  	
	 	  	for (var i = 1; i < res.length; i++) {
			   if (a == res[i]) hide = false;
			}
			
	 	  	if (hide) {
	 	  	  	ds_headline[a].active = false;
				ds_sub[a].style.display = "none";
				if (ds_change_css_class) ds_headline[a].className = "ds_menu_headline_off";
	 	  	}
	 	}
	 	
	}

	/* Change display property of clicked node and hide all the rest 
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function ds_showNode(node) {
		var ns = ds_sub[node.index];
		var nh = ds_headline[node.index];
		
		
		if (ns.style.display == "none") {
			ns.style.display = "block";
			nh.active = true;
			if (ds_change_css_class) {
			  nh.className = "ds_menu_headline_hover_active";	
			  
			}
		} else {
		  	ns.style.display = "none";
		  	nh.active = false;
		  	if (ds_change_css_class) nh.className = "ds_menu_headline_on";	
		}
	  	
	  	if (ds_hide_unactive) ds_hideAll(node.indexcontainer);
	  	
		
	}

	/* Progress throughout the div-hierarki and index all the groups/headlines and submenues.
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function fetch_groups(node, index) {
		
		var ar = node.childNodes;
		var head, sub, n;
	
		for (var a = 0; a < ar.length; a++) {
			n = ar[a];	
			if (ds_nodeType(n, 'DIV') && n.attributes) {
				
			  	if (n.id != "GROUP") {
			  	  	fetch_groups(n, index);

			  	} else {
			  	  	
					var elements = ds_getChildrenByType(n, 'DIV');

					if (elements.length > 1) 	
					if (!(elements.length % 2)) {

						var i;

						for (i = 0; i < elements.length; i += 2) {
						  
							var hindex = i;
							var sindex = (i + 1);

							ds_headline[ds_gindex] = elements[hindex];
							ds_sub[ds_gindex] = elements[sindex];
							
							ds_sub[ds_gindex].style.display = "none";

							head = ds_headline[ds_gindex];
							head.index = ds_gindex;
							head.indexcontainer = index + "." + ds_gindex;

							var oldfunc = head.onclick;
							head.onclick = function() {
							  	if (oldfunc) oldfunc();
								ds_showNode(this);
							}					
						
							ds_gindex++;
							fetch_groups(elements[sindex], head.indexcontainer);
						}
					}
				}
			}
		}
		

	}
	/* Initialize the menu structure by searching for every group / headline and submenu divs
	   in the ds_menu_container div
	 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	function ds_menu_init(element_name){
		ds_container = document.getElementById(element_name);
		if(!ds_container) return; /* alert("Could not find div # " + element_name); */
		fetch_groups(ds_container, 0);
		return;
	}

/*_______________________________________________________________________
 | End of file
  ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*/
	if(document.createElement) window.onload = function(){ds_menu_init("ds_menu_container")};
