function init() {
	engagement();
	form();
}
/************************************************
 *  DOM ready : initialisation des fonctions
 ************************************************/
$j(function(){
    menuNavigation.init();
    // on vérifie qu'on est pas sur une page internationale
    if ($j('#affichage').hasClass('int')=== false){
        accesClient.init();
	}
    if($j('#acces_client')){
        accesClientHome.init();
    }

});

/************************************************
 *  Helper : fonctions génériques
 ************************************************/
var helper = {
    getHash:function(){
        if (window.location.hash)
	    {
            var reg = new RegExp("[a-z]*$");
            var hash = reg.exec(window.location.hash);
            return(hash);
	    }else{
            return false;
        }
    },
    setHash: function(hash){
        window.location.hash = hash;
    }
};

/************************************************
 *  Comportement page accès Client sur la HOME
 ************************************************/
var accesClientHome = {
    data:{
        structure:['urlAcces','lienMdpOublie'],
		insito:['https://insito.financeactive.com/logon.do','mailto:contact@financeactive.com'],
		fxent:['https://fxent.financeactive.com/logon.do','https://fxent.financeactive.com/public/mot_de_passe_oublie.jsp'],
		premio:['https://insito.financeactive.com/logon.do','mailto:contact@financeactive.com'],
		alliance:['https://alliance.financeactive.com/logon.do','mailto:contact@financeactive.com'],
		profolio:['https://profolio.financeactive.com/logon.do','mailto:contact@financeactive.com'],
		inviseo:['https://inviseo.financeactive.com/logon.do','mailto:contact@financeactive.com'],
		previsio:['https://inviseo.financeactive.com/logon.do','mailto:contact@financeactive.com']
    },
    init:function(){
        accesClientHome.$formulaireConteneur = $j('#formulaire_service');
        accesClientHome.$formulaire = $j('#formulaire_service form');
        accesClientHome.$formulaireNomService = $j('#formulaire_service #nom_service');
        accesClientHome.$boutons = $j('#bouton_service li');
        accesClientHome.$lienMdp = $j('#lienMotDePasseOublie');

        accesClientHome.$boutons.bind('click',function(event){
            event.preventDefault;
            accesClientHome.changeOnglet($j(this));
        });
        // on change d'onglet si on a un hash
        if (helper.getHash() === false){
        }else{
            var hash = helper.getHash();
            accesClientHome.$boutons.find('.'+hash).parent().trigger('click');
        }
    },
    changeOnglet:function($bouton){
        var nomService = $bouton.find('a').attr('class');
        // Focus the right Tab
        accesClientHome.$boutons.filter('.focus').removeClass('focus');
        $bouton.addClass('focus');
        // Change Form
        accesClientHome.$formulaireConteneur
                .removeClass(accesClientHome.$formulaireConteneur.attr('class'))
                .addClass(nomService);
        accesClientHome.$formulaireNomService.text(nomService);
        accesClientHome.$formulaire.attr("action", accesClientHome.data[nomService][0]);
        // donne un nom différent au formulaire en fonction du service pour permettre au navigateur d'enregistrer le mot de passe du service
        accesClientHome.$formulaire.attr("name", "logonForm_"+nomService);
        //Mot de passe oublié
        accesClientHome.$lienMdp.attr('href',accesClientHome.data[nomService][1]);
        // on change le hash
        helper.setHash(nomService);
    }
};


/************************************************
 *  Comportement page accès Client
 ************************************************/
