/*
*Mod_Accordionmenu by James Frank
*
* License: GNU/GPL
*
*Based off of  v1.0 - by ah72, July 2008
*
*/

function init_accord(rt, md, cp, de, omo, ad, ps, tc1, tc2, thp, thh1, thh2, as){
	
	ddaccordion.init({
	headerclass: "silverheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: rt, //Reveal content when user clicks or onmouseover the header. Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: md, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: cp, //Collapse previous content (so only one open at any time)? true/false
	defaultexpanded: [de], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: omo, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: ad, //Should contents open by default be animated into view?
	persiststate: ps, //persist state of opened contents within browser session?
	toggleclass: [tc1, tc2], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: [thp, thh1, thh2], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: as, //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})
	}

function accordionMenu(menuId, srcExpandImage, srcCollapseImage, accOptions, accHoverDelay, bDoHover) {
if($(menuId)){

    // getting accordion parent items ("li" tags with class "parent")
    $(menuId).accParentItems = [];

    for(var i = 0; i < $(menuId).childNodes.length; i++) {

        if($(menuId).childNodes[i].className.indexOf('parent') >= 0){
            $(menuId).accParentItems.push($(menuId).childNodes[i]);
        }
    }


    $(menuId).accTogglers = [];
    $(menuId).accElements = [];
    var startItem = -1;

    for(var i = 0; i < $(menuId).accParentItems.length; i++) {

		if(srcExpandImage.length > 0){

			// creating accordion togglers
			var accToggler = document.createElement("img");
	
			accToggler.setAttribute("title","Expand");
			accToggler.setAttribute("src",srcExpandImage);
		}
		else
		{
			var accToggler = document.createElement("span");	
		}
			$(menuId).accParentItems[i].insertBefore(accToggler, $(menuId).accParentItems[i].firstChild);
		
			$(menuId).accTogglers.push(accToggler);
		

        // accordion elements
        $(menuId).accElements.push($(menuId).accParentItems[i].getElementsByTagName('ul')[0]);

        // searching for active menu item to make the accordion show its sub-items when page loads
        if ( $(menuId).accParentItems[i].className.indexOf('active') >= 0 ) {
            startItem = i;
        }
   }

    //create our Accordion instance
    if ( $(menuId).accParentItems.length > 0 ){
        $(menuId).Accordion = new Accordion($(menuId).accTogglers, $(menuId).accElements, $merge({
            opacity: false,
            alwaysHide: true,
            show: startItem,
            duration: 600,
            transition: Fx.Transitions.Bounce.easeOut,

            onActive: function(toggler, element){
                element.parentNode.parentNode.setStyle('height', 'auto');
                toggler.setAttribute("src", srcCollapseImage);
                toggler.setAttribute("title","Collapse");
            },
            onBackground: function(toggler, element){
                element.parentNode.parentNode.setStyle('height', 'auto');
                element.setStyle('height', element.offsetHeight+'px');
                toggler.setAttribute("src", srcExpandImage);
                toggler.setAttribute("title","Expand");
            }

            }, accOptions)

        );
    }


    accTimer = null;
    if (!accHoverDelay) var accHoverDelay = 200;
	
    for(var i = 0; i < $(menuId).accParentItems.length; i++) {

        eval("function accOnclickFunc(){return function(){ if( $('"+menuId+"').accElements["+i+"].style.height == '0px' ) { $('"+menuId+"').Accordion.display("+i+") }}}");
		eval("function accOnMouseoverFunc(){return function(){if( $('"+menuId+"').accElements["+i+"].style.height == '0px' ){accTimer = $('"+menuId+"').Accordion.display.delay("+accHoverDelay+", $('"+menuId+"').Accordion, "+i+");}}}");
		eval("function accOnmouseoutFunc(){return function(){if($defined(accTimer)){$clear(accTimer);}}}");

        $(menuId).accParentItems[i].firstChild.nextSibling.onclick = accOnclickFunc();
		if (bDoHover==1) {
			$(menuId).accParentItems[i].firstChild.nextSibling.onmouseover = accOnMouseoverFunc();
        }
		$(menuId).accParentItems[i].firstChild.nextSibling.onmouseout = accOnmouseoutFunc();
    }


    for(var i = 0; i < $(menuId).accElements.length; i++) {
        $(menuId).accElements[i].setAttribute('id', menuId+'_'+i);
        accordionMenu(menuId+'_'+i, srcExpandImage, srcCollapseImage, accOptions, accHoverDelay, bDoHover)
    }

}
}
