var noteApparence = function() {
	if(Browser.Platform.mac || Browser.Platform.win || Browser.Platform.linux) {
		$('note').setStyle('margin-top', '-28px');
	}
	var noteEffect = new Fx.Morph($('note'), {duration: '200', link: 'cancel', transition: Fx.Transitions.Sine.easeOut});
	$('crea').addEvent('mouseover', function(e){
		e.stop();
		noteEffect.start({ 'margin-top' : ['-63px'] });
	});
	$('crea').addEvent('mouseleave', function(e){
		e.stop();
		noteEffect.start({ 'margin-top' : ['-28px'] });
	});
	$('note').setStyle('opacity', '0.9');
}

var lienImg = function() {
	$$('.image-suivante').each(function(el, i){
		el.addEvent('click', function(e){
   			e.stop();
	   		var ajaxUrl = el.getProperty('href');
   			var ajRequest = new Request.HTML({
   				method: 'get',
   				url: ajaxUrl,
   				update: $('crea'),
	   			onRequest: function() {
	   				$('crea').fade('out');
				},
				onSuccess: function() {
					$('crea').fade('in');
					var ajaxRel = $('image-crea').getProperty('role');
	   				var dataID = ajaxRel.split('-')[0];
	   				var imgAct = Number(ajaxRel.split('-')[1]);
	   				var totalIMG = ajaxRel.split('-')[2];
					var numImgG = imgAct-1;
					var numImgD = imgAct+1;
					$('lien_vers_gauche').setProperty('href', '/image-crea.php?id='+dataID+'&img='+numImgG);
					$('lien_vers_droite').setProperty('href', '/image-crea.php?id='+dataID+'&img='+numImgD);
					if(imgAct==0){
						$('lien_vers_gauche').setProperty('href', '/image-crea.php?id='+dataID+'&img='+totalIMG);
					}
					if(imgAct==totalIMG){
						$('lien_vers_droite').setProperty('href', '/image-crea.php?id='+dataID+'&img=0');
					}
					noteApparence();
				}
   			}).send();
	   	});
	});
}

var vote = function() {
	$('avis').addEvent('click', function(e){
		e.stop();
		var ajaxUrl = $('avis').getProperty('href');
		var ajdRequest = new Request({
   				url: ajaxUrl,
	   			onRequest: function() {
	   				$('vote').set('html', 'En cours de traitement&#8230;');
				},
				onComplete: function(response){
					$('vote').set('html', response);
				}
   			}).get();
	});
}

var voteAjax = function() {
	var avisRequest = new Request.HTML({
		method: 'get',
		url: 'avis.php',
		onRequest: function() {
			$('vote').set('html', 'En cours de traitement&#8230;');
		},
		update: $('vote')
	});
	var AvisUrl = $('avis').getProperty('title');
	$('avis').addEvent('click', function(e){
		e.stop();
		avisRequest.send(AvisUrl);
	});
}

var retireMessage = function() {
	var ecrireEffect = new Fx.Morph($('panneau-bas'), {duration: '1000', link: 'cancel', transition: Fx.Transitions.Sine.easeOut});
	$('panneau-bas').set('html', '');
	$('panneau-bas').fade('out');
	ecrireEffect.start({ 'background-color' : ['transparent'],'padding' : ['0'] });
}

var similaire = function() {
	var ecrireEffect = new Fx.Morph($('panneau-bas'), {duration: '1000', link: 'cancel', transition: Fx.Transitions.Sine.easeOut});
	$('ecrire-similaire').addEvent('click', function(e){
		e.stop();
		var ajaxUrl = $('ecrire-similaire').getProperty('href');
		var ajdRequest = new Request({
   				url: ajaxUrl,
	   			onRequest: function() {
	   				ecrireEffect.start({ 'background-color' : ['#454c50'],'padding' : ['15px 0'] });
	   				$('panneau-bas').fade('in');
	   				$('panneau-bas').set('html', 'En cours de chargement&#8230;');
				},
				onComplete: function(response){
					$('panneau-bas').set('html', response);
					fermer();
					var validFormulaire = new FormCheck('formSimilaire', {
						submitByAjax: true,
						onAjaxRequest: function() {
							$('panneau-bas').set('html', 'En cours d&#8217;envoi&#8230;');
						},
						onAjaxSuccess: function(response) {
							$('panneau-bas').set('html', response);
							retireMessage.delay(2000);
							noteApparence();
							lienImg();
							if($('vote')) { vote(); }
							similaire();
						}
					});
				}
   			}).get();
	});
}

var fermer = function() {
	$$('.fermer').each(function(el, i){
	var ecrireEffect = new Fx.Morph($('panneau-bas'), {duration: '1000', link: 'cancel', transition: Fx.Transitions.Sine.easeOut});
	el.addEvent('click', function(e){
		e.stop();
		ecrireEffect.start({ 'background-color' : ['transparent'],'padding' : ['0'] });
		$('panneau-bas').fade('out');
		$('panneau-bas').set('html', '');
	});
});
}

window.addEvent('domready', function() {
	noteApparence();
	if($$('.image-suivante').length > 0){ lienImg(); }
	if($('vote')) { voteAjax(); }
	if($('ecrire-similaire')) { similaire(); }
	var myTips = new Tips('.tiptop', {
        className: 'tip-menu',
        fixed: true,
        offset: {'x': -111, 'y': 20}
    });
    var myTipsDown = new Tips('.tipdown', {
        className: 'tip-projet',
        fixed: true,
        offset: {'x': -111, 'y': -40}
    });
});
