var DanielMartinez = {
	timer : new Object(),
	
	LoadMenu: function()
	{
		$('menu').getElement('ul').getElements('li').each(function(item)
		{
			item.getElement('a').addEvent('click',function(e)
			{ 
				$('menu').getElement('ul').getElements('li').each(function(item)
				{
					if (item.getElement('a') != this) item.getElement('a').removeClass("currentMenu");
				}.bind(this));
				this.addClass("currentMenu");
				this.blur();
			});
		});
	},
	
	AjaxTransform : function(all)
	{
		if (all) var elems = $$('a');
		else var elems = $$('#content a');
		$each(elems, function(el){
			if (el.rel.test(/^noajax/i)) return;
			el.addEvent('click',function(e)
			{
				e = new Event(e).stop();
				var lien = this.href.replace(/.htm$/,".ajax");
				new Ajax(lien, {
					method: 'get',
					update: $('content'),
					onComplete : function()
					{
						if($('moretext')) DanielMartinez.TextLayer();
						if($('growingimgs')) DanielMartinez.GrowingImages();
						if($('otherprojects')) DanielMartinez.NoScroll();
						if($('contact_form')) DanielMartinez.SendMail();
						if($('enterbutton')){
							$('enterbutton').addEvent('click',function()
							{
								$('backtohome').removeClass("currentMenu");
								$('tomenu').addClass('currentMenu');
							});
						} 
						DanielMartinez.AjaxTransform(false);
					},
					onFailure: function(){alert("Page inaccessible")}
				}).request();
			});
		});
	},
	
	SendMail : function()
	{
		$('contact_form').action = $('contact_form').action.replace(/.htm$/,".ajax");
		$('contact_form').addEvent('submit',function(e)
		{
			e = new Event(e).stop();
			$('contact_form').send({onComplete: function(txt){
							var Result = txt.split(/:/);
							var Layer = new Element('div');
							Layer.setStyles({width:"150px",height:"100px",top:"50%",left:"50%",margin:"-50px 0 0 -75px"});
							Layer.innerHTML = Result[1];
							Layer.id = "moretext_layer";
							Layer.id = "moretext_layer";
							var CloseLink = new Element('a');
							CloseLink.innerHTML = "Fermer";
							CloseLink.id="closelink";
							CloseLink.href= "#";
							CloseLink.addEvent('click',function(e)
							{
								e = new Event(e).stop();
								this.getParent().remove();
							});
							Layer.adopt(CloseLink);
							document.body.adopt(Layer);
							if (parseInt(Result[0])) {$('contact_form').reset();}
						},
						onFailure: function(){alert("Une erreur a eu lieu, envoi du message impossible.")}});
		});
	},

	TextLayer : function()
	{
		$('moretext_text').setStyle("display","none");
		$('moretext').addEvent('click',function(e)
		{
			e = new Event(e).stop();
			var Layer = new Element('div');
			Layer.setStyles($('mainImg').getCoordinates());
			Layer.innerHTML = $('moretext_text').innerHTML;
			Layer.setStyle('height',parseInt($('mainImg').getStyle('height'))-10+"px");
			Layer.id = "moretext_layer";
			var CloseLink = new Element('a');
			CloseLink.innerHTML = "Fermer";
			CloseLink.id="closelink";
			CloseLink.href= "#";
			CloseLink.addEvent('click',function(e)
			{
				e = new Event(e).stop();
				this.getParent().remove();
			});
			Layer.adopt(CloseLink);
			document.body.adopt(Layer);
		});
	},

	FakeScrollUp : function()
	{
			var newMargin = 0;
			var actualMargin = parseInt($('otherprojects').getElement("div").getStyle('margin-top'));
			if (parseInt($('otherprojects').getElement("div").getStyle('margin-top')) < 0)
			{
				newMargin = (actualMargin >-4) ? 0 : actualMargin + 4;
			}
			$('otherprojects').getElement("div").setStyle('margin-top',newMargin+"px");
	},
	
	FakeScrollDown : function()
	{
			var Coord = $('otherprojects').getSize();
			var actualMargin = parseInt($('otherprojects').getElement("div").getStyle('margin-top'));
			var SizeLeft = Coord.scrollSize.y - Coord.size.y;
			//alert(SizeLeft);
			var newMargin = actualMargin;
			if (SizeLeft >= 4) newMargin = actualMargin - 4;
			//else if (SizeLeft > 0 && SizeLeft<4){newMargin = actualMargin - SizeLeft} 
			$('otherprojects').getElement("div").setStyle('margin-top',newMargin+"px");
	},
	
	NoScroll : function()
	{
			
			$$('#otherprojects').setStyles({'overflow':'hidden','padding-left':'18px'});
			$$('#otherprojects').getElement('div').setStyle('margin-top','0px');
			var ascensHaut = new Element('div');
			var ascensBas = new Element('div');
			ascensHaut.setStyles({  position:'absolute',
						left:'0px',
						top:'0px',
						background:'url(/imgs/arrow_up.png) no-repeat',
						width:'11px',
						height:'16px'
						});
			ascensBas.setStyles({   position:'absolute',
						left:'0px',
						bottom:'-2px',
						background:'url(/imgs/arrow_down.png) no-repeat',
						width:'11px',
						height:'16px'
						});
			ascensHaut.addEvent('mouseover',function(item)
			{
				DanielMartinez.timer = DanielMartinez.FakeScrollUp.periodical('50');
			});
			ascensHaut.addEvent('mouseout',function(item)
			{
				DanielMartinez.timer = $clear(DanielMartinez.timer);
			});
			ascensBas.addEvent('mouseover',function(item)
			{
				DanielMartinez.timer = DanielMartinez.FakeScrollDown.periodical('50');
			});
			ascensBas.addEvent('mouseout',function(item)
			{
				DanielMartinez.timer = $clear(DanielMartinez.timer);
			});
			$$('#otherprojects').adopt(ascensHaut);
			$$('#otherprojects').adopt(ascensBas);
			$$('#currentproject').setStyle('opacity','0.3');
	},
	
	GrowingImages: function()
	{
		var options = {wImg : 263, hImg : 145, hmargin:13,wmargin:13}; 
		var ImgList = Array();
		var i=0,j=0;
		$$('#growingimgs').setStyles($$('#growingimgs').getCoordinates());
		$$('#growingimgs').setStyle("position","absolute");
		$ES('#growingimgs p').each(function(item)
		{
			//On recense toutes les images pour les mettre dans un tableau
			//puis on recense les menus correspondants.
			j=0;
			i++;
			item.getElements('a').each(function(item)
			{
				item.setStyle("display","block");
				j++;
				ImgList[ImgList.length] = {elem:item,position:{x:j,y:i},menuItem:"",src:item.getElement("img").src};
				item.setStyle("position","absolute");
				item.setStyles({left:(options.wImg+options.wmargin)*(j-1)+"px",top:(options.hImg+options.hmargin)*(i-1)+"px","z-index":5});
				item.setStyles({width:options.wImg,height:options.hImg});
				item.setStyle("z-index","50");
				$ES('#menu ul li a').each(function(menu)
				{
					if (this.rel==menu.rel) ImgList[ImgList.length - 1].menuItem = menu;
				}.bind(item));
			});
		});
		ImgList.each(function(Img)
		{
			Img.elem.addEvent('mouseover',function(elem)
			{
				if (this.menuItem != "") this.menuItem.addClass("currentMenu");
				var MaxSize = ImgList[ImgList.length-1].position;
				var ArrondX = (Math.round(MaxSize.x/2)==this.position.x)?this.position.x+1:Math.round(MaxSize.x/2);
				var ArrondY = (Math.round(MaxSize.y/2)==this.position.y)?this.position.y+1:Math.round(MaxSize.y/2);
				var BlankPics = Array();
				BlankPics[0] = {x:ArrondX,y:this.position.y};
				BlankPics[1] = {x:this.position.x,y:ArrondY};
				BlankPics[2] = {x:ArrondX,y:ArrondY};
				ImgList.each(function(item)
				{
					for(i=0;i<BlankPics.length;i++)
					{
						if(BlankPics[i].x==item.position.x && BlankPics[i].y==item.position.y)
						{
							item.elem.getElement("img").src="imgs/blank_img.gif";
						}
					}
				}.bind(Img));
				//On regle la taille de l'image
				this.elem.getElement("img").setStyles({width:(options.wImg*2+options.wmargin)+"px",height:(options.hImg*2+options.hmargin)+"px"});
				this.elem.setStyles({width:(options.wImg*2+options.wmargin)+"px",height:(options.hImg*2+options.hmargin)+"px"});
				this.elem.setStyle("z-index",1);
				if (ArrondX<this.position.x)
				{
					this.elem.setStyle("left",parseInt(this.elem.getStyle("left"))-(options.wImg+options.wmargin)+"px");
				
				}
				if (ArrondY<this.position.y)
				{
					this.elem.setStyle("top",parseInt(this.elem.getStyle("top"))-(options.hImg+options.hmargin)+"px");
				}
			}.bind(Img));
			Img.elem.addEvent('mouseout',function(elem)
			{
				if (this.menuItem != "")
				{
					this.menuItem.removeClass("currentMenu");
				}
				//Gestion de l'agrandissement de l'image
				var MaxSize = ImgList[ImgList.length-1].position;
				var ArrondX = (Math.round(MaxSize.x/2)==this.position.x) ?  this.position.x+1:Math.round(MaxSize.x/2);
				var ArrondY = (Math.round(MaxSize.y/2)==this.position.y) ?  this.position.y+1:Math.round(MaxSize.y/2);
				var BlankPics = Array();
				BlankPics[0] = {x:ArrondX,y:this.position.y};
				BlankPics[1] = {x:this.position.x,y:ArrondY};
				BlankPics[2] = {x:ArrondX,y:ArrondY};
				ImgList.each(function(item)
				{
					for(i=0;i<BlankPics.length;i++)
					{
						if(BlankPics[i].x==item.position.x && BlankPics[i].y==item.position.y)
						{
							item.elem.getElement("img").src=item.src;
						}
					}

				}.bind(Img));
				this.elem.setStyles({left:(options.wImg+options.wmargin)*(this.position.x-1)+"px",top:(options.hImg+options.hmargin)*(this.position.y-1)+"px"});
				this.elem.setStyle("z-index",50);
				this.elem.getElement("img").setStyles({width:options.wImg+"px",height:options.hImg+"px"});
				this.elem.setStyles({width:options.wImg+"px",height:options.hImg+"px"});
			}.bind(Img));
			Img.elem.addEvent('click',function(elem)
			{
				$('tomenu').removeClass("currentMenu");
				
			});
		});		
	}
};

window.addEvent("domready",function()
{
	if($('moretext')) DanielMartinez.TextLayer();
	if($('growingimgs')) DanielMartinez.GrowingImages();
	if($('otherprojects')) DanielMartinez.NoScroll();
	if($('contact_form')) DanielMartinez.SendMail();
	DanielMartinez.AjaxTransform(true);
	DanielMartinez.LoadMenu();
});
