/**
 * fonctions ajax pour Indy-musics-V2.0
 * @author franckysolo
 * avec le framework mootools
 * 
 */
function setCssStyle(name) {
	var myUrl = 'application/ajax/ajax.php?action=setCssStyle&id='+name;	
	var req = new Request({
				url : myUrl ,
				method :  "get"	,
				onComplete: function() {					
				}
		});
	req.send();
}					
function harmonyTable() {
	var div = $('scale');
	if(div != undefined) {
		var value = $('tonique').value;
		var myUrl = 'application/ajax/ajax.php?action=harmonyTable&tonique='+value;	
		var req = new Request({
					url : myUrl ,
					method :  "post"	,
					evalScripts: true,
					onComplete: function() {
						div.set('html', this.response.text);
					}
			});
		req.send('tonique='+value);
	}
}
function tableMode() {
	var div = $('tableMode');
	if(div != undefined) {
		var link = $$('a.mode');
		var bg = $('bg');
		var value = 0;
		link.each(function(el){
			 value = el.get('rel');
			// alert(value);
		});
		var myUrl = 'application/ajax/ajax.php?action=tableMode&tonique='+value;	
		var req = new Request({
					url : myUrl ,
					method :  "get"	,
					evalScripts: true,
					onComplete: function() {
						div.set('html', this.response.text);
					}
			});
		req.send();
	}
}
function generatePassword() {	
	var myUrl = 'application/ajax/ajax.php?action=generatePassword';	
	var req = new Request({
				url : myUrl ,
				method :  "get"	,
				onComplete: function() {
					$('passClear').set('html', 'Votre nouveau mot de passe : '+ this.response.text);
					$('passClear').setProperty('class','indexMsg');
					$('passClear').highlight('#cce');
					$('pass').value = this.response.text;
					$('passConfirm').value = this.response.text;
					analyseSecurityPass();
				}
		});
	req.send();	
}
function wrappComment() {	
	var wrapper = $('wrapperCom');
	var txt = $('linkCom');
	var fx = new Fx.Slide(wrapper,  {
		mode:'vertical',
		open: false,
		wait: true,
		transition : Fx.Transitions.Quad.easeOut,
		onComplete : function(){		
			if(fx.open == false) {
				txt.set('html','Afficher les commentaires');	
			} else {
				txt.set('html','Cacher les commentaires');
			}
		}		
	});
	fx.toggle();	
}
function getNewsComments(id) {	
	var myUrl = 'application/ajax/ajax.php?action=getNewsComments&id='+id; 	
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {		
			
			var div = new Element('div');
			div.set('html', this.response.text);	
			$('contentNews').adopt(div).setStyle('padding' , '1%');
			sondageView();
		}
	}).send();		
}
function submitNewVote(ip, id) {
	var myUrl = 'application/ajax/ajax.php?action=submitNewVote'; 
	var div = $('alertVotation');
	var req = new Request({
		url : myUrl ,
		method :  "post"	,
		onComplete: function() {		
			div.set('html', this.response.text);			
			div.fade('in');
			timer = setTimeout( function(){
				div.fade('out');
				showMiniSondage();
				div.set('html', '');
			},3000);
			
		}
	}).send('id='+id+'&ip='+ip);	
}
function activeSondage(id) {
	var div = $('activeSondage');
	var myUrl = 'application/ajax/ajax.php?action=activeSondage&id='+id;
	var fx = Fx.Slide(div);
	var timer = null;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
			div.set('html', '<p class="info_msg">Le sondage est activé.</p>');	
		}
	}).send();
	div.fade('in');
	timer = setTimeout(function(){
		div.fade('out');
	},1000);
}
function desactiveSondage(id) {
	var div = $('activeSondage');
	var myUrl = 'application/ajax/ajax.php?action=desactiveSondage&id='+id;
	var fx = Fx.Slide(div);
	var timer = null;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
			div.set('html', '<p class="info_msg">Le sondage est désactivé.</p>');		
		}
	}).send();
	div.fade('in');
	timer = setTimeout(function(){
		div.fade('out');
	},1000);
}

function isPseudoFree() {
	var div = $('ajax');
	var value = $('login').value;
	var myUrl = 'application/ajax/ajax.php?action=isPseudoFree&login='+value;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
			div.set('html',this.response.text);
		}
	});
	req.send();		
}

