
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
		});
		if(options.onShow) this.onShow = options.onShow;
	},
	scrollToEl: function(itemToScrollTo)
	{
		this.startIndex = itemToScrollTo;
		this.scroll.toElement(this.options.slides[itemToScrollTo]);
		if(this.onShow){
			this.onShow();
		}
	}
});

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(6000, this);
	}, fadeInOut:function()
	{
		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;
		this.actual++;
		if(this.actual ==this.elms.length)
		{
			this.actual = 0;
		}
		this.newImage = this.elms[this.actual];
		this.oldImage = this.elms[this.prev];
	}
};

var actus = {
	size:{
		height:252,
		width:694,
		descW:220,
		photoW:445
	},
	elements:null,
	container:null,
	right:null,
	left:null,
	active:null,
	transitionTime:7000,
	displayTime:1,
	timer:null,
	init:function(options)
	{
		this.wrapper = options.wrapper;
		var that = this;
		this.c = options.container;
		this.c.addClass('scripted');
		this.e = options.elements;
		this.right = options.rightBt;
		this.left = options.leftBt;
		this.imgClass=options.imgClass;
		this.descClass=options.descClass;
		this.left.addEvent('click',function(e){
			if(e) e.stop();
			if(that.active==0)
			{
				that.change(that.e.length-1);
			}else{
				that.change(that.active-1);
			}
		});
		this.right.addEvent('click',function(e){
			if(e) e.stop();
			if(that.active==that.e.length-1)
			{
				that.change(0);
			}else{
				that.change(that.active+1);
			}
		});
		this.timer = this.autoChange.periodical(this.transitionTime, this);
		this.e.each(function(el,index)
		{
			var img = el.getChildren(that.imgClass)[0];
			var desc = el.getChildren(that.descClass)[0];
			desc.addEvent('mouseenter',function()
			{
				$clear(that.timer);
			});
			desc.addEvent('mouseleave',function()
			{
				//that.right.fireEvent('click','',7000);
				that.timer = that.autoChange.periodical(that.transitionTime, that);
			});
			el.setStyle('display','none');
			img.set('opacity',0);
			desc.setStyles({'position':'absolute', 'bottom':-that.size.height});
		});
		this.change(0);
	},
	autoChange:function()
	{
		this.right.fireEvent('click');
	},
	change:function(id)
	{
		var that = this;
		if(!this.stuck)
		{
			this.e[id].setStyle('display','block');
			this.stuck = true;
			var newPic = this.e[id].getChildren(this.imgClass)[0];
			var newDesc = this.e[id].getChildren(this.descClass)[0];
			newPic.setStyle('display','block');
			newPic.tween('opacity', 1);
			if(this.active!= null)
			{
				var oldPic = this.e[this.active].getChildren(this.imgClass)[0];
				var oldDesc = this.e[this.active].getChildren(this.descClass)[0];
				oldPic.tween('opacity', 0);
				oldDesc.tween('bottom', -this.size.height);
			}
			var tw = new Fx.Tween(newDesc).start('bottom', 0).chain(function()
			{
				if(that.active!= null){
					oldDesc.setStyle('bottom',-that.size.height);
					//oldPic.setStyle('display','none');
					that.e[that.active].setStyle('display','none');
				}
				that.active = id;
				that.stuck = false;
			});
		}
	}
};

function checkBTNS(startIndex, steps, stepsLength, el)
{
	var minus = false;
	var plus = false;
	//alert(Math.ceil((startIndex+stepsLength) / stepsLength));
	if(Math.ceil((startIndex+stepsLength) / stepsLength) == steps)
	{
		plus = false;
		el.getElements('.moveRight')[0].addClass('disabled');
	}else{
		plus = true;
		el.getElements('.moveRight')[0].removeClass('disabled');
	}
	if(startIndex == 0)
	{
		el.getElements('.moveLeft')[0].addClass('disabled');
	}else{
		el.getElements('.moveLeft')[0].removeClass('disabled');
	}
}

