// ----------------------------------------
// menu - accordion
// Version 1.0.2010408.1
// Dwain Blazej Dwain.Blazej.Public@gmail.com

// Example init:
// menu_accordion.init('menu_accordion','News');
// menu_accordion.init('[HTML ID of div containing the menu]',[index of menu to start as expanded]);
//
// This script expects an element (such as in the header) name id="menu_accordion_expansion-[menu_name]"
// The script expects two image files with the same name except one name contains the string " - expanded" while the other contains the name " - collapsed".  Example HTML:
//	<img class="menu_accordion_expansion" id="menu_accordion_expansion-2009_Parmelee_Lectures" src="../asset/image/interface/icon - triangle - expanded - 11x11.gif" width="11" height="11" />
	//
// In document header include:
//	<script type="text/javascript">
	//	  document.write(unescape('%3Cstyle type="text/css"%3E#menu_accordion { display:none } .menu_accordion_header {cursor:pointer;}%3C/style%3E'));
//	</script>
	// this is so if JavaScript is enabled, then make the initial page load render hidden.  Let the JavaScript menu_accordion_accordion function initialize (collapse) the menu before making the menu visible.  This prevents the menu from showing then shrinking when the page is loaded.
//
// Bugs:
// Menu height should be recalculated on window resize.  Workaround: Reload page.



