window.addEvent('load', function(){
	
	// Tool Tips
	var myEffects = new Fx.Styles($('Disclaimer'), {duration: 200, transition: Fx.Transitions.linear}).set({ 'opacity': 0 });
	$('Disclaimer').setStyles({'display':'block'});
	$('DisclaimerBt').addEvents({
		'mouseenter': function(){
			myEffects.start({ 'opacity': 1 });
		},
		'mouseleave': function(){
			myEffects.start({ 'opacity': 0 });
		}
	});
	

	
	var mouvement;
		
		try {
			$('Array_right').addEvents({
				'click': function(event) {
					moveRight();
				},
				'mouseenter': function(event) {
					//mouvement = moveRight.periodical(650);
						this.setStyles({'background': 'url(images/array_white.gif) no-repeat center center', 'background-color': "#fea520", 'border-color': "#fea520"});
				},
				'mouseleave': function(event) {
					//$clear(mouvement);
					this.setStyles({'background': 'url(images/array_white.gif) no-repeat center center', 'background-color': "#6b6251", 'border-color': "#999"});
				}
			})

			$('Array_left').addEvents({
				'click': function(event) {
					moveLeft();
				},
				'mouseenter': function() {
					//mouvement = moveLeft.periodical(650);
						this.setStyles({'background': 'url(images/array_white_2.gif) no-repeat center center', 'background-color': "#fea520", 'border-color': "#fea520"});
				},
				'mouseleave': function(event) {
					//$clear(mouvement);
					this.setStyles({'background': 'url(images/array_white_2.gif) no-repeat center center', 'background-color': "#6b6251", 'border-color': "#999"});
				}
			})
		} catch(e){}
});

function Highlight(id){
	$(id).setStyles({'color': '#fea520'});
	$$('.linkj').each( function(linkj) {
		if(linkj.id!=id)
			linkj.setStyles({'color': '#6b6251'});
	});
}

function StripSlashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
function sendForm(idform) {
	var frm;
	frm = document.getElementById(idform);
	frm.submit();
}
function moveRight(){
	if (Position_Galery<5){
		Position_Galery = NbProjet-(5-Position_Galery);
	} else {
		Position_Galery = Position_Galery-5;
	}
	$$('.Projet').each( function(projet) {
		var pos = projet.getProperty('pos');
		
			var posfin = (pos.toInt()-6)*181;
			var moveBg = new Fx.Styles(projet, {duration: 600, wait: false, transition: Fx.Transitions.Quad.easeInOut,
				onComplete: function() {
							if (pos<=5){
								projet.setStyles({'position': 'absolute', 'top': '0px', 'left': ((NbProjet.toInt()-1)-(5-pos.toInt()))*181 +'px'});
								projet.setProperty('pos', NbProjet.toInt()+(pos.toInt()-5));
							} else {
								projet.setProperty('pos', pos.toInt()-5);
							}
						}
					});
			moveBg.start({'left':  posfin, 'top': 0});
	});
}

function moveLeft(){

		if ((Position_Galery+5)>NbProjet){
			Position_Galery = (Position_Galery+5)-NbProjet;
		} else {
			Position_Galery = Position_Galery+5;
		}
	
	$$('.Projet').each( function(projet) {
		
		var pos = projet.getProperty('pos');
		if (pos>(NbProjet.toInt()-5)){
			projet.setStyles({'position': 'absolute', 'top': '0px', 'left': (pos.toInt()-(NbProjet.toInt()+1))*181 +'px'});
			var posLeftfin = (pos.toInt()-(NbProjet.toInt()-4))*181;
			//alert("pos= "+pos+"pos fin= "+(pos.toInt()-(NbProjet.toInt()+1))*181);
		} else {
			var posLeftfin = (pos.toInt()+4)*181;
			//alert("pos= "+pos+"pos fin= "+posLeftfin);
		}
		//alert("pos = "+pos+" posfin = "+posLeftfin);
		var moveBg = new Fx.Styles(projet, {duration: 600, wait: false, transition: Fx.Transitions.Quad.easeInOut,
			onComplete: function() {
							if (pos<=(NbProjet.toInt()-5)){
								projet.setProperty('pos', pos.toInt()+5);
							} else {
								projet.setProperty('pos', (pos.toInt()-(NbProjet.toInt()-5)));
							}
							//alert(projet.getProperty('pos'))	
					}
				});
		moveBg.start({'left':  posLeftfin, 'top': 0});
	});
}

function gotoprojet(id){
		window.location = 'projet.php?id='+id+'&pos='+Position_Galery+'&order='+order;
}

