////////////////////////////////////////////////////////////////////////////////
//                            INITS                                           //
////////////////////////////////////////////////////////////////////////////////
var window_h = null;
var window_w = null;
var menu_dessus = new Array();
var menu_dessous = new Array();

var horizontalCurtains = false;
var background = null;

var xm = null;
var ym = null;


initElements = function(){

	slider.init();

    Element.hide('contenu');
    //Element.hide('actualite');
    Element.hide('navigation_langue');
    //Element.hide('mentions_legales');
    Element.hide('fond');
    Element.hide('logo');
    
    logo_top = 87;
    
    for (i=1 ; i<6 ; i++){
        if (document.getElementById("menu_desssus_"+i)) {
            menu_dessus[i] = 127-Element.getStyle("menu_desssus_"+i, 'top');
            Element.hide("menu_desssus_"+i);
            Element.setStyle("menu_desssus_"+i, {top: px(logo_top)});
        }
        if (document.getElementById("menu_desssous_"+i)) {
            menu_dessous[i] = Element.getStyle("menu_desssous_"+i, px(logo_top+3));
            Element.hide("menu_desssous_"+i);
            Element.setStyle("menu_desssous_"+i, {top: px(logo_top+2)});
        }
    }
}

onload = function(){
    correctPNG();
    
   // new Effect.Appear('logo', {delay:0, duration: 0.7 } );
   // new Effect.Appear('fond', {delay:0.3, duration: 0.7 } );
   // window.setTimeout("showMenu()", 650);
    new Effect.Appear('contenu', {delay:0.95, duration: 0.5 } );
    window.setTimeout("Element.show('navigation_langue'); Element.show('actualite');  Element.show('mentions_legales');", 1700);
}


showMenu = function(){
    for (i=5 ; i>0 ; i--){
        if (document.getElementById("menu_desssus_"+i)) {
            Element.show("menu_desssus_"+i);
            new Effect.Move("menu_desssus_"+i, {delay:(0), duration: 0.7, y:-(29*i), mode: 'relative'});
        }
        if (document.getElementById("menu_desssous_"+i)) {
            Element.show("menu_desssous_"+i);
            new Effect.Move("menu_desssous_"+i, {delay:(0), duration: 0.7, y:+(29*i), mode: 'relative'});
        }
        
    }
}


////////////////////////////////////////////////////////////////////////////////
//                            FONCTIONS   interaction souris                  //
////////////////////////////////////////////////////////////////////////////////


