var Accordion = Fx.Elements.extend({

	options: {
		onActive: Class.empty,
		onBackground: Class.empty,
		display: 1,
		show: true,
		height: true,
		width: false,
		opacity: true,
		fixedHeight: false,
		fixedWidth: false,
		wait: false,
		alwaysHide: false
	},

	initialize: function(){
		var options, togglers, elements, container;
		$each(arguments, function(argument, i){
			switch($type(argument)){
				case 'object': options = argument; break;
				case 'element': container = $(argument); break;
				default:
					var temp = $$(argument);
					if (!togglers) togglers = temp;
					else elements = temp;
			}
		});
		this.togglers = togglers || [];
		this.elements = elements || [];
		this.container = $(container);
		this.setOptions(options);
		this.previous = -1;
		if (this.options.alwaysHide) this.options.wait = true;
		if ($chk(this.options.show)){
			this.options.display = false;
			this.previous = this.options.show;
		}
		if (this.options.start){
			this.options.display = false;
			this.options.show = false;
		}
		this.effects = {};
		if (this.options.opacity) this.effects.opacity = 'fullOpacity';
		if (this.options.width) this.effects.width = this.options.fixedWidth ? 'fullWidth' : 'offsetWidth';
		if (this.options.height) this.effects.height = this.options.fixedHeight ? 'fullHeight' : 'scrollHeight';
						
		for (var i = 0, l = this.togglers.length; i < l; i++)
		{
			this.addSection(this.togglers[i], this.elements[i]);
			this.elements[i]._idx = i;

		}
		this.elements.each(function(el, i){
			if (this.togglers[i].className.indexOf("active") != -1){
				this.fireEvent('onActive', [this.togglers[i], el]);
			} else {
				for (var fx in this.effects) el.setStyle(fx, 0);
			}
		}, this);
		this.parent(this.elements);
		if ($chk(this.options.display)) this.display(this.options.display);
	},

	addSection: function(toggler, element, pos){
		toggler = $(toggler);
		element = $(element);
		var test = this.togglers.contains(toggler);
		var len = this.togglers.length;
		this.togglers.include(toggler);
		this.elements.include(element);
		if (len && (!test || pos)){
			pos = $pick(pos, len - 1);
			toggler.injectBefore(this.togglers[pos]);
			element.injectAfter(toggler);
		} else if (this.container && !test){
			toggler.inject(this.container);
			element.inject(this.container);
		}
		var idx = this.togglers.indexOf(toggler);
		toggler.href="javascript:;";
		toggler.addEvent('click', this.display.bind(this, idx));
		if (this.options.height) element.setStyles({'padding-top': 0, 'border-top': 'none', 'padding-bottom': 0, 'border-bottom': 'none'});
		if (this.options.width) element.setStyles({'padding-left': 0, 'border-left': 'none', 'padding-right': 0, 'border-right': 'none'});
		element.fullOpacity = 1;
		if (this.options.fixedWidth) element.fullWidth = this.options.fixedWidth;

		if (this.options.fixedHeight) element.fullHeight = this.options.fixedHeight;
		element.setStyle('overflow', 'hidden');
		if (!test){
			for (var fx in this.effects) element.setStyle(fx, 0);
		}
		return this;
	},

	display: function(index){
		index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
		if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
		this.previous = index;
		var obj = {};
		this.elements.each(function(el, i){
			obj[i] = {};
			var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
			this.fireEvent(hide ? 'onBackground' : 'onActive', [this.togglers[i], el]);
			for (var fx in this.effects) obj[i][fx] = hide ? (0) : el[this.effects[fx]];
		}, this);
		return this.start(obj);
	},

	showThisHideOpen: function(index){return this.display(index);}

});

Fx.Accordion = Accordion;

var _as = [];
var _divs = [];

