//////////API Facebook
	function ShowMyName(firebase) {
		
			var messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/posts');	
			var user_id = "";
			FB.api("/me",
					function (response) {
						
						fbinfo = new Array();
						fbinfo[0] = response.id;
						fbinfo[1] = response.first_name;
						fbinfo[2] = response.last_name;
						fbinfo[3] = response.email;
						user_id = fbinfo[0];
						
						var div = document.createElement('div');
						div.className = 'row';

						div.innerHTML = '<div align="center">ID = '+response.id+'</div>';
						div.innerHTML += '<div align="center">first_name = '+response.name+'</div>';

						 document.getElementById('content').appendChild(div);
											
						var picture_pro = "";
						var user_cover = "";
						FB.api('/me/picture?width=4000&height=4000', function (response) {
							picture_pro = response.data.url;
							var im = document.getElementById("profileImage").setAttribute("src", response.data.url);
							//alert(im);
						});
						
						FB.api('/me?fields=cover&width=4000&height=4000', function (response) {
						//alert("Test");
						//alert(response.cover.source);
						user_cover = response.cover.source;
						var im = document.getElementById("profileCover").setAttribute("src", response.cover.source);
						});
						
						//insert user id
						messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/Users');
						user_id = response.id;
						//check exists 
						var userExists = false;
						userExists = checkUserExist(user_id);

						console.log("real --> "+userExists);
						if(userExists==false){
							console.log(user_id + " Insert ");
							messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/Users/');
							messagesRef.push({userID:user_id, Name:response.name,user_pic:picture_pro,userCover:user_cover});
							console.log("---push successful!!--" + user_id + " " );
						}else{
							console.log(user_id + " Update ");
							
							messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/Users/'+user_id);
							messagesRef.update({Name:response.name,user_pic:picture_pro,userCover:user_cover});
							
							console.log("---update successful!!--" + user_id + " " );
						}
						
					});
			
			console.log("---BestofMonth---" + user_id );
			insertPostBestofMonth(user_id);
			
				//getFriends();		
						
					

     //var im = document.getElementById("profileImage").setAttribute("src", "http://graph.facebook.com/" + response.id + "/picture?type=normal");
	}
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users'
	// Tests to see if /posts/<postId> has any data. 

function insertPostBestofMonth(user_id){
	
	FB.api('me/posts?fields=comments.limit(1).summary(true),likes.limit(1).summary(true),picture,place,updated_time,created_time', function (response) {
				alert(response.data.length);
						for ( i = 0; i < response.data.length; i++ ) {
							
							
							var created_time = response.data[i].created_time;
							var likes_count = response.data[i].likes.summary.total_count;
							
							var created_date =  getDate(created_time); // created_time.substring(0, 10);
							var created_tim =   getTime(created_time); //created_time.substring(11, 19);
							var created_time_d = getDay(created_date);
							var created_time_m  = getMonth(created_date);//new Date(created_date).getMonth();
							var created_time_y  = getYear(created_date);
							
							var current_time = new Date();
							//var current_date =  current_time.getDate(); 
							var current_time_d = current_time.getDate();
							var current_time_m  = current_time.getMonth();//new Date(created_date).getMonth();
							var current_time_y  = current_time.getFullYear();
							
							//alert( current_time + " all " + " year " + current_time_y  + "Month" +current_time_m + "DAY" + current_time_d );
							console.log(5 + 6);
							//alert( created_time + " all " + created_date + " time " + created_tim + " year " + created_time_y  + "Month" +created_time_m + "DAY" + created_time_d );
							//check post only in month
							if(current_time_y==created_time_y){
								//console.log("---same year");
								//check in same month
								if(current_time_m==created_time_m){
									//console.log("---same month");
								     //keep data
									  // process this row
										var isPost = false;
										console.log("--- Posts" + user_id);
										var isPost = checkPostExist(response.data[i].id,user_id);
									  
									  if(!isPost){
										var messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/Users/'+user_id+'/posts');										  
										var id = (response.data[i].id ? response.data[i].id : '');
										var full_picture = (response.data[i].full_picture ? response.data[i].full_picture : '');
										var description = (response.data[i].description ? response.data[i].description : '');
										var source = (response.data[i].source ? response.data[i].source : '');
										messagesRef.push({postID:id, picture:full_picture,description:description,source:source});
										console.log("---PUSH SUCCESSfUL!");
									  }else{//update posts
										  
										var messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/Users/'+user_id+'/posts/'+response.data[i].id);										  
										//var id = (response.data[i].id ? response.data[i].id : '');
										var full_picture = (response.data[i].full_picture ? response.data[i].full_picture : '');
										var description = (response.data[i].description ? response.data[i].description : '');
										var source = (response.data[i].source ? response.data[i].source : '');
										messagesRef.update({picture:full_picture,description:description,source:source});
										console.log("---PUSH SUCCESSfUL!");
										  
									  }
								}
								
							}
							else{
								return false;
							}				
						}

					});
	
}	


function checkUserExist(user_id){
    var UserExi = false;	
	messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/');
						var ref = messagesRef.child("Users");
						// Attach an asynchronous callback to read the data at our posts reference
						ref.on("value",function(snapshot,UserExi) {
						  console.log("test -- " + JSON.stringify(snapshot.val()));
						  
						   snapshot.forEach(function (childSnapshot,UserExi) {							
								var value = childSnapshot.val();
								console.log("Name is : " + value.userID);
								if(value.userID==user_id){
									console.log(user_id + " Exists!! ");
									UserExi = true;
									return UserExi;
								}
							});
						  console.log("checkUserExist : "+UserExi);
						  return UserExi;
						}, function (errorObject) {
						  console.log("The read failed: " + errorObject.code);
						});
		 console.log("User Exists : "+UserExi);				
	return UserExi;
}
	
function checkPostExist(postid,userid){
	messagesRef = new Firebase('https://kept-7b0c3.firebaseio.com/');
	var ch= "Users/"+userid+"/post";
	var ref = messagesRef.child();
	
	// Attach an asynchronous callback to read the data at our posts reference
	ref.on("value",function(snapshot) {
		console.log("Dinnnn test");
	  console.log("Dinnnn test -- " + JSON.stringify(snapshot.val()));
	  
	    snapshot.forEach(function (childSnapshot) {
			var value = childSnapshot.val();
			console.log("postID is : " + value.postID);
			if(value.postID==postid){
				console.log("loop"+userExists);
				console.log(postid + " Exists!! ");
				return true;
			}
		});
	  if(userExists){
		  return true;
	  }else{
		  return false;
	  }
	}, function (errorObject) {
	  console.log("The read failed: " + errorObject.code);
	  return false;
	});
	
}	
	
function checkIfPostIDExists(postId) {
  var usersRef = new Firebase(USERS_LOCATION);
  usersRef.child(userId).once('value', function(snapshot) {
    var exists = (snapshot.val() !== null);
    userExistsCallback(userId, exists);
  });
}
	
function getDate(timestamp){
return timestamp.substring(0, 10);
}

function getTime(timestamp){
	return timestamp.substring(11, 19);
}

function getDay(date){
	return parseInt(date.substring(8, 10));
}

function getMonth(date){
	return parseInt(date.substring(5, 7));
}

function getYear(date){
	return parseInt(date.substring(0, 4));
}

	