

window.addEvent("domready", function(){
	
	
	this.gallery = new Gallery();
	this.gallery.rollOver();
	
	//ie fix for tooltips
	//$$('.itemlist img.color').setProperty('title','caca');
	
	if ($('content').hasClass('home')){
		var myTips = new Tips('.itemlist a', {'showDelay':800, 'hideDelay':00, 'text':'','fixed':true, 'offset':{'x': 0, 'y': 100}}); //, 'fixed':true
		/*myTips.addEvent('show', function(tip, el){
	    	tip.fade('in');
		});
		myTips.addEvent('hide', function(tip, el){
	    	tip.fade('out');
		});*/
		
	}
	
	if ($('filtergallery') != null){
		this.gallery.filter();
		var myTips = new Tips('.itemlist a', {'showDelay':800, 'hideDelay':00, 'text':'', 'fixed':true,  'offset':{'x': 0, 'y': 100}}); //, 'fixed':true
		/*myTips.addEvent('show', function(tip, el){
	    	tip.fade('in');
		});
		myTips.addEvent('hide', function(tip, el){
	    	tip.fade('out');
		});*/

	}
	
	if ($('detailgallery') != null){
		this.gallery.arrangeThumbs();
		this.gallery.connectImageToThumb();
	}
	
	if ($('ateliergallery') != null){
		this.gallery.arrangeThumbs();
		this.gallery.connectImageToThumb();
		this.gallery.prepareAjaxQueries();
	}

				
});


