function addSmiley(id,key){
	if($(id))
		$(id).value += " "+key+" ";
}

function delAlbumPics(){
	return confirm("\352tes vous sure de vouloire supprimer cette photo ?");
}

function spanToInput(id,name,type){
	var span = $(id);
	if(!span)
		return;
	
	var value = span.innerHTML;
	var previous = span.previous();
	if(type == "text"){
		span.remove();
		var input = new Element("input",{'name':name,'id':name,'value':value,'class':"input250"});
		Element.insert(previous,{'after':input});
		previous.remove();
	}else if(type == "anniv"){
		span.remove();
		var daten = value.split("/");
		var nspan = new Element("span");
		nspan.insert(new Element("input",{'name':name+"_day",'id':name+"_day",'value':daten[0],'size':'2','maxlength':'2','class':'input'}));
		nspan.insert(" / ");
		nspan.insert(new Element("input",{'name':name+"_month",'id':name+"_month",'value':daten[1],'size':'2','maxlength':'2','class':'input'}));
		nspan.insert(" / ");
		nspan.insert(new Element("input",{'name':name+"_year",'id':name+"_year",'value':daten[2],'size':'4','maxlength':'4','class':'input'}));
		Element.insert(previous,{'after':nspan});
		previous.remove();
	}else if(type == "cp"){
		span.remove();
		var input = new Element("input",{'name':name,'id':name,'value':value,'class':"input250"});
		Element.insert(previous,{'after':input});
		previous.remove();
		input.observe("keyup",function(){
				if(this.value.length == 5){
					new Communes(this.value);
				}
		});


	}else if(type=='pays'){
		span.remove();
		var input = new Element("select",{'name':name,'id':name,'class':"input250"});
		Element.insert(previous,{'after':input});
		previous.remove();
		input.observe("change",function(){
			if(this.value != 0){
				if($('codepos')){
					$('codepos').hide();
					$('div_commune').hide();
				}
			}else{
				if(!$('codepos'))
					addCPchamps($('paysCont'));
				$('codepos').show();
				$('div_commune').show();
			}
			
			if(this.value <= 3){
				if($('paysPlus'))
					Element.remove($('paysPlus'));
				$('pays').setAttribute('name',"pays")
			}
			if(this.value > 3){
				this.setAttribute('name',"");
				if($('paysPlus'))
					Element.remove($('paysPlus'));
				var input1 = new Element("select",{'name':'pays','id':'paysPlus','class':"input250"});
				Element.insert(this,{'after':input1});	
				new updatePays(input1,'list',this.value);
			}
		});
		new updatePays(input,'cat',0);
	}

}

function addCPchamps(obj){
html = '<p class="ligneContener" id="codepos"><b class="leftTab">Code postal :</b> <a href="javascript://" onclick="spanToInput(\'mod_cp\',\'cp\',\'cp\');" class="rightTabLink">Modifier</a><span id="mod_cp">-</span></p><p class="ligneContener ligneBG" id="div_commune"><b class="leftTab">Ville :</b><span id="mod_ville">-</span></p>';
	Element.insert(obj,{after:html});
}

function showLoading(obj){
	var loading = new Element("div",{
		'class':'loading'
	}).update("Chargement en cours ...");
	loading.hide();
	obj.insert(loading);
	loading.style.width = loading.getWidth()+"px";
	loading.show();
	return loading;
}

var Message = Class.create({
	initialize:function(obj,id,fl,login){
		this.id = id;
		this.obj = obj;
		this.fl = fl;
		this.login = login;
		this.createBox();
	},
	createBox:function(){
		this.contener = new Element('div',{
			'id':'msg_'+this.id,
			'class':"messageContener"
		});
		Element.insert(this.obj,{'after':this.contener});
		showLoading(this.contener);
		var _self = this;	
		new Ajax.Request("/actions,readMessage.php",{
			method:'post',
			parameters:'id='+this.id+'&do='+this.fl,
			onSuccess:function(xhr){
				_self.displayMessage(xhr);
			}
		});
	},
	displayMessage:function(xhr){
		var jsonObj = xhr.responseJSON;
		if(jsonObj.error)
			return;
		
		var divCont = new Element('div');
		var divclear = new Element('div',{'class':'clear'});
		var divC = new Element('div',{'class':'left'});
		var div = new Element('div',{'class':'img100-100'});
		div.setStyle("margin-bottom:5px;");
		var img = new Element('img',{'src':'/photos/100-100/'+jsonObj.pics});
		div.update(img);
		divC.insert(div);
		var close = new Element('a',{'href':'javascript://','idMess':this.id}).update("Fermer");
		close.observe("click",function(){
			var idMess = this.getAttribute('idMess');
			if($('msg_'+idMess))
				$('msg_'+idMess).hide();
		});
		close.update(new Element('img',{'src':'/images/buttons/fermer.gif'}));
		close.setStyle("display:block;margin-bottom:5px;");
		divC.insert(close);

		if(this.fl == 'to'){
		var repondre = new Element('a',{'href':'/send-message.php?login='+this.login}).update("Fermer");
		repondre.update(new Element('img',{'src':'/images/buttons/repondre.gif'}));
		divC.insert(repondre);
			$('msgL_'+this.id).setStyle("font-weight:normal;color:#666;");
			$('msgD_'+this.id).setStyle("font-weight:normal;color:#666;");
			$('msgS_'+this.id).setStyle("font-weight:normal;color:#666;");
			$('msgI_'+this.id).setAttribute('src',jsonObj.mess.type == 0 ? '/images/icones/message-off.gif' : '/images/icones/video-off.gif');	
		}

		if(jsonObj.mess.type == 0){
			var p = new Element('p').update(jsonObj.mess.message);
			p.setStyle("width:410px;padding:0 4px 0 4px;");
		}else{
			var p = new Element('p',{'id':'flContener'+this.id});
			p.setStyle("width:410px;padding:0 4px 0 4px;");
		}
		divCont.insert(divC);
		divCont.insert(p);
		divCont.insert(divclear);
		this.contener.update(divCont);
		var so = new SWFObject('/flash/mediaplayer.swf','jstest','320','240','8',"#FFF");
                so.addParam('allowfullscreen','true');
                so.addVariable('width','320');
                so.addVariable('height','240');
                so.addVariable('file','http://chat.croquecoeur.fr/videos/'+jsonObj.mess.message+'.flv');
                so.write('flContener'+this.id);

		

		//this.contener.insert(p);
		//this.contener.insert(divclear);
	}
});


function readMessage(obj,id,fl,login){
	if(!$('msg_'+id))
		new Message(obj,id,fl,login);
	else{
		$('msg_'+id).toggle();
	}
}

function checkAll(className,check){
	var box = $$("."+className);
	if(box)
		box.each(function(n){
			n.checked = check; 
		});

}

function openDial(id,login,sex){
	if(!isAbo)
		location.replace("/abonnement.php");
	else
		parent.frames["chat"].openDial(id,login,sex);
}

function addInvitChat(idto){
	if(!isAbo)
		location.replace("/abonnement.php");
	else
		parent.frames["chat"].addInvit(idto);
}

function setVideoStats(pub){
	new Ajax.Request('actions,setVideoStats.php',{
		method:'post',
		parameters:'pub='+pub
	});
	alert("Le status de votre annonce vid\351o a bien \351t\351 modifi\351");
}
