/*$(document).ready(function () {
    MessengerConnect.Initializer.onAppLoaded(mcLoaded);
 
    $('.notimplemented').attr("rel", "#notimplemented");
    $(".notimplemented").overlay({
        mask: {
            color: '#ebecff',
            loadSpeed: 200,
            opacity: 0.9
        },
 
        closeOnClick: true
    });    
});*/
 
function mcLoaded() {
    Microsoft.Live.Core.Namespace.using("wl:Microsoft.Live");
    var auth = wl.App.get_auth();
    auth.add_propertyChanged(propertyChanged);
 
    if (auth.get_state() == Microsoft.Live.AuthState.authenticated) {
        onAuthenticated();
    }
}
 
function propertyChanged(sender, e) {
    var auth = Microsoft.Live.App.get_auth();
    if (e.get_propertyName() != 'state') return;
    switch (auth.get_state()) {
        case Microsoft.Live.AuthState.authenticated:
            onAuthenticated();
            break;
        case Microsoft.Live.AuthState.unauthenticated:
            onDeauthenticated();
            break;
    }
}
 
function onDeauthenticated() {
    $('#socialButtons').show();
    $('#loginFields').show();
    clearForm();
}
 
function onAuthenticated() {
    /*$('#socialButtons').hide();
    $('#connect').hide();
    $('#loginFields').hide();*/
    loadProfile();
}
 
function loadProfile() {
    dataContext = Microsoft.Live.App.get_dataContext();
    dataContext.loadAll(Microsoft.Live.DataType.profile, profileLoaded);
}
 
function profileLoaded(eventArgs) {
    if (eventArgs.get_resultCode() !== Microsoft.Live.AsyncResultCode.success) {
        alert("Profile not loaded.");
        return;
    }
    var data = eventArgs.get_data();
   var profile = data.getItem(0);
    var email = profile.get_emails().peek();
    if (email != null) {
        $('#correoElectronico').val(email.get_address());
    }
 
    $('#nombre').val(profile.get_firstName());
    $('#apellidos').val(profile.get_lastName());
    var displayName = ""; //pillarlo del messenger context
    /*var fecha = new Date();
    fecha = profile.get_birthDay();
    alert(fecha);
    $('#fechaDia').val(fecha.getDay());
    $('#fechaMes').val(fecha.getMonth());
    $('#fechaAnno').val(fecha.getYear());*/
    var gender = profile.get_gender();
    alert(gender);
    if(gender == '2') {   
    	alert(gender+"--");
    	$('#registrar_sexoArraychica').attr('checked',false);
    	$('#registrar_sexoArraychico').attr('checked',true);
    }
    /*var addresses = profile.get_addresses();
    var address = addresses.peek();
    if (address != null) {
        $('#Address1').val(address.get_streetAddress());
        $('#Address2').val(address.get_streetAddress2());        
        $('#postalcode').val(address.get_postalCode());
        $('#City').val(address.get_city());
    } else {
        $('#City').val(profile.get_location());
    }*/
}
 
function clearForm() {
    $('input[type=text]').val('');
}



var msn={		
		login: function(cid,urlDestino){
			if(cid != null && cid != "null") {
				window.location.href='login.action?msn='+cid+'&urlDestino='+urlDestino;
			}else{
				msn.logInMsnZona3();
			}
		},
		logInMsn: function() { //llama al login en msn, para el registro en Zona3
			abrirVentana('OAuth.action?logInMsn');
		},
		logInMsnZona3: function() {//llama al login en msn pero el callback le lleva al login de Zona3
			abrirVentana('OAuth.action?logInMsnZona3');
		},
		publicar: function(check,texto,imageUrl,urlOrigin, urlTitle) {
			var body = $('#'+texto).val();
			var chequeado= $('#'+check+':checked').val() != null;
			if(chequeado && (body != null && body != '' && body != 'undefined')){
			//  alert('public en MSN');
			  window.open('http://profile.live.com/badge/?url='+escape(urlOrigin)+'&title='+escape(urlTitle)+'&description='+escape(body));
			}
		}

};

function abrirVentana(href) {
	window.open(href, 'msn_login','scrollbars=no,status=no,location=no,toolbar=no,menubar=no,resizable=no,top=0,left=0,width=450,height=250');
}









