var Site = {
		
	start: function(){
		
		Site.setExternals();
		Site.setPrint();
		
		/*
		if ($('voeux2009')) {
			Site.showVoeux();	
		}else{*/
			if( $('bandeau') )
				$('bandeau').setStyle('z-index', '1');
		/*}*/
		
		if($('q')) Site.formSearch();
		
		if($('creer')) Site.showCreate();

		if( $('plan_visite') ) Site.popups();
		
		if( $('formateurs') ) Site.popCV();
		if( $$('ul.formateurs') ) Site.popEquipe();
		
		if( $('catalogue_formations') ) Site.catalogue();
		if( $('liste_partenaires') ) Site.partenaires();
		
		if ($('search_name')) Site.autoComplete();
		
		if( $('search_key')) Site.autoCompleteIspaia();
		
		if( $('plan_visite') ) {
			new Tips('.puce', {
				'showDelay': 10,
				'hideDelay': 10
			});
			
			$$('.tip').setStyles({
				background: '#fff',
				padding: '5px;'
			});
			
			$$('.tip-text').setStyles({color: '#fff'});
		}
		
		Site.showFx();

	},
	
	setExternals: function(){
		$$('.external').addEvent('click', function(ev){
			window.open(this.href); new Event(ev).stop(); return; });
	},
	
	setPrint: function(){
		
		$$('.goto_print').setStyle('cursor','pointer');
		
		$$('.goto_print').addEvent('click', function(ev){
			window.print(); new Event(ev).stop(); return; });
	},
	
	formSearch: function(){
		$('q').addEvents({
			'focus': function(){ if($('q').value=='Recherche') $('q').value=''; },
			'blur': function(){ if($('q').value=='') $('q').value='Recherche'; }
		});
	},
	
	fixExplorer: function() {
		
		$('container').getElements('input[type=radio]').setStyle('border', '0');
		$('container').getElements('input[type=checkbox]').setStyle('border', '0');
		
		$$('textarea').addEvents({
			'focus': function(){ this.addClass('highlight');},
			'blur': function(){ this.removeClass('highlight');}
		});
		
		$$('select').addEvents({
			'focus': function(){ this.addClass('highlight');},
			'blur': function(){ this.removeClass('highlight');}
		});
	},
	
	catalogue: function(){
		
		$$('.categorie_formation').setStyle('display', 'none');
		
		$$('h3.categorie_titre').addEvent('click', function(){
			
			$$('.categorie_formation').setStyle('display', 'none');
			formation = this.getNext('.categorie_formation');
			
			if( formation.getStyle('display')=='none' ){
				formation.setStyle('display','block');
			}else{
				formation.setStyle('display','none');
			}
		});
		
		
		$$('.sous_categorie_formation').setStyle('display', 'none');
		
		$$('.sous_categorie_titre').addEvent('click', function(){
			
			$$('.sous_categorie_formation').setStyle('display', 'none');
			if( this.getNext('.sous_categorie_formation').getStyle('display')=='none' ){
				this.getNext('.sous_categorie_formation').setStyle('display','block');
			}else{
				this.getNext('.sous_categorie_formation').setStyle('display','none');
			}
		});
		
	},
	
	partenaires: function(){
		
		$$('#liste_partenaires div.paragraphe').addClass('nodisplay');
		
		$$('#liste_partenaires h3').addEvents({
			
			'mouseover': function(el) {
				this.setStyle('cursor','pointer'); },
				
			'mouseout': function(el) { 
				this.setStyle('cursor','default'); }
		});
		
		$$('#liste_partenaires h3').addEvent('click', function(){
			
			$$('#liste_partenaires div.paragraphe').addClass('nodisplay');
			formation = this.getNext('div.paragraphe');
			
			//alert(formation.getStyle('display'));
			
			if( formation.hasClass('nodisplay') ){
				formation.removeClass('nodisplay');
			}else{
				formation.addClass('nodisplay');
			}
		});
		
	},
	
	popups: function() {
		
		$$('#plan_visite a.puce').addEvent('click', function(e){
			
			new Event(e).stop();
			
			$$('#plan_visite a.puce').removeClass('hover');
			$$('#plan_visite a.puce').removeClass('yellowhover');
			$$('#plan_visite a.puce').removeClass('redhover');
			$$('#plan_visite a.puce').removeClass('bluehover');
			$$('#plan_visite a.puce').removeClass('greenhover');
			
			
			$$('#plan_visite div.popup').setStyle('display', 'none');
			id = explode('_', this.getProperty('id'));
			num = id[1]; 
			
			if( $('popup_'+num) ) $('popup_'+num).setStyle('display', 'block');
			
			this.addClass('hover');
			if( this.hasClass('yellow') ) this.addClass('yellowhover');
			if( this.hasClass('blue') ) this.addClass('bluehover');
			if( this.hasClass('green') ) this.addClass('greenhover');
			if( this.hasClass('red') ) this.addClass('redhover');

			new Fx.Scroll(window).toBottom();
			
		});
		
		$$('#plan_visite p.close').addEvent('click', function(){
			$$('#plan_visite div.popup').setStyle('display', 'none');
		});
	},
	
	popCV: function() {
				
		$$('#formateurs li').addEvents({
			
			'mouseover': function(el) {
				this.setStyle('cursor','pointer');
				this.getElement('span').addClass('popcvhover'); },
				
			'mouseout': function(el) { 
				this.setStyle('cursor','default');
				this.getElement('span').removeClass('popcvhover'); },
				
			'click': function(el) { 
				url = BASEURL + 'public/files/cv/' + this.getProperty('id') + '.pdf';
				window.open(url); new Event(ev).stop(); return; }
		});
	},
	
	popEquipe: function() {
				
		$$('ul.formateurs li').addEvents({
			
			'mouseover': function(el) {
				this.setStyle('cursor','pointer');
				this.getElement('span').addClass('popcvhover'); },
				
			'mouseout': function(el) { 
				this.setStyle('cursor','default');
				this.getElement('span').removeClass('popcvhover'); }
		});
	},
	
	showFx: function() {
		
		//new viewer($$('#slogan h2'),{mode: 'alpha', interval: 7000}).play(true);
		
		new viewer($$('.newsticker'),{mode: 'alpha'}).play(true);
			
	},
	
	autoComplete: function() {
		
		new Autocompleter.Ajax.Xhtml($('search_name'), BASEURL+'system/assets/ajax/auto.php', {
			'delay': 20,
			'minLength': 2,
			'postData': {html: 1},
			'selectMode': 'type-ahead',
			'selectFirst': false
			
		});
		
		$('formAnnuaire').addEvent('submit', function(e){
			
			var event = new Event(e);
			
			if( $('search_name').value=='' && $('search_sector').value=='' ) {
				
				event.stop();
				
				$('formMessage').set({
					'style': 'color:red; text-align:center;',
					'text': 'Merci de bien vouloir remplir au moins un des champs !'
				});	
			}
		});
	},
	
	
	autoCompleteIspaia: function() {
		
		new Autocompleter.Ajax.Xhtml($('search_key'), BASEURL+'system/assets/ajax/auto_ispaia.php', {
			'delay': 20,
			'minLength': 2,
			'postData': {html: 1},
			'selectMode': 'type-ahead',
			'selectFirst': false
			
		});
		
		$('formAnnuaire').addEvent('submit', function(e){
			
			var event = new Event(e);
			
			if( $('search_key').value=='' ) {
				
				event.stop();
				
				$('formMessage').set({
					'style': 'color:red; text-align:center;',
					'text': 'Merci de bien vouloir remplir le champ !'
				});	
			}
		});
	},
	
	showCreate: function() {
		
		$$('#creation a').addEvents({
			'mouseover': function() {
				id = this.getProperty('id');
				$$('#infosuppl p').setStyle('display', 'none');
				$('infosuppl_'+id).setStyle('display', 'block');		
			},
			
			'mouseout': function(){		
				$$('#infosuppl p').setStyle('display', 'none');
			}
		});

	},
	
	showVoeux: function() {
		
		$(document.body).setStyle('position', 'relative');
		$('voeux2009').setStyle('display', 'block');
		
		var myEffect = new Fx.Morph('voeux2009', {duration: 2000, transition: Fx.Transitions.Sine.easeOut});
 
		myEffect.start({
		    'left': [-323, 0]
		});
		
		/*
		$('voeux2009').addEvent('click', function(){
			myEffect.start({
		   		'left': [0, -323]
			});
		});
		*/
		
		
		$('voeux2009').addEvents({
			'click': function(){
				myEffect.start({'left': [0, -313]});
			},
			'mouseover': function(){
				
				var left = this.getStyle('left').toInt();

				if( left>-323 && left<-312) {
					myEffect.start({
					    'left': [-313, 0]
					});
				}
			}
		});
		
	}

};

window.addEvent('domready', function(){
	Site.start();
});







function explode( delimiter, string, limit ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}