/* A workaround for IE issues in mootools 1.2.1
 * - Recreates FX.Scroll() but utilises 1.2.0's getPosition/getOffset routines.
 */
/*
Element.implement({
	followMe : function(offset){
		offset = offset || 0;
		var pos = this.getCoordinates();
		this.inject($(document.body)).setStyles({
			position:'absolute',
			margin:0,
			top:pos.top,
			left:pos.left
		}).set('morph',{
				link:'cancel',
				transition:Fx.Transitions.linear,
				duration:100
		});
		$(document.body).addEvent('mousemove',function(e){
			this.store('page',{top:e.page.y+offset,left:e.page.x+offset});
		}.bind(this));
		var goToMouse = function(){
			var page = this.retrieve('page');
			if(page){
				this.get('morph').start(page);
				this.store('page',false);
			}
		}
		goToMouse.periodical(100,this);
		return this;
	},
	stopFollowing : function()
	{
		$clear(this.retrieve('period'));
		$(document.body).removeEvents('mousemove');
	}
}); 
*/
Fx.Scroll2 = new Class({
 
    'Extends': Fx.Scroll,
    'styleString': Element.getComputedStyle,
    'styleNumber': function(element, style) {
        return this.styleString(element, style).toInt() || 0;
    },
    'borderBox': function(element) {
        return this.styleString(element, '-moz-box-sizing') == 'border-box';
    },
    'topBorder': function(element) {
        return this.styleNumber(element, 'border-top-width');
    },
    'leftBorder': function(element) {
        return this.styleNumber(element, 'border-left-width');
    },
    'isBody': function(element) {
        return (/^(?:body|html)$/i).test(element.tagName);
    }, 
    'toElement': function(el) {
        var offset   = {x: 0, y: 0};
        var element  = $(el);
       
        if (this.isBody(element)) {
            return offset;
        }
        var scroll = element.getScrolls();
               
        while (element && !this.isBody(element)){
            offset.x += element.offsetLeft;
            offset.y += element.offsetTop;
           
            if (Browser.Engine.gecko){
                if (!this.borderBox(element)){
                    offset.x += this.leftBorder(element);
                    offset.y += this.topBorder(element);
                }
                var parent = element.parentNode;
                if (parent && this.styleString(parent, 'overflow') != 'visible'){
                    offset.x += this.leftBorder(parent);
                    offset.y += this.topBorder(parent);
                }
            } else if (Browser.Engine.trident || Browser.Engine.webkit){
                offset.x += this.leftBorder(element);
                offset.y += this.topBorder(element);
            }
 
            element = element.offsetParent;
            if (Browser.Engine.trident) {
                while (element && !element.currentStyle.hasLayout) {
                    element = element.offsetParent;
                }
            }
        }
        if (Browser.Engine.gecko && !this.borderBox(element)){
            offset.x -= this.leftBorder(element);
            offset.y -= this.topBorder(element);
        }
       
        var relative = this.element;
        var relativePosition = (relative && (relative = $(relative))) ? relative.getPosition() : {x: 0, y: 0};
        var position = {x: offset.x - scroll.x, y: offset.y - scroll.y};
       
        return this.start(position.x - relativePosition.x, position.y - relativePosition.y);
    }
});

var fmcScrollTo = new Class({
	Implements: Options,
	options: {
		container: document.body,
		slides: [],
		startIndex: 0,
		wrap: true,
		duration:1500,
		onShow: Class.empty //Mootools 1.2: $empty
	},
	initialize: function(options){
		//this.options = 
		this.setOptions(options);
		this.startIndex = this.options.startIndex;
		this.scroll = new Fx.Scroll2(this.options.container, {
			wait: false,
			duration: this.options.duration,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Expo.easeOut
		});
	},
	scrollToEl: function(itemToScrollTo)
	{
		this.startIndex = itemToScrollTo;
		this.scroll.toElement(this.options.slides[itemToScrollTo]);
	}
});

var fadeImages = {
	actual:0,
	prev:false,
	on:false,
	init:function(options)
	{
		this.on=true;
		this.options = options;
		this.elms = options.elements;
		this.container = options.container;
		this.elms.each(function(el, index)
		{
			el.setStyles({'position':'absolute', opacity:0});
		});
		this.newImage = this.elms[this.actual]; 
		this.oldImage = this.elms[this.prev];
		this.fadeInOut();
		this.timer = this.fadeInOut.periodical(7000, this);
	}
	, fadeInOut:function(direction)
	{
		this.newImage.set('tween', {duration: 'long'});
		this.newImage.tween('opacity', 1);
		if(this.oldImage)
		{
			this.oldImage.set('tween', {duration: 'long'});
			this.oldImage.fade(0);
		}
		this.prev = this.actual;
		/*
		if(direction)
		{
			this.actual+=direction;
		}else{
		}
		*/
		this.actual++;
		if(this.actual == this.elms.length)
		{
			this.actual = 0;
		}
		this.newImage = this.elms[this.actual];
		this.oldImage = this.elms[this.prev];
	}
};


