// LSU ECE Web Site JavaScript Code
//
// Time-stamp: <11 September 2008, 17:02:58 CDT, koppel@nested.ece.lsu.edu>
//
// Insert banner and footer as appropriate.


// Width of banner and footer.
//
// Determined by width of LSU wordmark, DoECE graphic and their separation.
//
var ece_bf_width_px = 705;

ece_site_root_set();
ece_curator_set();

function ece_site_root_set()
{
  var root_url = "http://www.ece.lsu.edu/";  // Possibly overwritten.
  var doc_loc = new String( document.location.href );
  if( doc_loc.match( /^file:/ ) ) // Overwrite root_url
    {
      var scripts = document.getElementsByTagName("script");
      for(var i = 0; i < scripts.length; i++) {
        var src = new String(scripts[i].src);
        var parts = src.match( /(.*)(banner-footer\.js)$/ );
        if( parts.length != 3 ) continue;
        var pre_name = parts[1];
        if( pre_name.match( /^file:/ ) )
          {
            root_url = pre_name;
          }
        else
          {
            // This is for MSIE which won't load footer anyway.
            var doc_loc_parts = doc_loc.match( /(.*)\/([^/]+)/ );
            root_url = doc_loc_parts[1] + "/" + pre_name + "/";
          }
        break;
      }
    }

  window.ece_site_root = root_url;
  // Sloppy, but seems to work.
  window.parent.ece_site_root = root_url;

  var doc_href = window.parent.document.location.href;
  window.ece_content_page_href = doc_href;
  window.parent.ece_content_page_href = doc_href;

}

function ece_official_inserts(curator_email)
{
  var rwindow = window.parent;
  if( curator_email )
    rwindow.document.ece_curator_email = curator_email;
  else
    rwindow.document.ece_curator_email = "mort";

  ece_banner_insert();
  ece_footer_prepare();
}

function ece_banner_insert()
{
  var body = document.getElementsByTagName("body").item(0);
  var ie_family = typeof window.addEventListener == 'undefined';
  // Note: IE does not recognize frameborder if set as an attribute.
  var iframe_tag = ie_family ? "<iframe frameborder=0>" : "iframe";
  var iframe = document.createElement(iframe_tag);

  iframe.setAttribute("src", "http://www.ece.lsu.edu/banner.html");
  iframe.setAttribute("width",ece_bf_width_px);
  iframe.setAttribute("height","120");
  iframe.setAttribute("scrolling","no");
  iframe.setAttribute("frameborder","0");

  body.insertBefore(iframe,body.firstChild);
}

function ece_footer_prepare()
{
  // This may have been called from the banner iframe,
  // so climb out if necessary.
  //
  var rwindow = window.parent;
  if( typeof(rwindow.ece_footer_prepared) != "undefined" ) return;
  rwindow.ece_footer_prepared = 1;

  if( typeof rwindow.addEventListener == 'undefined' )
    {
      rwindow.attachEvent("onload", ece_footer_insert);
      ie_body_width_set();
      rwindow.attachEvent("onresize", ie_body_width_set);
    }
  else
    {
      rwindow.addEventListener("load", ece_footer_insert, false);
    }
}

function ie_body_width_set()
{
  // Very kludgy code to set body width in IE.
  var rwindow = window.parent;
  var body = rwindow.document.getElementsByTagName("body").item(0);
  // Note: the leftMargin below is not really what I want.
  var right_margin = body.clientWidth - body.leftMargin - ece_bf_width_px - 50;
  if ( 0 )
    window.document.write("Hello window width is "
                   + body.clientWidth + "left margin "
                   + body.leftMargin  + "right margin " + right_margin);
  body.style.marginRight = right_margin + " px";
}

function ece_footer_insert()
{
  ece_links();
  var rwindow = window.parent;
  var document = rwindow.document;
  var body = document.getElementsByTagName("body").item(0);
  var ie_family = typeof window.addEventListener == 'undefined';
  // Note: IE does not recognize frameborder if set as an attribute.
  var iframe_tag = ie_family ? "<iframe frameborder=0>" : "iframe";
  var iframe = document.createElement(iframe_tag);

  iframe.setAttribute("frameborder","0");
  //  window.alert("src: " + rwindow.ece_site_root + "footer.html");
  iframe.setAttribute("src", rwindow.ece_site_root + "footer.html");
  iframe.setAttribute("width",ece_bf_width_px);

  // Height Attribute
  //
  // Overestimated. Bug workaround for IE 7.
  //
  iframe.setAttribute("height","200"); 
  iframe.setAttribute("scrolling","no");
  body.appendChild(iframe);

  if ( ie_family )
    {
      //  alert("Hello once again Body width is " + body.width);
      body.setAttribute("margin-left",100);
    }
}

function ece_links()
{
  var document = window.parent.document;
  var links = document.links;
  for(var i = 0; i < links.length; i++) {
    var link = links[i];
    var href = new String(link.href);
    if ( href.match(/^(file|mailto):/i) ) continue;
    if ( href.match(/^[a-zA-Z]+:[/]+[^/]+lsu\.edu/) ) continue;
    if ( href.match(/^[a-zA-Z]+:[/]+[^/]*mardigrasconference\.org/) ) continue;
    if ( href.match(/^[a-zA-Z]+:[/]+[^/]*foreverlsu\.org/) ) continue;
    var warning = document.createTextNode(" (Outside LSU)");
    link.appendChild(warning);
  }
}

function ece_curator_set()
{
  var curator_url_part;
  // Quick and Dirty
  var href = new String( window.ece_content_page_href );

  if( href.match( /\/grad2?\// ) )
    {
      curator_url_part = "mort";
    }
  else if( href.match( /\/ugrad\// ) )
    {
      curator_url_part = "scalzo";
    }
  else
    {
      curator_url_part = "koppel";
    }
  window.parent.ece_curator_link =
    "http://www.ece.lsu.edu/" + curator_url_part;
}