{# Copyright The IETF Trust 2007, All Rights Reserved #}
{% load humanize %}


<html> <head>
<script type="text/javascript" src='/js/agenda2.js'></script>
<script type="text/javascript">
var items = new Array();
{% autoescape off %}
{% for slot in timeslots %}{% for session in slot.sessions %}
items.push({day:{{slot.day_id}}, time:"{{slot.time_desc}}", time_id:{{slot.time_id}}, name:"{{session.acronym_name}}", wg:"{{session.acronym}}", area:"{{session.area}}", room:"{{session.room_id.room_name}}", dayname:"{{ slot.meeting_date|date:"l"|upper }}, {{ slot.meeting_date|date:"F j, Y" }}"{% if session.agenda_file %}, agenda:"/meeting/{{ meeting.num }}/agenda/{{ session.acronym }}-agenda/"{% endif %}});{% endfor %}{% endfor %}
{% endautoescape %}


var fg = {
  'app': "#008",
  'gen': "#080",
  'int': "#088",
  'ops': "#800",
  'rai': "#808",
  'rtg': "#880",
  'sec': "#488",
  'tsv': "#484",
  'irtf': "#448"
};

var bg = {
  'app': "#eef",
  'gen': "#efe",
  'int': "#eff",
  'ops': "#fee",
  'rai': "#fef",
  'rtg': "#ffe",
  'sec': "#dff",
  'tsv': "#dfd",
  'irtf': "#ddf"
};

var day = [
  'Saturday',
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday'
];

var divlist = [];

var lastfrag;
var lastheight;
var lastwidth;

var padding = 2;
var border = 1;

setInterval("animate()",50);

function draw_calendar()
{
  window.setTimeout("draw_calendar()",1000);

  var width = document.body.clientWidth;
  var height = document.body.clientHeight;

  if (lastheight == height && 
      lastwidth == width &&
      lastfrag == window.location.hash)
  {
    return;
  }

  var i;
  var text="";
  var start_day = items[0].day;
  var end_day = start_day;
  var sessions = Array();
  var used = Array();

  var day_start = 0;
  var day_end = 0;
  var include = Array();

  var frag = window.location.hash.replace("#",'').split(',');
  for (i = 0; i < frag.length; i++)
  {
    include[(frag[i]+"").toLowerCase()] = true;
  }
  include[''] = true;
  include['1plenary'] = true;
  include['2plenary'] = true;

  /* Find our boundaries */
  for (i = 0; i < items.length; i++)
  {
    if (include[(items[i].wg).toLowerCase()] 
        || include[(items[i].area).toLowerCase()])
    {
      var start_time = parseInt(items[i].time.substr(0,2),10) * 60 +
                       parseInt(items[i].time.substr(2,2),10);
      var end_time   = parseInt(items[i].time.substr(5,2),10) * 60 +
                       parseInt(items[i].time.substr(7,2),10);

      if (!day_start || start_time < day_start) { day_start = start_time; }
      if (end_time > day_end) { day_end = end_time; }

      if (items[i].day < start_day) { start_day = items[i].day; }
      if (items[i].day > end_day) { end_day = items[i].day; }
      if (sessions[items[i].time_id])
      {
        sessions[items[i].time_id]++;
      }
      else
      {
        sessions[items[i].time_id] = 1;
        used[items[i].time_id] = 0;
      }
    }
  }

  var header_height = height * 0.05 ;

  var num_days = end_day - start_day + 1;
  var num_minutes = day_end - day_start;
  var day_width = width / num_days;
  var minute_height = (height - header_height)/num_minutes;
  var body = document.body;
  while (body.childNodes.length) { body.removeChild(body.childNodes[0]); }

  var j = start_day;
  for (i = 0; i < num_days; i++)
  {
    //-----------------------------------------------------------------
    // Draw weekday name
    //-----------------------------------------------------------------
    var e = document.createElement("div");

    e.style.border="solid";
    e.style.borderWidth=border;

    e.style.background="#2647a0";
    e.style.color="#fff";
    e.style.borderColor="#000 #fff";
    e.style.borderColor="#2647a0 #2647a0 #000 #2647a0";

    e.style.display="block";
    e.style.overflow="hidden";
    e.style.position="absolute";

    e.style.top=0;
    e.style.left = i*day_width;
    e.style.width=day_width - 2 * (padding + border);
    e.style.height=header_height;

    e.style.margin=0;
    e.style.padding=padding;
    e.style.fontFamily="sans-serif";
    e.style.fontSize=header_height * 0.6;

    e.style.textAlign="center";

    var div = document.createElement("div");
    div.appendChild(document.createTextNode(day[j+1]));
    j++;
    e.appendChild(div);
    body.appendChild(e);

    //-----------------------------------------------------------------
    // Draw weekday column border
    //-----------------------------------------------------------------
    e = document.createElement("div");

    e.style.border="solid";
    e.style.borderWidth=border;

    e.style.background="#fff";
    e.style.color="#000";
    e.style.borderColor="#fff #000";

    e.style.display="block";
    e.style.overflow="hidden";
    e.style.position="absolute";

    e.style.top=header_height;
    e.style.left=i*day_width;
    e.style.width=day_width - 2 * (padding + border);
    e.style.height=height - 
                   2 * (padding + border) - header_height;

    e.style.margin=0;
    e.style.padding=padding;

    body.appendChild(e);
  }

  //-----------------------------------------------------------------
  // Draw a block for each meeting
  //-----------------------------------------------------------------
  var resize_func = function(div,t,l,w,h,to_fit)
    { return function(){resize(div,t,l,w,h,to_fit);} }
  var maximize_func = function (e) { return function(){maximize(e);}}

  for (i = 0; i < items.length; i++)
  {
    if (include[(items[i].wg).toLowerCase()] 
        || include[(items[i].area).toLowerCase()])
    {
      var start_time = parseInt(items[i].time.substr(0,2),10) * 60 +
                       parseInt(items[i].time.substr(2,2),10);
      var end_time   = parseInt(items[i].time.substr(5,2),10) * 60 +
                       parseInt(items[i].time.substr(7,2),10);

      var sess_width = day_width / sessions[items[i].time_id];
      var sess_height = ((end_time - start_time) * minute_height) 
                        - 2 * (padding + border);
      var day_left = ((items[i].day - start_day) * day_width);
      var sess_left = day_left + sess_width * used[items[i].time_id];
      var sess_top = ((start_time - day_start) * minute_height) + header_height;

      sess_width = sess_width - 2 * (padding + border);

      used[items[i].time_id]++;

      var e = document.createElement("div");
      e.style.border="solid";
      e.style.borderWidth=border;

      if (fg[items[i].area])
      {
        e.style.background=bg[items[i].area];
        e.style.color=fg[items[i].area];
        e.style.borderColor=fg[items[i].area];
      }
      else
      {
        e.style.background="#e0e0e0";
        e.style.color="#000000";
        e.style.borderColor="#000000";
      }

      e.style.display="block";
      e.style.overflow="hidden";
      e.style.position="absolute";
      e.style.top=sess_top;
      e.style.left=sess_left;
      e.style.width=sess_width;
      e.style.height=sess_height;
      e.style.margin=0;
      e.style.padding=padding;
      e.style.fontFamily="sans-serif";
      e.style.fontSize="8pt";
      e.id=i;

      e.onmouseover=resize_func(e,sess_top,day_left,
                                day_width-2*(padding+border),
                                sess_height, true);

      e.onmouseout=resize_func(e,sess_top,sess_left,sess_width,sess_height,false);

      if (items[i].agenda)
      {
        e.onclick=maximize_func(e);
        e.style.cursor="pointer";
      }

      var div = document.createElement("div");
      div.appendChild(document.createTextNode(items[i].time));
      div.appendChild(document.createElement("br"));

      var label = items[i].name;
      if (fg[items[i].area])
      {
        label = label + " (" + items[i].wg + ")";
      }
      var bold = document.createElement("span");
      bold.appendChild(document.createTextNode(label));
      bold.style.fontWeight="bold";
      div.appendChild(bold);

      div.appendChild(document.createElement("br"));
      var italics = document.createElement("span");
      italics.appendChild(document.createTextNode(items[i].room));
      italics.style.fontStyle="oblique";
      div.appendChild(italics);

      e.appendChild(div);

      body.appendChild(e);
    }
  }

  lastheight = height;
  lastwidth = width;
  lastfrag = window.location.hash;
}

function resize(div,t2,l2,w2,h2,to_fit)
{
  // Move the element to the front
  document.body.removeChild(div);
  document.body.appendChild(div);

  div.t2 = t2;
  div.l2 = l2;
  div.w2 = w2;
  div.h2 = h2;
  div.to_fit = to_fit;
  div.percent = 0;
  divlist.push(div);
}

function animate()
{
  var i;
  for (i = divlist.length - 1; i >= 0; i--)
  {
    var div = divlist[i];
    if (div.percent < 100)
    {
      div.percent += 5;
      var t1 = parseFloat(div.style.top.replace("px",""));
      var l1 = parseFloat(div.style.left.replace("px",""));
      var w1 = parseFloat(div.style.width.replace("px",""));
      var h1 = parseFloat(div.style.height.replace("px",""));

      div.style.top = wavg(t1,div.t2,div.percent);
      div.style.left = wavg(l1,div.l2,div.percent);
      div.style.width = wavg(w1,div.w2,div.percent);
      div.style.height = wavg(h1,div.h2,div.percent);

      if (t1 == div.t2 && l1 == div.l2 &&
          w1 == div.w2 && h1 == div.h2) { div.percent = 100; }

    }
    else
    {
      if (div.to_fit)
      {
        var tmp = div.style.height;
        div.style.removeProperty("height");
        if (div.h2 < div.clientHeight)
        {
          div.h2 = div.clientHeight;
          div.percent = 0;
        }
        else
        {
          divlist.remove(i);
          if (div.callback)
          {
            var tmp = div.callback;
            div.callback = undefined;
            tmp();
          }
        }
        div.style.height = tmp;
      }
      else
      {
          divlist.remove(i);
          if (div.callback)
          {
            var tmp = div.callback;
            div.callback = undefined;
            tmp();
          }
      }
    }
  }

}

function finish_maximize(e)
{
  if (!items[e.id].agenda)
  {
    return;
  }

  e.insertBefore(document.createElement("br"),e.firstChild);

  var minimize_func = function (e) { return function(){minimize(e);} }
  var img = document.createElement("img");
  img.src = "/images/close.png";
  img.style.cssFloat="right";
  img.onclick = minimize_func(e);
  img.style.cursor="pointer";
  e.insertBefore(img,e.firstChild);

  var h = document.createElement("span");
  h.appendChild(document.createTextNode(items[e.id].dayname));
  h.style.fontWeight="bold";
  e.insertBefore(h,e.firstChild);
  e.style.fontSize="10pt";

  var tmp = e.style.height;
  e.style.removeProperty("height");
  var used_height = e.clientHeight;
  e.style.height = tmp;

  var frame = document.createElement("iframe");
  frame.setAttribute("src",items[e.id].agenda);

  frame.style.position = "absolute";
  frame.style.left = 8;
  frame.style.width = e.clientWidth - 16 - 2 * (padding + border);
  frame.style.top = used_height + 8;
  frame.style.height = e.clientHeight - used_height - 16 - 2*(padding+border);

  frame.style.background = "#fff";
  frame.style.overflow="auto";
  frame.id="agenda";

  frame.style.border = e.style.border;
  frame.style.borderWidth = border;
  frame.style.padding = padding;
  frame.style.borderColor = e.style.borderColor;
  e.appendChild(frame);
}

function finish_minimize(e)
{
  e.onmouseover = e.oldmouseover;
  e.onmouseout = e.oldmouseout;
  e.oldmouseover = undefined;
  e.oldmouseout = undefined;
  e.style.cursor="pointer";
}

function maximize(e)
{
  if (e.onmouseover)
  {
    e.oldmouseover = e.onmouseover;
    e.oldmouseout = e.onmouseout;
    e.onmouseover = undefined;
    e.onmouseout = undefined;
    e.style.cursor="auto";

    var callback_func = function (e) { return function(){finish_maximize(e);}}
    e.callback = callback_func(e);

    resize(e,0,0,
           document.body.clientWidth-2*(padding + border),
           document.body.clientHeight-2*(padding + border));
  }
}

function minimize(e)
{
   var agenda = document.getElementById("agenda");
   if (agenda)
   {
     e.removeChild(agenda);
   }

   var callback_func = function (e) { return function(){finish_minimize(e);}}
   e.callback = callback_func(e);
   e.oldmouseout();

   e.removeChild(e.firstChild);
   e.removeChild(e.firstChild);
   e.removeChild(e.firstChild);
   e.style.fontSize="8pt";
}

function wavg(x1,x2,percent)
{
  if (percent == 100) { return x2; }
  var res = x2 * (percent / 100) + x1 * ((100 - percent) / 100);
  return res;
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

</script>
</head>
<body onload="draw_calendar()" onresize="draw_calendar()" id="body">
<div id="cal"><span>Error loading calendar</span></div>
</body></html>
