window.addEvent("domready", function (j) {

	/* les hover post */
	hoverposts();
	
	/* ancres */
	initSmoothScroll(".menu");
	initSmoothScroll(".backtotop");
	
	/* changement de template screen */
	if($$('#display-mode li a')[0])
		changetemplate();
	
	/* load more */
	if($('nextcontent'))
		loadmore();
		if($('site-title'))		
			loadlogo();
	
	/* hover utilities */
	$$('.entry-utility span a').each(function(el,i){
		el.addEvents({
			'mouseover':function(){
				el.getParent().addClass('hover');
			},
			'mouseleave':function(){
				el.getParent().removeClass('hover');
			}
		});
	});
	
	
	
	//multisite();
	
	
	
	var randombubble = new Element('div',{'id':'bubble'}).inject($(document.body),'bottom').setStyles({'position':'absolute'});
	var randombubble2 = new Element('div',{'id':'bubbletop'}).inject($('bubble'),'bottom').setStyles({'position':'relative'});
	var randombubble3 = new Element('div',{'id':'bubblemiddle'}).inject($('bubble'),'bottom').setStyles({'position':'relative'});
	var randombubble4 = new Element('div',{'id':'bubblebottom'}).inject($('bubble'),'bottom').setStyles({'position':'relative'});

	
	/* tips */
	tips();
	

});
	/* logo loader */
	function loadlogo(){
		//var title = new Fx.Morph($('site-title'), {duration:800,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'opacity':0});		
		//var loader = new Fx.Morph($('gifloader'), {duration:800,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'opacity':1});		
		//$('gifloader').setStyle('z-index',20);		
		//$('site-title').setStyle('z-index',40);						
		var logoloader = new clrzMachinegun({			
			elt:'sitetitlelink',			
			fps:25		
		});		
		var timer;				
		$('site-title').addEvents({"mouseover": function(event){	
									
			logoloader.Play();	
			timer = (function(){
				logoloader.rewind();
				logoloader.Stop();
			}).delay(950);	
		},		
		"mouseleave":function(event){
			
			
			timer = $clear(timer);
			logoloader.rewind();
			logoloader.Stop();
			$('site-title').removeEvents('mouseleave');			
			$('site-title').removeEvents('mouseover');			
			loadlogo();		
		}});				
	}
	
/* tips */
function tips(){

	if($('bubble')){
		var randombubble = $('bubble');
		var randombubble2 = $('bubbletop');
		var randombubble3 = $('bubblemiddle');
		var randombubble4 = $('bubblebottom');

		var tipstable = [];
		var tipstable2 = [];
		
		$$('.entry-utility span a').each(function(element,i) {
			if(element.getProperty('rel') && element.getProperty('rel')!='')
				 tipstable[i]=element.getProperty('rel').toString();
			 else
				 tipstable[i]='pas encore fini';
				 
		});	
		
		$$('.bubbleme').each(function(element,i) {			
			if(element.getProperty('rel') && element.getProperty('rel')!='')				 
				tipstable2[i]=element.getProperty('rel').toString();			 
			else				 
				tipstable2[i]='pas encore fini';				 		
		});



		$$('.entry-utility span a').each(function(element,i) {
			element.addEvents({'mousemove':function(event){
						/*randombubble3.set('html', tipstable[i]);
						randombubble.setStyles({'top':(event.page.y-randombubble.getHeight().toInt()-3),'left':(event.page.x-randombubble.getWidth().toInt()/2) });*/
				   },
					'mouseleave':function(){
						randombubble.setStyles({'display':'none'});
					},
					'mouseover':function(){
						randombubble.setStyles({'display':'block'});
						randombubble3.set('html', tipstable[i]);
						randombubble.setStyles({'top':(element.getTop()-randombubble.getHeight().toInt()-3),'left':(element.getLeft()-randombubble.getWidth().toInt()/2+element.getWidth()/2) });
					}

				}

		   );
		});
		randombubble.addEvent('mousemove',function(event){
			 randombubble.setStyles({'top':(event.page.y-randombubble.getHeight().toInt()-5),'left':(event.page.x-randombubble.getWidth().toInt()/2) });
		});



		$$('.bubbleme').each(function(element,i) {
			element.addEvents({'mousemove':function(event){
						/*randombubble3.set('html', tipstable2[i]);
						randombubble.setStyles({'top':(event.page.y-randombubble.getHeight().toInt()-3),'left':(event.page.x-randombubble.getWidth().toInt()/2) });*/
				   },
					'mouseleave':function(){
						randombubble.setStyles({'display':'none'});
					},
					'mouseover':function(){
						randombubble.setStyles({'display':'block'});
						
						randombubble3.set('html', tipstable2[i]);
						randombubble.setStyles({'top':(element.getTop()-randombubble.getHeight().toInt()-3),'left':(element.getLeft()-randombubble.getWidth().toInt()/2+element.getWidth()/2) });
						
					}

				}

		   );
		});


	}

}	
	
	