function openBrWindow(theURL,winName) {
	  window.open(theURL,winName,'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
}


function InitalizeTrier(){
	//$('TrierMenu').empty();

	json_HomepageContent.classement.each( function(tri) {
		
				/*var TriLink = new Element('a').setHTML(tri.nom).injectInside($('TrierMenu'));
				
				TriLink.addEvents({
					'click': function(e) {
						TrierProjet(tri.nom,tri.id);
					},
					'mouseenter': function() {
						showtri2(tri.nom,this,tri.content);
					},
					'mouseleave': function() {
						//hidetri2();
					}
				});*/
				
				tri.content.each( function(ctri) {
					if (order==-1){
						cOrder = 'date';
					} else if (order==-2){
						cOrder = 'date_asc';
					} else {
						cOrder = order;
					}
					/*if (ctri.id==cOrder){
						$('TrierNom').setHTML(tri.nom);
					}*/
				});
	});
	
}


var delay;
function showTrierMenu(){
	$('TrierMenu').setStyles({'display': 'block'});
}

function hideTrierMenu(){
//	$clear(delay);
	$('TrierMenu').setStyles({'display': 'none'});
	$('TrierMenu').removeEvents();
	hidetri2();
}
function showtri2(nom, obj, content){
	$('TrierMenu2').empty();
	$('TrierMenu2').setStyles({
								'display': 'block',
								'margin-top': obj.getCoordinates().top - HAUTEUR_TRI +'px'
								});
								//alert($('TrierMenu').getCoordinates().left);
	content.each( function(contenu) {
		
				var TriLink = new Element('a').setHTML(contenu.nom).injectInside($('TrierMenu2'));
				
				TriLink.addEvents({
					'click': function(e) {
						TrierProjet(nom,contenu.id);
					},
					'mouseenter': function() {
						//showtri2(this,tri.content);
					},
					'mouseleave': function() {
						//hidetri2();
					}
				});

	});
}
function hidetri2(){
	$('TrierMenu2').empty();
	$('TrierMenu2').setStyles({'display': 'none'});
}
function TrierProjet(nom,id){
	order = id;
	var TopFrame = $('TopFrame');
	TopFrame.empty();
	//$('TrierNom').setHTML(nom);
	//$('TrierBt').setStyles({'color': '#666'});
	//$$('.Projet').removeEvents('mouseenter');
	//hideTrierMenu();
	var Target = $('Galery_Projet');
	var moveGallery = new Fx.Styles(Target, {duration: 750, wait: false, transition: Fx.Transitions.Back.easeInOut,
		onComplete: function() {
						TrierThumbs(id);
						// InitializeText();	
				}
	});
	moveGallery.start({'left':  -NbProjet*181, 'top': 247});
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){

	var slider = new Slider(scrollbar, handle, {
		steps: 100,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?(((content.getScrollSize().x-content.getSize().x)/100)*step):0);
			var y = (horizontal?0:(((content.getSize().scrollSize.y-content.getSize().size.y)/100)*step));
			content.scrollTo(x,y);
			}
			}).set(0);
			if( !(ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the
			// content or the scrollbar element.
			$$(content, scrollbar).addEvent('mousewheel', function(e){
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 10;
			slider.set(step);
		});
		}
			// Stops the handle dragging process when the mouse leaves the document body.
			$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

var ScrollBar = new Class({

		Implements: [Events, Options],

		options: {
			maxThumbSize: 10,
			wheel: 8
		},

		initialize: function(content, track, thumb, options){
			this.setOptions(options);

			this.content = $(content);
			this.track = $(track);
			this.thumb = $(thumb);

			this.bound = {
				'start': this.start.bind(this),
				'end': this.end.bind(this),
				'drag': this.drag.bind(this),
				'wheel': this.wheel.bind(this),
				'page': this.page.bind(this)
			};

			this.position = {};
			this.mouse = {};
			this.update();
			this.attach();
		},

		update: function(){

			this.contentSize = this.content.offsetHeight;
			this.contentScrollSize = this.content.scrollHeight;
			this.trackSize = this.track.offsetHeight;

			this.contentRatio = this.contentSize / this.contentScrollSize;

			this.thumbSize = (this.trackSize * this.contentRatio).limit(this.options.maxThumbSize, this.trackSize);

			this.scrollRatio = this.contentScrollSize / this.trackSize;

			this.thumb.setStyle('height', this.thumbSize);

			this.updateThumbFromContentScroll();
			this.updateContentFromThumbPosition();
		},

		updateContentFromThumbPosition: function(){
			this.content.scrollTop = this.position.now * this.scrollRatio;
		},

		updateThumbFromContentScroll: function(){
			this.position.now = (this.content.scrollTop / this.scrollRatio).limit(0, (this.trackSize - this.thumbSize));
			this.thumb.setStyle('top', this.position.now);
		},

		attach: function(){
			this.thumb.addEvent('mousedown', this.bound.start);
			if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
			this.track.addEvent('mouseup', this.bound.page);
		},

		wheel: function(event){
			this.content.scrollTop -= event.wheel * this.options.wheel;
			this.updateThumbFromContentScroll();
			event.stop();
		},

		page: function(event){
			if (event.page.y > this.thumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
			else this.content.scrollTop -= this.content.offsetHeight;
			this.updateThumbFromContentScroll();
			event.stop();
		},

		start: function(event){
			this.mouse.start = event.page.y;
			this.position.start = this.thumb.getStyle('top').toInt();
			document.addEvent('mousemove', this.bound.drag);
			document.addEvent('mouseup', this.bound.end);
			this.thumb.addEvent('mouseup', this.bound.end);
			event.stop();
		},

		end: function(event){
			document.removeEvent('mousemove', this.bound.drag);
			document.removeEvent('mouseup', this.bound.end);
			this.thumb.removeEvent('mouseup', this.bound.end);
			event.stop();
		},

		drag: function(event){
			this.mouse.now = event.page.y;
			this.position.now = (this.position.start + (this.mouse.now - this.mouse.start)).limit(0, (this.trackSize - this.thumbSize));
			this.updateContentFromThumbPosition();
			this.updateThumbFromContentScroll();
			event.stop();
		}

	});





function MM_preloadImages() {
  var d=document;
	if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

