/** Fonctions javascripts reprise tel-quel
 * Suite à l'ajout de mootools ca serait bien de revoir les choses
 */
window.onload = function()
{
	initSelect();
	initAutoBlankFields();
}

function initSelect()
{
	_initSelect('dep');
	_initSelect('type_contrat');
	_initSelect('fonction');
}

function _initSelect(ids)
{
	var sel = document.getElementById(ids);
	if ( !sel )
		return ;
	sel.onchange = function() { redirectSelect(sel); }
}


function redirectSelect(sel)
{
	var opt= sel.options[sel.selectedIndex];
	document.location.href = opt.value;
}


function initAutoBlankFields()
{
	_initAutoBlankField('SearchText');
	_initAutoBlankField('login');
}

function _initAutoBlankField(idf)
{
	var f = document.getElementById(idf);
	if ( !f )
		return ;
	f.onfocus = function() { if ( f.value==f.defaultValue ) { f.value=''; } }
}

function checkNewElementUnder(form)
{
	if ( form.elements['NodeID'].value == '0' )
	{
		alert('Vous devez sélectionner un thème');
		return false;
	}
	return true;
}

function toggleBlock(bid)
{
var b=document.getElementById(bid);
 if ( b )
 {
 if ( b.style.display == 'none' || b.style.display == '' )
 b.style.display = 'block';
 else
 b.style.display = 'none';
 }
}
/** Fin des fonction reprise tel-quel, la suite utilise mootools **/

/**
 * Synchronisation des couleurs du menu départements et
 * de l'annimation flash en page d'accueil.
 * @param string id Nom du menu (classe css de la balise li)
 * @return void
 */
function select_menu(id) {
    $$('#departementsLandingAccueil .highlighted').each(function (item) {
        item.removeClass('highlighted');
    });
    
    $$('#departementsLandingAccueil .'+id).each(function (item) {
        item.addClass('highlighted');
    });
}

/**
 * Suppression automatique des espaces sur les login à la soumission
 * des formulaires de login extranet
 */
var formIds = new Array('extranetEtudeForm', 'extranetEntrepriseForm', 'extranetRechercheForm');
var formLoginField = new Array('MAIL', 'login', 'login');
document.addEvent('domready', function() {
    formIds.each(function(formId, index) {
        var form = $(formId);
        if (form != null) {
            form.addEvent('submit', function() {
               var login = $(formLoginField[index]);
               login.value = login.value.trim();
            });
        }
    });
});

/**
  * Menu pour permettre le clic droit sur le header de TLR afin de récupérer le logo 
  *
  */ 
window.addEvent('domready', function() {

	//create a context menu
	var context = new ContextMenu({
		targets: '#bandeau', //menu only available on links
		menu: 'contextmenu',
		actions: {
			copy: function(element,ref) { //copy action changes the element's color to green and disables the menu
				element.setStyle('color','#090');
				ref.disable();
			}
		},
		offsets: { x:2, y:2 }
	});
	
	context.enable();	

	// On rajoute lorsque ça existe les onclick
	if ( $('lienDrapeau') != null ) 
		{ 
		 lienTrad = $('lienDrapeau').getProperty('href');
		 $('lienDrapeau').addEvent('click', function(e) 
			 {
				window.open(lienTrad, 'Traduction', 'height=400, scrollbars=yes, width=800, toolbar=no, menubar=no, status=no')
			 });
		 $('lienDrapeau').setProperty('href','#');
		}
		
});


/*
Class:      ContextMenu
Author:     David Walsh
Website:    http://davidwalsh.name
Version:    1.0
Date:       1/20/2009
SAMPLE USAGE :http://davidwalsh.name/js/contextmenu
*/
var ContextMenu = new Class({

//implements
Implements: [Options,Events],

//options
options: {
    actions: {},
    menu: 'contextmenu',
    stopEvent: true,
    targets: 'body',
    trigger: 'contextmenu',
    offsets: { x:0, y:0 },
    onShow: $empty,
    onHide: $empty,
    onClick: $empty,
    fadeSpeed: 200
},

//initialization
initialize: function(options) {
    //set options
    this.setOptions(options)
    
    //option diffs menu
    this.menu = $(this.options.menu);
    this.targets = $$(this.options.targets);
    
    //fx
    this.fx = new Fx.Tween(this.menu, { property: 'opacity', duration:this.options.fadeSpeed });
    
    //hide and begin the listener
    this.hide().startListener();
    
    //hide the menu
    this.menu.setStyles({ 'position':'absolute','top':'-900000px', 'display':'block' });
},

//get things started
startListener: function() {
    /* all elemnts */
    this.targets.each(function(el) {
        /* show the menu */
        el.addEvent(this.options.trigger,function(e) {
            //enabled?
            if(!this.options.disabled) {
                //prevent default, if told to
                if(this.options.stopEvent) { e.stop(); }
                //record this as the trigger
                this.options.element = $(el);
                //position the menu
                this.menu.setStyles({
                    top: (e.page.y + this.options.offsets.y),
                    left: (e.page.x + this.options.offsets.x),
                    position: 'absolute',
                    'z-index': '2000'
                });
                //show the menu
                this.show();
            }
        }.bind(this));
    },this);
    
    /* menu items */
    this.menu.getElements('a').each(function(item) {
        item.addEvent('click',function(e) {
            if(!item.hasClass('disabled')) {
                this.execute(item.get('href').split('#')[1],$(this.options.element));
                this.fireEvent('click',[item,e]);
            }
        }.bind(this));
    },this);
    
    //hide on body click
    $(document.body).addEvent('click', function() {
        this.hide();
    }.bind(this));
},

//show menu
show: function() {
    this.fx.start(1);
    this.fireEvent('show');
    this.shown = true;
    return this;
},

//hide the menu
hide: function() {
    if(this.shown)
    {
        this.fx.start(0);
        this.fireEvent('hide');
        this.shown = false;
    }
    return this;
},

//disable an item
disableItem: function(item) {
    this.menu.getElements('a[href$=' + item + ']').addClass('disabled');
    return this;
},

//enable an item
enableItem: function(item) {
    this.menu.getElements('a[href$=' + item + ']').removeClass('disabled');
    return this;
},

//diable the entire menu
disable: function() {
    this.options.disabled = true;
    return this;
},

//enable the entire menu
enable: function() {
    this.options.disabled = false;
    return this;
},

//execute an action
execute: function(action,element) {
    if(this.options.actions[action]) {
        this.options.actions[action](element,this);
    }
    return this;
}

});