var accesClient={
    init:function(){
        accesClient.$boutons=$j('#onglets li');
        accesClient.$formulaire=$j('.acces');

        accesClient.$boutons.data('permanent',false);
        accesClient.$boutons.each(function(index){
            $j(this).data('index',index)
        })
        // actions
        accesClient.$boutons.hover(
            function(){ accesClient.survol($j(this));},
            function(){ accesClient.survol($j(this));}
        );
        accesClient.$boutons.bind('click',function(event){
            event.preventDefault();
            accesClient.selectionBouton($j(this));
        });
    },
    survol:function($boutonSurvole){
        // survol actif si le bouton n'est pas selectionné
        if($boutonSurvole.data('permanent') === false){
            $boutonSurvole.toggleClass('out').toggleClass('in');
        }
    },
    selectionBouton : function($boutonSelectionne){
        accesClient.$boutons.filter('.in').removeClass('in').addClass('out').data('permanent',false);
        $boutonSelectionne.removeClass('out').addClass('in').data('permanent',true);
        accesClient.montreFormulaire($boutonSelectionne.data('index'));
    },
    montreFormulaire : function(index){
        accesClient.$formulaire.filter(':visible').hide();
        accesClient.$formulaire.eq(index).show();
    }
};

/************************************************
 *  Menu de navigation
 ************************************************/

var menuNavigation = {
    init : function(){
        menuNavigation.$boutons = $j('#menu dl');
        menuNavigation.$sousMenu = $j('#menu dd');

        menuNavigation.$boutons.each(function(){
           if ($j(this).find('dd').is('dd')){
               $j(this)
                       .bind('mouseenter',function(event){
                            event.preventDefault();
                            menuNavigation.ouverture($j(this));
                        })
                       .bind('mouseleave',function(event){
                            event.preventDefault();
                            menuNavigation.fermeture($j(this));
                        });
           }
        });
    },
    ouverture : function($bouton){
        $bouton.find('dd').fadeIn('medium');
    },
    fermeture : function($bouton){
        $bouton.find('dd').fadeOut('fast');
    }

};

/************************************************
 *  Autres fonctions : utilise prototype
 * TODO : virer ces prototypes fonctions !!
 ************************************************/

// callbacks & Mentions
function callback(){
	$('light').setStyle({'display':'block'});
	$('fade').setStyle({'display':'block'});
}
function nocallback(){
	$('light').setStyle({'display':'none'});
	$('fade').setStyle({'display':'none'});
}
function mentions(){
	$('light_mentions').setStyle({'display':'block'});
	$('fade_mentions').setStyle({'display':'block'});
}
function nomentions(){
	$('light_mentions').setStyle({'display':'none'});
	$('fade_mentions').setStyle({'display':'none'});
}

function accesDemo(){
	// verifie si on est sur une page 'acces securise' 
	if ($('cartouche').hasClassName('secure')){
		var urlAction;
		 $$('#affich0 form').each(function(s){
						urlAction =	s.readAttribute('action');
					});
		var expression = /^https\:\/\/([a-z]*)\.([a-z\.\/]*)$/;
		var result = expression.exec(urlAction);
		$$('#affich0 form').each(function(s){
						s.setAttribute('action','https://'+result[1]+'-demo.'+result[2]);
						s.submit();				  
					});
		//-demo
	} else {
		var form_actif;
		// on récupere le bon formulaire -> quel icône est actif
		var but_menu = $$('#onglets li');
		var elem_menu = $$('.acces');
		for (var i = 0; i < but_menu.length; i++){
			if (but_menu[i].permanent == true){
				form_actif = elem_menu[i].firstDescendant();
				var expression = /^([\:\/a-z]*)\.([a-z\.\/]*)$/;
				var result = expression.exec(form_actif.readAttribute('action'));
				form_actif.setAttribute('action',result[1]+'-demo.'+result[2]);
				form_actif.submit();
			}
		}
	}
}

function accesFormationEtDemo(url){
	// verifie que l'on est bien sur une page 'acces securise'
	if ($('cartouche').hasClassName('secure')){
		// remplace l'url de soumission
        $$('#affich0 form').each(function(s){
						s.setAttribute('action',"https://"+url);
						s.submit();
					});
		//-demo
	}
}

