//etoile
function star_zero(){
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		$(star_id).setProperty('src','commentaire/images/star1.gif');
	}
}

function star_rate(level){
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		if( level >= i ){
			$(star_id).setProperty('src','commentaire/images/star2.gif');
		}else{
			$(star_id).setProperty('src','commentaire/images/star1.gif');
		}
	}
}

function star_valider(level){
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		$(star_id).setProperty('onmouseout','star_rate('+level+')');
	}
}

function star_select(){
	var nb_star = 0;
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		if( $(star_id).getProperty('src') == "commentaire/images/star2.gif" ) nb_star = i; 
	}
	return nb_star;
}

function reset_star(){
	$('auteur').value = '';
	$('mail').value = '';
	$('comment').value = '';
	$('security_code').value = '';
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		$(star_id).setProperty('src','commentaire/images/star1.gif');
		$(star_id).setProperty('onmouseout','star_zero()');
	}
}


function unselect_star(){
	for(var i = 1; i <= 5; i++){
		var star_id = "star_"+i;
		$(star_id).setProperty('src','commentaire/images/star1.gif');
		$(star_id).setProperty('onmouseout','star_zero()');
	}
}

function maj_etoile(){
	var variable={
		nom:$('nom_commentaire').value
	}
	new Request({method:'post',url:'commentaire/include/maj_etoile.php',data:variable,evalResponse:true,	evalScripts: true}).send();
}
//fin etoile


//pays
function pays_new_comment(){
	var variable={
		nom:$('nom_commentaire').value
	}
	new Request({method:'post',url:'commentaire/include/pays_new_comment.php',data:variable,evalResponse:true,	evalScripts: true}).send();
}

window.addEvent('domready', function() {
//fx slide
/*var myVerticalSlide = new Fx.Slide('vertical_slide');
myVerticalSlide.hide();
$('comment_toggle').addEvent('click', function(e){
	e.stop();
	myVerticalSlide.toggle();
});*/
//fin fx slide






function new_captcha(){
	var variable = $random(0, 9);
	$('img_captcha').setProperty('src', 'commentaire/include/captcha.php?width=100&height=40&characters=5&'+variable);
}

$('bt_valider').addEvent('click', function(){
	var variable={
		nom:$('nom_commentaire').value,
		mail:$('mail').value,
		pseudo:$('auteur').value,
		comment:$('comment').value,
		security_code:$('security_code').value,
		nb_star:star_select()
	}
	new Request({method:'post',url:'commentaire/include/ajax.php',data:variable,
		onComplete: function(reponse){
			new_captcha();
			if(reponse != "true") return alert( reponse );
			if($('comment4')) $('comment4').setProperty('id','comment5');
			if($('comment3')) $('comment3').setProperty('id','comment4');
			if($('comment2')) $('comment2').setProperty('id','comment3');
			if($('comment1')) $('comment1').setProperty('id','comment2');

			//check la derniere div commentaire
			if($('comment5')){
				div_a_supprimer = "comment5";
				nb_comment = 5;
			}else{
				nb_comment = 0;
				div_a_supprimer = "";
			}

			if( (div_a_supprimer=="comment5")&&(nb_comment>0) ){
				//$(div_a_supprimer).fade(0);
				destroyer.delay(500,$(div_a_supprimer));
				insert_comment.delay(500);
			}else{
				insert_comment.delay(500);
			}
		},
		onFailure : new_captcha
	}).send();
	//new_captcha.delay(500);
	});

});



function destroyer(id){
	this.destroy();
}


function insert_comment(){
	var auteur = $('auteur').value;
	var mail = $('mail').value;
	var comment = $('comment').value;
	comment = comment.replace(/\n/g,"<br/>");
	var nb_etoile = star_select();
	var newComment = new Element('div', {
 		'id': 'comment1',
 		'styles': {
 			'width': '658px',
			'position': 'absolute',
			'top': '-500',
			'padding-bottom': '10px'
 		}
	});

	var format_date = new Date();
	var date_post = format_date.getDay()+"-"+format_date.getMonth()+"-"+format_date.getFullYear();
	var time_post = format_date.getHours()+"h"+format_date.getMinutes();
	
var contenu = "<div style='height:32px'>";
	contenu+= "<div style='background:url(commentaire/images/top-left2.gif) top left no-repeat;border:none;width:16px;height:32px;float:left'></div>";
	contenu+= "<div style='background:url(commentaire/images/top-middle2.gif) top repeat-x;border:none;width:616px;height:32px !important;height:25px;color:#FFFFFF;font-weight:bold;float:left;padding-top:7px'>";
	contenu+= "<span style='float:left'>Posted by <span style='color:#ff984d'>"+auteur+", </span><span id='pays_new_comment'></span></span><span style='float:right'>"+date_post+" at "+time_post+"</span>";
	contenu+= "</div><div style='background:url(commentaire/images/top-right2.gif) top right no-repeat;border:none;width:16px;height:32px;float:left'></div></div>";
	<!-- contenu -->
	contenu+= "<div style='width:646px;border-left:1px solid #5b5b5b;border-right:1px solid #5b5b5b;background:#ffffff;padding-top:4px !important'>";
	contenu+= "<div style='width:25px;height:42px;float:left;padding-left:10px'><img src='commentaire/images/poster.jpg'></div>";
	contenu+= "<div style='padding-left:10px;margin-right:10px !important;margin-right:0px;overflow:hidden'>"+comment+"</div></div>";
	<!-- pied -->
	contenu+= "<div><div style='width:6px;height:8px;background:url(commentaire/images/bottom-left2.gif) top left no-repeat'></div>";
	contenu+= "<div style='width:636px;height:8px;background:url(commentaire/images/bottom-middle2.gif) top repeat-x'></div>";
	contenu+= "<div style='width:6px;height:8px;background:url(commentaire/images/bottom-right2.gif) top right no-repeat'></div>";
	contenu+= "</div>";
	<!-- etoile -->
	var note = "<span style='float:right'>";
	for(var i = 1; i <= 5; i++){
		if( nb_etoile >= 1 ){
			nb_etoile = nb_etoile - 1;
			note+= "<img src='commentaire/images/star2.gif' />";
		}else{
			if( nb_etoile >= 0.5 ){
				nb_etoile = nb_etoile - 0.5;
				note+= "<img src='commentaire/images/star3.gif' />";
			}else{
				note+= "<img src='commentaire/images/star1.gif' />";
			}
		}
	}
	note+= "</span>";
	var form = "<div style='width:165px;padding-right:10px;height:21px;float:right'><span style='padding-top:5px;float:left'>voted : </span>"+note+"</div>";
	contenu+= form;
	<!-- fin etoile -->
	contenu+= "</div>";
	
	newComment.innerHTML = contenu;
	newComment.inject($('div_commentaire'), 'top');
	maj_etoile();
	pays_new_comment();
	reset_star();
	dimensionnement_remove_comment();
}


function dimensionnement_remove_comment(){
	var hauteur = $('comment1').getCoordinates();
	var hauteur = hauteur.height;
	var myFx = new Fx.Tween($('div_commentaire'));
	myFx.start('padding-top', hauteur);
	dimensionnement_insert_comment.delay(500);
}

function dimensionnement_insert_comment(){
	var top_div = $('div_commentaire').getPosition();
	top_div = top_div.y;
	var myFx = new Fx.Tween($('comment1'));
	myFx.start('top', top_div);

	remove_position_absolute.delay(500);
}

function remove_position_absolute(){
	$('div_commentaire').setStyle('padding-top', '');
	$('comment1').setStyle('position', '');
	$('comment1').setStyle('top', '');
}