window.addEvent("load", function(){
	
	
	if ($('content').hasClass('home')||$('content').hasClass('homeprojects')){
		var myTips = new Tips('.itemlist img');
		//console.log('hello home and home proj');
		this.gallery.letsSlide();
	}

				
});
		
		
		
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var Gallery = new Class({
	
	initialize: function(){
		this.items = $$('.itemlist');
		this.items.n = this.items.length;
		this.z = 100;
	},
	
	letsSlide:function(){
		var w = window.getSize().x
		
		this.gallery = $('gallery');
		this.gallery.setStyle('position','relative');
		this.gallery.fx = new Fx.Morph(this.gallery	, 
											{duration: 1200, 
											transition: Fx.Transitions.Sine.easeOut});
		
		this.slidebtn = new Element('a').setProperty('href','javascript:void(0)').addClass('nextbtn');
		this.actualslidepos = 0; 
		this.slidebtn.addEvent('click', function(e){
			if(this.actualslidepos == 0){
				this.bigSlideRight();
				this.slidebtn.addClass('backbtn');
			}else{
				this.bigSlideLeft();
				this.slidebtn.removeClass('backbtn');
			}
		}.bind(this));
		
		this.isinjected = false;
		
		var items_row4 = $$('#row4 .itemlist');
		
		items_row4.each(function(item){
			item.x = item.getPosition().x;
			item.w = item.getSize().x;
			
			var pos = item.x + item.w;
			
			if ((pos > w)&&(this.isinjected == false)) {
				//console.log("lets slide: "+this.slidebtn);
				this.slidebtn.inject(this.lastitem, 'before');
				this.isinjected = true
				this.lastitem_x = this.lastitem.x;
			}	
			
			this.lastitem = item;
			
		
		}.bind(this));
		
	},
	
	bigSlideRight: function(){
		this.actualslidepos = -this.lastitem_x+100;
		this.gallery.fx.start({'margin-left':this.actualslidepos});
	},
	
	bigSlideLeft: function(){
		this.actualslidepos = 0;
		this.gallery.fx.start({'margin-left':this.actualslidepos});
	},
	
	rollOver:function(){

		
		this.items.each(function(item){
			
			item.nb = item.getElement('.nb');
			item.nb.fx = new Fx.Morph(	item.nb, 
											{duration: 200, 
											transition: Fx.Transitions.Sine.easeOut});
			item.cl = item.getElement('.color');
			//if (!item.cl) return;								
			item.cl.fx = new Fx.Morph(	item.cl, 
											{duration: 200, 
											transition: Fx.Transitions.Sine.easeOut});
			
			
			item.nb.setStyle('opacity',0.7);
			item.cl.setStyle('opacity',0);
			
			
			if (item.hasClass('selected')) {
				this.actualselected = item;
				this.actualselected.cl.setStyle('opacity',1);
			}
			
			item.addEvent('mouseenter', function(e){
				if (item.hasClass('selected')) return;
				if (item.nb.hasClass('hidden')) return;
				item.cl.fx.cancel();
				item.nb.fx.cancel();
				item.cl.fx.start({'opacity':1});
				item.nb.fx.start({'opacity':0});

				//item.clr.setStyle('opacity',1);
			});
			item.addEvent('mouseleave', function(e){
				if (item.nb.hasClass('hidden')) return;
				if (item.hasClass('selected')) return;
				item.cl.fx.cancel();
				item.nb.fx.cancel();
				item.cl.fx.start({'opacity':0});
				item.nb.fx.start({'opacity':0.7});
				//item.clr.setStyle('opacity',0.1);
			});
			
		}.bind(this));
	},
	
	
	filter:function(){
		
		this.all_imgs = $$('#gallery img');
		
		this.concours_imgs = $$('.concours img');
		this.realisation_imgs = $$('.realisation img');
		this.en_cours_imgs = $$('.en_cours img');
		this.projets_imgs = $$('.projets img');
		
		var btn_concours = $('concours');
		var btn_realisations = $('realisations');
		var btn_en_cours = $('en_cours');
		var btn_projets = $('projets');
		
		btn_concours.addEvent('click', function(e){
				this.all_imgs.fade(0);
				this.concours_imgs.fade(1);
				this.all_imgs.addClass('hidden');
				this.concours_imgs.removeClass('hidden');
		}.bind(this));
			
		btn_realisations.addEvent('click', function(e){
				this.all_imgs.fade(0);
				this.realisation_imgs.fade(1);
				this.all_imgs.addClass('hidden');
				this.realisation_imgs.removeClass('hidden');
		}.bind(this));
			
		btn_en_cours.addEvent('click', function(e){
				this.all_imgs.fade(0);
				this.en_cours_imgs.fade(1);
				this.all_imgs.addClass('hidden');
				this.en_cours_imgs.removeClass('hidden');

		}.bind(this));
			
		btn_projets.addEvent('click', function(e){
				this.all_imgs.fade(0);
				this.projets_imgs.fade(1);
				this.all_imgs.addClass('hidden');
				this.projets_imgs.removeClass('hidden');
		}.bind(this));
	},
	
	
	arrangeThumbs:function(){
		
		this.cleanItems();
		
		var container = $('thumbs');
		
		var row1 = new Element('div').addClass('row').inject(container);
		var row2 = new Element('div').addClass('row').inject(container);
		var row3 = new Element('div').addClass('row').inject(container);
		var row4 = new Element('div').addClass('row').inject(container);
		
		// create nextButton
		this.nextbtn = new Element('a').setProperty('href','javascript:void(0)').addClass('nextbtn');				this.nextbtn.inject(row4);
		this.nextbtn.addEvent('click', function(e){
				this.nextImage();
		}.bind(this));
		
		var index = 0;
		if ($$('.doc')!= undefined) index = 1;
		
		this.items.each(function(item){
			switch (index){
				case 0: item.inject(row4); break;
				case 1: item.inject(row1); break;
				case 2: item.inject(row2); break;
				case 3: item.inject(row3); index=-1; break; 
			} 
			index++;			
		});
	},
	
	
	prepareAjaxQueries:function(target){
		
		this.basecontent = $('content').get('html');
		this.contenthaschanged = false;
		
		this.ajaxitems = $$('.ajax');
		
		this.ajaxitems.each(function(item){
			
			var a = item.getFirst('a');
			if(a == null) return;
			item.url = a.get('href'); 		
			a.set('href','javascript:void(0)');
			
		}.bind(this));
	},
	
	
	showAjaxContent:function(item){
		item.ajaxrequest = new Request.HTML({
            method: 'get',
            url: item.url,
            //data: { 'pathID' : target.name },
            onRequest: function() {},
            update: $('content'),
            onComplete: function(response) {}
        }).send();
        this.contenthaschanged = true;
	},
	

	showBaseContent:function(){
		if (this.contenthaschanged != true) return;
		$('content').set('html',this.basecontent);
		this.contenthaschanged = false;
	},
	
	
	
	nextImage:function(target){
		
		var actualpos = this.items.indexOf(this.actualselected).toInt()+1;
		if (actualpos > this.items.length-1) actualpos = 0;
		
		var item = this.items[actualpos];
		if (item.hasClass('ajax')==true){
			this.showAjaxContent(item);
		}else{
			this.showBigImage(actualpos);
			this.showBaseContent();
		}
		this.showThumb(item); 		
	},
	
	

	showBigImage:function(value){
		this.z++;		
		this.bigimages[value].setStyle('z-index',this.z);
	},
	
	
	showThumb:function(item){
		this.actualselected.removeClass('selected');
		this.actualselected.cl.fx.start({'opacity':0});
		this.actualselected.nb.fx.start({'opacity':0.7});
		this.actualselected = item;
		item.addClass('selected');
		item.cl.fx.start({'opacity':1});
	},
	
	
	cleanItems:function(){
			
				
		var temparray = [];
		this.items.each(function(item, index) {
			
			
			if(item.hasClass('ajax')==true){
				temparray.push(item);
				return;
			}
			
			var img = item.getFirst('img');
						
			if ((img.src.contains('.jpg')==true) ||
				(img.src.contains('.jpeg')==true) ||
				(img.src.contains('.gif')==true))   {
					temparray.push(item);
				}else{
				//this.items.erase(item);
				item.destroy();
			}
		});	
		this.items = temparray;
	},
	
	
	
	
	cleanBigImages:function(){
			
				
		var temparray = [];
		this.bigimages.each(function(item, index) {
			
			if (item.getFirst('img') != null){
				var el = item.getFirst('img');
			}else{
				var el = item;
			}
			if (el.src == undefined){el.src='';}	
						
			if (
				(el.src.contains('.jpg')==true)||
				(el.src.contains('.gif')==true)||
				(item.hasClass('ajax')==true)) {
					temparray.push(item);
			}else{
				item.destroy();
			}
		});	
		//this.bigimages.clean();
		this.bigimages = temparray;
	},
	
	
	
	connectImageToThumb:function(){
		
		this.items.indexOf(this.actualselected);
		
		this.bigimages = $$('#images img');
		this.bigimages.each(function(item, index){
			var wrapper = new Element('div', {'class': 'wrap'});
			wrapper.wraps(item);
			this.bigimages[index]=wrapper;
		}.bind(this));
		
		this.cleanBigImages();
		this.bigimages.reverse();
		
		this.items.each(function(item){
		
			var index = this.items.indexOf(item);
				
			item.addEvent('click', function(e){
				
				var actualpos = index;
				this.showThumb(item);
				if (item.hasClass('ajax')==true){
					this.showAjaxContent(item);
				}else{
					this.showBigImage(actualpos);
					this.showBaseContent();
				}
				
			}.bind(this));
		}.bind(this));
		
	}
	
	
	
});

























