﻿var Format = new Class({
	initialize:function(){}
});
Format.implement({
	messages:{
		bold:{
			enter:{
				fr:'Veuillez insérer le texte à afficher en gras:',
				en:'Please insert the text to display in bold:'
			}
		},
		italic:{
			enter:{
				fr:'Veuillez insérer le texte à afficher en italique:',
				en:'Please insert the text to display in italic:'
			}
		},
		link:{
			address:{
				fr:'Veuillez insérer l\'adresse de votre lien:',
				en:'Please insert the address of your link:'
			},
			enter:{
				fr:'Veuillez insérer le titre de votre lien:',
				en:'Please insert the title of your link:'
			}
		},
		quote:{
			enter:{
				fr:'Veuillez insérer la citation:',
				en:'Please insert the quote:'
			}
		}
	},
	insertTag:function(e,id){
		f=$('CommentMessage'+id);
		switch(e){
			case'bold':t={b:'[b]',e:'[/b]'};break;
			case'italic':t={b:'[i]',e:'[/i]'};break;
			case'link':
				u=prompt(this.messages.link.address[L],'http://');
				if(u) t={b:'[l '+u+']',e:'[/l]'};
				else return false;
			break;
			case'quote':t={b:'[q]',e:'[/q]'};break;
		}
		if(document.selection){
			f.focus();
			s=document.selection.createRange();
			if(s.text.length>0) s.text=t.b+s.text+t.e;
			else{
				var d=prompt(this.messages[e].enter[L],'');
				if(d) sel.text = t.b + d + t.e;
			}
			f.focus();
		}else if(f.selectionStart || f.selectionStart=='0'){
			var sP=f.selectionStart;
			var eP=f.selectionEnd;
			var cP=eP;
			var sT=f.scrollTop;
			if(sP!=eP){
				f.value=f.value.substring(0,sP)+t.b+f.value.substring(sP,eP)+t.e+f.value.substring(eP,f.value.length);
				cP+=t.b.length+t.e.length;
			}else{
				var d=prompt(this.messages[e].enter[L],'');
				if(d) f.value=f.value.substring(0,sP) + t.b+d+t.e+f.value.substring(eP,f.value.length);
				cP=sP+t.b.length+d.length+t.e.length;
			}
			f.focus();
			f.selectionStart=cP;
			f.selectionEnd=cP;
			f.scrollTop=sT;
		}else{
			var d=prompt(this.messages[e].enter[L],'');
			if(d) f.value += t.b + d + t.e;
			f.focus();
		}
	}
});
var F=new Format();