var Edito = {
	showPopup:true,
	init:function()
	{
		var that = this;
		this.overlay = new Element('div', {id:'editoOverlay'}).inject(document.body);
		this.edito = new Element('div', {id:'editoContainer'}).inject(this.overlay, 'after'); 
		this.overlay.setStyle('display','none');
		this.overlay.set('opacity',0);
		this.edito.setStyle('display','none');
		this.edito.set('opacity',0);
		Cookie.write('showPopup99',0,{'duration':30, 'domain':'laregion2010.fr', path:'/'});
		that.showEdito();
	},
	showEdito:function()
	{
		var that = this;
		this.overlay.setStyle('display','block');
		this.edito.setStyle('display','block');
		var tw = new Fx.Tween(this.overlay).start('opacity',1).chain(
			function()
			{
				var f = new Fx.Tween(that.edito, {
					duration:800,
					transition:Fx.Transitions.Linear,
					onComplete: function(){
						
					}
				}).start('opacity',1);
			}
		);
		this.template = this.loadPage(mainPath+'EditoTemplate.html', {
			onComplete:function(r)
			{
				//$('container').adopt(r); //r.inject('footer','after');
				that.edito.adopt(r);
				that.editoContent = $('editoContenu');
				//that.content = that.loadPage(mainPath + 'EditoContenu.html', {
				//that.content = that.loadPage('http://www.laregion2010.fr/index.php?id=201', {
				that.content = that.loadPage('http://www.laregion2010.fr/index.php?id=238', {
					onComplete:function(c)
					{
						that.editoContent.adopt(c);
						that.closeBt = $('editoFermer').addEvent('click', function(e)
						{
							if(e) e.stop();
							that.hideEdito();
						});
						that.dontShowBt = $('editoDontShowAgain').addEvent('click', function(e)
						{
							if(e) e.stop();
							that.toggleShowOrNot(this);
						});
						that.checkEditoDontShowAgainStatus();
						//videoEdito.inject($('editoVideo'));
						//that.addTheFlash.delay(1500,that);
					}
				});
			}
		});
	},
	addTheFlash:function()
	{
		var videoEdito = new Swiff(mainPath+"flash/v_player.swf", { id: 'EditoVideoPlayer', width:'643', height: 270, params: { 'wmode': 'transparent','AllowScriptAccess':'always' }, vars: { movieUrl:"EditoRCouderc.flv"} });
		$('editoVideo').adopt(videoEdito);
	},
	hideEdito:function()
	{
		var that = this;
		this.edito.setStyle('height',350);
		this.edito.set('html','');
		var tw = new Fx.Tween(this.edito).start('opacity',0).chain(
			function()
			{
				that.edito.setStyle('display','none');
				var f = new Fx.Tween(that.overlay, {
					duration:800,
					transition:Fx.Transitions.Linear,
					onComplete: function(){
						that.overlay.setStyle('display','none');
					}
				}).start('opacity',0);
			}
		);
	},
	toggleShowOrNot:function(el)
	{
		if(this.showPopup)
		{
			Cookie.write('showPopup99',0,{'duration':30, 'domain':'laregion2010.fr', path:'/'});
		}else{
			Cookie.write('showPopup99',1,{'duration':30, 'domain':'laregion2010.fr', path:'/'});
		}
		this.checkEditoDontShowAgainStatus();
	},
	checkEditoDontShowAgainStatus:function()
	{
		if(cook = Cookie.read('showPopup99'))
		{
			if(cook == 1)
			{
				this.showPopup = true;
			}else if(cook==0){
				this.showPopup = false;
			}
		}
		if(this.showPopup)
		{
			this.dontShowBt.removeClass('dontShow');
		}else{
			this.dontShowBt.addClass('dontShow');
		}
	},
	loadPage:function(url, options)
	{
		var myRequest = new Request.HTML(
		{
			method: 'get', url:url, encoding:'iso-8859-1',
			onSuccess:function(html)
			{
				if(options.onComplete)
				{
					options.onComplete(html);
				}
				if(options.target)
				{
					options.target.set('html', html);
				}
			},
			onFailure : function(t)
			{
				if(options.target)
				{
					options.target.set('html', '<h3>Impossible de charger le formulaire !');
				}
			}
		});
		myRequest.send();
	}
}
