 /**
  * Akkordion-JavaScript.
  *
  * conf:
  *		type: 'vertical'|'horizontal'
  *   inputs: Array of input-elements
  *		panels: Array of elements to resize.
  *		additionals: Array of additional elements to resize (inner elements).
  *		active: # of active Element
  *		openWidth: width of the open panel
  *		closeWidth: width of the closed panel
  *		content: id/element that holds the content (if other than the panels)
	*		ajax: AJAX-Element to receive content
	*		initialContent: Initial content to display while loading ajax-request
  *		steps: Steps to take fading
  *		time: Time for one fade in ms
  *
  * @author Steffen Friedrich (steffen.friedrich@pingping.ag, www.pingping.ag)
  *
  * - Animation
  ***/
function Akkordion(conf)
{	new CoreObject( this );

	this.defineMethods=_Akkordion_defineMethods;
	this.defineMethods();

	if(!this.checkConfig(conf))
		return;

	for(var d in conf)
		this[d]=conf[d];

	this.sizing=0;
	this.nextSizer=0;

	if(!this.action)
		this.action = 'click';

	this.stepTime=Math.round(this.time/this.steps);
	this.stepSize=100/this.steps;

	this.wholeWidth = (this.panels.length-1)*this.closeWidth+this.openWidth;

	this.sizes=new Array();

	this.prepare();
}

function _Akkordion_defineMethods()
{	this.open=_Akkordion_open;
	this.checkConfig=_Akkordion_checkConfig;
	this.prepare=_Akkordion_prepare;
	this.prepareContent=_Akkordion_prepareContent;
	this.resize=_Akkordion_resize;
	this.sizeTo=_Akkordion_sizeTo;
	this.resizeTo=_Akkordion_resizeTo;
}

function _Akkordion_checkConfig(conf)
{	if(!conf.inputs && conf.panels)
		conf.inputs = conf.panels;
	return (conf.type=='vertical'||conf.type=='horizontal') && conf.inputs && conf.panels && conf.openWidth && conf.closeWidth;
}

function __Akkordion_click(ev)
{	if(this.akkordion.inputs[this.akkordion.active]!=this)
	{	for(var s=0;s<this.akkordion.inputs.length;s++)
			if(this.akkordion.inputs[s]==this)
			{	this.akkordion.open(s);
				break;
			}
	}

	return false;
}

function __Akkordion_receive(pack)
{	this.innerHTML = pack.request.responseText;
}

function _Akkordion_prepare()
{	for(var sklave=0;sklave<this.inputs.length;sklave++)
	{	this.inputs[sklave].akkordion=this;
		this.inputs[sklave]['on'+this.action]=__Akkordion_click;
		this.sizes[sklave]=this.closeWidth;
	}
	
	this.sizes[this.active]=this.openWidth;

	if(this.content)
	{	this.prepareContent(this.content);
	
		this.contents = new Array();
		this.contents[this.active] = this.content;
	}
	
	if(this.additionals)
		this.additionalsWidth = this.additionals[this.active].offsetWidth-1;
}

function _Akkordion_prepareContent(content)
{	content.receive = __Akkordion_receive;
	content.fade = new Fader({
			'element': content,
			'steps': this.steps,
			'time': this.time,
			'skipIE': true
		});
}

function _Akkordion_open(nr)
{	if(this.content)
	{	if(!this.contents[nr])
		{	this.contents[nr] = this.contents[this.active].cloneNode(false);
			this.contents[nr].innerHTML = this.initialContent;
			
			this.prepareContent(this.contents[nr]);

			this.ajax.request(this.contents[nr], this.inputs[nr].ajaxHref);
			// individual stuff for CD
			this.contents[nr].className = this.contents[nr].className.replace(/padding-[0-9]/,'padding-'+nr);
			this.contents[nr].style.display='none';
			this.content.parentNode.insertBefore(this.contents[nr], this.content);
		}
		
	//	this.content.fade.outChainIn(this.contents[nr].fade); // style.display='none';
		this.content.style.display='none';	
		this.content = this.contents[nr];
	}
	this.active=nr;
	this.resize();
}

function _Akkordion_resize()
{	if(this.sizing || this.sizes[this.active]==this.openWidth) return;
		else this.sizing=1;

	this.sizeTo(0);
//	this.panels[nr].style.width=this.openWidth+'px';
//	this.panels[this.active].style.width=this.closeWidth+'px';

}

function _Akkordion_sizeTo(perc)
{	if(Math.round(perc)<100)
	{	var actSize=this.closeWidth+Math.round(perc/100*((this.openWidth-this.closeWidth)));
		this.resizeTo(this.active,actSize);

	 var rest=this.openWidth-actSize,sum=0,diff=0,adiff=0,chg,sklave;

	 for(sklave=0;sklave<this.sizes.length;sklave++)
		 if(sklave!=this.active && this.sizes[sklave]>this.closeWidth)
		 		sum+=this.sizes[sklave]-this.closeWidth;

	 for(sklave=0;sklave<this.sizes.length;sklave++)
		 if(sklave!=this.active && this.sizes[sklave]>this.closeWidth)
		 	{	chg=this.sizes[sklave]-this.closeWidth;
		 		chg=rest*(chg/sum);
		 		//chg=(this.sizes[sklave]-this.closeWidth)*(100-perc)/100;
	 			this.resizeTo(sklave,Math.round(chg+this.closeWidth));
		 	}

//		alert(diff+' vs. '+adiff);

	 window.setTimeout(this.getInstanceName()+'.sizeTo('+(perc+this.stepSize+(perc/20*this.stepSize))+')', this.stepTime);
	}
	else
	{ this.resizeTo(this.active,this.openWidth);
	  for(var sklave=0;sklave<this.sizes.length;sklave++)
		 if(sklave!=this.active && this.sizes[sklave]!=this.closeWidth)
		 		this.resizeTo(sklave,this.closeWidth);

	  if(this.content)
		this.content.fade.fadeIn();

		this.sizing=0;
	}
}

function _Akkordion_resizeTo(nr, size)
{	this.sizes[nr]=size;
	this.panels[nr].style.width=size+'px';
	size+=this.additionalsWidth-this.openWidth;
	if(size<0) size=0;
	if(this.additionals)
		this.additionals[nr].style.width=size+'px';	
}