var BlogManager = new Class({
	initialize:function(){
		this.txt=$('blog');
		l=this.txt.getElementsByTagName('ul'); for(i=0;i<l.length;i++) if(l[i].className=='format_buttons') l[i].style.display='block';
		this.status=$('blog_status');
		if(window.location.hash.length>=2){this.load(window.location.hash);}
		this.loc=window.location.href;
	}
});
BlogManager.implement({
	xhr:null,
	status:null,
	txt:null,
	post:null,
	int:null,
	timer:null,
	loc:null,
	messages:{
		added:{fr:'Votre commentaire a été ajouté!',en:'Your comment has been added!'},
		error:{fr:"Une erreur s'est produite. Veuillez réessayer plus tard.",en:"An error occured. Please try again later."},
		error_comment:{fr:"Une erreur s'est produite!",en:"An error occured!"},
		legal:{fr:"Mentions légales",en:"Legal information"},
		loading:{fr:'Chargement du contenu...',en:'Loading content...'},
		m_captcha:{fr:'Veuillez répondre à la question de sécurité.\n\n',en:'Please answer the security question.\n\n'},
		m_email1:{fr:'Veuillez saisir votre adresse email.\n\n',en:'Please enter your e-mail address. It will not be published.\n\n'},
		m_email2:{fr:'Veuillez saisir une adresse email valide.\n\n',en:'Please enter a valid e-mail address.\n\n'},
		m_link:{fr:'Veuillez saisir un lien valide (doit commencer par "http://" ou "https://").\n\n',en:'Please enter a valid link (must start with "http://" or "https://").\n\n'},
		m_message:{fr:'Veuillez saisir le texte de votre commentaire.\n\n',en:'Please enter the text of your comment.\n\n'},
		m_name:{fr:'Veuillez saisir votre nom.\n\n',en:'Please enter your name.\n\n'},
		s_category:{fr:'Veuillez choisir une catégorie.',en:'Please select a category.'},
		s_date:{fr:'Veuillez choisir une date.',en:'Please select a date.'},
		s_keyword:{fr:'Veuillez saisir un mot-clé.',en:'Please enter a keyword.'},
		sending:{fr:'Sauvegarde du commentaire...',en:'Saving the comment...'},
		title:{fr:'Gaganymède',en:'Gaganymede'}
	},
	changeStatus:function(t){
		if(B.int!=null) clearInterval(B.int);
		try{B.status.style.display=(t.length>0)?'inline':'none';}
		catch(er){B.status.style.display='none';}
		B.status.innerHTML=t;
	},
	flashStatus:function(t){
		B.changeStatus(t);
		B.int=setInterval(B.changeStatus,2000);
	},
	changeBar:function(h){
		window.location.hash=h;
	},
	setChecker:function(){
		B.timer=setInterval(B.check,750);
	},
	check:function(){
		if(B.loc!=window.location.href){
			h=window.location.hash;
			if(h.length>0) n=h.substr(1,h.length-1);
			else n=window.location.pathname;
			if(n=='/')n='/blog';
			B.load(n);
		}
	},
	load:function(n){
		if(n[0]!='/')n='/'+n;
		this.changeStatus(this.messages.loading[L]);
		this.xhr=new XHR({async: true, method: 'post',onSuccess:this.onLoaded.bind(this),onFailure:function(){B.onFailure();}});
		this.changeBar(n);
		this.loc=window.location.href;
		this.xhr.send(n+'?mode=ajax', null);
	},
	onLoaded:function(){
		this.txt.innerHTML=this.xhr.response.text;
		this.changeStatus();
		try{
			h=window.location.hash; if(h.indexOf('#')==0) h=h.substr(1);
			t='';
			if(h.indexOf('/blog')==0) t=this.messages.title[L];
			else {
				if(h.indexOf('/legal')==1) t=this.messages.legal[L];
				else if(h.indexOf('/search')==1 || h.indexOf('/category')==1 || h.indexOf('/archives')==1){t=$('indicator').innerHTML;}
				else{r=this.txt.getElementsByTagName('h2').item(0);if(r.innerHTML!='Notice'&&r.innerHTML!='Error')t=r.firstChild.innerHTML;}
				t+=' ['+this.messages.title[L]+']';
			}
			if(t.length>0){
				while(t.match(/<br(.*)>/)!=null) t=t.replace(/<br(.*)>/,' ');
				while(t.match(/<[^>]*>/)!=null) t=t.replace(/<[^>]*>/,'');
				document.title=t;
			}
			l=this.txt.getElementsByTagName('ul'); for(i=0;i<l.length;i++) if(l[i].className=='format_buttons') l[i].style.display='block';
		}catch(er){B.onFailure();}
		window.scrollTo(0,0);
		try{pageTracker._trackPageview(h);}catch(er){}
	},
	loadComments:function(u,t){
		this.post=$('post'+t);
		l=this.post.getElementsByTagName('ul');
		for(i=0;i<l.length;i++) if(l[i].className=='liens') {
			m=l[i].getElementsByTagName('li');
			for(j=0;j<m.length;j++) if(m[j].className=='comments') m[j].style.display='none';
		}
		this.changeStatus(this.messages.loading[L]);
		this.xhr=new XHR({async: true, method: 'post', onSuccess:this.onCommentsLoaded.bind(this),onFailure:function(){B.onFailure();}});
		this.xhr.send(u+'?comments=1&mode=ajax', null);
	},
	onCommentsLoaded:function(){
		l=this.post.getElementsByTagName('div'); for(i=0;i<l.length;i++) if(l[i].className=='comments') l[i].parentNode.removeChild(l[i]);
		this.post.innerHTML+=this.xhr.response.text;
		l=this.post.getElementsByTagName('ul'); for(i=0;i<l.length;i++) if(l[i].className=='format_buttons') l[i].style.display='block';
		this.changeStatus();
		l=this.post.getElementsByTagName('div'); for(i=0;i<l.length;i++) if(l[i].className=='comments') window.scrollTo(0,l[i].getPosition().y);
	},
	sendComment:function(f,i){
		s='';this.post=$('post'+i);
		d=['Name','Email','Link','Message','Captcha','Key'];
		if($('CommentEmail'+i).value.length==0) s+=this.messages.m_email1[L];
		else if($('CommentEmail'+i).value.length>0 && $('CommentEmail'+i).value.indexOf('@')>=$('CommentEmail'+i).value.lastIndexOf('.')) s+=this.messages.m_email2[L];
		if($('CommentMessage'+i).value.length==0) s+=this.messages.m_message[L];
		if($('CommentLink'+i).value.length>0 && ($('CommentLink'+i).value.indexOf('http://')!=0 && $('CommentLink'+i).value.indexOf('https://')!=0)) s+=this.messages.m_link[L];
		if($('CommentCaptcha'+i).value.length==0) s+=this.messages.m_captcha[L];
		if(s.length>0) alert(s);
		else {
			this.changeStatus(this.messages.sending[L]);
			if($('CommentName'+i).value.length>0) Cookie.set('UserName',$('CommentName'+i).value,{duration:30,path:'/'});
			if($('CommentEmail'+i).value.length>0) Cookie.set('UserEmail',$('CommentEmail'+i).value,{duration:30,path:'/'});
			if($('CommentLink'+i).value.length>0) Cookie.set('UserLink',$('CommentLink'+i).value,{duration:30,path:'/'});
			this.xhr=new XHR({async:true,method:'post',onSuccess:this.onCommentSent.bind(this),onFailure:function(){B.onFailure();}});
			if($('CommentMoodMad'+i).checked) cmv='mad';
			if($('CommentMoodSad'+i).checked) cmv='sad';
			if($('CommentMoodNormal'+i).checked) cmv='normal';
			if($('CommentMoodHappy'+i).checked) cmv='happy';
			r='data[Comment]';
			v=r+'[blog_post_id]='+i;
			for(j=0;j<d.length;j++) v+='&'+r+'['+d[j].toLowerCase()+']='+$('Comment'+d[j]+i).value;
			v+='&'+r+'[mood]='+cmv+'&'+r+'[cookie]='+(($('CommentCookie'+i).checked)?'1':'0');
			this.xhr.send(f.action+'?mode=ajax&comments=1',v);
		}
		return false;
	},
	onCommentSent:function(){
		if(this.xhr.response.text.indexOf('class="error"')>-1) this.flashStatus(this.messages.error_comment[L]);
		else this.flashStatus(this.messages.added[L]);
		l=this.post.getElementsByTagName('div'); for(i=0;i<l.length;i++) if(l[i].className=='comments') l[i].parentNode.removeChild(l[i]);
		this.post.innerHTML+=this.xhr.response.text;
		l=this.post.getElementsByTagName('ul'); for(i=0;i<l.length;i++) if(l[i].className=='format_buttons') l[i].style.display='block';
		l=this.post.getElementsByTagName('div'); for(i=0;i<l.length;i++) if(l[i].className=='comments') window.scrollTo(0,l[i].getPosition().y);
	},
	search:function(){
		if($('searchKeyword').value.length==0) alert(this.messages.s_keyword[L]);
		else this.load('/search/'+L+'/'+escape($('searchKeyword').value.replace('/',' ')));
	},
	date:function(){
		if($('searchDate').options[$('searchDate').selectedIndex].value.length==0) s+=this.messages.s_date[L];
		else this.load('/archives/'+L+'/'+escape($('searchDate').options[$('searchDate').selectedIndex].value));
	},
	category:function(){
		if($('searchCategory').options[$('searchCategory').selectedIndex].value.length==0) s+=this.messages.s_category[L];
		else this.load('/category/'+L+'/'+escape($('searchCategory').options[$('searchCategory').selectedIndex].value.replace('/',' ')));
	},
	onFailure:function(){this.flashStatus(this.messages.error[L]);}
});