function isValidMail() {
	var div = $('validMail');
	var value = $('email').value;
	var myUrl = 'application/ajax/ajax.php?action=isValidMail&email='+value;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
			div.set('html',this.response.text);
		}
	});
	req.send();	
}
function showMiniSondage() {
	var div = $('homeSondage');
	if(div != undefined) {
		var myUrl = 'application/ajax/ajax.php?action=showMiniSondage';
		var req = new Request({
			url : myUrl ,
			method :  "post",
			evalScripts: true,
			onComplete: function() {
				div.set('html',this.response.text);
			}
		});
		req.send();	
	}
}
function voteArtiste(id) {
	var timer = null;
	var div = $('vote');
	var message = " ";
	var myUrl = 'application/ajax/ajax.php?action=voteArtiste&id='+id;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
				div.set('html', this.response.text);
				div.fade('in');
				timer = setTimeout( function(){
					div.fade('out');
					div.set('html', '');
				},5000);
		}
	});
	req.send();	
}
function openImager(id) {
	var timer = null;
	var div = $('vote');
	var message = " ";
	var myUrl = 'application/ajax/ajax.php?action=openImager&id='+id;
	var req = new Request({
		url : myUrl ,
		method :  "get"	,
		onComplete: function() {
				//tips();
				halfBox(this.response.text);			
		}
	});
	req.send();	
}
function percent(x) {
	var txt = $('txt-percent').set('morph',{
		duration: 1000, 
		onComplete: function(){
			$('alert').fade('out');
			$('mB').fade('in');
		}
	});
	x++;
	if(x < 99) {
		txt.set('html',x+' % ');
	} else {
		txt.set('html',' 100% ');
		txt.morph({color: '#f00'});	
	}	
}
function progressBar() {	
	var div = $('myUpload');
	var percent = $('percent');
	var timer = null;
	var i = 0;
	if(div != undefined) {
		$('progressBar').set('tween', {duration: 1000});
		$('alert').set('html','Veuillez patientez...');		
		while(i < 100) {			
			i++;		
			timer = setTimeout('percent('+i+')',i * 10);
			$('progressBar').tween('width',[0, 4 * i]);		
		}		
	}	
}
//Fonctions Indy Chat

function checkNewChatMessage(id) {
	var i = j = 0;	
	var req = new Request({
		url : 'application/ajax/chat.php',
		evalScripts : true,
		evalResponse : true,
		method : 'post',
		onComplete : function() {			
			var text = this.response.text;	
			if(text != '') {
				formatResponse(id, text);
			}
		}					
	});
	req.send('action=checkNewChatMessage&id='+id);	
}

function formatResponse(id, text) {
	var chats = text.split(';');
	for( i = 0; i < chats.length ; i++ ) {		
		var arr = chats[i].split(',');
		var idC = arr[0];
		var pseudo = arr[4].toString();
		var status = arr[3].toString();
		var rel = 'chat-'+arr[1]+'-'+arr[2]+'.html';
		var img = setIconAlert(pseudo, i);
		var a = new Element('a', {
			href : '#',
			rel : rel,
			'class' : 'openChat'
		});
		
		if(status == "'diff'") {
			img.setProperty('src', 'public/images/chat-new.png');
			if(undefined == $('chatAlert-'+i)) {
				a.adopt(img);
				a.injectInside($('toolBar'), 'after', $('online'));		
			} 
			alertChatIcon(img);
			tips();
			iconChatEvent(id, idC, img);
		}	
	}		
}

function iconChatEvent(id, idC, img) {
	var pop = null;
	$$('.openChat').each(function(link){
		link.addEvent('click', function(e){	
			var href = link.get('rel');
			pop = window.open
			(
				href, 
				'chat', 
				'width=510, height=410'
			);
			if(pop) {
				img.setProperty('src', 'public/images/chat.png');
			} else {
				img.setProperty('src', 'public/images/chat-new.png');
			}	
		});
	});	
}

function alertChatIcon(el) {	
	var fx = new Fx.Tween(el, {property : 'opacity'});
	fx.start(1,0).chain(
		    function(){ this.start(0,1); },
		    function(){ this.start(1,0); },
		    function(){ this.start(0,1); },
		    function(){ this.start(1,0); },
		    function(){ this.start(0,1); }
	);	
}

function setIconAlert(pseudo, i) {
	return new Element('img',  {
		src : 'public/images/chat-new.png',
		alt : 'chat',
		id : 'chatAlert-'+i,
		title : 'Nouveau message de '+pseudo+' :: Sur le chat',
		width : 20,
		height : 20,
		'class' : 'tips'
	});
}

function isThereOtherConnected(id) {
	var req = new Request({
		url : 'application/ajax/chat.php',
		evalScripts : true,
		evalResponse : true,
		method : 'post',
		onComplete : function() {
			if(this.response.text == 1) {	
				checkNewChatMessage(id);
			} 
		}					
	});
	var rt = function() {
		req.send('action=isThereOtherConnected&id='+id);
	};
	rt.periodical(10000);		
}

