'use strict';

angular.module('qusion.ui.redactor', [])
    .directive('redactor', [
        function () {
            return {
                require: '?ngModel',
                link: function (scope, el, attrs, ngModel) {

                    var updateModel, errorHandling;
                    // Function to update model
                    updateModel = function() {
                        if(!scope.$$phase) {
                            scope.$apply(ngModel.$setViewValue(el.redactor('get')));
                        }
                    };

                    //uploadErrorHandling = function(response)
                    //{
                    //   console.log(response.error);
                    //    alert("Error: "+ response.error);
                    //}; 

                    // Get the redactor element and call update model
                    el.redactor({
                        minHeight: 300,
                        keyupCallback: updateModel,
                        keydownCallback: updateModel,
                        changeCallback: updateModel,
                        execCommandCallback: updateModel,
                        autosaveCallback: updateModel,
                        imageGetJson: '/Areas/Qusion/Scripts/App/images/json/data.json',
                        imageUploadErrorCallback: function (json) {
                            alert(json.error);
                            alert(json.anothermessage);
                        }
                    });

                    // Call to sync the redactor content
                    ngModel.$render = function(value) {
                        el.redactor('set', ngModel.$viewValue);
                    };
                }
            
            };
        }
    ]);