var contact_picker = null;
var overlay = null;
var msgToSend = 'Check deze reis:';
var psmToSet = 'Deze reis lijkt me wel wat:'

function sendLink() {
	$('wllSendDialog').hide();
	if (overlay) overlay.style.display = 'none';
	var obj = document.forms.wllform;
	if (obj.editpsm.checked) {
		setPSM();
	}
	var objs = obj.elements;
	var len = objs.length;
	var cids = new Array();
	for (var i = 0; i < len; i++) {
		if (objs[i].name == 'cid[]') {
			startConversation(objs[i].value, "text");
		}			
	}
}

function startConversation(cid, msg) {
	if (cid) {
		var tag = Microsoft.Live.Messenger.UI.Tags.TagsFactory.createTag('conversation', { cid: cid, onconversationcreated : 'sendConvMsg' });	
		document.body.appendChild(tag);
		tag.style.display = 'none';
	}			
}

function setPSM() {
	if (pageTracker) pageTracker._trackPageview('/live/set-psm');
	
	var msg = psmToSet + window.location;
	var _user = Microsoft.Live.Messenger.UI.Tags.TagsFactory.get_user();
	_user.get_presence().set_personalMessage(msg);	
}

function sendConvMsg(e) {
	if (pageTracker) pageTracker._trackPageview('/live/send-msg');
	
	var msg = new Microsoft.Live.Messenger.TextMessage(msgToSend + window.location, null);
	var conv = e.get_conversation();
	conv.sendMessage(msg, null);	
	
//	Microsoft.Live.Messenger.UI.Tags.TagsFactory.disposeTag(contact_picker);
//	document.body.removeChild(contact_picker);
//	contact_picker = null;			
}

function createContactPicker() {	
	if (pageTracker) pageTracker._trackPageview('/live/show-contact-picker');
	
	if (contact_picker == null) {
		contact_picker = Microsoft.Live.Messenger.UI.Tags.TagsFactory.createTag('contact-picker');
	}
	if(!overlay){
		overlay = document.createElement('div');
		overlay.setAttribute('id', 'overlay' );
		overlay.onclick = function(){
			$('wllSendDialog').hide();
			this.style.display = 'none';
		};
		document.body.appendChild(overlay);
		document.getElementById('closeWllSend').onclick = function(){
			$('wllSendDialog').hide();
			overlay.style.display = 'none';
		};
	}
	overlay.style.display = 'block';
	
	$('contactPicker').appendChild(contact_picker);
	$('wllSendDialog').show();		
}

function ajaxLogin() {
	var user = Microsoft.Live.Messenger.UI.Tags.TagsFactory.get_user();
	ajaxWll.login(user.get_identity().get_cid());
}

var ajaxWll = {
	login: function(cid) {
		var url = '/live/ajax.php?cid='+cid;
		new Ajax.Request(url, { 
			onSuccess: function(transport) {
				//replace loader with nr of available results
				$('resultsFound').style.opacity = '';
				$('resultsFound').style.filter = '';
				nrTarget.innerHTML = transport.responseText;
			},
			onFailure: function(transport) {
				alert ('ajax error')
			}
		});
	}
}