var accordion;
var paramUrl = window.location.hash;

var browserName=navigator.appName;var browserVer=parseInt(navigator.appVersion);var version="";var msie4=(browserName=="Microsoft Internet Explorer"&&browserVer>=4);if((browserName=="Netscape"&&browserVer>=3)||msie4||browserName=="Konqueror"||browserName=="Opera"){version="n3";}else{version="n2";}
function blurLink(theObject){if(msie4){theObject.blur();}}


//DEBUG Affichage diaporama (ticket mantis 10309) sous IE	
function debugDiapo10309_intro(toggler, element) {
	toggler.removeClass('open');

	element.setStyle('display', 'none');

}

//DEBUG Caractere speciaux de la recherche
function HTMLentities(texte) {
	var result = "";
	for (var i = 0; i < texte.length; i++) {
		switch (texte.charCodeAt(i)) {
			//E
			case 232 : result += "&egrave;";break;
			case 233 : result += "&eacute;";break;
			case 234 : result += "&ecirc;";break;
			case 235 : result += "&euml;";break;
			//Tiret
			case 45 : result += "&nbsp;";break;
			//A
			case 224 : result += "&agrave;";break;
			case 226 : result += "&acirc;";break;
			//O
			case 244 : result += "&ocirc;";break;
			case 246 : result += "&ouml;";break;
			//U
			case 249 : result += "&ugrave;";break;
			//I
			case 238 : result += "&icirc;";break;
			case 239 : result += "&iuml;";break;
			//C
			case 231 : result += "&ccedil;";break;
			
			default : result += String.fromCharCode(texte.charCodeAt(i));
		}
	}
	return result;
}

