_user = null;
_identity = null;
_delAuthControl= null;

function onScriptsLoaded(){
	//console.log("scriptLoad");
	//delegationToken = $("#delegationTokenMsn").val();
	//appVerifierToken = $("#appVerifierTokenMsn").val();
	
	if (typeof Microsoft.Live.Messenger != "undefined" && $("#signinMsn,#signinMsn1").size() != 0) {
		listenerMsn();
	}else {
		loginMsn();
	}
}
function listenerMsn(){
	//alert($("#appVerifierTokenMsn").val()+"-"+$("#delegationTokenMsn").val());
	try {
		appVerifierToken = $("#appVerifierTokenMsn").val();
		
		var hostUrl = window.location.protocol + "//" + window.location.host;
		var privUrl = hostUrl + "/social/live/privacy.html";
		var chanUrl = hostUrl + "/social/live/channel.html";
		
		//console.log("listenerMsn",appVerifierToken);
		if (typeof Microsoft.Live.Messenger != "undefined" && $("#signinMsn,#signinMsn1").size() != 0) {
			//alert($("#delegationTokenMsn").val()+"-");
			if ($("#signinMsn").size() != 0 && typeof appVerifierToken != "undefined") {
				_delAuthControl = new Microsoft.Live.Messenger.UI.DelegatedAuthControl("signinMsn", privUrl, chanUrl, "pt-BR", appVerifierToken);
				_delAuthControl.set_applicationLogoUrl(hostUrl + "/img/logo_sadia_live.png");
				_delAuthControl.add_consentCompleted(consentCompleted);
			}
			if ($("#signinMsn1").size() != 0 && typeof appVerifierToken != "undefined") {
				_delAuthControl2 = new Microsoft.Live.Messenger.UI.DelegatedAuthControl("signinMsn1", privUrl, chanUrl, "pt-BR", appVerifierToken);
				_delAuthControl2.set_applicationLogoUrl(hostUrl + "/img/logo_sadia_live.png");
				_delAuthControl2.add_consentCompleted(consentCompleted);
			}
		}
	}catch(e){if(window.console)console.log(e)}
}
function loginMsn(){
	delegationToken = $("#delegationTokenMsn").val();
	
	if(_user){
		addMsn();
		return false;
	}
	
	if (typeof delegationToken != "undefined" && delegationToken != "") {
		onGetDelegationToken(delegationToken);
	}
}
function consentCompleted(sender, e){
	$.getJSON("/social/live/set-consent-token.action?consentToken="+escape(e.get_consentToken()), function(data) {
		$("#delegationTokenMsn").val(data.delegationToken);
		onGetDelegationToken(data.delegationToken);
		pageTracker._trackPageview("/menu_compartilhe_login_msn/");
		dataMetrics({pageName:"Compartilhe - Login Rede Social MSN",channel:"Header",events:"event29"});
	});
}

function onGetDelegationToken(delegationToken){
	_identity = new Microsoft.Live.Messenger.DelegatedAuthIdentity(delegationToken);
	_identity.add_authenticationCompleted(onAuthCompleted);
}
function onAuthCompleted(sender, e){
	if (!e.get_authenticationFailure()){
	  	_user = new Microsoft.Live.Messenger.User(e.get_identity());
		_user.add_signInCompleted(onSignInCompleted);
		_user.add_signOutCompleted(onSignOutCompleted);
		_user.add_signedOutRemotely(onSignedOutRemotely);
		addMsn();
	}
}

function addMsn(){
	if($(".lstIntervencaoRedesSociais #signinMsn").size() == 0){
		redeSocial.addCommunitiesFriends("Msn");
	}else{
		redeSocial.add("Msn");
	}
}

function onSignInCompleted(sender, e){
   if (e.get_resultCode() === Microsoft.Live.Messenger.SignInResultCode.success){
		//if(window.console) console.log(_user,_user.get_onlineContacts().get_count(),e);
		//if(window.console) console.log( "get_isAlreadySignedIn: " + e.get_isAlreadySignedIn() );
		//if(window.console) console.log( "get_status: " + e.get_status() );
		//if(window.console) console.log( "get_multipleEndpointsNotSupported: " + e.get_multipleEndpointsNotSupported());
		
		MSN.signIn = true;
		setTimeout('addContactsListener()',2000);
   }
}

function addContactsListener(){
	MSN.showFriends();
	_user.get_onlineContacts().add_collectionChanged(MSN.updateFriends);
}

 function onSignOutCompleted(sender, e) {
	redeSocial.remove("Msn");
}
function onSignedOutRemotely(sender, e) {
	redeSocial.remove("Msn");
}

function initMsn(){
	var loaderMsn = Microsoft.Live.Core.Loader;
	loaderMsn.load(['messenger.core'], onScriptsLoaded);
}

window.onload = initMsn;
/*
var loaderMsn = Microsoft.Live.Core.Loader;
loaderMsn.onDocumentReady(documentReady);

function documentReady(){
	loaderMsn.load(['messenger.core'], onScriptsLoaded)
}
*/
