window.addEvent('domready', function() {
	
	if ($$('.text')!=null){
		for(i=0 ; i<$$('.text').length ; i++){
			if ($$('.text')[i].getSize().y > 20){
				$$('.text')[i].getParent().getParent().getParent().getChildren()[2].setStyle('cursor', 'pointer');
				$$('.text')[i].getParent().getParent().getParent().getChildren()[2].getChildren()[0].innerHTML = 'Dérouler';
			}
		}
	}
	
	$$('.removable').addEvents({
		'mouseenter': function(){
			var id=this.get('id').split('-')[1];
			
		},

		'mouseleave': function(){
			
		}
	});
	
	$$('.notPub').addEvents({
		'mouseenter': function(){
			this.set('opacity', '.50');
			this.setStyle('background-color', '#353535');
			this.setStyle('border', '1px solid black');
			this.set('tween', {
			duration: 1000,
			transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			}).tween('width', '85px');
		},

		'mouseleave': function(){
			/*var bulle = $('bulle');
			bulle.destroy();*/
			this.set('tween', {}).tween('width', '40px');
			this.setStyle('background-color', 'transparent');
			this.set('opacity', '1');
			this.setStyle('border', 'none');
		}
	});
	$$('.pub').addEvents({
		'mouseenter': function(){
			this.set('opacity', '.50');
			this.setStyle('background-color', '#353535');
			this.setStyle('border', '1px solid black');
			this.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
			}).tween('width', '85px');
		},

		'mouseleave': function(){
			/*var bulle = $('bulle');
			bulle.destroy();*/
			this.set('tween', {}).tween('width', '40px');
			this.setStyle('background-color', 'transparent');
			this.set('opacity', '1');
			this.setStyle('border', 'none');
		}
	});
	$$('.explicite').addEvents({
		'mouseenter': function(event){
			if ($('bulle')==null){
				var id = '';
				if (this.get('id')!=null)
				{
					id = this.get('id');
				}
				title=this.title;
				this.title="";
				var offset = 0;
				var bulle = new Element('div', {'class': 'bulle', 'id': 'bulle'});
				var bulleLeft = new Element('div', {'class': 'bulle-left'});
				var bulleCenter = new Element('div', {'class': 'bulle-center'});
				var bulleRight = new Element('div', {'class': 'bulle-right'});
				var div = new Element('div', {'html':title});
				bulleLeft.inject(bulle);
				div.inject(bulleCenter);
				bulleCenter.inject(bulle);
				bulleRight.inject(bulle);
				bulle.inject($('content'), 'top');
				(id.split('-')[0]=='suppr') ? offset = 75 : offset = 20;
				$('bulle').setStyles({
				    top: (this.getPosition().y-26)+'px'/*event.page.y-45+'px'*/,
				    left: (this.getPosition().x)-offset/*event.page.x-5+'px'*/,
				    opacity: '0'
				});
				$('bulle').set('tween', {
					duration: 500,
					transition: Fx.Transitions.linear // This could have been also 'bounce:out'
				}).tween('opacity', '1');
			}
		}
	});
	$$('.expliciteHor').addEvents({
		'mouseenter': function(event){
			if ($('bulle')==null){
				title=this.title;
				this.title="";
				var bulle = new Element('div', {'class': 'bulle', 'id': 'bulle'});
				var bulleLeft = new Element('div', {'class': 'bulle-left'});
				var bulleCenter = new Element('div', {'class': 'bulle-center'});
				var bulleRight = new Element('div', {'class': 'bulle-right'});
				var div = new Element('div', {'html':title});
				bulleLeft.inject(bulle);
				div.inject(bulleCenter);
				bulleCenter.inject(bulle);
				bulleRight.inject(bulle);
				bulle.inject($('content'), 'top');
				$('bulle').setStyles({
				    top: (this.getPosition().y-26)+'px'/*event.page.y-45+'px'*/,
				    left: (this.getPosition().x-bulle.getSize().x)+'px'/*event.page.x-5+'px'*/,
				    opacity: '0'
				});
				$('bulle').set('tween', {
					duration: 500,
					transition: Fx.Transitions.linear // This could have been also 'bounce:out'
				}).tween('opacity', '1');
			}
		}
	});
	$$('.explicite').addEvents({
		'mouseleave': function(){
			if ($('bulle')!=null){
				this.title=title;
				$('bulle').destroy();
			}
		}
	});
	$$('.expliciteHor').addEvents({
		'mouseleave': function(){
			if ($('bulle')!=null){
				this.title=title;
				$('bulle').destroy();
			}
		}
	});
	$$('.boxBottom').addEvents({
		'click': function(event){
			var div = this.getParent().getChildren()[1].getChildren()[0].getChildren()[0];
			var taille = div.getSize().y;
			if (div.getParent().getSize().y>45){
				div.getParent().setStyle('overflow-y', 'hidden');
				div.getParent().set('tween', {}).tween('height', '45px');
				div.getParent().getParent().set('tween', {}).tween('height', '45px');
				div.getParent().getParent().getParent().getChildren()[2].getChildren()[0].innerHTML = 'Dérouler';
			}
			else if (taille > 45){
				$$('.boxCenter').set('tween', {}).tween('height', '45px');
				$$('.boxSec').set('tween', {}).tween('height', '45px');
				$$('.boxSec').setStyle('overflow-y', 'hidden');
				for(i=0 ; i<$$('.roll').length ; i++){
					if ($$('.roll')[i].innerHTML=='Enrouler'){
						$$('.roll')[i].innerHTML='Dérouler';
					}
				}
				if (taille > 200){
					div.getParent().setStyle('overflow-y', 'scroll');
					div.getParent().getParent().getParent().getChildren()[2].getChildren()[0].innerHTML = 'Enrouler';
					div.getParent().set('tween', {}).tween('height', '200px');
					div.getParent().getParent().set('tween', {}).tween('height', '200px');
				}
				else{
					div.getParent().set('tween', {}).tween('height', taille+'px');
					div.getParent().getParent().set('tween', {}).tween('height', taille+'px');
					div.getParent().getParent().getParent().getChildren()[2].getChildren()[0].innerHTML = 'Enrouler';
				}
			}
		}
	});
	$$('.photoElem').addEvents({
		'click': function(event){
			idGalerie = this.get('id').split('-')[2];
			getGalerie(idGalerie);
		}
	});
	if ($('a-prec')!=null)
	{
		$('a-prec').addEvents({
			'click': function(event){
				for(i=0 ; i<$$('.img-apercu').length ; i++) {
					$$('.img-apercu')[i].setStyle('display', 'none');
				}
				actuelle--;
				if (actuelle<0)
				{
					actuelle=$$('.img-apercu').length-1;
				}
				$$('.img-apercu')[actuelle].setStyle('display', 'inline');
				return false;
			}
		});
	}
	if ($('a-suiv')!=null)
	{
		$('a-suiv').addEvents({
			'click': function(event){
				for(i=0 ; i<$$('.img-apercu').length ; i++) {
					$$('.img-apercu')[i].setStyle('display', 'none');
				}
				actuelle++;
				if (actuelle>$$('.img-apercu').length-1)
				{
					actuelle=0;
				}
				$$('.img-apercu')[actuelle].setStyle('display', 'inline');
				return false;
			}
		});
	}
	if ($('apercu')!=null)
	{
		$('apercu').addEvents({
			'click': function(event){
				this.setStyle('display', 'none');
			}
		});
	}
});


