/*
 *
 *  Phonalisa translation plugin
 *
 *
*/
(function($) {
    $.plTranslator = function(options) {
        var defaults = {
            url: '',
            translationId: 'translation-id',
            translationTarget: 'translation-target',
            propertyName: 'value',
            onSomeEvent: function() {}
        }
        
        var plugin = this;
        var translations;
        var groupedTranslations = new Object;
        
        plugin.settings = {};
        
        var init = function() {
            plugin.settings = $.extend({}, defaults, options);
            setupContextMenuData();
        }
        
        
        plugin.displayTranslationModal = function(translationKey) {
            $.ajax({
                type: 'GET',
                url: plugin.settings.url,
                data: { key: translationKey }
            })
            .done(function( data ) {
                $('body').append(data);
                $('#'+translationKey).modal();
            });            
        }
        
        plugin.update = function() {
            $(".context-menu").closest("table").remove();   // remove existing context menues workaround for multible call  
            delete translations;
            groupedTranslations = new Object;
            init();            
        }        
        
        var createSingleTargetMenu = function(el, translateId){
        
            var tslButton = {};
            var keyName = translateId;
            tslButton[keyName] = (function (mylocalvariable) { return function(){plugin.displayTranslationModal(mylocalvariable);}})(keyName);
        
            $(el).contextMenu(
                [tslButton],
                {theme:'osx'}
            );

        }
        
        
        var createMultipleTargetMenu = function(){
        
            
        
            for (var key in groupedTranslations) {           
                
               var obj = groupedTranslations[key];
               
               var tslOptions = [];
               
               for (var prop in obj) {
                    var tslButton = {};
                    var keyName = obj[prop];
                    tslButton[keyName] = (function (mylocalvariable) { return function(){plugin.displayTranslationModal(mylocalvariable);}})(keyName);
                    tslOptions.push(tslButton);
               }            
                 
               $("[data-translation-target='" + key +"']").contextMenu(
                    tslOptions,
                    {theme:'osx'}
               );
            }        
            
        }
        
        
        var setupContextMenuData = function() {
            
            // find all elements to translate
            translations = findElements();            
            
            $.each(translations, function(index, el) {
            
                // translation target data
                var translateTarget = $(el).data(plugin.settings.translationTarget);
                var translateId = $(el).data(plugin.settings.translationId);
                                
                // single translation
                if(translateTarget == undefined){
                    createSingleTargetMenu(el, translateId);
                }
                // grouped translation
                else{                
                    if(!groupedTranslations[translateTarget]){
                        groupedTranslations[translateTarget] = new Array(translateId);
                    }
                    else{
                        groupedTranslations[translateTarget].push(translateId);
                    }
                }

            });
            
            createMultipleTargetMenu();
            
        }
        
        
        var findElements = function() {
            return $('[data-' + plugin.settings.translationId + ']');
        }
        
        init();
    }
})(jQuery);