function salveazaSharing(unde, ce, element){

   if (element.checked){
       if(unde=='facebook'){    //facebook  
            FB.ensureInit(function() {
            FB.Connect.requireSession(function(){
                //alert('check is user already granted');
                //check is user already granted for this permission or not
                FB.Facebook.apiClient.users_hasAppPermission('publish_stream',
                function(result) {
                    // prompt offline permission  
                    if (result == 0) {
                        // show the facebook permission dialog
                        FB.Connect.showPermissionDialog('publish_stream',
                        function(result){
                            if (!result || result!='publish_stream') { //  result!='publish_stream' - later edit (nu intra aici)
                                // no permisson granted, pop up facebook dialog to post;                                
                                //alert('not granted - salveaza');
                                
                                $.ajax({
                                    url: 'cont/seteaza_sharing/facebook/'+ce+'/0',
                                    type: "GET",
                                    dataType: "html",
                                    success: function(mesaj) { 
                                        //console.log(parseInt(mesaj));
                                        element.checked=false; 
                                    }
                                });
                            } else {
                                // permission granted, post without facebook dialog
                                //alert('granted - salveaza');                                
                                $.ajax({
                                    url: 'cont/seteaza_sharing/facebook/'+ce+'/1',
                                    type: "GET",
                                    dataType: "html",
                                    success: function(mesaj) { 
                                        //console.log(parseInt(mesaj));
                                    }
                                });
                            }
                        }, true, null);
                    } else {
                        // permission already granted, post suddenly
                        // without facebook dialog 
                        //alert('deja permis - salveaza');
                        $.ajax({
                            url: 'cont/seteaza_sharing/facebook/'+ce+'/1',
                            type: "GET",
                            dataType: "html",
                            success: function(mesaj) { 
                                //console.log(parseInt(mesaj));
                            }
                        });
                    }
                });           
            });
        });

            
       }  //end unde=='facebook'
       else if (unde=='twitter'){
            $.ajax({
                url: 'cont/seteaza_sharing/twitter/'+ce+'/1',
                type: "GET",
                dataType: "html",
                success: function(mesaj) { 
                    //console.log(parseInt(mesaj));
                }
            });
       }
   }
   else {
        //l-a debifat
        if(unde=='facebook'){ 
             $.ajax({
                url: 'cont/seteaza_sharing/facebook/'+ce+'/0',
                type: "GET",
                dataType: "html",
                success: function(mesaj) { 
                    //console.log(parseInt(mesaj));
                }
            });
        }
        else if(unde=='twitter'){
        
        }        
   }
 
}



function jscallback (post_id, exception) {
        
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));
    
    /*if (post_id == null || post_id == undefined ) {
        // failed
        document.location.href=baseURL+"/cont";
    } else {
        // success
        document.location.href=baseURL+"/cont";
    } */
    
    
}




function publish_with_permission(permission, vb_redirect, uid, mesaj) { 
    FB.ensureInit(function() {    
        FB.Connect.requireSession(function(){
            
            //check is user already granted for this permission or not
            //extra check, se verifica la inscriere daca se face sharing
            FB.Facebook.apiClient.users_hasAppPermission( permission,
            function(result) {
                // prompt offline permission
                if (result == 0) {
                    // show the facebook permission dialog
                    FB.Connect.showPermissionDialog(permission,
                    function(result){
                        if (!result) {
                            // no permisson granted, pop up facebook dialog to post;
                            //FB.Connect.streamPublish('', attachment, action_links, null, "Post this:", jscallback);
                            if (vb_redirect==1)
                                FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:", jscallback, false, uid);
                            else 
                                FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:",'', false, uid);
                               
                                                                                                         
                        } else {                      
                            // permission granted, post without facebook dialog
                            FB.Connect.forceSessionRefresh(function() {
                                if (vb_redirect==1)  
                                    FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:", jscallback,false, uid);
                                else 
                                    FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:", null,false, uid);                                            
                            
                            });
                        }
                    }, true, null);
                } else {              
                    // permission already granted, post suddenly
                    // without facebook dialog
                    if (vb_redirect==1)
                        FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:", jscallback,false);
                    else
                        FB.Connect.streamPublish(mesaj, attachment, action_links, uid, "Post this:", null ,false);
                
                  
                }
            }, uid);
        }, function (){  // userul nu vrea sa se conecteze cu FB 
            $.ajax({
                url: 'cont/unset_facebook_sharing_data',
                type: "GET",
                dataType: "html"
            });
        });
    });
}  



function respinge_conectare_facebook(){
   $.ajax({
        url: 'info/respinge_conectare_facebook',
        type: "GET",
        dataType: "html"
    }); 
}
