//returns true if an accordion could be found inside the #content
function hasAccordion() {
	if(!$("content"))
		return false;
	return Element.hasClassName($("content"), "accordion");
}

//returns the accordion-div, null if no accordion was found
function getAccordion() {
	if(hasAccordion())
		return $("content");
	return null;
}

//returns an array with all accordion tabs in it
function getTabs() {
	var accordion=getAccordion();
	return accordion.select('.accordion_tab');
}

//returns the max-hegight of a single accordion-content
function getMaxAllowedTabContentSize() {
	var tabs = getTabs();
	var countTabs = tabs.length;
	var size=calculateMaxContentSizeStatic();
	size=size-((countTabs-1)*20);
	return size;
}

/*
returns the max height of the content. this is hard coded and - if changed has to be applied to scrollFloatingDiv as well
*/
function calculateMaxContentSizeStatic() {
	return $("content").clientHeight;
}

//returns the tab element of a specific index
function getTab(tabNumber) {
	return getTabs()[tabNumber];
}

//returns the tab number of a specific tab element
function getTabNumber(tabRef) {
	var tabs = getTabs();
	var i=0;
	for(i=0;i<tabs.length;i++)
	{
		if(tabs[i]==tabRef)
			return i;
	}
	return -1;
}

//returns the link (title a) for a tab
function getLinkForTab(tabRef) {
	return tabRef.down(".accordion_title");
}

//returns the tab that is currently open
function getOpenTab() {
	var tabs = getTabs();
	var i=0;
	for(i=0;i<tabs.length;i++)
	{
		if(tabs[i].clientHeight>21)
			return tabs[i];
	}
	return null;
}

//initializes the accordion, opens whichever accordion is specified in the anchor, the first one otherwise
function initAccordion() {
	var initial = 0+getAnchorTabIndex();
	var tab1 = getTab(initial);
	tab1.style.height=""+getMaxAllowedTabContentSize()+"px";
	if(document.recalc)
		document.recalc();
	finishedAccordion(tab1);
	
	var tabs = getTabs();
	var i=0;
	for(i=0;i<tabs.length;i++)
	{
		var tabLink=getLinkForTab(tabs[i]);
		tabLink.href="#"+i;
		tabLink.onclick=new Function("activateTab(getTab("+i+"));return true;");
		
		//Init content navigation
		initContentNavigationForTab(tabs[i]);
	}
}

//returns a specific accordion tab's content
function getAccordionContent(tabRef) {
	if (tabRef === null) {
    return null;
	}
	return possibleContents=$(tabRef).down(".content");
}

//handles the accordion aspect of a browser window resize, called from handleResize
function resizeAccordion() {
	getOpenTab().style.height=""+getMaxAllowedTabContentSize()+"px";
	getAccordionContent(getOpenTab()).style.height=""+getMaxAllowedTabContentSize()-50+"px";
}

//called when the accordion has been finished, changes the title color and handles scrolling, also highlights submenu items
function finishedAccordion(tabRef) {
	getAccordionContent(tabRef).style.overflow="auto";
	getAccordionContent(tabRef).style.height=""+getMaxAllowedTabContentSize()-50+"px";
	Element.addClassName(getLinkForTab(tabRef), "on");
	getLinkForTab(tabRef).blur();
	
	scrollTop(tabRef);
	
	//Activate correct menu item
	var location = window.location.href;
	/*First: deactivate all selections in the submenu items*/
	iterateChildren(header, "div", function(subMenu){iterateChildren(subMenu, "a", function(menuItem){Element.removeClassName(menuItem, "on");});});
	/*
	Second: look for matching items and activate
	*/
	iterateChildren(header, "a", function(menuItem)
	{
		subMenuItemLink = menuItem.href;
		if(subMenuItemLink.indexOf("#")!=-1)
			subMenuItemLink=subMenuItemLink.substring(0, subMenuItemLink.indexOf("#"));
		if((location.indexOf(subMenuItemLink)!=-1)&&(getAnchorTabIndex()==getAnchorTabIndex(menuItem.href)))
			Element.addClassName(menuItem, "on");
	});
}

