function initCustomDropDown(inpt, btn){
  var _ext = '.html';
  inpt.hide();
  var _div;
  var _autofeed = [];
  var _li_btn = btn.parents('li:first');
  var _lis = [];
  var s_value = [];
  var createBox = function(text){
    var _span = $('<span></span>')
    .insertAfter(_li_btn).wrap('<li><div></div></li>');
    var _text = $('<em>'+text+'</em>').appendTo(_span);
    $('<a class="close" href="javascript:;"/>').click(function(){
      dispose(text);
    }).insertAfter(_text);
    return _span.parents('li');
  }
  var update = function(){
    inpt.get(0).value = '';
    var separator = '';
    $(s_value).each(function(){
      inpt.get(0).value += separator + this;
      separator = ', ';
    });
  }
  var add = function(text){
    s_value.push(text);
    _lis.push(createBox(text));
    update();
  }
  var dispose = function(text){
    var temp = s_value;
    s_value = [];
    $(_lis).each(function(){
      $(this).remove();
    });
    _lis = [];    
    $(temp).each(function(i){
      if (this != text){
        s_value.push(this);
        _lis.push(createBox(this));
      }
    });
    _div.find('label').each(function(){
      if (this.innerHTML == text){
        $(this).parent().find('input:first').attr('checked', false);
      }
    })
    update();
  }
  var dispose_all = function(){
    s_value = [];
    $(_lis).each(function(){
      $(this).remove();
    });
    _lis = [];
    update();
  }
  var save_chk = function(){
    _div.find('input:checked').each(function(){
      add($(this).parents('li').find('label').html());
    });
    _div.hide();
    inpt.parents('.row:first').css('position', 'static');
  }
  var save_chk2 = function(){
    _div.filter(':visible').find('input:checked').each(function(){
      add($(this).parents('li').find('label').html());
    });
    _div.hide();
    inpt.parents('.row:first').css('position', 'static');
  }
  var initDefault = function(){
    inpt.parents('ul').find('li').each(function(i){
      if (i !=0 && !($(this).hasClass('custom-drop-down'))){
        add($(this).find('em').html());
        $(this).remove();
      }
    })
  }
  
  $(document).click(function(e){
      if (!$(e.target).parents('div.drop-suport').length && !$(e.target).parents('ul.input-area').length) {
        save_chk2();
      }
  })
  
  var initDDCheckBox = function(arr){
    initDefault();
    //create select-box
    _div = $('<div class="drop-suport"></div>')
                            .insertAfter(inpt.parents('.input-holder:first'));
    var _ul = $('<ul></ul>')
                            .appendTo(_div).wrap('<div class="stretch-bg"></div>');
    var _save = $('<a class="btn-enter" href="javascript:;"><img alt="" src="/images/layout/btn-enter.gif"/></a>')
                            .insertAfter(_ul).click(function(){save_chk()});
    $('<span class="bottom-shadow"/>').appendTo(_div.find('.stretch-bg:first'));
    $(arr).each(function(i){
      if (this.checked){
        var _li = $('<li><input type="'+this.type+'" name="'+this.name+'" value="'+this.value+'" checked="checked" id="'+inpt.attr('id')+'-chk-'+i+'"/><label for="'+inpt.attr('id')+'-chk-'+i+'">'+this.text+'</label></li>').appendTo(_ul);
      }else{
        var _li = $('<li><input type="'+this.type+'" name="'+this.name+'" value="'+this.value+'" id="'+inpt.attr('id')+'-chk-'+i+'"/><label for="'+inpt.attr('id')+'-chk-'+i+'">'+this.text+'</label></li>').appendTo(_ul);
      }
    });
    _save.click();
  }
  $.getJSON(inpt.attr('alt'), function(json) {
    $.each(json, function(idx,str) {
          _autofeed.push(str);
      });
    if (_autofeed.length && _autofeed[0].type == 'checkbox')initDDCheckBox(_autofeed);
    if (_autofeed.length && _autofeed[0].type == 'radio')initDDCheckBox(_autofeed);
  });
  btn.attr('href', 'javascript:;');
  btn.click(function(){
    if (_autofeed.length && _autofeed[0].type == 'checkbox') {
      dispose_all();
      inpt.parents('.row:first').css('position', 'relative');
      _div.show();      
    };
    if (_autofeed.length && _autofeed[0].type == 'radio') {
      dispose_all();
      inpt.parents('.row:first').css('position', 'relative');
      _div.show();      
    };    
  })
}
