/*** TOGGLE ***/
function toggle( opt) {
	if($defined(opt.classDefaut)) {
		var s = $.extend({
			classDefaut: '', // ni ouvert, ni fermé
			classClose: $defined(opt.classClose) ? opt.classClose : opt.classDefaut + '-close', // fermé
			classOpen: $defined(opt.classOpen) ? opt.classOpen :opt.classDefaut + '-open', // ouvert
			openStart: false, // false = fermé, true = ouvert au démarrage
			cookieName: '', // si non vide, save ds cookie
			idPanel: '', // si le "contenu click" et éloigné du "contenu à ouvrir" Ex : idName-idPanel-Id
			idName: $defined(opt.idName) ? opt.idName : opt.cookieName + '-',
			forceId: [], // force des contenus au démarrage
			animation: '' // cf. slideToggle
		}, opt);

		var bCookie = false, aCookie = [];
		if( $defined(s.cookieName) && s.cookieName != '' ) {
			aCookie = ($.cookie( s.cookieName) || '').split('\.');
			bCookie = true;
		}

		$('.' + s.classDefaut).click(function() {
			var iId = this.id.replace(s.idName, '');

			// Action
			if(s.idPanel == '') {
				if ( s.animation != '') {
					$(this).next().slideToggle(s.animation);
				} else {
					$(this).next().toggle();
				}
			} else {
				if ( s.animation != '') {
					$('#' + s.idPanel + '-' + this.id).slideToggle(s.animation);
				} else {
					$('#' + s.idPanel + '-' + this.id).toggle();
				}
			}
			$(this).inverseClass(s.classClose, s.classOpen);
			if (bCookie) {
				if ( iId + '' != '' ) {
					var iPos;
					if ( (iPos = $.inArray(iId, aCookie)) != -1) {
						do {
							aCookie.splice(iPos, 1);
						} while( (iPos = $.inArray(iId, aCookie)) != -1)
					} else {
						aCookie.push(iId);
					}
					$.cookie( s.cookieName, aCookie.join('.'), {expires: 365} );
				}
			}
		}).each(function() {
			// Initialisation
			var iPosF = -1, iId = this.id.replace(s.idName, '');
			if ( !(s.openStart ^ $.inArray(iId, aCookie) != -1) && !(s.openStart ^ (iPosF = $.inArray(iId, s.forceId)) != -1) ){
				$(this).replaceClass(s.classDefaut, s.classClose)
				if(s.idPanel == '') {
					$(this).next().hide();
				} else {
					$('#' + s.idPanel + '-' + this.id).hide();
				}
			} else {
				$(this).replaceClass(s.classDefaut, s.classOpen);
				if(s.idPanel == '') {
					$(this).next().show();
				} else {
					$('#' + s.idPanel + '-' + this.id).show();
				}
			}
			// ajout le forceid en cookie si non stocké
			if (bCookie && iPosF != -1) {
				if ( iId + '' != '') {
					var iPos;
					if ( (iPos = $.inArray(iId, aCookie)) == -1) {
						aCookie.push(iId);
						$.cookie( s.cookieName, aCookie.join('.'), {expires: 365} );
					}
				}
			}
		});
	}
}

/*** CAPSLOCK **/
var capslock = {
	oImgWarning : document.createElement('img'),
	init: function( sRacineCharte) {
		$(':password').each(function() {
			$(this).keypress( function(e) {
				if (((e.which >= 65 && e.which <=  90) && !e.shiftKey) ||
					((e.which >= 97 && e.which <= 122) && e.shiftKey)) {
					// majusule, sans shift
					capslock.show_warning(getSrcElement(e));
				} else {
					capslock.hide_warning(getSrcElement(e));
				}
			});
		});
		$(this.oImgWarning).attr({
			src: sRacineCharte + 'image/capslock.png',
			alt: _lg("Attention, la touche de Verr. Maj est activée.")
		}).css({
			position: 'absolute',
			display: 'none',
			zIndex: 999
		}).click( function() { $(this).hide() });
		document.body.appendChild(this.oImgWarning);
	},
	show_warning: function(targ) {
		var offset = $(targ).offset();
		$(this.oImgWarning).css({
			top: (offset.top - 65),
			left: (offset.left + $(targ).outerWidth())
		}).show();
	},
	hide_warning: function(targ) {
		$(this.oImgWarning).hide();
	}
};

/*** MULTISELECT **/
var multiselect = {
	o: {
		selectBox1: null, // selecteur jquery
		selectBox2: null, // selecteur jquery
		defautBox2: null,
		url: ''
	},
	init: function(options) {
		if(options) {
			$.extend(this.o, options);
			$(this.o.selectBox1).change(this.upd);
			this.upd();
		}
	},
	upd: function() {
		var s = this.o;
		$.getJSON(s.url,{id: $(s.selectBox1).val(), ajax: 'true'}, function(j) {
			var oSel = $id(	$(s.selectBox2).attr('id'));
			oSel.option.length = 0;
			for (var i = 0; i < j.length; i++) {
				oSel.options[i] = new Option(j[i].optionDisplay, j[i].optionValue);
			}
		});
	}
};