/* animation multi site */
function multisite(){
	var itemmulti = [];
	$$('#multisite-header ul li').each(function(el,i){
		itemmulti[i] = new Fx.Morph(el, {duration:800,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'margin-top':10});

		el.addEvents({
		'mouseover':function(e){
			itemmulti[i].start({'margin-top':0});
		},
		'mouseleave':function(e){
			itemmulti[i].start({'margin-top':10});
		},
		
		});
	});
}

	
/* HOVER POSTS */
function hoverposts(){
	/* hover mosaique */
	var imgmosa = [];
	$$('.itemmosaic .content-overflow p').each(function(el,i){
		imgmosa[i] = new Fx.Morph(el, {duration:600,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'margin-top':0});
	});
	
	$$('.itemmosaic .content-overflow').each(function(el,i){
		el.addEvents({
		'mouseover':function(){
			imgmosa[i].start({'margin-top':-125});
		},
		'mouseleave':function(){
			imgmosa[i].start({'margin-top':0});
		}
		
		});
	});
	
	/* hover full */
	var imgfull = [];
	$$('.itemfull .content-overflow').each(function(el,i){
		
		imgfull[i] = new Fx.Morph(el.getChildren('p')[0], {duration:600,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'margin-left':0});
	});
	$$('.itemfull .infos').each(function(el,i){
		el.addEvents({
		'click':function(e){
			e.stop();
			if(!el.hasClass('active')){
				$$('.itemfull .infos')[i].addClass('active');
				
				
				var scrolltop = $$('.itemfull .content-overflow')[i].getPosition().y;
				var boxheight = $$('.itemfull .content-overflow')[i].getHeight();
				var bodyheight = window.getHeight();
				var newtop = scrolltop - (bodyheight-boxheight)/2;
				new Fx.Scroll(window, {duration: 600,wait:false,transition:Fx.Transitions.Expo.easeInOut}).start(0, newtop).chain(function(){
					imgfull[i].start({'margin-left':-300});
				});
				
			}else{
				$$('.itemfull .infos')[i].removeClass('active');
				imgfull[i].start({'margin-left':0});
			}
		}
		
		});
	});
	
	
	/* hover random */
	var imgrandom = [];
	$$('#randompics .content-overflow p').each(function(el,i){
		imgrandom[i] = new Fx.Morph(el, {duration:600,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'margin-top':0});
	});
	
	$$('#randompics .content-overflow').each(function(el,i){
		el.addEvents({
		'mouseover':function(){
			imgrandom[i].start({'margin-top':-125});
		},
		'mouseleave':function(){
			imgrandom[i].start({'margin-top':0});
		}
		
		});
	});
}
	
/* hide next link */
function hideNext(){
	if($('nextcontent'))
		$('nextcontent').setStyles({'text-indent':'-9999px','visibility':'hidden','position':'absolute'});
}
	
