Ext.regController('Slideshows', {

    index: function() {

    },

    share: function(options) {
        //console.log(options);

        //construct obj to send to server
        var reqObj = options.data.slideshow;
        reqObj.source_id = 1;
        reqObj.text = '';
        reqObj.created = +new Date();
        reqObj.photos = [];

        options.data.photos.each(function(rec) {
            reqObj.photos.push({name: rec.data.name, file_path: rec.data.source});
        });

        Ext.Msg.confirm('Share on Facebook', 'Share this slideshow with your friends on Facebook?', function(val) {
            if(val === 'yes') {
                Ext.Ajax.request({
                    url: 'http://fotoblogist.com/slideshow/create',
                    method: 'POST',
                    jsonData: reqObj,
                    success: function(response, opts) {
                      var obj = Ext.decode(response.responseText);
                      //console.dir(obj);
                      if(obj.success === true) {
                        var body = 'Check out my new slideshow! ' + obj.url;
                        FB.api('/me/feed', 'post', { message: body }, function(response) {
                          if (!response || response.error) {
                            //console.log(response);
                            //alert('Error occured');
                            Ext.Msg.alert('Error occured', 'Slideshow could not be shared. :(', Ext.emptyFn);
                          } else {
                            //console.log(response);
                            //alert('Post ID: ' + response.id);
                            Ext.Msg.alert('Shared successfully! :)', 'This slideshow can be seen by your friends now.', Ext.emptyFn);
                          }
                        });
                      }
                    },
                    failure: function(response, opts) {
                      //console.log('server-side failure with status code ' + response.status);
                    }
                });
            }
        });
    }
});