var slider = function() {

	/* ==== private methods ==== */

	function getElementsByClass(object, tag, className) {
		var o = object.getElementsByTagName(tag);
		for ( var i = 0, n = o.length, ret = []; i < n; i++) {
			if (o[i].className == className) ret.push(o[i]);
		}
		if (ret.length == 1) ret = ret[0];
		return ret;
	}

	function setOpacity (obj,o) {
		if (obj.filters) obj.filters.alpha.opacity = Math.round(o);
		else obj.style.opacity = o / 100;
	}


	/* ==== Slider Constructor ==== */

	function Slider(speed, ratio) {
		this.slides = [];
		this.buttons= [];
		this.over   = false;
		this.S      = this.S0 = speed;
		this.ratio  = ratio;
		this.presentation = document.getElementById("presentation");
		this.oc     = document.getElementById("slides");
		this.frm    = getElementsByClass(this.oc, 'div', 'slide');
		this.drm    = getElementsByClass(this.oc, 'img', 'diapo');
		this.brm    = getElementsByClass(document.getElementById("logos"), 'img', 'logo');
		this.trm    = getElementsByClass(document.getElementById("texts"), 'div', 'text');
		this.NF     = this.frm.length;
		this.ecart_h = 6.5;
		this.bande_h= (300-((this.NF-1)*this.ecart_h) )/this.NF;
		this.resize();
		for (var i = 0; i < this.NF; i++) {
			this.slides[i]  = new Slide(this, i);
			this.buttons[i] = new Button(this, i);
		}
		this.oc.parent = this;
		this.view      = this.slides[0];
		this.Z         = this.mx;

		this.oc.onmouseout = function () {
			this.parent.mouseout();
			return false;
		}
	}
	
	Slider.prototype = {
		run : function () {
			if (this.over) this.Z += (this.mn - this.Z) * .5 ;
			else this.Z += (this.mx - this.Z) * .5;
			this.view.calc();
			var i = this.NF;
			while (i--) this.slides[i].move();
		},
		resize : function () {
			
			this.wh = 300;//this.oc.clientHeight;
			this.ht = 480;//this.oc.clientWidth;     <<<<<<<<<< bug safari
			this.wr = this.wh * this.ratio;
			this.r  = this.ht / this.wr;
			this.mx = this.wh / this.NF;
			this.mn = (this.wh * (1 - this.ratio)) / (this.NF - 1);
		},
		mouseout : function () {
			this.over      = false;
		}
	}
	
	
	Button = function (parent, N) {
		this.parent = parent;
		this.N      = N;
		this.obj    = parent.brm[N];
		this.obj.parent = this;
		this.obj.onmouseover = function() {
			this.parent.over();
			return false;
		}
		this.obj.onmouseout = function() {
			this.parent.out();
			return false;
		}
	}
	
	Button.prototype = {
		over : function () {
			this.parent.slides[this.N].over();
		},
		out : function () {
			this.parent.over      = false;
			this.parent.view.text.style.visibility = 'hidden';
		}
	}
	
	Slide = function (parent, N) {
		this.parent = parent;
		this.N      = N;

		this.loaded = false;
		this.cpt    = 0;
		this.start  = new Date();
		this.obj    = parent.frm[N];
		this.diapo  = parent.drm[N];
		this.delta = parseInt(this.diapo.getAttribute('name'));
		this.button = parent.brm[N];
		this.text   = parent.trm[N];
		
		this.x0     = this.x1 = N * parent.mx;
		this.hx0 	= parent.bande_h;
		this.hx1	= 300;
		this.dx0 	= this.delta;
		this.dx1	= 0;
		this.a0 	= 100;
		this.a1		= 0;
		this.v      = 0;
		
		//alert((N+1)*( (parent.bande_h-25)/2 ))
		this.button.style.top = px( (parent.bande_h-25)/2 + N*(parent.bande_h+parent.ecart_h-1.5) ) ;
		//alert(this.button.style.top)
		
		
		this.txt    = getElementsByClass(this.obj, 'div', 'text');
		this.img    = getElementsByClass(this.obj, 'img', 'diapo');
		this.obj.style.top = px( Math.floor(this.x0) );
		this.diapo.style.top = px( this.dx0 );
	}
	Slide.prototype = {
		calc : function() {
			var that = this.parent;
			for (var i = 0 ; i <= this.N; i++) {
				that.slides[i].x1 = i * that.Z;
			}
			for (var i = this.N + 1; i < that.NF; i++) {
				that.slides[i].x1 = that.wh - (that.NF - i) * that.Z;
			}
			for (var i = 0; i < that.NF; i++) {
				
				if (this.N==i && that.over) {
					that.slides[i].dx1 = 0;
						that.slides[i].hx1 = 300;
				}
				else {
					that.slides[i].dx1 = that.slides[i].delta;
					that.slides[i].hx1 = that.bande_h;
				}			
				if (this.N!=i && that.over) that.slides[i].a1 = 0;
				else that.slides[i].a1 = 100;
				
			}
		},
		move : function() {
			var that = this.parent;
			var s = (this.x1 - this.x0) / that.S;
			var sd = (this.dx1-this.dx0) / that.S;
			var hd = (this.hx1-this.hx0) / that.S;
			var sa = (this.a1-this.a0) / that.S;
			
			this.obj.style.top = px( Math.floor(this.x0 += s) );
			this.diapo.style.top = px( Math.floor(this.dx0 += sd) );
			this.obj.style.height = px( Math.floor(this.hx0 += hd) );
			setOpacity(this.button, Math.floor(this.a0 += sa));
			
			if (this.parent.view==this && !that.over && Math.abs(s) < .5) {
				this.parent.presentation.style.visibility = 'visible';
			}
			
			if (!this.loaded) {
				if (this.img.complete) {
					//this.img.style.visibility = 'visible';
					this.loaded = true;
				}
			}
		},
		over : function () {
			this.parent.presentation.style.visibility = 'hidden';
			this.text.style.visibility = 'visible';
			this.parent.resize();
			this.parent.over = true;
			this.parent.view = this;
			this.start = new Date();
			this.cpt = 0;
			this.pro = false;
			this.calc();
			
		}
	}
	
	return {
		init : function() {
			//name, speed (2 - 20), ratio
			this.s1 = new Slider(12, 1);
			setInterval("slider.s1.run();", 16);
		}
	}
}();

function px(x){
        return ''.concat(Math.round(x), 'px');
}

