// -*- c -*-

// JavaScript functions for Web pages at www.ece.lsu.edu.
// Time-stamp: <29 September 2008, 16:44:56 CDT, koppel@nested.ece.lsu.edu>

function _getcookies()
{
  // Return an array of cookies that start with string "_ecemain".
  // Array is indexed by cookie name (without _ecemain prefix) and
  // holds cookie values.
  //
  var cookie = document.cookie;
  if( cookie == null ) cookie = "";
  var nospaces = "";
  var pos = 0, ind = 0;
  var len = cookie.length;
  while( ind < len )
    {
      ind = cookie.indexOf(" ",pos);
      if( ind == -1 ) ind = len;
      nospaces += cookie.substring(pos,ind);
      pos = ind + 1;
    }

  var jar = new Object();
  for( pos = 0; pos < len ; )
    {
      ind = nospaces.indexOf("=",pos);
      if( ind == -1 ) break;
      var cname = nospaces.substring(pos,ind);
      pos = ind + 1;
      ind = nospaces.indexOf(";",pos);
      if( ind == -1 ) ind = len;
      var cval = nospaces.substring(pos,ind);
      pos = ind + 1;
      if( cname.charAt(0) == "_" ) {
        if( cname.substring(0,8) != "_ecemain" ) continue;
        cname = cname.substring(8+1);
      }
      jar[cname] = cval;
    }
  return jar;
}

function setcookie(pname,val,expiresecs)
{
  // Set cookie with name obtained by concatenating _ecemain_ and PNAME and
  // having value VAL.  If EXPIRESECS is null cookie never expires, if 0,
  // no expiration set (expires at end of session), otherwise set expiration to
  // EXPIRESECS seconds from now.
  //
  var name = "_ecemain_" + pname;
  if( name != escape(name) )
    window.alert("Cookie name "+name+" may not work.");
  if( expiresecs == null ) expiresecs = 15552000;
  var exp_time = new Date();
  exp_time.setTime(exp_time.getTime() + expiresecs * 1000);
  var the_cookie = name + "=" + escape(val);
  if( expiresecs > 0 )
    the_cookie = the_cookie + ";expires="+exp_time.toGMTString() + ";path=/";
  document.cookie = the_cookie;
}

//
// Feature Code
//

// Feature is item shown on upper right of home page, consisting of an
// image, and an optional caption and link.

var feature_array = new Array();  // One element per feature.

function feature_array_insert(file,caption,url)
{
  // Append item to feature_array using image in file FILE, optional
  // CAPTION and URL.  Images are assumed to be in subdirectory images.
  //
  var idx = feature_array.length;
  var feature = new Object;
  feature.file = file;
  feature.caption = caption;
  feature.url = url;
  feature_array[idx] = feature;
}

function feature_array_setup()
{
  // Add items to feature array.
  //

  feature_array_insert
    ("2008/computer-room-tree-600px.jpeg",
     "ECE Computer Room, Post Gustav");

  if( 1 ) {
  feature_array_insert
    ("2007/ee_bdg_ed.jpeg",
     "Home, Sweet Home",
     "http://www.lsu.edu/campus/maps/ELEN01.html");

  feature_array_insert
    ("2004/cockpit2.jpeg",
     "Faculty Research: Aircraft Safety",
     "http://www.ece.lsu.edu/mcu/");

  }


  feature_array_insert
    ("2007/elab_ed.jpeg",
     "EE 3221 Electronics II Lab" );


  if( 1 ) {
  feature_array_insert
    ("2005/ee2120_day1_spring.jpeg",
     "EE 2120, First Day");
  }

  if( 1 ) {
  feature_array_insert
    ("2004/ee3755_last_day.jpeg", "EE 3755, Last Day",
     "http://www.ece.lsu.edu/alex/EE3755.html");

  feature_array_insert
    ("pse_demo.png","CPU Visualization using PSE",
     "http://www.ece.lsu.edu/koppel/pse");

  }

  feature_array_insert
    ("2005/robot_2005_team2.jpeg","2005 IEEE Robot Contest Winners",
     "http://www.ece.lsu.edu/ieee/");

  feature_array_insert
    ("2007/ee_bdg_to_ur_ed.jpeg",
     "Home, Sweet Home",
     "http://www.lsu.edu/campus/maps/ELEN01.html");


  feature_array_insert
    ("2004/robot_contest_winners.jpg","2004 IEEE Robot Contest Winners",
     "http://www.ece.lsu.edu/ieee/");

  feature_array_insert
    ("2004/Robot_Poster_small.jpg","2004 IEEE Robot Contest",
     "http://www.ece.lsu.edu/images/2004/Robot_Poster_small.jpg");

  if ( 0 )
  feature_array_insert
    ("working_on_the_robot_2003.jpg","Working on the 2003 Robot",
     "http://www.ece.lsu.edu/ieee/");

  feature_array_insert
    ("robot2003.jpg","2003 IEEE Student Robot Contest Entry",
     "http://www.ece.lsu.edu/ieee/");

  if(1){

  feature_array_insert
    ("2008/clean-room-a.jpeg", "In the Solid State Lab Clean Room",
     "emdl");

  feature_array_insert
    ("essen.jpg", "Book Home Page",
     "http://www.ece.lsu.edu/kemin/essentials.htm");

  feature_array_insert
    ("ram-colp.png", "Book Home Page",
     "http://www.wkap.nl/prod/b/1-4020-7573-1");

  feature_array_insert
    ("vaidy-book.jpeg", "Book Home Page",
     "http://www.ece.lsu.edu/vaidy/book.html");

  feature_array_insert
    ("2006/woods-book.jpeg", "Book Home Page",
     "http://www.bh.com/bookscat/links/details.asp?isbn=0750645822");

  }

}

function main_feature_emit()
{
  feature_array_setup();
  var count = feature_array.length;
  var cookiehash = _getcookies();
  var noticeidx_raw = cookiehash["noticeidx"];
  var noticeidx_rare =
    noticeidx_raw == null
    ? Math.floor(Math.random() * count)
    : parseInt(noticeidx_raw);
  var noticeidx = noticeidx_rare < count ? noticeidx_rare : 0;

  feature_emit(noticeidx,true);

  noticeidx++;
  if( noticeidx >= count ) noticeidx = 0;
  setcookie("noticeidx",noticeidx,0);

}

function feature_emit(idx,include_link)
{
  var file_info = feature_array[idx];
  var file_name = file_info.file;
  var have_caption = file_info.caption != null && file_info.caption != "";
  var alt_text = "Reload to change image, click image to see all images";
  var have_link = file_info.url != null;

  if( have_link ) alt_text += ", click caption for more info.";
  alt_text += ".";

  if( include_link )
    document.write("<a href=\"features.html\" title=\"" + alt_text + "\">\n");
  document.write
    ("<img src=\"images/" + file_name + "\" alt=\"" + alt_text + "\""
     + " width=325 border=0>");
  if( include_link ) document.write("</a>");

  if( have_caption ) document.write("<div class=\"feature-link\">");
  if( have_link ) document.write("<a href=\"" + file_info.url + "\">");
  if( have_caption ) document.write(file_info.caption);
  if( have_link ) document.write("</a>");
  if( have_caption ) document.write("</div>");
}
