﻿

AccordeonMenu.prototype.$= function (destination_id)
{
	return document.getElementById(destination_id);
}

/*
	Klasse
	Aufruf:
	
	var obj_acc = new AccordeonMenu('Container Div ID',Slide_Time);
*/
function AccordeonMenu(base_container,classname_menus,slide_time,action_mode,array_menu,classname_submenus)
{
	this.openedIndex = ""; // speichert die ID des geöffneten Menüpunkts
	this.slide_time =slide_time; // speihcert die geschwindidkeit der slides in ms
	this.array_menu=array_menu;
	
	// alle Menus mit onclick Event versehen

	if (this.$(base_container))
	{
			var temp_obj=this;
			var menus=this.$(base_container).getElementsByTagName('a');
			
				for (var i=0;i<menus.length;i++)
				{
					
					// Content pro Punkt ausbeldnen und höhe ermitteln function hinzufeügen 
					if (action_mode=="onclick")
					{
						 var array_classnames_menu=menus[i].className.split(" "); // mehrere Classnames getrennt
						 
						  if (array_classnames_menu.in_array(classname_menus))
						  {
							// Hauptmenuüunkt gefunden
							// submenupunkte dynamisch anhängen
							for (var key in array_menu)
							{
								 if (key.indexOf(menus[i].id) != -1 && key.indexOf("_") != -1)   
								 {
									// Submenupunkt gefunden
									var newdiv=document.createElement("span");
									newdiv.className=classname_submenus;
									var newtext=document.createTextNode(array_menu[key][0]);
									var newlink=document.createElement("a");
									newlink.href=array_menu[key][1];
									newlink.appendChild(newtext);
									newdiv.appendChild(newlink); //append link to new div
									this.$(menus[i].id+'Content').appendChild(newdiv);
								 }
							}
							
							menus[i].onclick=function(){temp_obj.openmenu(this.id);return false;}
							this.$(menus[i].id+'Content').meovis_content_height=this.$(menus[i].id+'Content').offsetHeight;
							this.$(menus[i].id+'Content').style.display="none";
							
						  }	
					}
					else
					{
					
							var array_classnames_menu=menus[i].className.split(" "); // mehrere Classnames getrennt
							
						  if (array_classnames_menu.in_array(classname_menus))	  
						  {
						  
						  // Hauptmenuüunkt gefunden
							// submenupunkte dynamisch anhängen
							for (var key in array_menu)
							{
								 if (key.indexOf(menus[i].id) != -1 && key.indexOf("_") != -1)   
								 {
									// Submenupunkt gefunden
									var newdiv=document.createElement("span");
									newdiv.className=classname_submenus;
									var newtext=document.createTextNode(array_menu[key][0]);
									var newlink=document.createElement("a");
									newlink.href=array_menu[key][1];
									newlink.appendChild(newtext);
									newdiv.appendChild(newlink); //append link to new div
									this.$(menus[i].id+'Content').appendChild(newdiv);
								 }
							}
						  
							menus[i].onmouseover=function(){temp_obj.openmenu(this.id);return false;}
							this.$(menus[i].id+'Content').meovis_content_height=this.$(menus[i].id+'Content').offsetHeight;
							this.$(menus[i].id+'Content').style.display="none";
						   }	
					}
				}
			
	}
}


AccordeonMenu.prototype.openmenu = function(menu_id)
{
	var content_id=menu_id+"Content";
	var open_new_menu_id=content_id;
	if (this.openedIndex==content_id)
	{
		open_new_menu_id="";
	}
	var slt=this.slide_time;
	var open_id=this.openedIndex;
	var temp_obj=this;
	setTimeout(function () {temp_obj.animation(new Date().getTime() , slt , open_id ,  open_new_menu_id );}, 33);
    this.openedIndex=open_new_menu_id;
}

AccordeonMenu.prototype.animation = function(letzter_start, slidetime_now, closingId, openingId)
{  
	
  var aktuelle_zeit = new Date().getTime();
  var sec_diff = aktuelle_zeit - letzter_start;

  var opening = (openingId == '') ? null : this.$(openingId);
  var closing = (closingId == '') ? null : this.$(closingId);
  
  var content_height_closing=0;
  var content_height_opening=0;
  
  if (opening!=null){ var content_height_opening=opening.meovis_content_height;}
  if (closing!=null){ var content_height_closing=closing.meovis_content_height;}
  
  if(slidetime_now <= sec_diff)
  {
	
 	if(opening != null) 
	{
		 opening.style.height = content_height_opening + 'px';
	}
	
	
    if(closing != null)
    {
      closing.style.display = 'none';
      closing.style.height = '0px';
    }
	
    return;
	
  }
 
  slidetime_now -= sec_diff;

  var newClosedHeight = Math.round((slidetime_now/this.slide_time) * content_height_closing);
 
  
  if(opening != null)
  {
    if(opening.style.display != 'block')
	{
      opening.style.display = 'block';
	 } 
	 
	 var set_height=(opening.meovis_content_height - newClosedHeight);
	 if (set_height<0){set_height=0;}
    opening.style.height = (set_height) + 'px';
  }
  
  if(closing != null)
  {
		closing.style.height = newClosedHeight + 'px';
	}
	
	var temp_obj=this;
	setTimeout(function () {temp_obj.animation(aktuelle_zeit , slidetime_now , closingId ,  openingId );},33);
}

Array.prototype.in_array = function(suchwert) 
{
	for(var i=0; i < this.length; i++) 
		{
			if(this[i] === suchwert) return true;
		}
	return false;
}