var MakeScrollbar = new Class({
	Implements: Options,
	content:null,
	ignoreMouse:false,
	id:'',
	top:100,
	scrollbar:null,
	handle:null,
	horizontal:false,
	slider:null,
	offsetX:0,
	initialize:function(options){
		if(options.content) { this.content = options.content; } else { return false; }
		if(options.ignoreMouse) this.ignoreMouse = options.ignoreMouse;
		(options.id) ? this.id = options.id : this.id = 'scrollbar_' + (100 * random());
		if(options.top) this.top = options.top;
		if(options.offsetX) this.offsetX = options.offsetX;
		var that = this;
		this.scrollbar = new Element('div',{id:this.id, 'class':'scrollbar-vert'}).inject(this.content, 'after');
		this.content.setStyles({ overflow:'hidden'});//width:(content.getSize().x-15),
		this.scrollbar.setStyles(
		{
			top:this.top,
			left:(this.content.getParent().getStyle('position')=='absolute' ||this.content.getParent().getStyle('position')=='relative') ? this.content.getSize().x+this.offsetX : this.content.getPosition().x+ this.content.getSize().x+this.offsetX,
			height:this.content.getSize().y,
			position:'absolute'
		});
		//alert(this.content.getParent().getStyle('position'));
		this.handle = new Element('div',{'class':'handle-vert'}).inject(this.scrollbar);
		this.steps = this.countSteps();
		this.slider = new Slider(this.scrollbar, this.handle, {	
			steps: this.steps,
			mode: (this.horizontal?'horizontal':'vertical'),
			onChange: function(step){
				//alert(step + '- ' +that.steps);
				// Scrolls the content element in x or y direction.
				var x = (that.horizontal?step:0);
				var y = (that.horizontal?0:step);
				that.content.scrollTo(x,y);
			}
		}).set(0);
		if( !(this.ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the 
			// content or the scrollbar element.
			$$(this.content, this.scrollbar).addEvent('mousewheel', function(e){
				e = new Event(e).stop();
				//alert(that.slider.stepSize+ ' - ' +that.slider.stepWidth);
				var st = that.slider.step - (e.wheel * 30);
				//alert(st + ' -> '+ that.slider.step+ ' - ' + e.wheel + ' Steps : '+ that.slider.steps);
				that.slider.set(st);
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		//$(document.body).addEvent('mouseleave',function(){that.slider.drag.stop()});
	},
	countSteps:function()
	{
		var nb = (this.horizontal?(this.content.getScrollSize().x - this.content.getSize().x):(this.content.getScrollSize().y - this.content.getSize().y))
		return nb;
	},
	changeSteps:function()
	{
		this.slider.steps = this.countSteps();
		this.slider.max = this.slider.steps;
		this.slider.range = this.slider.steps;
		this.slider.stepSize = Math.abs(this.slider.range) / this.slider.steps;
		this.slider.stepWidth = this.slider.stepSize * this.slider.full / Math.abs(this.slider.range) ;

		if(this.countSteps() <= 0)
		{
			this.slider.set(0)
			this.scrollbar.setStyle('visibility','hidden');
		}else{
			this.scrollbar.setStyle('visibility','visible');
		}
	}
});

var myConfirm = {
	overlay:null,
	init:function()
	{
		var that = this;
		this.overlay = new Element("div", {'id': "myConfirmOverlay"});
		this.c = new Element("div", {'class': 'myConfirm'});
		this.ok = new Element('a', {'class':'myConfirm-ok', 'href':'#'}).set('text','OK').addEvent('click', function(e){ 
				if(e) e.stop();
				if(that.onConfirm) that.onConfirm();
				that.hide();
			});
		this.cancel = new Element('a', {'class':'myConfirm-cancel', 'href':'#'}).set('text','Annuler').addEvent('click', function(e){ 
				if(e) e.stop();
				if(that.onCancel) that.onCancel();
				that.hide();
			});
		this.message = new Element('p');
		this.c.adopt($$([this.message, this.ok, this.cancel]));
		this.overlay.adopt(this.c);
		$(document.body).adopt(this.overlay);
	},
	show:function(message, options)
	{
		if(options && message)
		{
			if(!this.overlay) this.init();
			this.message.set('html', message);
			if(options.onConfirm) this.onConfirm = options.onConfirm;
			if(options.onCancel) this.onCancel = options.onCancel;
			if(options.okText) this.ok.set('text', options.okText);
			if(options.cancelText) this.ok.set('text', options.cancelText);
			this.overlay.show();
			this.pos();
			this.shown = true;
		}else{
			return;
		}
	},
	hide:function()
	{
		this.overlay.hide();
		this.onCancel = null;
		this.onConfirm = null;
		this.shown = false;
	}
	,pos: function()
	{
		var scSize = $(document.body).getSize();
		var cSize = this.c.getSize();
		this.c.setStyle('top',(scSize.y/2) - (cSize.y/2));
	}
}

var loading = {
	overlay:null,
	shown:false,
	init:function(where)
	{
		if(!$(where)) where = $(document.body);
		if(!this.overlay){
			this.overlay = new Element('div', {'id':'loadingC'}).inject(where);
			var t = new Element('div').set('text', 'chargement en cours').inject(this.overlay);
		}
		/*
		this.l = new Element('div', {'id':'loaderFlash'}).inject(document.body);
		var sc = new Swiff(mainPath+"flash/cursor.swf", {id: 'cursorSwiff', width:26, height: 26, params: { 'wmode':'transparent','AllowScriptAccess':'always' }}).inject(this.l);
		*/
	},
	show:function(where)
	{
		if(!this.overlay) this.init(where);
		this.overlay.show();
		//this.l.followMe(15);
		//this.l.show();
		this.shown = true;
	},
	hide:function()
	{
		this.overlay.hide();
		/*
		this.l.stopFollowing();
		this.l.hide();
		*/
		this.shown = false;
	}
};
