// JavaScript Document
var mainPath = 'fileadmin/templates/';
window.addEvent('domready', function()
{
	var menu = {
		init: function(){
			var that = this;
			if(this.c = $('menu')){
				this.c.removeClass('nojs');
				this.c = this.c.getFirst('div');
				this.ssmenusTogglers = this.c.getFirst('ul').getChildren('li');
				this.ssmenusTogglers.each(function(li,index)
				{
					li.addEvent('mouseenter', function()
					{
						this.addClass('over');
					});
					li.addEvent('mouseleave', function() 
					{
						this.removeClass('over'); 
					});
					if(li.getElement('ul')) 
					{
						that.splitUl(li.getElement('ul')); 
					}
				});
			}else {
				return false; 
			}
		},
		splitUl: function(ul)
		{
			var maxLength = 8; 
			var lis = ul.getElements('li'); 
			var cols = 0; 
			var nbNow = 0;
			var nbCols = Math.ceil(lis.length/maxLength);
			var newElms = [];
			var div = new Element('div', {'class':'sousmenu'}).inject(ul, 'after');
			if(nbCols>1){
				var width = 0;
				lis.each(function(el, ind)
				{
					newElms.push(el.dispose());
					nbNow++;
					if(nbNow==maxLength)
					{
						nbNow=0;
						var newul = new Element('ul').inject(div).adopt(newElms);
						//if(cols>0) newul.addClass('col'); 
						width = ul.getSize().x; 
						//alert(ul); 
						newElms = []; 
						cols++; 
					}
				});
				if(newElms.length>0)
				{
					var newul = new Element('ul').inject(div).adopt(newElms);
					newul.addClass('lastcol');
					//alert('test '+newElms);
					//alert(ul);
				}
				ul.destroy();
				div.setStyle('width', 172 * nbCols);
			}else {
				ul.addClass('lastcol');
				ul.inject(div);
			}
			if(!div) {
				div = ul;
			}
			var size = div.measure( function(){ return this.getSize(); }); 
			var pos = div.getParent().getCoordinates(div.getParent().getParent()).left + div.getParent().getParent().getCoordinates(div.getParent().getParent().getParent()).left + size.x;
			if(pos > 960)
			{
				div.setStyle('left', 960-pos);
				div.getParent().ssmenuPos = 960-pos;
			}
			//alert(size.x + ' - '+pos.x);
			/*
			if((pos.x + size.x) > 600 ) { 
				//alert(size.x +pos.x);
				ul.setStyle('margin-left', (600-(size.x +pos.x)));
			}
			*/
			
		}
	};
	
	/* ANIM MENU */ 
	var animMenu = {
		init:function(elms, container)
		{
			// BASE
			var that = this;
			this.elms = $$(elms);
			this.container = $(container);
			this.border = new Element('div', {'id':'menuBorder'}).inject(this.container);
			this.tw = new Fx.Morph(this.border, {duration: 'short', transition: Fx.Transitions.Sine.easeOut, wait:false}); 
			this.elms.each(function(el,index)
			{
				if(el.hasClass('active'))
				{
					that.active = el;
				}
				if(el.getElement('div'))
				{
					el.border = new Element('div',{'class':'ulborder'}).inject(el.getElement('div')).setStyle('width',el.getSize().x);
					if(el.ssmenuPos) el.border.setStyle('left',-el.ssmenuPos);
				}
			});
			if(!this.active) this.active = this.elms[0];	
			// EVENTS
			this.elms.addEvents(
			{
				'mouseenter': function()
				{
					that.move(this); 
				},
				'mouseleave':function()
				{
					that.move(that.active);
				}
			});
			
			this.move(this.active);
		}
		, move: function(elm)
		{
			var positions = this.getPositions(elm);
			this.tw.start(positions);
		},
		getPositions: function(elm)
		{
			var size = elm.getCoordinates(this.container);
			return { width:size.width, left:size.left };
		}
	};

	
	var actus = {
		init: function(){
			var that = this;
			if(this.c = $('actus'))
			{
				this.next = $('actus-next');
				this.prev = $('actus-prev');
				this.els = this.c.getElements('.actu');
				this.wrap = $('actus-wrapper');
				this.len = this.els.length;
				this.autoPlay = null;
				//actus-wrapper
				if(this.len>0){
					this.wrap.setStyle('width', this.els[0].getSize().x * this.len + 5);
					//alert(this.wrap.getSize().x);
					this.scroller = new fmcScrollTo({
						slides: this.els,
						container: this.c,
						duration:700
					});
					
					var stepLength = Math.ceil(this.c.getSize().x / this.els[0].getSize().x);
					var steps = Math.ceil(this.len / stepLength); 
					
					this.checkBTNS( this.scroller.startIndex, steps, stepLength );
					this.prev.addEvent('click',function(e)
					{
						if(e) e.stop();
						if(that.scroller.startIndex != 0 && (that.scroller.startIndex-stepLength)>=0)
						{
							that.scroller.scrollToEl(that.scroller.startIndex-stepLength);
						}else{
							that.scroller.scrollToEl(0);
						}
						that.checkBTNS(that.scroller.startIndex, steps, stepLength);
					});
					this.next.addEvent('click',function(e)
					{
						if(e) e.stop();
						if(that.scroller.startIndex < that.len-1 && Math.ceil((that.scroller.startIndex + stepLength) / stepLength) < steps)
						{
							//alert('t : ' + that.scroller.startIndex + ' - ' + (that.len-1) + ' - ' + (Math.ceil(that.scroller.startIndex + stepLength / stepLength) - 1) + ' <= ' + steps);
							that.scroller.scrollToEl(that.scroller.startIndex + stepLength);
						}else{
							//alert('Last : '+ that.scroller.startIndex +' - '+ (stepLength*steps-stepLength));
							//that.scroller.scrollToEl(stepLength*steps-stepLength);
							that.scroller.scrollToEl(0);
						}
						that.checkBTNS(that.scroller.startIndex, steps, stepLength);
					});
					this.autoPlay = this.autoChange.periodical(5000, this);
				}
				
			}else{
				return false;
			}
		}
		, autoChange: function()
		{
			this.next.fireEvent('click');
		}
		, checkBTNS:function (startIndex, steps, stepsLength)
		{
			var minus = false;
			var plus = false;
			//alert(Math.ceil((startIndex+stepsLength) / stepsLength));
			if(Math.ceil((startIndex+stepsLength) / stepsLength) == steps)
			{
				plus = false;
				this.next.addClass('disabled');
			}else{
				plus = true;
				this.next.removeClass('disabled');
			}
			if(startIndex == 0)
			{
				this.prev.addClass('disabled');
			}else{
				this.prev.removeClass('disabled');
			}
		}

	};
	if($('partenaires') != null){
		var partenaires = new Autoslider({ wrap:$('partenaires-wrapper'), c:$('partenaires'), itemsClass:'.partenaire', transitionTime:5000 });
	}
	if($('mecenes') != null){
		var mecenes = new Autoslider({wrap:$('mecenes-wrapper'), c:$('mecenes'), itemsClass:'.mecene', transitionTime:5900});
	}
	
	var alaune = {
		init:function()
		{
			var that = this;
			this.items = $$('.alaune-item');
			this.items.hide();
			this.menuItems = $$('#alauneMenu img');
			this.liItems = $$('#alauneMenu li');
			this.current = 0;
			
			this.menuItems.each(function(el, index)
			{
				if(that.menuItems[index].hasClass('active')) that.current = index;
				el.addEvent('click', function(e)
				{
					if(e) e.stop();
					if(index != that.current)
					{
						that.show(index);
					}
				});
			});
			this.show(this.current);
		}
		, show: function(index)
		{
			
			this.liItems[this.current].removeClass('active');
			this.items[this.current].hide();
			this.liItems[index].addClass('active');
			this.items[index].show();
			this.current = index;
		}
	};
	
	var animRegions = null;
	if(animRegions = $('regions-anim'))
	{
		var animRegionsSwf = new Swiff(mainPath+'flash/carte.swf', {'id':'animRegionsSwf', width:221, height:300});
		animRegionsSwf.inject(animRegions);
	}
	
	var animDepartements = null;
	if($('departements-anim'))
	{
		animDepartements = {
			cont:$('departements-anim'),
			link:null,
			init:function()
			{
				this.link = this.cont.getElement('a').get('href');
				this.cont.empty();
				this.anim = new Swiff(mainPath+'flash/carte-departements-AS3.swf', {'id':'animDeptsSwf', width:200, height:200, 'vars':{'xmlFile':this.link}, 'params':{'wmode':'transparent'}});
				this.anim.inject(this.cont);
			}
		};
		animDepartements.init();
		
	}
	
	
	menu.init();
	animMenu.init(menu.ssmenusTogglers, menu.c);
	if($('alauneMenu')) alaune.init();
	
	if($('pub1') && $$('#pub1 .banner-item').length > 1) {
		var slideshow = new SimpleSlideshow('pub1','#pub1 .banner-item');
  		slideshow.start();
	}
	
	if($('pubhome') && $$('#pubhome .banner-item').length > 1) {
		var slideshow = new SimpleSlideshow('pubhome','#pubhome .banner-item');
  		slideshow.start();
	}
	
	
});