window.addEvent('domready', function() {
	/**
		Newsletter
	 **/	
	window.addEvent("domready", function() {
		var newsletterName = document.getElementById("name");
		var newsletterEMail = document.getElementById("login");
		
		if (newsletterName != null && newsletterName.value == "") {
			newsletterName.value = 'Votre nom';
		}
		if (newsletterEMail != null && newsletterEMail.value == "") {
			newsletterEMail.value = 'Votre adresse email';
		}
	});
	
	/**
		Fil d'ariane : Pas top -> A ameliorer
	**/
	$$('.ariane a').each(function(o,i){
		if (i == 1) {
			if (o.href.indexOf("musee") != -1 || o.href.indexOf("jardins") != -1 || o.href.indexOf("albert-kahn") != -1 || o.href.indexOf("archives-de-la-planete") != -1 || o.href.indexOf("venir-au-musee") != -1) {
				o.setAttribute("onclick", "return false;");
				o.onclick = function() { return false; };
			}
		}
	});

	/**
		Display Top-bar
	**/
	$$('.display-top-bar').addEvent('click', function(e) {
		var el = $$(this.hash)[0];
		var other = $$('.top-bar');
		other = other.erase(el);
		var bg = $$('.bg');
		var bodinside = new Fx.Tween($$('body.inside')[0]);
		if (!el.hasClass('revealed')){
			if(other.hasClass('revealed')) {
				other.dissolve();
				other.removeClass('revealed');
			}
			el.reveal();
			bg.tween('top', '37px');
			bodinside.set('background-position', 'center 71px');
			el.addClass('revealed');
		}
		else {
			el.dissolve();
			bg.tween('top', '0');
			bodinside.set('background-position', 'center 34px');
			el.removeClass('revealed');
		}
		return false;
	});
	
	/**
		Fermer l'onglet
	**/
	$$('.close a').addEvent('click', function(e) {
		var el = $$('.top-bar');
		var bg = $$('.bg');
		var bodinside = new Fx.Tween($$('body.inside')[0]);
		el.dissolve();
		bg.tween('top', '0');
		bodinside.set('background-position', 'center 34px');
		el.removeClass('revealed');
		return false;
	});
	
	/**
		Afficher articles
	**/
	$$('.list-summary a').each(function(o,i){
		o.setProperty('index',i);
	});
	$$('.sk-pagecomments-counter a').each(function(o,i){
		o.setProperty('index',i);
	});
	$$('.list-summary a').addEvent('click',function(e){
		return false;
	});
	
	//liens internes
	$$('.lien_interne a').addEvent('click',function(e){
		return false;
	});
	
	
	
	contentArticles = Fx.Accordion;
	
	contentArticles.implement({
		$this: this,
		paginationLinks: $$('.sk-pagecomments-counter a'),
		paginationLinks2: $$('.lien_interne a'),
		categoryLinks: $$('.list-summary a'),
		pouet: function(o) {
			this.paginationLinks.each(function(o) { 
				o.addEvent('click',function(){
					this.categoryLinks[o.getProperty('index')].fireEvent('click');
					return false;
				}.bind(this));
			}.bind(this));
			this.paginationLinks2.each(function(o) { 
				o.addEvent('click',function(){
					this.categoryLinks[o.getProperty('index')].fireEvent('click');
					return false;
				}.bind(this));
			}.bind(this));
		},
		updateLink: function(){
			this.paginationLinks.each(function(o) { o.removeClass('open');});
			this.paginationLinks[this.current].addClass('open');
		}
	});
	
	
	
	accordion0 = new contentArticles($$('.list-summary a'), $$('.content-articles .inner-content-article'), {
		show: 0,
		opacity: false,
		returnHeightToAuto: false,
		initialDisplayFx: false,
		fixedHeight: false,
		onActive: function(toggler, element) {
			toggler.addClass('open');
			
			element.setStyle('display', 'block');
			
			if(this.current == null) {
				this.current = 0;	
			}

			this.current = toggler.getProperty('index');// Stockage de l'index actuel

			this.pouet(this.current);
		},
		onBackground: function(toggler, element) {
			toggler.removeClass('open');
			
			element.setStyle('display', 'none');

		},
		onComplete: function(toggler, element) {
			this.updateLink();	
		}
	});
	

	// DEBUG Affichage diaporama (ticket mantis 10309) sous IE	
	if(msie4){
		$$('.list-summary a').each(function(o,i){
			if (i>=1){
				debugDiapo10309_intro(o,$$('.content-articles .inner-content-article')[i]);
			}	
		});
			
		
	}
	
	
	if(paramUrl != ""){
		paramUrl=paramUrl.split("#");
		paramUrl=paramUrl[1];

		$$('.list-summary')[0].getElements('a.'+paramUrl)[0].fireEvent('click');
	}
	
	/**
		Dropdown "accès rapides" 
	**/
	$$(".dropdown").addEvent('mouseover', function(e) {
		$this = this.getFirst();
		$this.set('tween', {duration: 200});
		$this.tween('height', $this.getScrollSize().y);
	});
	$$(".dropdown").addEvent('mouseout', function(e) {
		$this = this.getFirst();
		$this.tween('height', $this.getFirst().getScrollSize().y);
	});

	/**
		Champs autoclear
	**/
	// Stockage de la valeur initale au chargement
	$$('.autoclear').each(function(o){
		o.store('initValue',o.getProperty('value'));
	});
	$$('.autoclear').addEvent('focus',function(){
		if (this.getProperty('value') == this.retrieve('initValue')) {
			this.setProperty('value','');
		}
	});
	$$('.autoclear').addEvent('blur',function(){
		if (this.getProperty('value') == '') {
			this.setProperty('value',this.retrieve('initValue'));
		}
	});
	
	/**
		Menu de navigation
	**/
    $$("#navigation-main li").addEvent('mouseover',function(e){
		this.addClass('over');
	});
	$$("#navigation-main li").addEvent('mouseout',function(e){
		this.removeClass('over');
	});
	
	/**
		Accordion
	**/
	$$('#headlines h2 a').each(function(o,i){
		o.innerHTML = o.innerHTML + '<span></span>';
	});
	$$('#headlines h2').each(function(o,i){
		o.setProperty('index',i);
	});
	$$('#headlines h2').addEvent('click',function(e){
		return false;
	});
	
	$$('#headlines input').each(function(o,i){
		var bgimg = document.getElementById("background-images");
		if (i == 0) {
			bgimg.innerHTML = '<div class="bg" id="bg' + i + '" style="background-image: url(' + o.value + '); z-index:-' + (i+1) +';"></div>';
		} else {
			bgimg.innerHTML += '<div class="bg" id="bg' + i + '" style="background-image: url(' + o.value + '); z-index:-' + (i+1) +';"></div>';
		}
	});
	$$(".bg").setStyle('opacity','0');
	accordion = new Fx.Accordion($$('#headlines h2'),$$('.headline-content'), {
		opacity: false,
		returnHeightToAuto: false,
		initialDisplayFx: false,
		fixedHeight: 350,
		onActive: function(toggler, element) { 
			toggler.addClass('open');
			if(this.current == null) {
				this.current = 0;	
			}
			$("bg"+this.current).fade('out');
			this.current = toggler.getProperty('index'); // Stockage de l'index actuel
			$("bg"+this.current).fade('in');
		},
		onBackground: function(toggler, element) { 
			toggler.removeClass('open');  
		}
	});
});