var r=null;
var PicasaManager = new Class({
	initialize:function(){
		$('picasa_whole').style.display='block';
		this.err=$('picasa_error');
		this.whole=$('picasa_whole');
		this.sel=$('picasa_select');this.sel.parent=this;
		this.gal=$('picasa_gallery');
		this.board=$('board');
		this.txt=$('txt');
		var e=$('boardElem');
		this.zoom=document.createElement('img');
		this.zoom.id='zoom';
		this.zoom.empty=true;
		e.appendChild(this.zoom);
		//this.list();
		this.LIST=r;
		this.formatList();
	}
});
PicasaManager.implement({
	xhr: null,
	LIST:null,
	ALBUM:null,
	INDEX:null,
	fAlbumRowByPage:4,
	fAlbumPictureByRow:5,
	sel:null,
	gal:null,
	whole:null,
	err:null,
	board:null,
	zoom:null,
	txt:null,
	albumPage:0,
	messages:{
		loading_album:{fr:"Chargement de l'album en cours...",en:"Loading album..."},
		loading:{fr:"Chargement...",en:"Loading..."},
		previous:{fr:"Précédent",en:"Previous"},
		next:{fr:"Suivant",en:"Next"}
	},
	formatList:function() {
		r=this.LIST;
		this.sel.innerHTML='';
		for(i=0;i<r.length;i++){
			o=document.createElement('option');
			o.value=r[i].id;
			o.innerHTML=r[i].date+' '+r[i].title;
			this.sel.appendChild(o);
		}
		this.album(r[0].id);
		this.sel.onchange=function(){this.parent.album(this.options[this.selectedIndex].value)};
		this.sel.onblur=function(){};
	},
	album:function(id){
		$('picasa_display').innerHTML=this.messages.loading_album[L];
		this.xhr=new XHR({async: true, method: 'post', onSuccess:this.onAlbumLoaded.bind(this), onFailure: function(){P.onFailure();}});
		this.xhr.send('/outer_feeds/request/picasa_album/'+id, null);
	},
	onAlbumLoaded:function(){
		try{
			a=this.xhr.response.xml.getElementsByTagName('entry');
			r=new Array();
			for(i=0;i<a.length;i++) {
				b=a[i].childNodes;
				u=q=d=w=h=W=H='';
				for(j=0;j<b.length;j++) switch(b[j].nodeName){
					case 'link':try{if(u.length==0&&b[j].getAttribute('type')=='text/html')u=b[j].getAttribute('href');}catch(er){u='';} break;
					case 'content':try{q=b[j].getAttribute('src')+'?imgmax=512';}catch(er){q='';} break;
					case 'summary':try{d=b[j].firstChild.nodeValue;}catch(er){d='';} break;
					case 'media:group':
						c=b[j].childNodes;mi=-1;
						for(k=0;k<c.length;k++) {
							switch(c[k].nodeName) {
								case 'media:thumbnail':
									if(mi==-1||mi>Number(c[k].getAttribute('width'))){
										mi=Number(c[k].getAttribute('width'));
										t=c[k].getAttribute('url');
										w=c[k].getAttribute('width');
										h=c[k].getAttribute('height');
									}
									break;
								case 'media:description':
									try{d=c[k].firstChild.nodeValue;}catch(er){d='';}
									break;
							}
						}
						break;
					case 'gphoto:width':
						try{W=b[j].firstChild.nodeValue;}catch(er){w='';}
						break;
					case 'gphoto:height':
						try{H=b[j].firstChild.nodeValue;}catch(er){w='';}
						break;
				}
				r.push({image:q,description:d,url:u,thumbnail:t,width:Number(w),height:Number(h),W:Number(W),H:Number(H)});
			}
			this.ALBUM=r;
			this.albumPage=0;
			this.formatAlbum(0);
		} catch(er){this.onFailure();}
	},
	formatAlbum:function(page){
		this.albumPage+=page;
		try{
			r=this.ALBUM;
			rbp=this.fAlbumRowByPage;
			pbr=this.fAlbumPictureByRow;
			rata=rbp*pbr;
			start=((this.albumPage*rata)>r.length)?r.length:(this.albumPage*rata);
			limit=((start+rata)>r.length)?r.length:(start+rata);
			d=$('picasa_display');d.innerHTML='';
			for(i=start;i<limit;i+=pbr) {
				a2=new Array();
				sdiv=document.createElement('div');
				sdiv.className='row';
				if(i==start) sdiv.className+=' first';
				limit2=(i+pbr>r.length)?r.length:(i+pbr);
				for(j=i;j<limit2;j++) {
					div=document.createElement('div');
					div.className='clipper';
					if(j%5==0) div.className+=' first';
					an=document.createElement('a');
					an.index=j;
					an.onclick=function(){P.formatImage(this.index);};
					img=document.createElement('img');img.alt=img.title=r[j].description;img.src=r[j].thumbnail;
					img.style.marginTop=((39-r[j].height)/2)+'px';img.style.marginLeft=((39-r[j].width)/2)+'px';
					an.appendChild(img);div.appendChild(an);sdiv.appendChild(div);
				}
				d.appendChild(sdiv);
			}
			d=$('picasa_browse');
			d.innerHTML='';
			dd=document.createElement('div');
			dd.className='previous';
			d.appendChild(dd);
			if(this.albumPage>0){
				an=document.createElement('a');
				an.href='javascript:P.formatAlbum(-1);';
				an.innerHTML=this.messages.previous[L];
				dd.appendChild(an);
			}
			else{
				dd.innerHTML='&nbsp;';
				dd.style.backgroundImage='none';
			}
			n=Math.ceil(r.length/rata);
			dd=document.createElement('div');
			dd.className='pages';
			dd.innerHTML='Page '+(this.albumPage+1)+'/'+n;
			d.appendChild(dd);
			dd=document.createElement('div');
			dd.className='next';
			d.appendChild(dd);
			if(this.albumPage+1<n){
				an=document.createElement('a');
				an.href='javascript:P.formatAlbum(1);';
				an.innerHTML=this.messages.next[L];
				dd.appendChild(an);
			}
			else{
				dd.innerHTML='&nbsp;';
				dd.style.backgroundImage='none';
			}
		}catch(er){this.onFailure();}
	},
	formatImage:function(index){
		this.INDEX=index;e=this.ALBUM[index];
		if(this.zoom.empty) {
			this.centerImage(P.ALBUM[P.INDEX]);
			this.zoom.empty=false;
		}
		this.zoom.style.MozOpacity=0.50;this.zoom.style.opacity=0.50;this.zoom.style.filter='alpha(opacity=50)';this.zoom.src=e.image;this.zoom.alt=e.description;
		this.zoom.style.margin='0px';
		e=$('veil');
		e.style.MozOpacity=0.50;e.style.opacity=0.50;e.style.filter='alpha(opacity=50)';
		e.style.width=Window.getScrollWidth()+'px';e.style.height=Window.getScrollHeight()+'px';e.style.display='block';
		this.board.style.display='block';
		b=Window.getScrollTop()+20;this.board.style.top=b+'px';
		a=(Window.getScrollWidth()-552)/2;a=(a>20)?a:20;this.board.style.left=a+'px';
		this.txt.innerHTML=(this.INDEX+1)+'/'+this.ALBUM.length+'<'+'br'+'/'+'>'+this.messages.loading[L];
		this.zoom.onload=function(){
			this.style.MozOpacity=1;this.style.opacity=1;this.style.filter='alpha(opacity=100)';
			P.centerImage(P.ALBUM[P.INDEX]);
			P.txt.innerHTML=(P.INDEX+1)+'/'+P.ALBUM.length+'<'+'br'+'/'+'>'+P.ALBUM[P.INDEX].description;
		};
	},
	centerImage:function(e){
		w=e.W;h=e.H;if(w>512||h>512){if(w>h){h=h*512/w;w=512;}else{w=w*512/h;h=512;}}mV=(512-h)/2+'px';mH=(512-w)/2+'px';this.zoom.style.padding=mV+' '+mH;
	},
	formatPreviousImage:function(){this.INDEX--;if(this.INDEX<0)this.INDEX=this.ALBUM.length-1;this.formatImage(this.INDEX);},
	formatNextImage:function(){this.INDEX++;if(this.INDEX>=this.ALBUM.length)this.INDEX=0;this.formatImage(this.INDEX);},
	onFailure:function(){this.whole.style.display='none';this.err.style.display='block';this.err.innerHTML='An error occured. Please try again later.';},
	shutImage:function(){this.board.style.display='none';e=$('veil');e.style.display='none';}
});