/* LOAD MORE */
function loadmore(){
	
	hideNext();
		
	if($('loaderpng')){
		loader1 = new Fx.Morph($('loaderpng'), {duration:800,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'opacity':0});
		loader2 = new Fx.Morph($('loaderpng1'), {duration:800,wait:false,transition:Fx.Transitions.Expo.easeOut}).set({'opacity':1});
	}

			
	if($('loading')){
		var loaderhtml = $('loading').get('html');
	}
	
	var href=$$('#display-mode li a')[0].getProperty('href');
	if($$('#display-mode li a')[1].hasClass('current'))
		href=$$('#display-mode li a')[1].getProperty('href');
	var tabhref = href.split('=');
	if($$('#nextcontent a')[0]){
		var getsend = '';
		var tabsend = '';
		tabsend = $$('#nextcontent a')[0].getProperty('href').split('?');
		getsend = tabsend[0]+'?ajax=1&view='+tabhref[1];
		$('loading').setStyles({'display':'block'});
	}else{
		$('loading').setStyles({'display':'none'});
	}
	
	if($('loaderpng')){          
		var loader = new clrzMachinegun({
		elt:'loaderpng',
			fps:48,
			length:500
		});
	}
	var request = new Request({
            url: getsend,
            method: 'get',
            update: 'refresh-me',
            onRequest: function(response) {
					loader.Play();      
				
            },
            onComplete: function(response) {
                if($('nextcontent')){
						$$('.reloaddiv #nextcontent')[0].destroy();
						var newelement = new  Element('div').injectInside($$('.reloaddiv')[0]);
						newelement.set('html',response);
						$$('.itemfull .infos').each(function(el,i){el.removeEvents('click');});
						$$('#nav-below p.load a').each(function(el,i){el.removeEvents('click');});
						$$('#display-mode li a').each(function(el,i){el.removeEvents('click');});
						hoverposts();
						tips();
						hideNext();
						loader1.set({'opacity':0});
						loader2.set({'opacity':1});
						loader.Stop();
						loader.rewind();
						loadmore();
						changetemplate();
						
						$$('#display-mode li a').each(function(el,i){el.removeEvents('mouseover');});
						$$('#display-mode li a').each(function(el,i){el.removeEvents('mouseleave');});
						$$('.entry-utility span a').each(function(el,i){
							el.addEvents({
								'mouseover':function(){
									el.getParent().addClass('hover');
								},
								'mouseleave':function(){
									el.getParent().removeClass('hover');
								}
							});
						});
                }
            }
    });

    var doRefresh = function() {
		var href=$$('#display-mode li a')[0].getProperty('href');
		if($$('#display-mode li a')[1].hasClass('current'))
			href=$$('#display-mode li a')[1].getProperty('href');
		tabhref = href.split('=');
		
		tabnext = $$('#nextcontent a')[0].getProperty('href').split('page/');
		tabnext2 = tabnext[1].split('/');
		
		getsend = $$('#nextcontent a')[0].getProperty('href');
        request.send();
    };
	
	
	$$('#nav-below p.load a').addEvents({
		'click':function(e){
			e.stop();
			loader1.set({'opacity':1});
			loader2.set({'opacity':0});
			$$('.itemfull .infos').removeClass('active');
			doRefresh();
		}
	});
}
	
	
	
	
	
	
	
	
	
	
	
	
/* CHANGEMENT DE TEMPLATE */
function changetemplate(){
	var templates = [];
	
	var href=$$('#display-mode li a')[0].getProperty('href');
	if($$('#display-mode li a')[0].hasClass('current'))
		href=$$('#display-mode li a')[1].getProperty('href');
	var tabhref = href.split('=');
	var getsend2 = href+'&ajax=2';

	var request = new Request({
            url: getsend2,
            method: 'get',
            update: 'refresh-me',
            onRequest: function(response) {
            },
            onComplete: function(response) {
				$$('.reloaddiv')[0].set('html',response);
				$$('.itemfull .infos').each(function(el,i){el.removeEvents('click');});
				$$('#nav-below p.load a').each(function(el,i){el.removeEvents('click');});
				$$('#display-mode li a').each(function(el,i){el.removeEvents('click');});
				$$('.bubbleme').each(function(el,i){el.removeEvents('mouseover');});
				$$('.bubbleme').each(function(el,i){el.removeEvents('mousemove');});
				$$('.bubbleme').each(function(el,i){el.removeEvents('mouseleave');});
				hoverposts();
				hideNext();
				loadmore();
				changetemplate();
				
				if($('content')){
					if($('content').hasClass('display-full')){
						$('content').removeClass('display-full');
						$('content').addClass('display-mosaic');
					}else{
						$('content').addClass('display-full');
						$('content').removeClass('display-mosaic');
					}
				}
						$$('#display-mode li a').each(function(el,i){el.removeEvents('mouseover');});
						$$('#display-mode li a').each(function(el,i){el.removeEvents('mouseleave');});
						$$('.entry-utility span a').each(function(el,i){
							el.addEvents({
								'mouseover':function(){
									el.getParent().addClass('hover');
								},
								'mouseleave':function(){
									el.getParent().removeClass('hover');
								}
							});
						});
				
				tips();
            }
    });

    var doRefresh = function() {
        request.send();
    };
	$$('#display-mode li a').each(function(el,i){
		if(!el.hasClass('singlelink')){
			el.addEvents({
			'click':function(e){
				e.stop();
				if(!el.hasClass('current')){
					$$('#display-mode li a').removeClass('current');
					$$('#display-mode li a')[i].addClass('current');
					doRefresh();
				}
			}
			
			});
		}
	});

}
	
	
/* ANCRE SMOOTHSCROLL */
function smoothScroll(e) {
	var event = new Event(e);event.stop();
	var target = (!event.target.href) ? event.target.parentNode : event.target;
	var divToScrollTo = target.title.split('#')[1];
	var divToScrollTo = target.href.split('#')[1];
	new Fx.Scroll(window, {
		duration: 2000,transition:Fx.Transitions.Quart.easeInOut
		}).toElement(divToScrollTo);
		
}
function initSmoothScroll(linkancre) {
	var smooth_links = $$(linkancre);
	for (i = 0; i < smooth_links.length; i++) {
			smooth_links[i].addEvent("click", smoothScroll);
	}

}



