/*
  Tabbing
  By Arun Chauhan, Department of Computer Science, Indiana University, 2008.
 */

/*
  The tabs are created by wrapping the existing content div in a table, as follows.

  <table id="container">
    <tbody>
      <tr>
        <td></td>
	<td> <!-- Top corner divs come here --> </td>
      </tr>

      <tr>
        <td id="tab">
	  <ul>
	    <!-- One list item is one tab -->
	    <li>
	      <div class="corner">
	        ...
	      </div>
	      <a href="...">
	        <em>H</em>
		<em>o</em>
		<em>m</em>
		<em>e</em>
	      </a>
	      <!-- The following div creates a nice rounded corner at the bottom left -->
	      <div class="corner">
	        ...
	      </div>
	    </li>

	    <!-- Next tab -->
	    <li>
	      ...
	    </li>
	  </ul>
	</td>

	<td>
	  <div class="content">
	    ...
	  </div>
	</td>
      </tr>

      <tr>
        <td></td>
	<td> <!-- Bottom corner divs come here --> </td>
      </tr>
    </tbody>
  </table>
 */
function decorate_page (cur)
{
  /* Set up the container table body */
  var container_table_body = document.createElement('tbody');

  /* Add the row for top corner divs */
  var top_frill_row = document.createElement('tr');
  var empty_td = document.createElement('td');
  empty_td.setAttribute('class','blank');
  empty_td.setAttribute('className','blank'); // MS IE B*** S***
  top_frill_row.appendChild(empty_td);
  var top_frill_td = document.createElement('td');
  top_frill_td.appendChild(create_top_corners('corner_selected','cr'));
  top_frill_row.appendChild(top_frill_td);
  container_table_body.appendChild(top_frill_row);

  /* Add the row containing the tabs and the main content */
  var main_row = document.createElement('tr');
  var tabs_td = document.createElement('td');
  tabs_td.setAttribute('id','tabs');
  main_row.appendChild(tabs_td);
  tabs_td.appendChild(create_tabs(cur)); /* This is the helper function call where tabs get created */
  var content_td = document.createElement('td');
  main_row.appendChild(content_td);
  var content_div = document.getElementById('content');
  content_td.appendChild(content_div);
  container_table_body.appendChild(main_row);

  /* Add the row for bottom corner divs */
  var bottom_frill_row = document.createElement('tr');
  empty_td = document.createElement('td');
  empty_td.setAttribute('class','blank');
  empty_td.setAttribute('className','blank'); // MS IE B*** S***
  bottom_frill_row.appendChild(empty_td);
  var bottom_frill_td = document.createElement('td');
  bottom_frill_td.appendChild(create_bottom_corners('corner_selected','cr'));
  bottom_frill_row.appendChild(bottom_frill_td);
  container_table_body.appendChild(bottom_frill_row);

  /* Finally, create an enclosing table and add it to the document */
  var container_table = document.createElement('table');
  container_table.setAttribute('id', 'container');
  container_table.appendChild(container_table_body);
  document.getElementsByTagName('body').item(0).appendChild(container_table);
}


function create_tabs (cur)
{
  var pages = new Object();
  pages["HOME"] = "index.html";
  pages["PUBLICATIONS"] = "pubs.html";
  pages["PRESENTATIONS"]  = "presentations.html";
  pages["BIO"]  = "education.html";

  var tab_list = document.createElement('ul');
  for (menu_item in pages) {
    var tab = document.createElement('li');
    tab_list.appendChild(tab);
    if (menu_item == cur) {
      tab.appendChild(create_top_corners('corner_selected','tr'));
      var i;
      for (i=0; i < menu_item.length; i++) {
	var em = document.createElement('em');
	em.setAttribute('class','selected');
	em.setAttribute('className','selected'); // MS IE B*** S***
	em.appendChild(document.createTextNode(menu_item.charAt(i)));
	tab.appendChild(em);
      }
      tab.appendChild(create_bottom_corners('corner_selected','tr'));
    }
    else {
      tab.appendChild(create_top_corners('corner','tr'));
      var link = document.createElement('a');
      link.setAttribute('href', pages[menu_item]);
      tab.appendChild(link);
      var i;
      for (i=0; i < menu_item.length; i++) {
	var em = document.createElement('em');
	em.appendChild(document.createTextNode(menu_item.charAt(i)));
	link.appendChild(em);
      }
      tab.appendChild(create_bottom_corners('corner','tr'));
    }
  }
  return tab_list;
}


function create_top_corners (rounding_class, rdiv_name)
{
  var rounding_div = document.createElement('div');
  rounding_div.setAttribute('class', rounding_class);
  rounding_div.setAttribute('className', rounding_class); // MS IE B*** S***
  var i;
  for (i=1; i <= 5; i++) {
    var new_div = document.createElement('div');
    new_div.setAttribute('class',rdiv_name+i.toString());
    new_div.setAttribute('className',rdiv_name+i.toString()); // MS IE B*** S***
    rounding_div.appendChild(new_div);
  }
  return rounding_div;
}


function create_bottom_corners (rounding_class, rdiv_name)
{
  var rounding_div = document.createElement('div');
  rounding_div.setAttribute('class', rounding_class);
  rounding_div.setAttribute('className', rounding_class); // MS IE B*** S***
  var i;
  for (i=5; i >= 1; i--) {
    var new_div = document.createElement('div');
    new_div.setAttribute('class',rdiv_name+i.toString());
    new_div.setAttribute('className',rdiv_name+i.toString()); // MS IE B*** S***
    rounding_div.appendChild(new_div);
  }
  return rounding_div;
}