var BackGroundManager = new Class({
	initialize:function(i){
		if(i!=null){
			Cookie.set('BackGround',i,{duration:30,path:'/'});
		}
	}
});
BackGroundManager.implement({
	xhr:null,
	css:null,
	prepare:function(){
		$('background').style.display='block';
		this.theme(Cookie.get('BackGround'));
	},
	change:function(i){
		Cookie.set('BackGround',i,{duration:30,path:'/'});
		this.theme(i);
		this.xhr=new XHR({async:true,method:'post'});
		this.xhr.send('/variables/setBackGround/'+i, null);
	},
	theme:function(i){
		document.body.style.backgroundImage='url(/img/big'+i+'.jpg)';
		switch(Number(i)){
			case 1:c='369';break;
			case 2:c='000';break;
			case 3:c='663';break;
			case 4:c='f63';break;
			case 5:c='f36';break;
			case 6:c='390';break;
			case 7:c='99c';break;
			case 8:c='666';break;
			case 9:c='f90';break;
			case 10:c='339';break;
			case 11:c='f30';break;
			default:c='000';break;
		}
		if(this.css==null){this.css=document.createElement('style');this.css.setAttribute('type', 'text/css');document.head.appendChild(this.css);this.css.innerHTML='a{color:#'+c+'}';}
		else this.css.innerHTML='a{color:#'+c+'}';
	}
});

var LanguageManager=new Class({initialize:function(){}});
LanguageManager.implement({
	xhr:null,
	set:function(l){
		L=l;
		Cookie.set('lang',l);
		this.xhr=new XHR({async:true,method:'post',onFailure:function(){}});
		this.xhr.send('/variables/setLanguage/'+l, null);
	}
});

var L,B,P,BG,init1,init2,init3,pageTracker;
L=LM=B=P=BG=pageTracker=null;
init=function(){B=new BlogManager();B.setChecker();BG.prepare();P=new PicasaManager();LM=new LanguageManager();};
if((p=window.location.href.indexOf('#'))>=0) window.location.href=window.location.href.substr(p+1);
else window.addEvent('domready',init);