﻿var ddaccordion={


	headergroup: {}, //object to store corresponding header group based on headerclass value
	contentgroup: {}, //object to store corresponding content group based on headerclass value

	collapseall:function(headerclass){ //PUBLIC function to collapse all headers based on their shared CSS classname
		var $headers=this.headergroup[headerclass]
		this.contentgroup[headerclass].filter(':visible').each(function(){
			$headers.eq(parseInt($(this).attr('contentindex'))).trigger("evt_accordion")
		})
	},


	expandit:function($targetHeader, $targetContent, config, useractivated, directclick, skipanimation)
	{
		//alert('Header after class='+$targetHeader.attr('className')+' Header ID='+$targetHeader.attr('id')+' | Content class='+$targetContent.attr('className')+'  | ID='+$targetContent.attr('id'));
		//alert($targetHeader.attr('className'));
		this.transformHeader($targetHeader, config, "expand");

		check_classID=$targetHeader.attr('id');
			check_item=check_classID.indexOf('menu_');	
			if (check_item!=-1)
			{
				ind_st=check_classID.indexOf('menu_');
				ind_st+=5;	
				cat_code=check_classID.substr(ind_st);
			}
			//alert('cat_code='+cat_code);	
		
			///////// закрываем все субкатегории

			var sub_close_class=$('#content_'+cat_code+'').attr('className');
			$('.'+sub_close_class+'').slideUp(skipanimation? 0 : config.animatespeed);
			//////////////////////////////////////////////////////////////////////////	

		$('#content_'+cat_code+'').slideDown(skipanimation? 0 : config.animatespeed);
		//alert($('#content_'+cat_code+'').attr('className'));

	  	
	},

	collapseit:function($targetHeader, $targetContent, config, isuseractivated)
	{
		this.transformHeader($targetHeader, config, "collapse")
		$targetContent.slideUp(config.animatespeed, function(){config.onopenclose($targetHeader.get(0), parseInt($targetHeader.attr('headerindex')), $targetContent.css('display'), isuseractivated)})
	},

	transformHeader:function($targetHeader, config, state)
	{
		$targetHeader.addClass((state=="expand")? config.cssclass.expand : config.cssclass.collapse) //alternate btw "expand" and "collapse" CSS classes
		$targetHeader.removeClass((state=="expand")? config.cssclass.collapse : config.cssclass.expand)
	
	},
	
	
	
	init:function(config)
	{
			//$('#content_152').css('display','block');
		//alert('.'+config.contentclass+'{display: none}\n');
		document.write('<style type="text/css">\n')
		document.write('.'+config.contentclass+'{display: none}\n') //generate CSS to hide contents
		document.write('.showcontent{display: block}\n') //generate CSS to hide contents
		document.write('a.hiddenajaxlink{display: none}\n') //CSS class to hide ajax link
		document.write('<\/style>')
		jQuery(document).ready(function($)
		
		{
			  

			ddaccordion.headergroup[config.headerclass]=$('.'+config.headerclass) //remember header group for this accordion
			ddaccordion.contentgroup[config.headerclass]=$('.'+config.contentclass) //remember content group for this accordion
			var $headers=ddaccordion.headergroup[config.headerclass]
			var $subcontents=ddaccordion.contentgroup[config.headerclass]
			config.cssclass={collapse: config.toggleclass[0], expand: config.toggleclass[1]} //store expand and contract CSS classes as object properties
			config.revealtype=config.revealtype || "click"
			config.revealtype=config.revealtype.replace(/mouseover/i, "mouseenter")
		
			if (typeof config.togglehtml=="undefined")
				config.htmlsetting={location: "none"}
			else
				config.htmlsetting={location: config.togglehtml[0], collapse: config.togglehtml[1], expand: config.togglehtml[2]} //store HTML settings as object properties
				config.oninit=(typeof config.oninit=="undefined")? function(){} : config.oninit //attach custom "oninit" event handler
				config.onopenclose=(typeof config.onopenclose=="undefined")? function(){} : config.onopenclose //attach custom "onopenclose" event handler
				var lastexpanded={} //object to hold reference to last expanded header and content (jquery objects)

		
			var expandedindices=config.defaultexpanded; 
			
			if (typeof expandedindices=='string') //test for string value (exception is config.defaultexpanded, which is an array)
				expandedindices=expandedindices.split(','); 
			if (expandedindices.length==1 && expandedindices[0]=="-1") 
				expandedindices=[]

			if (config["collapseprev"] && expandedindices.length>1) //only allow one content open?
				expandedindices=[expandedindices.pop()] //return last array element as an array (for sake of jQuery.inArray())
			if (config["onemustopen"] && expandedindices.length==0) //if at least one content should be open at all times and none are, open 1st header
				expandedindices=[0];

			$headers.each(function(index)  //loop through all headers
			{
				var $header=$(this)
				//if ( ($header.attr('className')=='subexpandable') && ($header.attr('id')=='menu_152_19') ) alert('H='+$header.attr('id'));
				
				
				$header.attr('headerindex', index+'h') //store position of this header relative to its peers

				//alert($subcontents.eq);
				$subcontents.eq(index).attr('contentindex', index+'c') //store position of this content relative to its peers
				var $subcontent=$subcontents.eq(index);
				//alert($subcontent);
								

// NEEDLE CHANGED			

				var needle=$(this).attr('id');
				//alert('needle='+needle);
			
				if(jQuery.inArray(needle, expandedindices)==-1) //check for headers that should be expanded automatically (convert index to string first)
				{
					$subcontent.hide()
					config.onopenclose($header.get(0), parseInt($header.attr('headerindex')), $subcontent.css('display'), false) //Last Boolean value sets 'isuseractivated' parameter
					ddaccordion.transformHeader($header, config, "collapse")
				}

				else   
				{ 
					//alert('needle='+needle+' | id='+$(this).attr('id')+' | class='+$(this).attr('className'));
					ddaccordion.expandit($header, $subcontent, config, false, false, !config.animatedefault) //3rd last param sets 'isuseractivated' parameter, 2nd last sets isdirectclick, last sets skipanimation param
					lastexpanded={$header:$header, $content:$subcontent}
				}  




			
			})
			
			$headers.bind("evt_accordion", function(e, isdirectclick) //assign CUSTOM event handler that expands/ contacts a header
			{ 
				
				
				var $subcontent=$subcontents.eq(parseInt($(this).attr('headerindex'))) //get subcontent that should be expanded/collapsed
				var cur_item=$(this).attr('id');
				var cur_content_id=cur_item.replace('menu_', "content_");	
				//alert('bind='+$(this).attr('id')+' | sub='+$subcontent.attr('id')+' | repl='+cur_content_id);
				if(cur_content_id!='')	
				$subcontent=$('#'+cur_content_id+'');

				if ($subcontent.css('display')=="none")
				{
					//alert('expand'+$(this).attr('className'));
					
					ddaccordion.expandit($(this), $subcontent, config, true, isdirectclick) //2nd last param sets 'isuseractivated' parameter
					if (config["collapseprev"] && lastexpanded.$header && $(this).get(0)!=lastexpanded.$header.get(0)){ //collapse previous content?
						ddaccordion.collapseit(lastexpanded.$header, lastexpanded.$content, config, true) //Last Boolean value sets 'isuseractivated' parameter
					}
					lastexpanded={$header:$(this), $content:$subcontent}
					
				}
				else if (!config["onemustopen"] || config["onemustopen"] && lastexpanded.$header && $(this).get(0)!=lastexpanded.$header.get(0))
				{

					//alert('collapse');
					var targetLink=($targetHeader.is("a"))? $targetHeader.get(0) : $targetHeader.find('a:eq(0)').get(0)
					if (targetLink) //if this header is a link
					ddaccordion.collapseit($(this), $subcontent, config, true) //Last Boolean value sets 'isuseractivated' parameter
				}
 			})


			$headers.bind(config.revealtype, function()
			{
				$(this).trigger("evt_accordion", [true]) //last parameter indicates this is a direct click on the header
				return false; //cancel default click behavior
			})

			$headers.bind("mouseleave", function()
			{
				clearTimeout(config.revealdelay)
			})
		
		})
	} // init EOF
}


