/**
 * @author Alexandre araste
 */

var OBJmsnDisplay=Class.create()
OBJmsnDisplay.prototype = {
	initialize:function(divId, msn_ToSend, msn_contenu){
		
		this.divContainer = $(divId)
		this.message = new OBJdisplayMessage('pwPerdu_filter')
		this.message.setMessage(divId, false)
		this.login=''
		this.password=''
		this.msn_ToSend = (typeof msn_ToSend == 'string')? $(msn_ToSend):msn_ToSend
		this.msn_contenu = (typeof msn_contenu == 'string')? $(msn_contenu):msn_contenu
				
		Event.observe('msnOk','click',this.__msnValide.bindAsEventListener(this), true)
		Event.observe('msnCancel','click',this.__cancelMsn.bindAsEventListener(this), true)
		Event.observe('edit_msnContacts','click',function(e){this.message.showMessage()}.bindAsEventListener(this), true)
	},
	
	__getCompte:function(){
		var params='action=1&account=' + this.login + '&pw=' + this.password
			var x = new Ajax.Request('msnUtils.ajax', {method:'post',
				postBody:params,
				onLoading:function(){this.msn_contenu.innerHTML = 'Chargement en cours'}.bind(this),
				onLoaded:function(){this.msn_contenu.innerHTML = ''}.bind(this),
				onComplete:function(o){this.compte=eval(o.responseText);this.__bind()}.bind(this)
				})
	},
	
	__ulOnClick:function(e){
		var tg = Event.element(e)
			if (tg.tagName == 'INPUT'){
				var c = tg.getAttribute('index')
				this.compte.contacts[c].checked=tg.checked
			}
	},
	
	__bind:function(){
		this.disconnect()
		if (!this.compte.result) {this.msn_contenu.innerHTML=this.compte.message;return}
		var ul = document.createElement("UL")
		ul.id = 'listemsn'
		ul.className='listemsn'
		var i=-1
		this.compte.contacts.each(function(o){
			i +=1
			var li = document.createElement('LI')
			li.innerHTML="<input type=checkbox checked='true' value='" + o.mail + "' index='" + i + "'/>" + o.name
			ul.appendChild(li) 
		})
		
		Event.observe(ul, 'click',this.__ulOnClick.bindAsEventListener(this), true)
		$('msn_contenu').appendChild(ul)
	},
	
	__msnValide:function(e){
		var ul=$('listemsn')
		msnNbMails = 0
		this.msn_ToSend.value=''
		$A(ul.childNodes).each(function(o){
			   if(o.firstChild.checked) msnNbMails++
			   if (this.compte.contacts[o.firstChild.getAttribute('index')].checked) this.msn_ToSend.value += this.compte.contacts[o.firstChild.getAttribute('index')].mail +';'
		}.bind(this))
		
		$("msn_nombre").innerHTML=msnNbMails
		Element.hide('msn_login')
		Element.show('msn_result')
		this.message.hideMessage()
	},
	
	__cancelMsn:function(e){
		this.message.hideMessage()
	},
	
	show:function(msnLogin, msnPw){
		this.message.showMessage()
		this.login = msnLogin
		this.password = msnPw
		this.__getCompte()
			
	},
	
	getListeContacts:function(){
		this.__getCompte()
	},
	
	disconnect:function(){
		var a = new Ajax.Request('msnUtils.ajax',{method:'post', postBody:'action=3', asynchronous:false})
		var el = eval(a.transport.responseText)
		if (!el.result){alert(el.message)}
	}
}
