// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Director = Class.create();


function add_row_to_table(table, table_row) {
  for(i=0;i<table.childNodes.length;i++) {
    child = table.childNodes[i];
    if(child.tagName == "TBODY") {
      child.insertBefore(table_row,child.lastChild);
      break;
    }
  }
}

/*
  TableRowBuilder class         - helps create a table row (tr) element with any number of table cells (td)
  ExistingTableRowBuilder class - helps extend an existing table row (tr) element with any number of table cells (td)

  Example:

    trb = new TableRowBuilder('id');
    trb.addTableCell('Content1');
    trb.addTableCell('Content2');
    alert(trb.getTableRow());


    trb = new ExistingTableRowBuilder($('the_tr'));
    trb.addTableCell('Content1');
    trb.addTableCell('Content2');
    alert(trb.getTableRow());
*/
function addTableCell(text) {
  td = document.createElement("td");
  td.innerHTML = text;
  this.tr.appendChild(td);
}

function getTableRow() {
  return this.tr;
}

function addRowToTable(table) {
  for(i=0;i<table.childNodes.length;i++) {
    child = table.childNodes[i];
    if(child.tagName == "TBODY") {
      child.appendChild(this.tr,child.lastChild);
      break;
    }
  }
}

function addRowToTableAfter(table, row) {
  for(i=0;i<table.childNodes.length;i++) {
    child = table.childNodes[i];
    if(child.tagName == "TBODY") {
      child.insertBefore(this.tr, row.nextSibling);
      break;
    }
  }
}

function TableRowBuilder(id) {
  this.tr = document.createElement("tr");
  this.tr.id = id;
  this.addTableCell = addTableCell;
  this.getTableRow = getTableRow;
  this.addRowToTable = addRowToTable;
  this.addRowToTableAfter = addRowToTableAfter;
}

function ExistingTableRowBuilder(tr) {
  this.tr = tr;
  this.addTableCell = addTableCell;
  this.getTableRow = getTableRow;
}



function moveRelativeTo(sourceName, targetName, leftOffset, topOffset) {
    source = $(sourceName);
    target = $(targetName);
    var offsets = Position.cumulativeOffset(source);
    target.style.top  = (offsets[1]+topOffset) + 'px';
    target.style.left = (offsets[0]+leftOffset) + 'px';
}

/* Misc Form Helper Functions */
function removeExampleText(textfield) {
    if(textfield.className == "textfieldExample") {
        textfield.className = "";
        textfield.value = "";
    }
}

function switchPlanner(switch_planner_select) {
    planner_id = switch_planner_select.options[switch_planner_select.selectedIndex].value;
    if (planner_id != "") {
        document.location = "/planner/"+planner_id;
    }
}