var menu_accordion=function(){
	// speed: larger number means *slower*.
	// timer:  the number of milliseconds (1/1000 of a second) to wait between menu silde animation frames.
	// animateP: Set to 'yes' for animation.
	var speed=2; var timer=42;
	animateP='yes'
	// animateP='no'
	var aContentNames=[]; var menu_id;
	var js_file_name='menu - accordion.js';
	return{
		init:function(menu_id_arg,c){
			menu_id = menu_id_arg;
			var s,ds,l,i,header_name_tail  // ,header_index
			var header_re = new RegExp('^'+menu_id+'_header-(.+)$');
			var d,did,section,eltExpandImg;
			s=document.getElementById(menu_id); 
			var tagSet=[];
			var tagSetIndex;
			// s.getElementsByTagName('td')
			tagSet = tagSet.concat(s.getElementsByTagName('tr'), s.getElementsByTagName('div'));
			for (tagSetIndex=0;tagSetIndex<tagSet.length;tagSetIndex++) {
				ds=tagSet[tagSetIndex];
				l=ds.length; i=0;   // header_index=0;
				for(i=0;i<l;i++){
					d=ds[i]; did=d.id;
					if (typeof(did)=="undefined") {
						continue;
					}
					// alert('did: '+did);
					if(did.indexOf('_header-')!=-1){
						// header_index++;
						section = did.replace(menu_id+'_header-','');
						// Color -> Levels; center point 1.00 -> 1.33

						d.onmouseover=new Function('menu_accordion.mouseAction("'+section+'", "in");');
						d.onmouseout=new Function('menu_accordion.mouseAction("'+section+'", "out");');
						d.onclick=new Function('menu_accordion.process(this)');
						if (c != '') {
							header_name_tail=did.replace(header_re, '$1');
						}
						// alert('header: '+header_name_tail+'\ndid: '+did);
					}else if(did.indexOf('_subheader-')!=-1){
						section = did.replace(menu_id+'_subheader-','');
						// Color -> Levels; center point 1.00 -> 1.33
						// d.onmouseover=new Function('this.className = unescape(this.className).replace(/mouse_out/, "mouse_in"); var h = document.getElementById("'+sHeaderMain+'"); h.className = ');
						// d.onmouseover=new Function('this.className = unescape(this.className).replace(/mouse_out/, "mouse_in");');
						// d.onmouseout=new Function('this.className = unescape(this.className).replace(/mouse_in/, "mouse_out");');
						d.onmouseover=new Function('menu_accordion.mouseAction("'+section+'", "in");');
						d.onmouseout=new Function('menu_accordion.mouseAction("'+section+'", "out");');

						d.onclick=new Function('menu_accordion.process(this)');
					}else if(did.indexOf('_content-')!=-1){
						section=did.replace(menu_id+'_content-','');
						aContentNames.push(section);
						// alert('section: '+section);

						eltExpandImg=document.getElementById(menu_id+'_expansion-'+section);
						if((c != '')&&(c==header_name_tail)){  // (c==header_index)
							d.style.display='block';
							if(eltExpandImg) {this.expansion(eltExpandImg,'expanded'); }
						}
						else{
							d.style.height='0px'; d.style.display='none';
							if (eltExpandImg) {this.expansion(eltExpandImg,'collapsed');}
						}
						// if(1){ alert('End:\n'+eltExpandImg.getAttribute('src')+'\n'+unescape(x.getAttribute('src')));}
					} 
				}
			}

			// Once the visibility is setup, make the entire menu visible.
			s.style.display='block'
		},
		// This is needed to make headings composed of multiple elements behave as one logical element.
		// "section" a string containing the name of the section to act on.
		mouseAction:function(section,action){
			var elt=document.getElementById(menu_id+'_header-'+section);
			elt.className = unescape(elt.className).replace(/_mouse_[a-z]+/, '_mouse_'+action);
			elt = document.getElementById(menu_id+'_subheader-'+section);
			elt.className = unescape(elt.className).replace(/_mouse_[a-z]+/, '_mouse_'+action);
		},
		process:function(d){
			var cl,i; cl=aContentNames.length; i=0;
			for(i;i<cl;i++){
				var s,h,c,eltExpandImg,cd;
				s=aContentNames[i];
				// Assumes only one "header" ond "subheader"
				h=document.getElementById(menu_id+'_header-'+s);
				eltHeaderSub=document.getElementById(menu_id+'_subheader-'+s);
				eltExpandImg=document.getElementById(menu_id+'_expansion-'+s);
				c=menu_id+'_content-'+s; cd=document.getElementById(c); clearInterval(cd.timer);
				if(((h==d)||(eltHeaderSub==d))&&cd.style.display=='none'){
					this.expansion(eltExpandImg,'expanded');
					if(animateP=='yes') {
						cd.style.display='block';
						if(!cd.maxh){cd.style.height='auto'; cd.maxh=cd.offsetHeight; cd.style.height='0px'}
						this.islide(c,1);
					} else {
						cd.style.height='100%';
						cd.style.display='block';
						// cd.style.display='inline';
					}
				}else if(cd.style.display=='block'){ 
					this.expansion(eltExpandImg,'collapsed');
					if(animateP=='yes') { 
						this.islide(c,-1);
					} else {
						cd.style.display='none';
					}
				}
			}
		},
		// Changes the icon img src in elemnet "eltExpandImg" to the expansion status in "to".
		expansion:function(eltExpandImg,to){
			return; // disabling.
			var expansion_src;
			// On windows: systems the "src" attribute is URL escaped even when the source HTML is not.
			// On windows: "escape"ing  the "unescape"d string fails to produce the original URL.  
			// Work around by "unescape"ing but not re-"escape"ing. 
			expansion_src=unescape(eltExpandImg.getAttribute('src'));
			if(to=='expanded'){
				eltExpandImg.setAttribute('src', expansion_src.replace(/- expand( -|\.)([^\/]*)$/i, '- collapse$1$2'));
				//eltExpandImg.setAttribute('src', expansion_src.replace(/collapsed/, 'expanded'));
			}else if(to=='collapsed') {
				eltExpandImg.setAttribute('src', expansion_src.replace(/- collapse( -|\.)([^\/]*)$/i, '- expand$1$2'));
				//eltExpandImg.setAttribute('src', expansion_src.replace(/expanded/, 'collapsed'));
			} else {
				throw('menu - accordion: unrecognized expansion \"'+to+'\".');
			}
		},
		islide:function(i,d){var c,m; c=document.getElementById(i); m=c.maxh; c.direction=d; c.timer=setInterval("menu_accordion.slide('"+i +"')",timer)},
		slide:function(i){
			var c,m,h,dist; c=document.getElementById(i); m=c.maxh; h=c.offsetHeight;
			dist=(c.direction==1)?Math.round((m-h)/speed):Math.round(h/speed);
			if(dist<=1){dist=1}
			c.style.height=h+(dist*c.direction)+'px'; c.style.opacity=h/c.maxh; c.style.filter='alpha(opacity='+(h*100/c.maxh)+')';
			if(h<2&&c.direction!=1){
				c.style.display='none'; clearInterval(c.timer);
			}else if(h>(m-2)&&c.direction==1){clearInterval(c.timer)}
		}
	};}();

