Tower.ContentTypesSelector = function(options) {
  var instance = this;
  instance._contentTypes = options.contentTypes || [];
  instance._targetId = options.targetId;
  instance._target = jQuery('#' + options.targetId);
  instance._curValues = instance._target.val();
  if (!Liferay.Util.isArray(instance._curValues)) {
    if (instance._curValues == '') {
      instance._curValues = [];
    } else {
      instance._curValues = instance._curValues.split(',');
    }
  }
  var html = '';
  html += '<div><div class="group-container">';
  for (var i = 0; i < instance._contentTypes.length; i++) {
    html += '<div class="group-row';
    if (i % 2 == 0) {
      html += ' one';
    } else {
      html += ' two';
    }
    html += '">';
    html += '<input type="checkbox" name="ContentTypesSelectorCKB"';
    html += ' id="ContentTypesSelectorCKB' + i + '"';
    html += ' value="' + instance._contentTypes[i].contentType + '"';
    if (instance._curValues.indexOf(instance._contentTypes[i].contentType) != -1) {
      html += ' checked="checked"';
    }
    html += '/>';
    html += '<label for="ContentTypesSelectorCKB' + i + '">';
    html += '&#160;<strong>' + instance._contentTypes[i].ext + '</strong>&#160;(' + instance._contentTypes[i].contentType + ')';
    html += '</label></div>';
  }
  html += '</div>'
  html += '<table class="overlay-action"><tr><td>';
  html += '<a id="ContentTypesSelectorCancel" href="javascript:void(0);" class="overlay-close">' + Liferay.Language.get('cancel') + '</a>';
  html += '</td><td>';
  html += '<a id="ContentTypesSelectorConfirm" href="javascript:void(0);" class="overlay-confirm">' + Liferay.Language.get('confirm') + '</a>';
  html += '</td></tr></table>';
  html += '</div>';
  var w = Tower.Util.getWindowWidth(800);
  w = (w - 500);
  var position = 'center';
  if (w > 0) {
    position = [(w / 2), 50];
  }
  instance._popup = Liferay.Popup({
    message: html,
    modal: true,
    title: Liferay.Language.get('select-content-type'),
    width: 500,
    position: position
  });
  jQuery('#ContentTypesSelectorCancel', instance._popup).click(function() {
    Liferay.Popup.close(instance._popup);
  });
  jQuery('#ContentTypesSelectorConfirm', instance._popup).click(function() {
    var contentTypes = jQuery(instance._popup).find("input[name='ContentTypesSelectorCKB']:checked");
    var values = new Array();
    for (var i = 0; i < contentTypes.length; i++) {
      if (values.indexOf(contentTypes[i].value) == -1) {
        values.push(contentTypes[i].value);
      }
    }
    instance._target.val(values.join(','));
    Liferay.Popup.close(instance._popup);
  });
};
