/*
 * Karel, utility library
 *
 * Licenced under GNU GPLv2
 *
 * Author: Jonoski Pavle
 * Date: 30.04.2009
 * 
 */

(function($){

util = {
	namespace: {
		createNamespace: function(namespace){
		  var o = null;
	 
		  if(typeof namespace == 'undefined'){
			 // nothing to create
			 return;
		  }
	 
		  var tts = namespace.split('.');
	 
		  eval('if(typeof (' +  tts[0] + ')== "undefined"){' + tts[0] + '= {}; } o = ' + tts[0]  + ';');
	 
		  for(var i = 1; i < tts.length; i++){
			 o[tts[i]] = o[tts[i]] || {};
			 o = o[tts[i]];
		  }
	   }
	},
	trim: function(str){
        str = str.replace(/^\s+/, '');
        for (var i = str.length - 1; i >= 0; i--) {
            if (/\S/.test(str.charAt(i))) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    },
	ui:{
	    Widget: function(config){
            config.cmp = config.cmp || $(config.html);
            this.id = config.id || 'widget-' + karel.core._id_seq++;
            $.extend(true, this, new karel.core.Bindable(config));
            
            this.el = this._handle[0];
            
            this.el.id = this.id;
            
            
            this.valueField = config.valueField || 'value';
            
            this.getValue = function(){
                return this.el[this.valueField];
            };
            
            this.setValueTo = function(property, value){
                this.el[property] = value;
            };
            
            this.setValue = function(value){
                this.el[this.valueField] = value;
            };
            
            this.appendTo = function(selector){
                var je = $(selector)[0];
                if(je)
                    je.appendChild(this.el);
            };
            
            this.setVisible = function(v){
                if(v){
                    this._handle.show();
                }else{
                    this._handle.hide();
                }
            };
            
            this.show = function(){this.setVisible(true);};
            this.hide = function(){this.setVisible(false);};
            
            this.destroy = function(){
                return this._handle.remove();
            };
            
            if(config.attributes){
                for(var a in config.attributes){
                    var av = config.attributes[a];
                    if(typeof(av) != 'function'){
                        this.setValueTo(a,av);
                    }
                }
            }
            
            // init
            if(config.appendTo)
                this.appendTo(config.appendTo);
                
            this.visible = !(!config.visible);
            
            if(this.visible)
                this.show();
            
            
	    },
	    
	    Container: function(config){
	        config.cmp = config.cmp || $('<div/>');
	        
	        $.extend(true, this, new util.ui.Widget(config));
	        
	        this.items = [];
	        
	        this.addEl = function(el){
	            this.el.appendChild(el);
	        };
	        
	        this.addItem = function(widget){
	            this.items.push(widget);
	            this.addEl(widget.el);
	        };
	        
	        
	        this.getItem = function(id){
	            for(var i = 0; i < this.items.length; i++){
	                if(this.items[i].id == id){
	                    return this.items[i];
	                }
	            }
	            return undefined;
	        };
	        
	        this.removeItem = function(id){
	            for(var i = 0; i < this.items.length; i++){
	                if(this.items[i].id == id){
	                    var item = this.items[i];
	                    this.items.splice(i,1);
	                    
	                    return item.destroy();
	                }
	            }
	            return undefined;
	        };
	        
	        // ovverrides
	        this.setValue = function(v){
	            for (var k in v){
	                if(typeof(v[k]) != 'function'){
	                    item = this.getItem(k);
	                    if(item){
	                        item.setValue(v[k]);
	                    }
	                }
	            }
	        };
	        
	        this.getValue = function(){
	            var r = {};
	            for(var i = 0; i < this.items.length; i++){
	                r[this.items[i].id] = this.items[i].getValue();
	            }
	            return r;
	        };
	    },
	    
	    TextField: function(config){
	        config.html = '<input type="text"/>';
	        $.extend(true, this, new util.ui.Widget(config));
	    },
	    
	    ComboBox: function(config){
	        config.cmp = $('<select/>');
	        config.attributes = config.attributes ||{
	            'name': config.name
	        };
	        $.extend(true, this, new util.ui.Container(config));
	        var _this = this;
	        this.addOption = function(key, value){
	            var item = new util.ui.Widget({
	                id: _this.id + '-' + key,
	                html: '<option/>',
	                attributes: {
	                    value: key
	                }
	            });
	            item.el.innerHTML = value;
	            this.addItem(item);
	        };
 
	        this.select = function(v){
	            for(var i = 0; i < this.items.length; i++){
	                var option = this.items[i];
	                option.setValueTo('selected', option.getValue() == v ? 'selected' : undefined);
	            }
	        };
	        
	        this.setValue = function(v){
	            //this.el.value = v;
	            this.select(v);
	        };
	        
	        this.getValue = function(){
	            return this.el.value;
	        };
	    },
	    
	    CheckBox: function(config){
	        config.html = '<input type="checkbox"/>"';
	        config.valueField = 'checked';
	        config.attributes = config.attributes ||{
	            'name': config.name
	        };
	        $.extend(true,this, new util.ui.Widget(config));
	    },
	    
	    Radio: function(config){
	        config.html = '<input type="radio" />';
	        config.valueField = 'checked';
	        config.attributes = config.attributes ||{
	            'name': config.name
	        };
	        $.extend(true, this, new util.ui.Widget(config));
	    },
	    
	    TextArea: function(config){
	        config.html = '<textarea/>';
			var c = config.cols || '10';
			var r = config.rows || '5';
	        config.attributes = config.attributes ||{
	            'cols': config.cols || 10,
	            'rows': config.rows || 5
	        };
	        
	        $.extend(true, this, new util.ui.Widget(config));
	    }
	}
};


})(jQuery);

