var uid;
function submitCommentsForm(uid)
{
	if(!document.uid)
	{
		alert("per aggiungere un commento devi prima collegarti con facebook");
		return false;
	}
	else
	{
		uid = document.uid;
		username = document.getElementById('username').value;
		if(!username || username.length<2)
		{
			alert("devi inserire un nickname");
			document.getElementById('username').focus();
			return false;
		}
		comment = document.getElementById('comment').value;
		www = document.getElementById('www').value;
		
		comment2 = comment + "\n\n http://giarty.it"+www;
		
		if(!comment|| comment.length<2)
		{
			alert("devi inserire un commento");
			document.getElementById('comment').focus();
			return false;
		}
	
		if(document.getElementById('publish_to_facebook').checked==true)
		{
			var publish = {
					  method: 'stream.publish',
					  message: comment2,
					  attachment: {
					  },
					  action_links: [
					    { text: 'Giarty.it', href: 'http://giarty.it' }
					  ],
					  user_prompt_message: 'Publish your comment on facebook?'
					};
			$("#comment_form").dialog('close');
			FB.ui(publish,function(){submit_comments_form()});
//                        document.location.href = '/moduli_new/redirect/to_mie_preferti.php?uid='+document.uid;
		}
			//FB.Connect.streamPublish(comment2, null, null, null, "Publish your comment on facebook?", function(){document.getElementById('comments_form').submit();});
		else
			submit_comments_form();
	
	}
}

function submit_comments_form(){
	form_data = $("#comments_form").serializeArray();
	$.post("/comments.php", 
		   form_data,   
		   function(data){$('#wallkit_posts').html(data);
		   				  $("#comment_form").dialog('close');
//		   				  if(document.should_reload == 1){
//                                                      document.location.href = '/moduli_new/redirect/to_mie_preferti.php?uid='+document.uid;
////		   					document.should_reload = 0;
////		   					window.location.reload();
//                                                  }
		}, 
		   				  "html");
}

function send_like(url, data){
	$.post(url, 
		   data,   
		   function(data){$('#likes').html(data);
		   				  $("#comment_form").dialog('open');	}, 
		   				  "html");
}
function unlike(xidd){
	if(!xidd || !(xidd*1>0))
		return;
	if($('#unliked_'+xidd).html()!=null){
		code = $('#unliked_'+xidd).html();
	} else {
		code =null;
	}
	data = {'xidd':xidd, 'code':code};
	$.post('/unlike.php', 
			data,   
			function(data){$('#unlike_'+xidd).html(data);}, 
	"html");
}
function send_like_yuku(uid, url, data, reload_val, ragsoc){
	if (uid && uid.length > 0 && uid != '0'){
        document.should_reload = reload_val;
        document.uid = uid;
	    www = document.getElementById('www').value;
	    comment2 = "Un consiglio da amico:  " + "\n\n http://giarty.it"+www;
        var my_array=data.split(":");
        try {
         document.getElementById('xid').value = my_array[1];
        }
        catch (e){}
        $.post(url, 
	       data,   
	       function(data){$('#likes').html(data);
                              if (ragsoc && ragsoc.length > 0){
                                $("#dialog_ragsoc").val(ragsoc);  
                              }
		              $("#comment_form").dialog('open');	
                             }, 
	       "html");
        }
        else {
          	FB.login(function(response) {
		     if (response.authResponse) {
              uid = response.authResponse.userID;
              try {
			   document.getElementById('username').value = response.name; 
              }
              catch (e){}
			  sendRequest(uid, url, method, div, data);
			  document.should_reload = 1;
			
		     } else {
			  alert("per aggiungere un commento devi prima collegarti con facebook");
			  return false;
		     }
		});  
        }
}
function fb_login(){
     FB.login(function(response) {
       if (response.authResponse) {
         var uid = response.authResponse.userID;
         document.location.href='/moduli_new/redirect/to_mie_preferti.php?uid='+uid;
       } 
     }); 
//    FB.login(function(response) {
//		  if (response.session.uid) {
//			  document.location.href='/moduli_new/redirect/to_mie_preferti.php?uid='+response.session.uid;
//		  } 
//    });
}
function fb_login_mobile(){
    FB.login(function(response) {
       if (response.authResponse) {
         var uid = response.authResponse.userID;
         document.location.href='/moduli_new/mobile/redirect/to_mie_preferti.php?uid='+uid;
       } 
     });
//    FB.login(function(response) {
//		  if (response.session.uid) {
//			  document.location.href='/moduli_new/mobile/redirect/to_mie_preferti.php?uid='+response.session.uid;
//		  } 
//   });
}
function sendRequest(uid, url, method, div, data){
	document.should_reload = 0;
	if(!uid)
	{
		FB.login(function(response) {
		  if (response.authResponse) {
			  uid = response.authResponse.userID;
			  document.getElementById('username').value = response.name; 
			  sendRequest(uid, url, method, div, data)
			  document.should_reload = 1;
			
		  } else {
			  alert("per aggiungere un commento devi prima collegarti con facebook");
			  return false;
		  }
		});
	}
	
	if (uid && uid.length>0)
	{
		document.uid = uid;
		www = document.getElementById('www').value;
		comment2 = "Un consiglio da amico:  " + "\n\n http://giarty.it"+www;
		
		var publish = {
			  method: 'stream.publish',
			  message: comment2,
			  attachment: {
			  },
			  action_links: [
			    { text: 'Giarty.it', href: 'http://giarty.it' }
			  ],
			  user_prompt_message: 'Publish your comment on facebook?'
			};
		FB.ui(publish,function(){send_like(url, data);});
	}
}

function sendRequest_list(uid, url, method, div, data)
{
        //document.getElementById('fbname').value = uid;
        var my_array=data.split(":");
        document.getElementById('xid').value = my_array[1];
	document.should_reload = 0;
	if(!uid)
	{
		FB.login(function(response) {
		  if (response.authResponse) {
			  uid = response.authResponse.userID;
			  document.getElementById('username').value = response.name; 
			  sendRequest_list(uid, url, method, div, data)
			  document.should_reload = 1;
			
		  } else {
			  alert("per aggiungere un commento devi prima collegarti con facebook");
			  return false;
		  }
		});
	}
	
	if (uid && uid.length>0)
	{
		document.uid = uid;
		www = document.getElementById('www').value;
		comment2 = "Un consiglio da amico:  " + "\n\n http://giarty.it"+www;
		
		var publish = {
			  method: 'stream.publish',
			  message: comment2,
			  attachment: {
			  },
			  action_links: [
			    { text: 'Giarty.it', href: 'http://giarty.it' }
			  ],
			  user_prompt_message: 'Publish your comment on facebook?'
			};
		FB.ui(publish,function(){send_like(url, data);});
	}
}

function sendPreferityRequest(uid, url, method, div, data)
{
	document.should_reload = 0;
	if(!uid)
	{
		FB.login(function(response) {
			if (response.authResponse) {
			    uid = response.authResponse.userID;
				console.log(response);
				//uid =response.session.uid;
				//document.getElementById('username').value = response.session.first_name; 
				sendPreferityRequest(uid, url, method, div, data)
				document.should_reload = 1;
				
			} else {
				alert("per aggiungere un commento devi prima collegarti con facebook");
				return false;
			}
		});
	}
	
	if (uid && uid.length>0)
	{
		$.post(url, 
			   data,   
			   function(data){$('#likes').html(data); }, 
				   				 "html");
	}
}
