function ajaxParameters(pars, form_name, timeout) {
  if (!ColorPicker) {
    return false;
  }
  var args = {
    'url' : '/ajax/handlers/parameters.xml',
    'groupName' : 'global'
  };
  var params = {};
  if (form_name) {
    var form = document.forms[form_name];
    if (form) {
      args['groupName'] = 'popup';
      params = getFormParameters(form);
    }
  }
  if (typeof(pars) == 'object') {
    for (var i in pars) {
      params[i] = pars[i];
    }
  }
  var handlers = {
    'onSuccess' : manageParameters
  };
  AjaxEngine.makeReplaceCall('parameters', args, params, handlers, timeout);
  return false;
}

function manageParameters(response, response_data) {
  if (response_data['ClosePopup']) {
    hidePopup();
  }
  if (response_data['ShowPopup']) {
    return prefillAndShowPopup(response_data['PopupContent']);
  }
  if (response_data['UpdatePopup']) {
    return prefillPopup(response_data['PopupContent']);
  }
  return false;
}

function editParameters(widget_id) {
  return ajaxParameters(
    {
      'widget_id' : widget_id
    }
  );
}

function customizeWidget(widget_id, platform) {
  return ajaxParameters(
    {
      'widget_id' : widget_id,
      'platform' : platform,
      'cmd' : 'customize'
    }
  );
}

function playWidget(widget_id, platform) {
  return ajaxParameters(
    {
      'widget_id' : widget_id,
      'platform' : platform,
      'cmd' : 'play'
    }
  );
}

function getPreviewParametersForm() {
  return document.forms['previewparameters'];
}

function getEditParameterForm() {
  return document.forms['editparameter'];
}

function addParameter() {
  return ajaxParameters(
    {
      'cmd' : 'add'
    },
    'previewparameters'
  );
}

function editParameter(parameter_index) {
  return ajaxParameters(
    {
      'cmd' : 'edit',
      'parameter_index' : parameter_index
    },
    'previewparameters'
  );
}

function moveParameterUp(parameter_index) {
  return ajaxParameters(
    {
      'cmd' : 'moveup',
      'parameter_index' : parameter_index
    },
    'previewparameters'
  );
}
function moveParameterDown(parameter_index) {
  return ajaxParameters(
    {
      'cmd' : 'movedown',
      'parameter_index' : parameter_index
    },
    'previewparameters'
  );
}
function changeParameterStyle(style) {
  return ajaxParameters(
    {
      'cmd' : 'changestyle',
      'style' : style
    },
    'editparameter'
  );
}

function addParameterOption() {
  return ajaxParameters(
    {
      'cmd' : 'addoption'
    },
    'editparameter'
  );
}

function deleteParameterOption(option_index) {
  return ajaxParameters(
    {
      'cmd' : 'deleteoption',
      'option_index' : option_index
    },
    'editparameter'
  );
}

function cancelParameter() {
  return ajaxParameters(
    {
      'cmd' : 'cancel'
    },
    'editparameter'
  );
}

function saveParameter() {
  return ajaxParameters(
    {
      'cmd' : 'save'
    },
    'editparameter'
  );
}

function deleteParameter(parameter_index) {
  return ajaxParameters(
    {
      'cmd' : 'delete',
      'parameter_index' : parameter_index
    },
    'previewparameters'
  );
}

function publishParameters() {
  return ajaxParameters(
    {
      'cmd' : 'publish'
    },
    'previewparameters'
  );
}

function revertParameters() {
  return ajaxParameters(
    {
      'cmd' : 'revert'
    },
    'previewparameters'
  );
}

function saveParameterDefaults() {
  return ajaxParameters(
    {
      'cmd' : 'savedefaults'
    },
    'previewparameters'
  );
}

function refreshWidget() {
  return ajaxParameters(
    {
      'cmd' : 'refresh'
    },
    'previewparameters'
  );
}

function togglePreview(visible) {
  try {
    var div = document.getElementById('wpdc_widget_preview');
    if (visible) {
      h('wpdc_widget_preview');
    } else {
      s('wpdc_widget_preview');
    }
  } catch(e) {
    return true;
  }
  return false;
}

var colorPickerInitialized = null;
var colorPickerIndex = null;
var colorPickerPrefix = '#';

function initializeColorPicker() {
  if (colorPickerInitialized) {
    return;
  }
  colorPickerInitialized = 1;
  ColorPicker.initialize();
}

function toggleColorPicker(index) {
  var color_picker = ColorPicker.object('wpdc_color_picker');
  var display = color_picker.style.display;
  if (index === colorPickerIndex) {
    display = display == 'none' ? 'inline' : 'none';
  } else {
    display = 'inline';
  }
  color_picker.style.display = display;
  if (display == 'inline') {
    m('wpdc_color_picker', 'wpdc_color_swatch_' + index, 0, 0);
    updatePickerColor(index);
  }
  return false;
}

function fixColor(color) {
  return color.replace(/^0x/, '').replace('/^#/', '').replace(/[^0-9A-Fa-f]+/g, '');
}

function updatePickerColor(index) {
  initializeColorPicker();
  colorPickerIndex = index;
  var color = fixColor(ColorPicker.object('wpdc_color_input_' + index).value);
  ColorPicker.setColor('#' + color, true);
  ColorPicker.object('wpdc_color_swatch_' + index).style.backgroundColor = ColorPicker.getColor();
}

ColorPicker.handlerOnColorUpdate = function() {
  if (colorPickerIndex == null) {
    return;
  }
  var color = ColorPicker.getColor();
  ColorPicker.object('wpdc_color_input_' + colorPickerIndex).value = colorPickerPrefix + fixColor(color);
  ColorPicker.object('wpdc_color_swatch_' + colorPickerIndex).style.backgroundColor = '#' + fixColor(color);
  if (ColorPicker.object('wpdc_widget_preview')) {
    saveParameterDefaults();
  }
}

function getPostToMySpaceForm() {
  return document.forms['myspace'];
}

function postToMySpace() {
  var pform = getPreviewParametersForm();
  var mform = getPostToMySpaceForm();
  var params = getFormParameters(pform);
  mform.elements['t'].value = params.t;
  mform.elements['u'].value = params.u;
  mform.elements['c'].value = params.c;
  mform.elements['l'].value = params.l;
  mform.submit();
  return false;
}
