/******************************************************************************
 * SCRIPTAJAX JS                                                              *
 * by Iacopo Giammaria                                                        *
 * (c)2010 - Under GPL Public License                                         *
 ******************************************************************************/
var Server = "";
var ServerAction = "";

function populatePage() {
   var server = 'http://'+location.host+location.pathname;
   Server = server;
   ServerAction = server+'action.php';
   new Ajax.Updater({ success: 'lists' }, ServerAction, { 
      parameters: { g: 'populatepage' }, onSuccess: function() { loadFacebook(); } });
}

function loadFacebook() {
   var apikey = 'e34e0c25983b09ad1fd3bc7540a74ce0';
   FB.init(apikey, 'xd_receiver.html');
}

function showBox(index) {
   var div = $('box');
   var layer = $('layer');
   if(div && layer) {
      var height = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
         height = window.innerHeight;
      } else if(document.documentElement && 
               (document.documentElement.clientHeight )){
         height = document.documentElement.clientHeight;
      } else if(document.body && ( document.body.clientHeight ) ) {
         height = document.body.clientHeight;
      }
      var top = (height-600)/2;
      if(top<0) top =0;
      layer.setStyle({ 'margin' : top+'px auto 0pt' });
      layer.addClassName('layer'+index);
      new Ajax.Updater({ success: 'results' }, ServerAction, { parameters: { g: 'populatebox', i : index } });
      new Effect.Appear(div, { duration: 0.5, from: 0.5, to: 1, 
         afterFinish: function() { new Effect.Grow(layer, { duration : '0.5', direction : 'top-left' } ) } });
   }
}

function hideBox() {
   var div = $('box');
   var layer = $('layer');
   if(div && layer) new Effect.Fade(div, { duration: 0.3, from: 0.6, to: 0, 
              afterFinish: function() { layer.hide();layer.className = ''; } } );
}

function randomize(index) {
   if(index !=null && index >= 0) {
      new Ajax.Updater({ success: 'phrase' }, ServerAction, { parameters: { g: 'randomphrase', i: index } });
   }
}

function sendText() {
   if($('phrase')) {
      var phrase = $('phrase').innerHTML;
      if(phrase != "" && phrase != " ") {
         shareOnFacebook(phrase);
      } else {
         alert('Attenzione: occorre selezionare una frase da un elenco!');
      }
   }
}

function shareOnFacebook(phrase) {
   if(phrase && phrase != "") {
      phrase += ' - http://www.d-o-c.it/';
      FB.Connect.requireSession(null, loginCancelledCallback);
      FB.Facebook.get_sessionWaitable().waitUntilReady(
         function() {
            FB.Facebook.apiClient.users_hasAppPermission(
               'status_update', function(result,ex)
               {
                  if(true == result){
                     FB.Facebook.apiClient.users_setStatus(
                        phrase,false,true,function(re,ex)
                        {
                           if(null != re){
                              alert('Messaggio inviato con successo!');
                              hideBox();
                           }else{
                              alert('Impossibile inviare il messaggio.');
                           }
                        }
                     );
                  }else{
                     FB.Connect.showPermissionDialog(
                        'status_update', function(re,ex)
                        {
                          if(re != ""){
                              shareOnFacebook(phrase);
                          }else{
                             alert('Occorre abilitare i permessi per inviare il messaggio.');
                          }
                        }
                     );
                  }
               }
            );
         }
      );
   }
}

function loginCallback(result) {
   /*var attachment = {
      'name': 'La tua frase DOC',
      'href': Server,
      'caption': '{*actor*} '+Phrase+' - www.d-o-c.it',
      'description': ''
   };
   var action_links = [{ "text": "Scegli la tua frase DOC!", "href": Server }];
   FB.Connect.streamPublish('', attachment, action_links);*/
}

function loginCancelledCallback() {}

function streamCallback(post_id, exception) {
   if(exception) alert(exception.toSource());
}



Event.observe(window, 'load', function() { populatePage(); });

