/********************************************
 * Selector Simple
 ********************************************/
Em_selector_simple = function(){

}

Em_selector_simple.factory = function(xInput, datas,opt, boxOptions){
    selector = new Em_selector_simple();
    selector._init(xInput, datas,opt, boxOptions);
    return selector;
}

//init actions
Em_selector_simple.prototype._init = function(xInput, datas, opt, boxOptions){
    
    //init optins
    if(boxOptions == null){
        boxOptions = {};
    }

    this.input = xInput;
    this.datas = datas;
    this.items = [];
    this.items_selected = [];
    this.boxOptions = boxOptions;
    this.options = {
        showDesc : true,
        copyDescTo : null,
        showInput:true,
        multiple : false,
        multipleMax : 0,
        showControlPanel:true,
        separator:'|'
    };
    
   for(key in opt){
        this.options[key] = opt[key];
    }
    
    opt = this.options;
    
    //read input valus
    this.readInput();
    
    //try replace original input
    var xDiv =  $("<div class='Em_selector_desc'></div>");
    xDiv.attr({id : this.input.attr('id') + "_show"});
    $("<span></span>").appendTo(xDiv);
    $("<div class='Em_selector_btn'></div>").appendTo(xDiv);
    if(!opt.showInput){
        opt.showDesc = true;
        //Copy style of input
        EmAx.copyCss(xDiv, this.input);
        this.input.hide();
    }
    EmAx.copyCss(xDiv, this.input);
    this.descDiv = xDiv;
    this.descDiv.insertAfter(this.input);
    if(!opt.showDesc){
        this.descDiv.hide();
    }
    
    //Bind click event to item
    this.descDiv.bind('click',{selector : this},this.onShowBtnClick);
    this.input.bind('click',{selector : this},this.onShowBtnClick);
    
    //Set attach
    if(!('attach' in boxOptions)){
        if(opt.showDesc){
            boxOptions.attach = this.descDiv;
        }else if(opt.showInput){
            boxOptions.attach = this.input;
        }
    }
    
    this.setDesc();
}

Em_selector_simple.prototype.show = function(){

    if(this.box == null){
        //creat show box and show divs
        
        this.box = new Em_box(this.boxOptions);
        
        this.win = $(this.box.getContentDiv());
        this.win.empty();
        this.win.addClass('Emxs_box');
        
        this.headerDiv = $("<div class='Emxs_header'></div>").appendTo(this.win);
        this.notesDiv = $("<div class='Emxs_notes'></div>").appendTo(this.headerDiv);
        this.btnDiv = $("<div class='Emxs_btn'></div>").appendTo(this.headerDiv);
        this.contentDiv = $("<div class='Emxs_content'></div>").appendTo(this.win);
    
        this.box.btnOk.bind('click', {selector: this}, this.onOkClick);
        
        if(!this.options.showControlPanel){
            this.headerDiv.hide();
        }
    
        win = this.win;
        this.contentDiv.css('height',win.height()-60);
        this.contentDiv.css('overflow','auto');
        win.css('height','auto');
        
        this.onShowFirst();
    }
    
    //show select box
    this.box.show();
    this.onShow();
}

//This function will be called for first time of show box
Em_selector_simple.prototype.onShowFirst = function(){
    
    var ul = $("<ul></ul>").appendTo(this.contentDiv);
    if(this.options.multiple){
        var inputBox = '<input type="checkbox">'; 
    }else{
        var inputBox = '';
    }
    
    for(key in this.datas){
        var item = $("<li></li>").appendTo(ul);
        item.html(inputBox + this.datas[key]);
        item.bind('click', {selector : this, index : key}, this.onItemClick);
        item.mouseover(function(){$(this).addClass('active');});
        item.mouseout(function(){$(this).removeClass('active');});
        this.items[key] = item;
    }
}

//This function will be called when box is open every time
Em_selector_simple.prototype.onShow = function(){
    this._heightSelected();
}

//try height selected items when box showing
Em_selector_simple.prototype._heightSelected = function(){
    
    //heihtlight selected
    for(key in this.items_selected){
        if(key in this.items){
            var item = this.items[key];
            item.addClass('selected');
            $('input:checkbox',item).attr('checked',true);
            
            //scrool
            i = item.position().top;
            $(this.box.getContentDiv()).scrollTop(i-50);            
        }
    }
}

Em_selector_simple.prototype.close = function(){
    this.box.hide();
}

Em_selector_simple.prototype.cleareSelect = function(){
    for(key in this.items_selected){
        this.removeSelect(key);
    }
}

Em_selector_simple.prototype.countSelected = function(){
    var i = 0;
    for(key in this.items_selected){
        i++;
    }
    return i;
}

Em_selector_simple.prototype.addSelect = function(key){
    
    if(!this.options.multiple){
        this.cleareSelect();
    }
    
    if(this.options.multiple && this.options.multipleMax > 0){
        if(this.countSelected() >= this.options.multipleMax){
            alert('Max Selected!');
            return;
        }
    }
    
    if(key in this.datas){
        this.items_selected[key] = this.datas[key];
    }
    
    if(key in this.items){
        this.items[key].addClass('selected');
        $('input:checkbox',this.items[key]).attr('checked',true);
    }
    
}

Em_selector_simple.prototype.removeSelect = function(key){

    if(key in this.items_selected){
        delete this.items_selected[key];
    }
    
    if(key in this.items){
        this.items[key].removeClass('selected');
        $('input:checkbox',this.items[key]).attr('checked',false);
    }
}

Em_selector_simple.prototype.setDesc = function(){
    var arr = new Array();
    var desc = '';
    for(key in this.items_selected){
        arr.push(this.datas[key]);
    }
    if(arr.length > 0){
        desc = arr.join(this.options.separator);
        this._setDesc(desc);
    }else{
        this._setDesc(' ');
    }
    if(this.options.copyDescTo != null){
        $(this.options.copyDescTo).val(desc);
    }
}

Em_selector_simple.prototype._setDesc = function(desc){
    $('span',this.descDiv).html(desc);
}

Em_selector_simple.prototype.setInput = function(){

    if(this.options.multiple){
        var arr = new Array();
        for(key in this.items_selected){
            arr.push(key);
        }
        this.input.val(arr.join(this.options.separator));
    }else{
        for(key in this.items_selected){
            this.input.val(key);
        }
    }
    
}

Em_selector_simple.prototype.readInput = function(){
    if(this.options.multiple){
        var str = new String(this.input.val());
        var vals = str.split(this.options.separator);
        for(i in vals){
            this.addSelect(vals[i]);
        }
    }else{
        this.addSelect(this.input.val());
    }
}

Em_selector_simple.prototype.itemClick = function(key){
    
    if(!this.options.multiple){
        this.addSelect(key);
        this.okClick();
    }else{
	    if(key in this.items_selected){
	        this.removeSelect(key);
	    }else{
	        this.addSelect(key);
	    }
    }
    
}

Em_selector_simple.prototype.okClick = function(){
    this.setInput();
    this.setDesc();    
    this.close();
}

Em_selector_simple.prototype.onShowBtnClick = function (event){
    event.data.selector.show();
}

Em_selector_simple.prototype.onItemClick = function(event){
    event.data.selector.itemClick(event.data.index);
}

Em_selector_simple.prototype.onOkClick = function(event){
    event.data.selector.okClick();
}