function initPage()
{
	var accordion_block = document.getElementById('accordion');
	if(accordion_block)
	{
		var accordion = new Fx.Accordion('a.opener', 'div.ablock', {
			opacity: false,
			height:true,
			width: false,
			show: -1,
			display:-1,
			onActive: function(toggler, element){
			},		 
			onBackground: function(toggler, element){
			}
		}, accordion_block);

		if(accordion_block.getAttribute('rel') != "")
		{
			accordion.showThisHideOpen(accordion_block.getAttribute('rel'));
		}
		var __as = $$('#accordion a');
		for(var i=0; i < __as.length; i++)
		{
			if(__as[i].className == "" || __as[i].className.indexOf("active")!= -1 || __as[i].className.indexOf("no-ajax-reload") != -1)
			{
				var _id = __as[i].innerHTML.replace(/[ ]/g,"-").toLowerCase();
				if(__as[i].id == "")
				{
					__as[i].id = _id;
				}
				
				if (activephotoflow==1 || activesociete==1)
				{
					__as[i].href = 'javascript:;';
					__as[i].onclick = function()
					{
						clearAll(_as);
						if(this.className.indexOf("active") == -1)
							this.className += " active";
						accordion.showThisHideOpen(this.parentNode.parentNode.parentNode._idx);
						if(this.className.indexOf("no-ajax-reload") != -1)
						{
							var _id = document.getElementById('relation-links');
							if(_id)
							{
								hightlight(this.id);
								gotoVignette(this.id);
							}
							else
							{
								if (activephotoflow==1)
								{
									getPage('relation-links','initRelationLinks(); hightlight("'+this.id+'")');
									gotoVignette(this.id);
								}
							}
						}
						else
						{
							if (activephotoflow==1)
							{
								getPage(this.id);
								gotoVignette(this.id);
							}
							if (activesociete==1)
							{
								getHttpRequest_Generique('update-liste-ste-ajax',_url + this.id + '.php', 'fl='+langue);
								getHttpRequest_Generique('update-descr-ste-ajax',_url + 'desc-' + this.id + '.php', 'fl='+langue);
							}						
						}					
					}
				}
				__as[i].onclickFlow = function()
				{
					clearAll(_as);
					if(this.className.indexOf("active") == -1)
						this.className += " active";
					accordion.showThisHideOpen(this.parentNode.parentNode.parentNode._idx);
					if(this.className.indexOf("no-ajax-reload") != -1)
					{
						var _id = document.getElementById('relation-links');
						if(_id)
						{
							hightlight(this.id);
						}
						else
						{
							getPage('relation-links','initRelationLinks(); hightlight("'+this.id+'")');
							allowSlide('false')
						}
					}
					else
					{
						getPage(this.id);
						allowSlide('false');
					}					
				}
				_as[_as.length] = __as[i];
			}
		}
		initRelationLinks();
		initSocieteLinks();
	}
}


function initRelationLinks()
{
		_divs = $$('#relation-links div.col div');
		for(var i=0; i < _divs.length; i++)
		{
			_divs[i].onclick = function()
			{
				var _id = this.id.replace("-relation","");
				
				gotoVignette(_id);
				
				var _asi = document.getElementById(_id);
				if(_asi)
				{
					_asi.onclick();

				}
				clearAll(_divs);
				if(this.className.indexOf("active") == -1)
					this.className += " active";
			}
		}
}

function initSocieteLinks()
{
		var __as = $$('#societe a');
		for(var i=0; i < __as.length; i++)
		{
			if(__as[i].id != "")
			{
				
				__as[i].href = 'javascript:;';

				__as[i].onclick = function()
				{
					getHttpRequest_Generique('update-descr-ste-ajax',_url + 'descste-' + this.id + '.php', 'fl='+langue);
					clearAll(__as);
					this.className += " active";
				}
			}
		}
}

function clearAll(arr)
{
	clear="";
	for(var i=0; i < arr.length; i++)
	{
		clear+=arr[i].className+"-";
		arr[i].className = arr[i].className.replace("active"," ");
		
	}
	//alert (clear);
}

function clickOn(_id_)
{
	var obj = document.getElementById(_id_);
	if(obj)
	{
		obj.onclickFlow();
	}	
}

function hightlight(_id_)
{
	clearAll(_divs);
	var _id = _id_ + "-relation";
	var _asi = document.getElementById(_id);
	if(_asi)
	{
		if(_asi.className.indexOf("active") == -1)
			_asi.className += " active";
	}	
}

function hightlight_load(_id_)
{
	clearAll(_divs);
	var _id = _id_;
	var _asi = document.getElementById(_id);
	if(_asi)
	{
		if(_asi.className.indexOf("active") == -1)
			_asi.className += " active";
	}	
}



if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);