/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

var smConstants, smGridConfig, smUtils, smLabels, smValidation, smMessages;

require.config({
    baseUrl: '/',
    paths: {
        underscore: 'assets/underscore/underscore-min',
        backbone: 'assets/backbone/backbone-min',
        text: 'assets/requirejs/text',
        knockout: 'assets/knockout/knockout-3.0.0',
        knockback: 'assets/backbone/knockback.min',
        validation: 'assets/backbone/backbone-validation-amd'
    }
});

require([
    'underscore',
    'validation',
    'common/ui/js/constants',
    'common/ui/js/grid.config',
    'common/ui/js/utils',
    'common/ui/js/labels',
    'common/ui/js/messages',
    'knockout'
], function (_, validation, Constants, GridConfig, Utils, Labels, Messages, Knockout) {
    smConstants = new Constants();
    smUtils = new Utils();
    smLabels = new Labels();
    smMessages = new Messages();
    smGridConfig = new GridConfig();
    smValidation = validation;
    initBackboneValidation(_);
    initCustomKOBindings(Knockout);
});

function initBackboneValidation(_) {
    _.extend(smValidation.callbacks, {
        valid: function (view, attr, selector) {
            /*
             var $el = $(view.modalElementId).find('[name=' + attr + ']'),
             $group = $el.closest('.form-element');

             $group.removeClass('has-error');
             $group.find('.help-block').html('').addClass('hidden');
             */
        },
        invalid: function (view, attr, error, selector, validation) {
            var model = view.model;
            model.validateAttr(attr, validation);
            /*
             var $el = $(view.modalElementId).find('[name=' + attr + ']'),
             $group = $el.closest('.form-element');
             $group.addClass('has-error');
             $group.find('.help-block').html(error).removeClass('hidden');
             */
        }
    });
};

function initCustomKOBindings(Knockout) {
    Knockout.bindingHandlers.contrailDropdown = {
        init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var valueObj = valueAccessor(),
                allBindings = allBindingsAccessor(),
                dropDown = $(element).contrailDropdown(valueObj).data('contrailDropdown');

            if (allBindings.value) {
                var value = ko.utils.unwrapObservable(allBindings.value);
                if(typeof value === 'function') {
                    dropDown.value(value());
                } else {
                    dropDown.value(value);
                }
            }
            else {
                dropDown.value('');
            }

            ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                $(element).select2('destroy');
            });
        },
        update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            $(element).trigger('change');
        }
    };

    Knockout.bindingHandlers.contrailMultiselect = {
        init  : function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var valueObj = valueAccessor(),
                allBindings = allBindingsAccessor(),
                lookupKey = allBindings.lookupKey,
                multiselect = $(element).contrailMultiselect(valueObj).data('contrailMultiselect');

            if (allBindings.value) {
                var value = ko.utils.unwrapObservable(allBindings.value);
                if (typeof value === 'function') {
                    multiselect.value(value());
                } else {
                    multiselect.value(value);
                }
            }

            ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                $(element).select2('destroy');
            });
        },
        update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            $(element).trigger('change');
        }
    };

    var updateSelect2 = function (element) {
        var el = $(element);
        if (el.data('select2')) {
            el.trigger('change');
        }
    }
    var updateSelect2Options = ko.bindingHandlers['options']['update'];

    ko.bindingHandlers['options']['update'] = function (element) {
        var r = updateSelect2Options.apply(null, arguments);
        updateSelect2(element);
        return r;
    };

    var updateSelect2SelectedOptions = ko.bindingHandlers['selectedOptions']['update'];

    ko.bindingHandlers['selectedOptions']['update'] = function (element) {
        var r = updateSelect2SelectedOptions.apply(null, arguments);
        updateSelect2(element);
        return r;
    };
};
@@ -2371,34 +2371,40 @@
     var value = 
-ko
+Knockout
 .utils.unwrapObs
@@ -2700,34 +2700,40 @@
  %7D%0A%0A            
-ko
+Knockout
 .utils.domNodeDi
@@ -3474,18 +3474,24 @@
 value = 
-ko
+Knockout
 .utils.u
@@ -3741,18 +3741,24 @@
         
-ko
+Knockout
 .utils.d
@@ -4221,34 +4221,40 @@
 elect2Options = 
-ko
+Knockout
 .bindingHandlers
@@ -4273,34 +4273,40 @@
 'update'%5D;%0A%0A    
-ko
+Knockout
 .bindingHandlers
@@ -4508,18 +4508,24 @@
 tions = 
-ko
+Knockout
 .binding
@@ -4568,18 +4568,24 @@
 %5D;%0A%0A    
-ko
+Knockout
 .binding

Fix issue with binding of dropdowns.