// JavaScript Document
	window.fbAsyncInit = function() {
		FB.init({
			appId : '141294415924147',
			status : true, // check login status
			cookie : true, // enable cookies to allow the server to access the session
			xfbml : true, // parse XFBML
			oauth : true
		});										
		
		//ESTAQ SE EJECUTA CUANDO HACES CLICK EN EL BOTNO DE FACEBOK PARA LOGEART
		FB.custom_connect = function(){
			FB.getLoginStatus(function(response){
			   if (response.authResponse) {
				   FB.api('/me', function(response){					
						if(response.id) {												
							$.ajax({
								type: 'POST',
								url: '/users/facebook/',
								data: {c_url:window.location.href,prop_id: $('#prop_id').val(),ltype: $('#LeadLtype').val(),fb_user_id: response.id, fb_name: response.name, fb_first_name: response.first_name, fb_last_name: response.last_name, fb_email: response.email},
								success: function(data){
									window.location.reload();
								}
							});							
							//window.location.href = "http://<? echo $_SERVER['SERVER_NAME']; ?>/users/facebook/?prop_id="+$('#prop_id').val()+"&ltype="+$('#LeadLtype').val();
						}
						else{
							FB.custom_login();
						}						
				   });
			   }
			   else{
				   //alert('custom login');
				   FB.custom_login();
			   }
			});			
		
		};
		
		//cuando la app homeconnector NO esta dada de alta pide los permisos
		FB.custom_login = function(){                    
			FB.login(function(response){
				if (response.authResponse) {
					//user accepted the terms
						$.ajax({
							type: 'POST',
							url: '/users/facebook/',
							data: {c_url:window.location.href,prop_id: $('#prop_id').val(),ltype: $('#LeadLtype').val(),fb_user_id: response.id, fb_name: response.name, fb_first_name: response.first_name, fb_last_name: response.last_name, fb_email: response.email},
							success: function(data){
								openRegisterStep2();
							}
						});					
					
				}
				else
				{
					//user didnt acepted the terms
					window.location.reload();
				}
			},{scope:'email,offline_access'});
			
		};
		
		//SE EJECUTA SIEMPRE QUE LA PAGINA SE CARGA, ES LA QUE VALIDA SI YA TIENES SESSION EN FACEBOK
		//Y= YA NOMAS CHECA SI ESTAS REGISTRADO
		// whenever the user logs in, we refresh the page
		//c_url es una variable que trae la url de donde se registra para guardarla
		FB.Event.subscribe('auth.login', function() {			
			FB.getLoginStatus(function(response){				
				if (response.authResponse) {				
				     FB.api('/me', function(response){							 				
					     $.ajax({
						     type: 'POST',
						     url: '/users/facebook/',
						     data: {c_url:window.location.href,prop_id: $('#prop_id').val(),ltype: $('#LeadLtype').val(),fb_user_id: response.id, fb_name: response.name, fb_first_name: response.first_name, fb_last_name: response.last_name, fb_email: response.email},
						     success: function(data){
								
							     $('#InfoLogin').load('/users/info_login/');
							     /*
							     if($('#register').dialog( "isOpen" )){
								     $('#register').dialog('close');
								     window.location.reload();
							     }
							     */
							
						     }
					     });
				     });
				}	
			});	
				//window.location.href = "http://<? echo $_SERVER['SERVER_NAME']; ?>/users/facebook/?prop_id="+$('#prop_id').val()+"&ltype="+$('#LeadLtype').val();
		});		
				
	};	
	(function() {
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
	}());							
