
/****** MainMenu *******/
var expand_height = 100;// top position wenn element offen ist
var fold_height = 20;// wenn element zu ist

var subboxes; //alle Hintergründe die eingeblendet werden
var fx;

function initMenu (){
	
	subboxes = $$('#mainmenu .menu li div').filter(function (li_elem){
		if (!li_elem.hasClass('tx-rlmpflashdetection-pi1'))
	 		return true;
	 	else
	 		return false;
		}); 
	//Listen Element die Menüpunkte sind! ... der filter verhindert, dass auch auch falsche listenElemente verwendet werden
	var li_elements = $$('#mainmenu .menu li').filter(function (li_elem){
		if (li_elem.getElement('ul') && !li_elem.hasClass('tx-rlmpflashdetection-pi1'))
			return true;
		else
			return false;
	});	
	
	//Effekt zum einblenden generieren
	fx = new Fx.Elements(subboxes, {wait: false, duration: 700, transition: Fx.Transitions.Back.easeOut});
	li_elements.each(function(li_elem, i) {
		li_elem.addEvent("mouseenter", function(event) {
			//alert ('mouseenter id: '+i);
			var o = {};
			var subbox = li_elem.getElement('div');
			if (subbox){
				o[i] = {'top': [subbox.getStyle("top").toInt(), expand_height]}
			}
			subboxes.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("top").toInt();
					if(w != fold_height) o[j] = {'top': [w, fold_height]};
				}
			});
			fx.start(o);
			
		});	
	
	});
	 
	$('mainmenu').addEvent("mouseleave", function(event) {
		//alert('mousleave tabmenu');
		var o = {};
		subboxes.each(function(subbox, i) {
			o[i] = {'top': [subbox.getStyle("top").toInt(), fold_height]}
		});
		fx.start(o);
	});
}

/****** Slider ******/
var mySlider = new Class({
 
 //var contentWrapper;
 //var numberOfElements
 
  initialize: function (wrapper, noofelements){
  	if (!noofelements)
  		var noofelements = wrapper.getChildren().length;
  
    this.contentWrapper = wrapper;
    this.numberOfElements = noofelements;
    this.currentPos = 0;
    this.tabWidth = 192; // der Contentbereich ist 866px breit
    
    
    //ScrollFX
    this.Fx = new Fx.Scroll(this.contentWrapper, {
    	duration: 400,
    	transition: Fx.Transitions.Expo.easeInOut
 	});
	
	this.prevLink = new Element("a", {id: "kPrevLink", href: "#"}).addEvent("click", this.previous).inject($('container'));
    this.nextLink = new Element("a", {id: "kNextLink", href: "#"}).addEvent("click", this.next).inject($('container'));
	
	this.linkFx = {
			prevLink: new Fx.Tween(this.prevLink, {property: "opacity", duration: 1000, transition: Fx.Transitions.Expo.easeInOut}).set(0),
			nextLink: new Fx.Tween(this.nextLink, {property: "opacity", duration: 1000, transition: Fx.Transitions.Expo.easeInOut})
		};
	
	//nextLink ausblenden wenn nicht genug element sind:
	if (this.numberOfElements < 5)
		this.linkFx.nextLink.set(0);
		
	var currentElem = this.getCurrentElem();
	if (currentElem > 3) this.slideTo(currentElem);
	
  },
  
  slideTo: function(index){
  	if (index != this.currentPos){//wenn nicht aktuelles Element
  		/**** berprfen der Ober und Untergrenzen ***/
  		if (index>=this.numberOfElements-5){
  			index = this.numberOfElements-5;
			this.hideNextButton();
  		}
  		else if (index<0){
  			index = 0;
			this.hidePrevButton();
  		}
  		
  		//Sliden
   		this.Fx.start(index*this.tabWidth + ((index != 0)?(this.tabWidth/2):0), this.contentWrapper.getScroll().y);
  		//this.contentWrapper.scrollTo(index*this.tabWidth, 0);
  		
  		this.currentPos = index;
  		}
  }, 
  
  
  slideSteps: function (number){
  	if (!number)
  		var number = 1;
  		
  	var nextPos = this.currentPos + number;
  	
  	this.slideTo(nextPos);
  	
  	//
  	
  
  },
  
  previous: function (){
	  menuSlider.slideSteps (-4);
	  
	  return false; //to prevent the link-action
  },
  
  next: function (){
	  menuSlider.slideSteps(4);
	  
	  return false; //to prevent the link-action
  },
  
  hideNextButton: function (){
	  //new Fx(this.nextLink, { duration: 400, transition: Fx.Transitions.Expo.easeInOut }).start({ 'opacity': 0});
	  this.linkFx.nextLink.start("opacity", 0, 1); 
	  this.linkFx.prevLink.start("opacity", 1, 0); 
  },
  
  hidePrevButton: function (){
	  this.linkFx.nextLink.start("opacity", 1, 0); 
	  this.linkFx.prevLink.start("opacity", 0, 1);
  },
  
  getCurrentElem: function (){
  	var li_elements = $$('#mainmenu ul.menu')[0].getChildren();
  	var current_index = -1;
  
  	li_elements.each(function(li_elem, i) {
  		if (li_elem.hasClass('current')){
   			current_index = i;
  		}
  	});
  	
  	return current_index;
  
  }
});


var menuSlider;

/***** Dom ready ****/
window.addEvent('domready', function() {
	//initMenu ();
	initMenu ();
	
	menuSlider = new mySlider ($('mainmenu'),$$('#mainmenu ul.menu')[0].getChildren().length);
	 
});

