/*
	Actions au chargement du DOM
*/

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


	// gestion du bloc des messages privées
	if ($('entete_droite'))
	{
		var url_cible = '/welcome_ajax.php';
		
		/* version local */
		//var url_cible = 'welcome_ajax.php';
		var req = new Request.HTML({
			method: 'get',
			url: url_cible,
			update: $('entete_droite'),
			onComplete: function(response) { // nothing
			}
		}).send();	
	}

	 
	 
	// fonction qui ouvre le chat en Ajax
	 function openInfosChat(url_cible)
	{

				var req = new Request.HTML({
					method: 'get',
					url: url_cible,
					update: $('actu_mf'),
					onComplete: function(response) { 
						
						if (Browser.Platform.other != true)
						{
							var elZeroB = $('slideFete');
							var elOneB = $('slideTwitter');
							var elTwoB = $('slideRadio');
							var elThreeB = $('slideMsgs');
					
							// on regarde si un cookie contient l'info de la tab a afficher par defaut
							if (Cookie.read('chat_info_tab') != null || tab_actuel != '')
							{
								hideAll();
								if (tab_actuel != '')
								{
									$(tab_actuel).setStyle('display', 'block');	
								}
								else if ($(Cookie.read('chat_info_tab')))
								{
									var tabDefaut = $(Cookie.read('chat_info_tab'));
									tab_actuel = Cookie.read('chat_info_tab');
									tabDefaut.setStyle('display', 'block');
									showFunction.bind($(elZeroB));
								}
							}
							else
							{
								elZeroB.setStyle('display', 'block');	
								tab_actuel = elZeroB.id;
							}
	
							$('tab_zero').addEvent('click', showFunction.bind(elZeroB));
							$('tab_one').addEvent('click', showFunction.bind(elOneB));
							$('tab_two').addEvent('click', showFunction.bind(elTwoB));
							$('tab_three').addEvent('click', showFunction.bind(elThreeB));
						}
						else
						{
							// cas des webphones non compatibles
							
							$('chat_fixed').setStyle('display', 'none');
							
						}

					}
				});		
			
				req.send();
				var doMany = function() {
					//alert(tab_actuel)
					
					req.send();
				};
				
				doMany.periodical(180000);				
								
				
	} 


	var showFunction = function() {
		hideAll();
		tab_actuel = this.id;
		var cookieChatInfo = Cookie.write('chat_info_tab', this.id, {duration: 30, domain:  '.marseilleforum.com', path: '/'});
		this.setStyle('display', 'block');
	}
	 
	var hideAll = function() {
		var tabContents = $$('#actu_mf .slide');
		
		tabContents.each(function(slide, index){
			slide.setStyle('display', 'none');
		});
	}





	// gestion de la fenetre du chat 
	if ($('chat_fixed'))
	{

		var encartChat = $('upshrinkHeader').getStyle('display')
		var url_cible = "/ajax_actu_mf.php";
		var tab_actuel = '';
		
		// comportement par defaut (si bloc ouvert ou non)
		if (encartChat == 'block')
		{
			// on charge les infos chat	
			openInfosChat(url_cible);
		}
		
		// chargement du bloc si on clique sur le titre
		$('titre_chat').addEvent('click', function(e){
			e.stop();
			
			if ($('upshrinkHeader').getStyle('display') == 'block')
			{
				openInfosChat(url_cible);
			}
			
		});		
		
		
		
		
		/* version facebook like */
		
		/*
		if (Browser.Platform.other != true) 
		{ 
		
			
			$('bloc_chat').set('style', 'display:block');
			
			
			// apparence par defaut
			$('chat_fixed').setStyles({
				'opacity': '0.80'
			});
			 $('cboxdiv').setStyles({
				'height': '50px'
			}); 
			 $('bloc_chat').scrollTo(0,200);		
			 
			
			
			// gestion de la transparence au survol
			$('chat_fixed').addEvent('mouseenter', function(event){
				$('chat_fixed').setStyles({
					'opacity': '1'
				});
				
				$('cboxdiv').setStyles({
					'height': '303px'
				});					
				
				 $('bloc_chat').scrollTo(0,0);
 				 $('titre_reduit').setStyle('display', 'none');
		
			});
			
			// reduit la fenetre de chat quand la souris quitte la zone
			$('chat_fixed').addEvent('mouseleave', function(event){
				$('chat_fixed').setStyles({
					'opacity': '0.80'
				});

				 $('cboxdiv').setStyles({
					'height': '50px'
				}); 
				 
				 $('bloc_chat').scrollTo(0,200);
				 $('titre_reduit').setStyle('display', 'block');



			});				
		

			
		}*/
		
		
				
		
	}
	

	
	if ($('form_ajout_info'))
	{

		var myVerticalSlide2 = new Fx.Slide('form_ajout_info');
		
		$('lien_nouveau_lieu').addEvent('click', function(e){
			e.stop();
			myVerticalSlide2.slideIn();
		});	
		
		myVerticalSlide2.hide();		
		
	}
	
	if ($('TickerNews'))
	{
		//var myTicker = new mooTicker('TickerNews',{interval:15000});
		mySlideShow = new SlideShow('TickerNews',{
			delay: 7000,
			autoplay: true,
			transition:'pushUp',
			duration:800
		});			
	}		

	if ($('meteo'))
	{
		/*
		mySlideShow = new SlideShow('meteo',{
			delay: 10000,
			autoplay: true,
			transition:'fadeThroughBackground',
			duration:1600
		});				
		*/
	}	
	

});


