function init() {
	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 && $j('#cartouche').hasClass('secure')){
        accesClient.init();
        lienDemo.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[0]);
	    }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');

        // autoclick : trigger automatique quand on arrive sur la page : utilisé pour la gestion du message d'erreur
        accesClientHome.$boutons.bind('click',function(event, autoClick){
            event.preventDefault;
            accesClientHome.changeOnglet($j(this), autoClick);
        });
        // on change d'onglet si on a un hash
        var hash = helper.getHash();
        if (hash === false){
        }else{
            accesClientHome.$boutons.find('.'+hash).parent().trigger('click',['true']);
        }
    },
    changeOnglet:function($bouton, autoClick){
        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]);
        // Si il y a un message d'erreur on l'enleve
        if(!autoClick){
            accesClientHome.$formulaire.find('label.logonErreur').removeClass('logonErreur');
            accesClientHome.$formulaire.find('p.logonMessageErreur').remove();
        }
        // on change le hash
        helper.setHash(nomService);
    }
};


/************************************************
 *  Comportement page accès Client
 ************************************************/
var accesClient={
    'data' :{
        'insito':{
            'action':'https://insito.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'https://insito.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#insito',
                'premiereConnexion' : 'https://insito.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#insito',
                'demonstration': 'https://insito-demo.financeactive.com',
                'formation' : 'http://insito-formation.financeactive.com'
            }
        },
        'premio':{
            'action':'https://insito.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'https://insito.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#premio',
                'premiereConnexion' : 'https://insito.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#premio',
                'demonstration': 'https://insito-demo.financeactive.com',
                'formation' : ''
            }
        },
        'alliance':{
            'action':'https://alliance.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'https://alliance.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#alliance',
                'premiereConnexion' : 'https://alliance.financeactive.com/demandeMotDePasse.do?action=motDePasse&urlOrigine=https://www.financeactive.com/AccesClient.do#alliance',
                'demonstration': '',
                'formation' : ''
            }
        },
        'fxent':{
            'action':'https://fxent.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'https://fxent.financeactive.com/public/mot_de_passe_oublie.jsp',
                'premiereConnexion' : 'https://fxent.financeactive.com/public/premiere_connexion.jsp',
                'demonstration': '',
                'formation' : ''
            }
        },
        'inviseo':{
            'action':'https://inviseo.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'mailto:contact@financeactive.com',
                'premiereConnexion' : '',
                'demonstration': 'https://inviseo-demo.financeactive.com',
                'formation' : 'http://inviseo-formation.financeactive.com/'
            }
        },
        'previsio':{
            'action':'https://inviseo.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'mailto:contact@financeactive.com',
                'premiereConnexion' : '',
                'demonstration': 'https://inviseo-demo.financeactive.com/',
                'formation' : ''
            }
        },
        'profolio':{
            'action':'https://profolio.financeactive.com/logon.do',
            'lien':{
                'motDePasse':'mailto:contact@financeactive.com',
                'demonstration': '',
                'premiereConnexion' : '',
                'formation' : ''
            }
        }
    },
    'init':function(){
        accesClient.$boutons=$j('#onglets li');
        accesClient.$formulaire=$j('.acces');
        accesClient.$divForm=$j('#affich0');
        accesClient.$form=$j('#affich0 form');
        accesClient.$boutons.data('permanent',false);
        // actions
        accesClient.$boutons.hover(
            function(){ accesClient.survol($j(this));},
            function(){ accesClient.survol($j(this));}
        );
        accesClient.$boutons.bind('click',function(event, autoClick){
            event.stopPropagation();
            event.preventDefault();
            accesClient.selectionBouton(this,autoClick);
        });

        // on change d'onglet si on a un hash
        var hash = helper.getHash();
        if (hash === false){
        }else{
            accesClient.$boutons.children('a.'+hash).parent().trigger('click',[true]);
        }
    },
    '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, autoClick){
        var $bouton = $j(boutonSelectionne);
        var $ancienBouton = accesClient.$boutons.filter('.in');
        var ancienService = helper.getHash();

        $ancienBouton.removeClass('in').addClass('out').data('permanent',false);
        $bouton.removeClass('out').addClass('in').data('permanent',true);
        var service =  $bouton.children('a').attr('class');
        accesClient.updateFormulaire(service, ancienService);
        helper.setHash(service);
        if(!autoClick){
            $j('.logonMessageErreur').remove();
        }
    },
    'updateFormulaire' : function(nomService, nomAncienService){
        accesClient.$divForm.removeClass(nomAncienService).addClass(nomService);
        $j('#nomServiceAccesClient').text(nomService);
        accesClient.$form.attr({'action': accesClient.data[nomService].action});
        $j.each(accesClient.data[nomService].lien, function(index, value){
            $currentLink = $j('#'+index);
            if (value && value!='url'){
                $currentLink.show();
                if($currentLink.is('a')){
                    $currentLink.attr('href', value);
                }else{
                    $currentLink.find('a').attr('href', value);
                }
            }else{
                $currentLink.hide();
            }
        });
    }
};

var lienDemo ={
    'init' : function(){
        var $listeLienDemo = $j('a.lienDemonstration');
        var $formulaireConnection = $listeLienDemo.parents('div.acces').children('form');
        $listeLienDemo.bind('click',function(event){
             var $lienDemo = $j(this);
             // on vérifie si le formulaire est vide
             var $formulaireCorrespondant  = $formulaireConnection.eq($listeLienDemo.index($lienDemo));
             var $input = $formulaireCorrespondant.find('input[name=login], input[name=password]');
             var formulaireVide = true;
             $input.each(function(index, elem){
                 if($j(elem).val() != ''){
                     formulaireVide = false;
                 }
             });
             // s'il ne l'est pas on soumet sur la démo avec les valeurs de l'input
             if(!formulaireVide){
                 event.preventDefault();
                 var href = $lienDemo.attr('href');
                 $formulaireCorrespondant.attr('action', href).submit();
             }
         });
    }
};
/************************************************
 *  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 jquery
 ************************************************/

// callbacks & Mentions
function callback(){
    $j('#light, #fade').show();
}
function nocallback(){
    $j('#light, #fade').hide();
}
function mentions(){
    $j('#light_mentions, #fade_mentions').show();
}
function nomentions(){
    $j('#light_mentions, #fade_mentions').hide();
}
// changement de langue
function submitLang() {
	$j('#name').val($j('#language').val());
	document.DynaLanguageForm.submit();
}
/************************************************
 *  Autres fonctions : javascript 'pur'
 ************************************************/

// 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");
	}
}
//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);
}
/************************************************
 *  Autres fonctions : utilise prototype
 * TODO : virer ces prototypes fonctions !!
 ************************************************/

// 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;
		var 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]){
		var leService = $('affichage').readAttribute('class');
		leForm = $('contactInputs');	
		var formulaire_service = new Element ('input', {
			'id' : 'service',
			'type': 'hidden',
			'name': 'service',
			'value': leService
   		 });
		leForm.insert(formulaire_service);
	}
}