function engagement() {
	boutons = $$('#gauche.engagement dt');
	textes = $$('#gauche.engagement dd');
	for (var i = 0; i < textes.length; i++){
		textes[i].hide();
		boutons[i].lequel = i;
		boutons[i].observe('click', function(){montre(this.lequel);});
		textes[i].ouvert = false;
	}	
	function montre(lequel){
		if(textes[lequel].ouvert == false){
			boutons[lequel].addClassName('in');
			for (var i = 0; i < textes.length; i++){
				if(textes[i].ouvert == true){
					new Effect.SlideUp(textes[i],{duration:0.5});
/*					new Effect.toggle(textes[lequel],'blind');*/
					textes[i].ouvert = false;
					boutons[i].removeClassName('in');
				}
			}
			new Effect.SlideDown(textes[lequel],{duration:0.5});
/*			new Effect.toggle(textes[lequel],'blind');*/
			textes[lequel].ouvert = true;
		}
	}
}
// ajout aux favoris
function favoris() {
	if ( navigator.appName != 'Microsoft Internet Explorer' ){
		window.sidebar.addPanel("Finance active","https://www.financeactive.com",""); }
	else { window.external.AddFavorite("https://www.financeactive.com","Finance active");
	}
} 

// Input cachés CallBack & page Contact
function form() {
	
	var expression = /^([A-Z]*)\-([a-z]*)$/;
	var result = expression.exec(langue);
	var pays = result[1];
	var laLangue = result[2];
		
	lesServices = $$('#droite .rappel');
    lesSecteurs = $$('h3.secteur');
    toutes = $$('#footer div.rappel');
	
    // toutes les pages
	var leForm = $('contactInputs');
	var formulaire_langue = new Element ('input', {
		'id' : 'langue',
		'type': 'hidden',
		'name': 'langue',
		'value': laLangue
	 });
	var formulaire_pays = new Element ('input', {
		'id' : 'pays',
		'type': 'hidden',
		'name': 'pays',
		'value': pays
	 });
	leForm.insert(formulaire_langue);
	leForm.insert(formulaire_pays);
	
	// page Contact
	pageContact = $('contactB');
	if (pageContact){
		var contact_langue = new Element ('input', {
			'id' : 'langue',
			'type': 'hidden',
			'name': 'langue',
			'value': laLangue
		 });
		var contact_pays = new Element ('input', {
			'id' : 'pays',
			'type': 'hidden',
			'name': 'pays',
			'value': pays
		 });
		pageContact.insert(contact_langue);
		pageContact.insert(contact_pays);	
	} 
 
 	// pages secteurs && produits
	if (lesSecteurs[0] || lesServices[0]){
	   var spanSecteur = $$('div#cartouche div h3 span');
	   var quelSecteur = spanSecteur[0].innerHTML;
		bal = $$('#cartouche h3');
		leForm = $('contactInputs');
		var form_secteur = new Element ('input', {
			'id' : 'secteur',
			'type': 'hidden',
			'name': 'secteur',
			'value': quelSecteur
		 });
		leForm.insert(form_secteur);
	}
	
    // pages produits
	if (lesServices[0]){
		leService = $('affichage').readAttribute('class');
		leForm = $('contactInputs');	
		var formulaire_service = new Element ('input', {
			'id' : 'service',
			'type': 'hidden',
			'name': 'service',
			'value': leService
   		 });
		leForm.insert(formulaire_service);
	}

	
}
// changement de langue
function submitLang() {
	$('name').value = $('language').value;
	/*alert($('name').value);*/
	document.DynaLanguageForm.submit();
}

//ouverture de popup
function openPopUp(which)
{
	url=which + '.do'; //url of popup window
	id='offre'; //id of popup window
	w=540; //width of popup window
	h=400; //height of popup window
	popup=window.open(url,id,'toolbar=no,menubar=no,location=no,resizable=yes,status=no,scrollbars=yes,menubar=no,height='+h+',width='+w);
}