// Dependency on Loop.js

/*
---

script: Loop.js

description: Runs a class method on a periodical

license: MIT-style license.

authors: Ryan Florence <http://ryanflorence.com>

docs: http://moodocs.net/rpflo/mootools-rpflo/Loop

requires:
- core:1.2.4/'*'

provides: [Loop]

...
*/

var Loop = new Class({

    loopCount: 0,
    isStopped: true,
    isLooping: false,
    loopMethod: $empty,

    setLoop: function(fn,delay){
        if(this.isLooping) {
            this.stopLoop();
            var wasLooping = true;
        } else {
            var wasLooping = false;
        }
        this.loopMethod = fn;
        this.loopDelay = delay || 3000;
        if(wasLooping) this.startLoop();
        return this;
    },

    stopLoop: function() {
        this.isStopped = true;
        this.isLooping = false;
        $clear(this.periodical);
        return this;
    },

    startLoop: function(delay) {
        if(this.isStopped){
            var delay = (delay) ? delay : this.loopDelay;
            this.isStopped = false;
            this.isLooping = true;
            this.periodical = this.looper.periodical(delay,this);
        };
        return this;
    },

    resetLoop: function(){
        this.loopCount = 0;
        return this;
    },

    looper: function(){
        this.loopCount++;
        this.loopMethod(this.loopCount);
        return this;
    }

});




// THE CLASS

/*
---

script: SlideShow.js

description: Easily extendable, class-based, slideshow widget. Use any element, not just images. Comes with packaged transitions but is easy to extend and create your own transitions.  The class is built to handle the basics of a slideshow, extend it to implement your own navigation piece and custom transitions.

license: MIT-style license.

authors: Ryan Florence

docs: http://moodocs.net/rpflo/mootools-rpflo/SlideShow

requires:
  - Loop

provides: [SlideShow]

...
*/


var SlideShow = new Class({
    
    Implements: [Options, Events, Loop],
        
        options: {
            /*
            onShow: $empty,
            onShowComplete: $empty,
            onReverse: $empty,
            onPlay: $empty,
            onPause: $empty
            */
            delay: 7000,
            transition: 'crossFade',
            duration: '500',
            autoplay: false
        },
    
    initialize: function(element, options){
        this.setOptions(options);
        this.setLoop(this.showNext, this.options.delay);
        this.element = document.id(element);
        this.slides = this.element.getChildren();
        this.current = this.slides[0];
        this.setup();
        if(this.options.autoplay) this.startLoop();
    },
    
    setup: function(){
      this.setupElement();
      this.setupSlides();
        return this;
    },
    
    setupElement: function(){
        var el = this.element;
        if(el.getStyle('position') != 'absolute' && el != document.body) el.setStyle('position','relative');
        return this;
    },
    
    setupSlides: function(){
        this.slides.each(function(slide, index){
            this.storeTransition(slide).reset(slide);
            if(index != 0) slide.setStyle('display','none');
        }, this);
        return this;
    },
    
    storeTransition: function(slide){
        var classes = slide.get('class');
        var transitionRegex = /transition:[a-zA-Z]+/;
        var durationRegex = /duration:[0-9]+/;
        var transition = (classes.match(transitionRegex)) ? classes.match(transitionRegex)[0].split(':')[1] : this.options.transition;
        var duration = (classes.match(durationRegex)) ? classes.match(durationRegex)[0].split(':')[1] : this.options.duration;
        slide.store('ssTransition', transition);
        slide.store('ssDuration', duration);
        return this;
    },
    
    getTransition: function(slide){
        return slide.retrieve('ssTransition');
    },
    
    getDuration: function(slide){

        return slide.retrieve('ssDuration');
    },
    
    show: function(slide){
        this.fireEvent('show');
        if(slide != this.current){
            var transition = this.getTransition(slide);
            var duration = this.getDuration(slide);
            var previous = this.current.setStyle('z-index', 1);
            var next = this.reset(slide);
            this.transitions[transition](previous, next, duration, this);
            (function() { 
                previous.setStyle('display','none');
                this.fireEvent('showComplete');
            }).bind(this).delay(duration);
            this.current = next;
        }
        return this;
    },
    
    reset: function(slide){
        return slide.setStyles({
            'position': 'absolute',
            'z-index': 0,
            'display': 'block',
            'left': 0,
            'top': 0
        }).fade('show');
        return this;
    },
    
    nextSlide: function(){
        var next = this.current.getNext();
        return (next) ? next : this.slides[0];
    },

    previousSlide: function(){
        var previous = this.current.getPrevious();
        return (previous) ? previous : this.slides.getLast();
    },
    
    showNext: function(){
        this.show(this.nextSlide());
        return this;
    },
    
    showPrevious: function(){
        this.show(this.previousSlide());
        return this;
    },
    
    play: function(){
        this.startLoop();
        this.fireEvent('play');
        return this;
    },
    
    pause: function(){
        this.stopLoop();
        this.fireEvent('pause');
        return this;
    },
    
    reverse: function(){
        var fn = (this.loopMethod == this.showNext) ? this.showPrevious : this.showNext;
        this.setLoop(fn, this.options.delay);
        this.fireEvent('reverse');
        return this;
    }
    
});

