// cflib basic shop objexts  ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

CF.Lib.Shop || (CF.Lib.Shop = {});


CF.Lib.Shop.ValueProvider = CF.Lib.Object.extend({
    construct: function () {
        this.inherited();
        this.items = [];
    },
    set: function (key, str) {
        this.items[key] = str;
    },
    get: function (str) {
        if (this.items[str] !== undefined) {
            return this.items[str];
        }
        return str;
    }
});

/* global translations */
CF.Lib.Shop.Translate = CF.Lib.Shop.ValueProvider.extend({
    __: function (str) {
        return this.get(str);
    }
});
CF.translate = CF.Lib.Shop.Translate.create();


/* global configuration settings */
CF.Lib.Shop.Config = CF.Lib.Shop.ValueProvider.extend({
});

CF.config = CF.Lib.Shop.Config.create();


/* base control for visual elements */
CF.Lib.Shop.Control = CF.Lib.Control.extend({
    construct: function (elem) {
        elem = (elem) ? jQuery(elem) : null;
        this.inherited(elem);
    }
});


CF.Lib.Shop.Autocomplete = CF.Lib.Shop.Control.extend({
    jqcontrol: null,
    current: null,
    url: '',
    hint: null,
    construct: function (elem, url) {
        this.inherited(elem);
        this.hint = this.element.val();
        var self = this;
        this.element.blur(function(){self.handleBlur();});
        this.element.focus(function(){self.handleFocus();});
        this.element.keydown(function(e) { return self.handleKeyDown(e); });
        this.current = null;
        url && this.setUrl(url);
        var options = this.createOptions();
        this.jqcontrol = this.element.autocomplete(options);
        this.checkHintState();
    },
    checkHintState: function(){
        if (this.element.val() == this.hint) {
            this.element.addClass('ac-hint');                
        } else {
            this.element.removeClass('ac-hint');                
        }
    },
    handleBlur: function(){
        if (this.element.val() == '') {
           this.element.val(this.hint);
        }
        this.checkHintState();
    },
    handleFocus: function(){
        if (this.element.val() == this.hint) {
           this.element.val('');
        }
        this.checkHintState();
    },
    handleKeyDown: function(e){
        this.checkHintState();
        switch (e.keyCode) {
            case 13: //KEY_RETURN:
              //e.stopImmediatePropagation();
              //e.preventDefault();        
              //return false;
              break;
            default:
              return;
        }
    },
    handleFocus: function(){
        if (this.element.val() == this.hint) {
            this.element.val('');
        }
    },    
    setUrl: function(url) {
        this.url = url;
        if (this.url && this.jqcontrol) {
            this.jqcontrol.serviceUrl = this.url;
        }
    },
    getUrl: function() {
        return this.url;  
    },
    clearQuery: function() {
      this.getElement().val('');  
    },
    createOptions: function() {
        var self = this;
        url = this.getUrl();
        var result = {
            serviceUrl: url,
            minChars: 2,
            /*minLength: 1,*/
            delimiter: /(,|;)\s*/, // regex or character
            maxHeight: 400,
            width: 500,
            zIndex: 9999,
            deferRequestBy: 100, //miliseconds
            params: {}, //aditional parameters
            noCache: true, //default is false, set to true to disable caching
            // callback function:
            onSelect: function(value, data, el) {
                return self.handleOnSelect(value, data, el);
            },
            fnFormatResult: function(value, data, currentValue){
                return self.handleFormatResult(value, data, currentValue);
            }
        }
        return result;
    },
    getCurrent: function() {
        return this.current;  
    },
    setCurrent: function(item) {
        if (item !== this.current) {
            this.current = item;
            this.notify('changed');
        }
    },
    handleFormatResult: function (value, data, currentValue) {
        this.notify('format_result');
        
    },
    handleOnSelect: function (value, data, currentValue) {
        this.setCurrent(data);
        this.notify('selected');
    },
    forceQuery: function(){
        this.jqcontrol.ignoreValueChange = false;
        this.jqcontrol.onValueChange();
    }
    
});