function getGalerie(idGalerie){
	$('apercu').innerHTML='';
	//var aPrec = new Element ('a', {'id': 'a-prec', 'href': 'javascript:;'});
	var precArrow = new Element ('img', {'id': 'a-prec', 'src': '../maquillage/images/fleche_D.png', 'alt': '&lt;'});
	//aPrec.innerHTML = precArrow;
	precArrow.addEvents({
		'click': function(event){
			for(i=0 ; i<$$('.img-apercu').length ; i++) {
				$$('.img-apercu')[i].setStyle('display', 'none');
			}
			actuelle--;
			if (actuelle<0)
			{
				actuelle=$$('.img-apercu').length-1;
			}
			$$('.img-apercu')[actuelle].setStyle('display', 'inline');
			return false;
		}
	});
	//aPrec.inject($('apercu'));
	precArrow.inject($('apercu'));
	//var aSuiv = new Element ('a', {'id': 'a-suiv', 'href': 'javascript:;'});
	var suivArrow = new Element ('img', {'id': 'a-suiv', 'src': '../maquillage/images/fleche_R.png', 'alt': '&gt;'});
	//aSuiv.innerHTML = '&gt;';
	suivArrow.addEvents({
		'click': function(event){
			$('apercu').setStyle('width', $('outerWrapper').getSize().x/1.5 + 'px');
			$('apercu').setStyle('height', '400px');
			for(i=0 ; i<$$('.img-apercu').length ; i++) {
				$$('.img-apercu')[i].setStyle('display', 'none');
			}
			actuelle++;
			if (actuelle>$$('.img-apercu').length-1)
			{
				actuelle=0;
			}
			$$('.img-apercu')[actuelle].setStyle('display', 'inline');
			var imgActuelle = $$('.img-apercu')[actuelle];
			if (imgActuelle.getSize().x>$('apercu').getSize().x)
			{
				alert('plus large');
				$('apercu').setStyle('width', $('apercu').getSize().x+(imgActuelle.getSize().x-$('apercu').getSize().x) + 'px');
			}
			if (imgActuelle.getSize().y>$('apercu').getSize().y)
			{
				$('apercu').setStyle('height', $('apercu').getSize().y+(imgActuelle.getSize().y-$('apercu').getSize().y)+80 + 'px');
			}
			return false;
		}
	});
	suivArrow.inject($('apercu'));
	
	var request = new Request.JSON({
		method : 'post', url: '../ajax/controleurAjax.php',
		onSuccess:function(transport){
		var body = $$('body')[0];
	    $('apercu').setStyle('width', $('outerWrapper').getSize().x/1.5);
        $('apercu').setStyle('height', '400px');
        $('apercu').setStyle('top', body.getScroll().y+100);
        //alert($('apercu').getStyle('width'));
        $('apercu').setStyle('left', $('outerWrapper').offsetLeft+$('outerWrapper').offsetWidth/2-$('apercu').getStyle('width').toInt()/2);//+14+($('outerWrapper').getSize().x/1.5)/4);
		    for(i=0 ; i<transport.length ; i++) {
		        var photo =  transport[i];
		        //alert(photo["url"]);
		        var img;
		        if (i==0)
		        {
		        	img = new Element('img', {'class': 'img-apercu', 'src': photo["url"]+'imagePrincipale_web.jpeg', 'style': 'z-index: '+(2001+i)+';display: inline;'});
		        }
		        else
		        {
		        	img = new Element('img', {'class': 'img-apercu', 'src': photo["url"]+'imagePrincipale_web.jpeg', 'style': 'z-index: '+(2001+i)+';'});
		        }
		        img.inject($('apercu'));
		        img.setStyle('left', $('apercu').offsetLeft+$('apercu').getStyle('width').toInt()/2-209);//$('outerWrapper').getSize().x/3.25-200+14);
		        img.setStyle('top', '40px');
		    }
		    actuelle = 0;
	        $('apercu').setStyle('display', 'block');
		}
	});
	request.send('action=getGalerie&idGalerie='+idGalerie);
	$('apercu').set('tween', {
		duration: 5000,
		transition: Fx.Transitions.linear // This could have been also 'bounce:out'
	}).tween('opacity', '1');
}