SlideShow.adders = {
    
    transitions:{},
    
    add: function(className, fn){
        this.transitions[className] = fn;
        this.implement({
            transitions: this.transitions
        });
    },
    
    addAllThese : function(transitions){
        $A(transitions).each(function(transition){
            this.add(transition[0], transition[1]);
        }, this);
    }
    
}

$extend(SlideShow, SlideShow.adders);
SlideShow.implement(SlideShow.adders);

SlideShow.add('fade', function(previous, next, duration, instance){
    previous.set('tween',{duration: duration}).fade('out');
    return this;
});

SlideShow.addAllThese([

    ['none', function(previous, next, duration, instance){
        previous.setStyle('display','none');
        return this;
    }],

    ['crossFade', function(previous, next, duration, instance){
        previous.set('tween',{duration: duration}).fade('out');
        next.set('tween',{duration: duration}).fade('in');
        return this;
    }],
    
    ['fadeThroughBackground', function(previous, next, duration, instance){
        var half = duration/2;
        next.set('tween',{
            duration: half
        }).fade('hide');
        previous.set('tween',{
            duration: half,
            onComplete: function(){
                next.fade('in');
            }
        }).fade('out');
    }],

    ['pushLeft', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('width').toInt();
        next.setStyle('left', distance);
        [next, previous].each(function(slide){
            var to = slide.getStyle('left').toInt() - distance;
            slide.set('tween',{duration: duration}).tween('left', to);
        });
        return this;
    }],
    
    ['pushRight', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('width').toInt();
        next.setStyle('left', -distance);
        [next, previous].each(function(slide){
            var to = slide.getStyle('left').toInt() + distance;
            slide.set('tween',{duration: duration}).tween('left', to);
        });
        return this;
    }],
    
    ['pushDown', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('height').toInt();
        next.setStyle('top', -distance);
        [next, previous].each(function(slide){
            var to = slide.getStyle('top').toInt() + distance;
            slide.set('tween',{duration: duration}).tween('top', to);
        });
        return this;
    }],
    
    ['pushUp', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('height').toInt();
        next.setStyle('top', distance);
        [next, previous].each(function(slide){
            var to = slide.getStyle('top').toInt() - distance;
            slide.set('tween',{duration: duration}).tween('top', to);
        });
        return this;
    }],
    
    ['blindLeft', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('width').toInt();
        next
            .setStyles({
                'left': distance,
                'z-index': 1
            })
            .set('tween',{duration: duration})
            .tween('left', 0);
        return this;
    }],

    ['blindRight', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('width').toInt();
        next
            .setStyles({
                'left': -distance,
                'z-index': 1
            })
            .set('tween',{duration: duration})
            .tween('left', 0);
        return this;
    }],
    
    ['blindUp', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('height').toInt();
        next
            .setStyles({
                'top': distance,
                'z-index': 1
            })
            .set('tween',{duration: duration})
            .tween('top', 0);
        return this;
    }],
    
    ['blindDown', function(previous, next, duration, instance){
        var distance = instance.element.getStyle('height').toInt();
        next
            .setStyles({
                'top': -distance,
                'z-index': 1
            })
            .set('tween',{duration: duration})
            .tween('top', 0);
        return this;
    }],
    
    ['blindDownFade', function(previous, next, duration, instance){
        this.blindDown(previous, next, duration, instance).fade(previous, next, duration, instance);
    }],
    
    ['blindUpFade', function(previous, next, duration, instance){
        this.blindUp(previous, next, duration, instance).fade(previous, next, duration, instance);
    }],
    
    ['blindLeftFade', function(previous, next, duration, instance){
        this.blindLeft(previous, next, duration, instance).fade(previous, next, duration, instance);
    }],
    
    ['blindRightFade', function(previous, next, duration, instance){
        this.blindRight(previous, next, duration, instance).fade(previous, next, duration, instance);
    }]
    
]);