var adivProg = { tl : {} };

var tl;
		function onLoad() {
            var eventSource = new Timeline.DefaultEventSource(0);
            
            var theme = Timeline.ClassicTheme.create();
            theme.event.instant.icon = "no-image-40.png";
            theme.event.instant.iconWidth = 40;  // These are for the default stand-alone icon
            theme.event.instant.iconHeight = 40;
            
			var d0 = new Date();
			var m = (d0.getMonth().toInt()+1)
			var day = d0.getDate();
            var d = Timeline.DateTime.parseIso8601DateTime(d0.getFullYear()+'-'+((m.toString().length<2)?"0".concat(m.toString()):m)+'-'+((day.toString().length<2)?"0".concat(day.toString()):day));
            
            var bandInfos = [
                Timeline.createBandInfo({
                    width:          "85%", 
                    intervalUnit:   Timeline.DateTime.WEEK, 
                    intervalPixels: 100,
                    eventSource:    eventSource,
                    date:           d,
                    theme:          theme,
                    eventPainter:   Timeline.CompactEventPainter,
                    eventPainterParams: {
                        iconLabelGap:     15,
                        labelRightMargin: 20,
                        iconWidth:        60, // These are for per-event custom icons
                        iconHeight:       60,
						
                        stackConcurrentPreciseInstantEvents: {
                            limit: 5,
                            moreMessageTemplate:    "%0 More Events",
                            icon:                   "/templates/01/image/image-none.png", // default icon in stacks
                            iconWidth:              60,
                            iconHeight:             60
                        }
                    }
                }),
                Timeline.createBandInfo({
                    width:          "15%", 
                    intervalUnit:   Timeline.DateTime.MONTH, 
                    intervalPixels: 100,
                    eventSource:    eventSource,
                    date:           d,
                    theme:          theme,
                    layout:         'overview'  // original, overview, detailed
                })
            ];
            bandInfos[1].syncWith = 0;
            bandInfos[1].highlight = true;
            
            tl = Timeline.create(document.getElementById("tl"), bandInfos, Timeline.HORIZONTAL);
			tl.loadJSON("/include/timeline-json.php", function(json, url) { eventSource.loadJSON(json, url); });
			tl.layout();
        }
        
        var resizeTimerID = null;
        function onResize() {
            if (resizeTimerID == null) {
                resizeTimerID = window.setTimeout(function() {
                    resizeTimerID = null;
                    tl.layout();
                }, 500);
            }
        }

window.addEvent('domready', function() {	

	// Execution automatique
	(function () {
		
		$$('ul.submenuNav li a').each(function(elt, i) {
			if (elt.getNext('span') != null) {
				(elt.getNext('span')).addEvent('click', function() { document.location = elt.get('href'); });
			}
		});
		
		if ($('content-form') != null) {
				var myForm = new Forms('content-form');
				
			($('content-form').getElements('input[type=text]')).each(function(elt,i) { elt.addEvent('click', function() { this.select(); }); });
		}
		
			
		$$($(document.body).getElements('a')).each(function(elt, i) {		
			if (elt.get('href') != null) {
				if ((elt.get('href')).indexOf('http\://') == -1 && (elt.get('href')).substr(0,1) != "/") {	
 					elt.setProperty('target', '_blank');
				}
			}
		});
		
			
			
/*			var reqNews = new Request({
				method : 'get', 
				url : '../include/exposants.php?action=searchLst',
				onRequest: $('searchExpList').set('value', 'en cours de chargement...'),
				onComplete: function (response) { 
					$('searchExpList'). set('html', response);
				}
			}).send(null);*/
	})();
	
	
	
	String.prototype.getRoot = function () {
		return this.replace(/^.*\/([^\/]+)$/g,"$1");
	}
	
	

	
});


var Forms = new Class ({
		Implements: Options,
		
		options : {
			formValidInput : 'valid-form',
			errorClass : 'error'
		},

		initialize : function (container, options) {
			this.setOptions(options);
			this.validForm = $(this.options.formValidInput);
			
			
			this.container = $(container);

			this.obl = [];
			this.initUrl();
			this.initPopup();

			$('valid-form').addEvent('click', function(event) {
				event.stop();
				this.obl.empty();
				this.parseInput(this.container); 
				if (this.obl.length > 0) {
					event.preventDefault(); 
					this.displayObl();
				}else $('content-form').submit();
			}.bind(this));
		},
		
		initUrl : function () {
			this.action = this.container.get('action');
		},
		
		initPopup : function () {
			this.popup = new Element ('div', {'id' : 'popup'});
			this.popup.inject(document.body, 'top');
			
			this.popup.addEvents({
				'show' : function () { this.toggleClass('show'); },
				'hide' : function () { this.toggleClass('show'); }
			});
		},
		
		parseInput : function (container) {
			var bool = true;
			(container.getElements('input, textarea, select')).each(function (elt, i) {
				if (elt.hasClass('obligatoire') && this.testInput(elt) == false) {
					elt.addClass(this.options.errorClass);
					this.obl.push(elt);
				}else if (elt.hasClass('error')) elt.removeClass('error');
			}, this);
		},
		
		testInput : function (tag) {
			var result =  true;

			switch (tag.get('tag')) {
				case 'input'	:	if (tag.get('type') == 'text') {
										if (tag.get('value').trim() == '')
											result = false;
										tag.addEvent('click', function() { this.select(); });
									}
									else if (tag.get('type') == 'checkbox' && tag.checked == false) result = false;
									else if (tag.get('type') == 'radio' && tag.checked == false) result = false
									break;

				case "textarea"	:	if (tag.get('value').trim() == '') result = false;
									break;

				case "select"	:	if (tag.get('value') == 0) result = false;
									break
			}
			
			return result;
		},

		displayObl : function () {
			this.popup.empty();
			
			var background = new Element('div', {'class': 'background'});
			background.inject(this.popup);
			
			var message = new Element('div', {'class': 'message'});
			message.addEvent('click', function () { this.popup.fireEvent('hide'); }.bind(this));

			if (this.obl.length > 0) {
				(new Element('p', {'html' : 'Les champs du formulaire <span class="rouge">en rose</span> contiennent des erreurs ou ne sont pas remplis correctement'})).inject(message);
				(new Element('p', {'html' : '&gt;&gt; Revenir sur le formulaire', 'class' : 'link'})).inject(message);
				(new Element('p', {'class' : 'footer'})).inject(message);
	
				message.inject(this.popup);
				
				this.popup.fireEvent('show');
			}
		}
	});
