var changed_list = false;
var movement_controls_on = false;

var last_shown = '';

function toggleMovementControls() {
  controls = getSpansByStyleClass('movement_control');

  for (i = 0; i < controls.length; i++) {
    if (controls[i].style.display == 'none') {
      controls[i].style.display = 'inline';
    } else {
      controls[i].style.display = 'none';
    }
  }
  
  movement_controls_on = !movement_controls_on;
}

function getSpansByStyleClass(className) {
  var all = document.all ? document.all : document.getElementsByTagName('SPAN');
  var elements = new Array();

  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];

  return elements;
}


function toggle_item(checkbox, item) {
  response = xml_request(toggle_url, 'id=' + item.id + '&completed=' + (checkbox.checked ? 1 : 0));
  
  if (response.length > 1) { document.location.href = login_url; return false; }
  
  switch_lists(item, checkbox.checked);

  if (movement_controls_on) { toggleMovementControls(); }
  changed_list = true;

  toggle_reorder_link(checkbox.checked);
  toggle_delete_link(checkbox.checked);

  item.children[1].id = 'fade';
  fadeIn(7);

  if (parent_table_of_a_row(item).id == "completed_items") {
    checkbox.checked = true;
  } else {
    checkbox.checked = false;
  }
}

function toggle_reorder_link(toggle) {
  if (toggle) { incomplete_items_count--; } else { incomplete_items_count++; }
  if (incomplete_items_count == 0) { 
    $("reorder_link").style.display='none';
  } else {
    $("reorder_link").style.display='inline';
  }
}

function toggle_delete_link() {
  if (!$("delete_link")) { return false; }
  
  if (incomplete_items_count == 0) { 
    $("delete_link").style.display='inline';
  } else {
    $("delete_link").style.display='none';
  }
}

function switch_lists(item, toggle) {
  if (toggle) {
    prepend_child_to_table($("completed_items"), item);
  } else {
    append_child_to_table($("incomplete_items"), item);  
  }
}

function add_item(content) {
  changed_list = true;
  if (movement_controls_on) { toggleMovementControls(); }
  toggle_reorder_link(false);
  toggle_delete_link(false);
  
  id = xml_request(add_url, 'content=' + encodeURIComponent(content));
  
  if (id.length > 11) { document.location.href = login_url; return false; }
  
  append_child_to_table($("incomplete_items"), tr_with_todo_item(id, content));
}

function show_hide3(object1, object2, object3)
{
    show(object1);
    hide(object2);
    hide(object3);
}

function show_hide4(object1, object2, object3, object4)
{
    show(object1);
    hide(object2);
    hide(object3);
    hide(object4);
}

// Shows the first element and hides the second
function show_hide(object1, object2) {
  a = $( object1 );
  b = $( object2 );
  a.style.display = "inline";
  b.style.display = "none";
}

// Shows an element
function show(object1) {
  a = $( object1 );
  a.style.display = "inline";
}

function show_el(el) {
    el.style.display = "inline";
}

// Shows an element
function show_block(object1) {
  a = $( object1 );
  a.style.display = "block";
}

function show_el_block(el) {
    el.style.display = "block";
}

// Shows an element
function show_general(object1) {
  a = $( object1 );
  a.style.display = "";
}

function show_el_general(el) {
    el.style.display = "";
}

// Shows an element
function show_i(object1) {
  a = $( object1 );
  a.style.display = "inline";
  last_shown = a;
}

// Shows an element
function hide(object1) {
  a = $( object1 );
  a.style.display = "none";
}

function hide_el(el) {
    el.style.display = "none";
}

// Shows an element
function hide_last_shown() {
  if( last_shown != '' )
     last_shown.style.display = "none";
}

function toggle_display(object) {
  if (object.style.display == "none") {
    object.style.display = "block";
  } else {
    object.style.display = "none";
  }
}

function toggle_display_by_id(id) {
  object = document.getElementById(id);
  if (object.style.display == "none") {
    object.style.display = "block";
  } else {
    object.style.display = "none";
  }
}

function tr_with_todo_item(id, content) {
  input = document.createElement("input");

  if (browser.isIE) {
    input.type = 'checkbox';
    input.onclick = function () { eval("toggle_item(window.event.srcElement, window.event.srcElement.parentNode.parentNode)"); }  
  } else {
    input.setAttribute('type','checkbox');
    input.setAttribute('onClick','toggle_item(this, this.parentNode.parentNode)');  
  }

  td_with_input = document.createElement("td");
  td_with_input.appendChild(input);

  if (browser.isIE) {
    td_with_input.className = 'check';
  } else {
    td_with_input.setAttribute('class','check');  
  }

  text = document.createTextNode(content);
  td_with_text   = document.createElement("td");
  td_with_text.appendChild(text);
  td_with_text.setAttribute('class','itemtext');

  tr = document.createElement("tr");
  tr.setAttribute('id',id);

  tr.appendChild(td_with_input);
  tr.appendChild(td_with_text);
  
  return tr;
}


function xml_request(url, data) {
  req = xml_http_request_object();
  req.open("POST", url, false);
  req.send((data ? data + "&" : "")+"_=");
  return req.responseText;
}

function xml_http_request_object() {
  var req = false;
  try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      req = false;
    }
  }

  if (!req && typeof XMLHttpRequest!='undefined') {
    req = new XMLHttpRequest();
  }

  return req;
}

function append_child_to_table(collection, child) {
  if (browser.isIE) { 
    collection.childNodes.item(0).appendChild(child);
  } else {
    collection.appendChild(child);
  }
}

function prepend_child_to_table(collection, child) {
  collection.appendChild(child);
}

function parent_table_of_a_row(row) {
  if (browser.isIE) {
    return row.parentNode.parentNode;
  } else {
    return row.parentNode;
  }
  
}

// Toggles the display of two elements
function toggle_display_pair(a,b) {
  toggle_display($(a));
  toggle_display($(b));
}
