var insite_innovaeditor = "";
var insite_editor_width = 750;
var insite_editor_height = 500;

function insite_get_new_http_object() {
  var xmlhttp;

  /** Special IE only code ... */
  /*@cc_on
    @if (@_jscript_version >= 5)
      try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
        try {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
          xmlhttp = false;
        }
     }
     @else
       xmlhttp = false;
  @end @*/

  /** Every other browser on the planet */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

function insite_get_element(path, container_element_id) {
  var xmlhttp = insite_get_new_http_object();
  xmlhttp.open('GET', path, true);

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
        var response = xmlhttp.responseText.split("|");
        // Set title
        insite_editing_title.innerHTML = response[0];
        // Set HTML
        insite_innovaeditor = initialize_editor_create(insite_editor_width, 500);
        insite_innovaeditor.RENDER(response[1], container_element_id);
      } else {
        // alert('Blah');
      }
    }
  }

  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.send();
}

function insite_open_editor(element_type, element_id) {
  // // Background
  // var div_editor_background = document.createElement('div');
  // div_editor_background.setAttribute('id', 'insite_editing_background');
  // div_editor_background.style.width = document.body.clientWidth + "px";
  // div_editor_background.style.height = document.body.clientHeight + "px";
  // div_editor_background.onclick = function(e) { close_editor(); }
  // document.body.appendChild(div_editor_background);

  // Container
  var ta_width = insite_editor_width;
  var ta_height = insite_editor_height + 80;
  var div_editor_container = document.createElement('div');
  div_editor_container.setAttribute('id', 'insite_editing_container');
  div_editor_container.style.width = ta_width + "px";
  div_editor_container.style.height = ta_height + "px";
  div_editor_container.style.left = (document.body.clientWidth / 2) - (ta_width / 2) + "px";
  div_editor_container.style.top = "25px";
  document.body.appendChild(div_editor_container);

  // Header: Close link
  var image_close = document.createElement('img');
  image_close.setAttribute('id', 'insite_editing_close_image');
  image_close.setAttribute('src', '/images/insite_editing_close.png');
  image_close.onclick = function(e) { insite_close_editor(); }
  div_editor_container.appendChild(image_close);

  // Header: Title
  var div_title = document.createElement('div');
  div_title.setAttribute('id', 'insite_editing_title');
  div_editor_container.appendChild(div_title);

  // Editor
  var ta_width = insite_editor_width;
  var ta_height = insite_editor_height;
  var div_editor_editor = document.createElement('div');
  div_editor_editor.setAttribute('id', 'insite_editing_editor');
  div_editor_editor.style.width = ta_width + "px";
  div_editor_editor.style.height = ta_height + "px";
  div_editor_container.appendChild(div_editor_editor);

  // Save button
  var button_editor_save = document.createElement('button');
  button_editor_save.setAttribute('id', 'insite_editing_button_save');
  button_editor_save.className = "button";
  button_editor_save.innerHTML = "Vista";
  button_editor_save.onclick = function(e) { insite_save(); }
  div_editor_container.appendChild(button_editor_save);

  // Cancel link
  var button_editor_cancel = document.createElement('a');
  button_editor_cancel.setAttribute('id', 'insite_editing_button_cancel');
  button_editor_cancel.setAttribute('href', 'javascript:insite_close_editor();');
  button_editor_cancel.innerHTML = "Hætta við";
  div_editor_container.appendChild(button_editor_cancel);

  var url = "/admin/pages/edit_ajax/" + element_id;
  insite_get_element(url, "insite_editing_editor");

  return false;
}

function insite_close_editor() {
  document.body.removeChild(document.getElementById("insite_editing_container"));
  // document.body.removeChild(document.getElementById("insite_editing_background"));
}

function insite_save() {
  insite_close_editor();
}