//activates one specific tab programmatically, event handler of the tabs
function activateTab(tabRef) {
	openContentNavigationItem(0, tabRef);
	if(getOpenTab()==tabRef)
		return;
	getAccordionContent(getOpenTab()).style.overflow="hidden";
	Element.removeClassName(getLinkForTab(getOpenTab()), "on");
	new Effect.AccordionSize(getOpenTab(), tabRef, 20, getMaxAllowedTabContentSize(), 5, 10, {complete: function(){finishedAccordion(tabRef)}});
	exchangeBackground(tabRef);
}

function hasContentNavigation(tabRef) {
	return Element.hasClassName(getAccordionContent(tabRef), "content_navigation");
}

function initContentNavigationForTab(tabRef) {
	if(!hasContentNavigation(tabRef))
		return;
	
	var tabIndex = getTabNumber(tabRef);
	
	iterateChildren(getContentNavigation(tabRef), "a", function(link, counter)
	{
		link.href="#"+tabIndex+"_"+counter;
		
		link.onclick=new Function("openContentNavigationItem("+counter+");");
		
		if(Element.hasClassName(link, "invisible"))
			link.style.display="none";
	}, true);
	
	var cNavToOpen = getAnchorContentNavigationIndex();
	if(getAnchorTabIndex()==getTabNumber(tabRef))
		openContentNavigationItem(cNavToOpen, tabRef, false);
}

function openContentNavigationItem(itemIndex) {
	var tabRef = arguments[1] || getOpenTab();
	var shouldExchangeBackground = arguments[2] || true;
	
	if(!hasContentNavigation(tabRef))
		return;
	
	iterateChildren(getAccordionContent(tabRef), "div", function(contentItem, counter) {
		if(!Element.hasClassName(contentItem, "splitLayoutMiddleColumn"))
			return true;
		if(counter-1 == itemIndex)
			contentItem.style.display="block";
		else
			contentItem.style.display="none";
	}, true);
	
	iterateChildren(getContentNavigation(tabRef), "a", function(link, counter) {
		if(counter == itemIndex)
			Element.addClassName(link, "on");
		else
			Element.removeClassName(link, "on");
	}, true);
	
	if(shouldExchangeBackground)
		exchangeBackground();
		
	scrollTop(tabRef);
}

function getContentItem(tabRef, itemIndex) {
	return iterateChildren(getAccordionContent(tabRef), "div", function(contentItem, counter) {
		if(Element.hasClassName(contentItem, "splitLayoutMiddleColumn") && counter==itemIndex)
			return false;
	}, true);
}

function getContentNavigation(tabRef) {
	return iterateChildren(getAccordionContent(tabRef), "div", function(contentItem) {
		if(Element.hasClassName(contentItem, "splitLayoutLeftColumn"))
			return false;
	}, true);
}

/*
From Rico.js
*/
Effect.AccordionSize = Class.create({

   initialize: function(e1, e2, start, end, duration, steps, options) {
      this.e1       = $(e1);
      this.e2       = $(e2);
      this.start    = start;
      this.end      = end;
      this.duration = duration;
      this.steps    = steps;
      this.options  = arguments[6] || {};

      this.accordionSize();
   },

   accordionSize: function() {

      if (this.isFinished()) {
         // just in case there are round errors or such...
         this.e1.style.height = this.start + "px";
         this.e2.style.height = this.end + "px";

         if(this.options.complete)
            this.options.complete(this);
         return;
      }

      if (this.timer)
         clearTimeout(this.timer);

      var stepDuration = Math.round(this.duration/this.steps) ;

      var diff = this.steps > 0 ? (parseInt(this.e1.offsetHeight) - this.start)/this.steps : 0;
      this.resizeBy(diff);

      this.duration -= stepDuration;
      this.steps--;

      this.timer = setTimeout(this.accordionSize.bind(this), stepDuration);
   },

   isFinished: function() {
      return this.steps <= 0;
   },

   resizeBy: function(diff) {
      var h1Height = this.e1.offsetHeight;
      var h2Height = this.e2.offsetHeight;
      var intDiff = parseInt(diff);
      if ( diff != 0 ) {
    
	     this.e1.style.height = (h1Height - intDiff) + "px";
	     this.e2.style.height = (h2Height + intDiff) + "px";
      }
   }

});
