var Kaimite_Accordeon = Class.create();

Kaimite_Accordeon.prototype =  {
	initialize:function (Pere, options) {
		this.options = Object.extend({
			vitesseOuvrir	: 1,
			vitesseFermer 	: 0.4,
			nomClass		: 	{
									clickDefaut		: "clickAccordeon",
									clickEnCours	: "clickAccordeonEnCours",
									voletEnCours	: "voletAccordeonEnCours"
								},
			voletOuvert		: "",
			ouvrirInit		: true,
			elementsClick	: $$("#" + Pere + " > dt"),
			elementsVolets	: $$("#" + Pere + " > dd")
			
		}, options || {});
		
		this.initAccordeon ();
	},
	
	initAccordeon: function () {
		this.options.elementsVolets.invoke("hide");
		
		if ( this.options.ouvrirInit == true ) {
			voletURL = this.ancreUrl(document.location);
			
			premierVolet = (voletURL != false) ? voletURL : this.options.voletOuvert;
			
			this.ouvrirPremierVolet(premierVolet);
		}
	},
	
	ouvrirPremierVolet: function (leVolet) {
		if ( $(leVolet) != undefined) {
			$(leVolet).show();
			$(leVolet).previous().firstDescendant().removeClassName(this.options.nomClass.clickDefaut);
			$(leVolet).previous().firstDescendant().addClassName(this.options.nomClass.clickEnCours);
			$(leVolet).addClassName(this.options.nomClass.voletEnCours);
			this.options.voletOuvert = leVolet;
		}
	},
	
	Afficher: function (urlVolet) {
		leVolet = this.ancreUrl(urlVolet);
		if ( $(leVolet) != undefined ) {
			if ( this.fermerVoletEnCours() != leVolet ) {
				this.ouvrirVolet(leVolet);
			}
			else {
				
				this.options.voletOuvert = "";
			}
		}
	},
	
	fermerVoletEnCours: function () {
		if ( $(this.options.voletOuvert) != undefined ) {
			var voletOuvert = this.options.voletOuvert;
			var onClickSauv = $(this.options.voletOuvert).previous().firstDescendant().getAttribute("onclick");
			$(this.options.voletOuvert).previous().firstDescendant().setAttribute("onclick", "");
			
			new Effect.BlindUp( this.options.voletOuvert, { duration: this.options.vitesseFermer, afterFinish: function () { $(voletOuvert).previous().firstDescendant().setAttribute("onclick", onClickSauv); } } );
			$(this.options.voletOuvert).previous().firstDescendant().removeClassName(this.options.nomClass.clickEnCours);
			$(this.options.voletOuvert).previous().firstDescendant().addClassName(this.options.nomClass.clickDefaut);
			$(this.options.voletOuvert).removeClassName(this.options.nomClass.voletEnCours);
			return this.options.voletOuvert;
		}
		else {
			return false;
		}
	},
	
	ouvrirVolet: function (leVolet) {
		if ( $(leVolet) != undefined) {
			var onClickSauv = $(leVolet).previous().firstDescendant().getAttribute("onclick");
			$(leVolet).previous().firstDescendant().setAttribute("onclick", "");
			
			new Effect.BlindDown( leVolet, { duration: this.options.vitesseOuvrir, afterFinish: function () { $(leVolet).previous().firstDescendant().setAttribute("onclick", onClickSauv); } } );
			$(leVolet).previous().firstDescendant().removeClassName(this.options.nomClass.clickDefaut);
			$(leVolet).previous().firstDescendant().addClassName(this.options.nomClass.clickEnCours);
			$(leVolet).addClassName(this.options.nomClass.voletEnCours);
			this.options.voletOuvert = leVolet;
		}
	},
	
	ancreUrl: function (chaineURL) {
		urlPage = new String(chaineURL);
		if ( urlPage.lastIndexOf('#') == -1 ) {
			return false;
		}
		else {
			return urlPage.substring(urlPage.lastIndexOf('#'), urlPage.length).sub('#', '');
		}
	}
};
