var Util;

if (Util==null)
  Util = {};

Util.days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

Util.classTimes = {};

Util.tableStart = '<table class="pretty_table"><thead><tr><th scope="col" colspan="3">When</th><th scope="col">Information</th></tr></thead><tbody>';
Util.tableEnd   = '</tbody></table>';

Util.toTime = function(t) {
  var hrs=Math.floor(t/60), mins=Math.floor(t%60);
  return ((hrs<10) ? ('0'+hrs) : (''+hrs)) + ':' + ((mins<10) ? ('0'+mins) : (''+mins));
}

Util.classDesc = function(data) {
  var desc;
  if (data.when!=null) {
    desc = '<tr><td colspan="3">'+data.when+'</td><td>'+data.info+'</td></tr>';
  }
  else {
    
    desc = '<tr><td>'+Util.days[data.day]+'</td><td>'+Util.toTime(data.start)+'</td><td>'+Util.toTime(data.end)+'</td><td>'+data.info+'</td></tr>';
  }

  return desc;
}

Util.describeTimes = function(ID) {
  var classes = Util.classTimes[ID];
  if (classes==null)
    return true;

  // Build description of available classes
  classes.sort(
    function (a,b) {
      if (a.when==null && b.when!=null)
        return -1;
      else if (a.when!=null && b.when==null)
        return 1;
      return a.day-b.day;
    }
  );
  var h='';

  for (var i=0;i<classes.length;i++)
    h += Util.classDesc(classes[i]);
  return Util.tableStart + h + Util.tableEnd;
}

Util.openTimes = function(ID) {

  // Show data in-line if available
  var block = document.getElementById('popup-win');
  var shade = document.getElementById('popup-shield');
  if (block==null) //  || shade==null)
    return true;

  // Need to find the advert that triggered the event
  var ad = document.getElementById('ad_'+ID);
  if (ad==null)
    return true;

  // Now - get information about this advert.
  var times = Util.describeTimes(ID);

  var bdy = document.getElementById('popup-body');
  if (times!=null && bdy!=null)
    bdy.innerHTML = times;

	var posx = 0;
	var posy = 0;

	if (ad.pageX!=null || ad.pageY!=null) {
		posx = ad.pageX;
		posy = ad.pageY;
	}
	else if (ad.clientX || ad.clientY) {
		posx = ad.clientX + document.body.scrollLeft;
		posy = ad.clientY + document.body.scrollTop;
	}
	else if (ad.offsetTop!=null || ad.offsetLeft!=null) {
		posx = ad.offsetLeft;
		posy = ad.offsetTop;
	}

  posy += 25;
  posx += 20;

  block.style.top  = ''+posy+'px';
  block.style.left = ''+posx+'px';

  if (shade!=null)
      shade.style.display='block';

  return false;
}

Util.closePopup = function() {
  var block = document.getElementById('popup-win');
  if (block!=null)
    block.style.top = '-1000px';

  var shade = document.getElementById('popup-shield');
  if (shade!=null)
    shade.style.display='none';
  return false;
}

Util.openEnhanced= function(url) {
  window.open(url,'enhanced').focus();
  return false;
}

Util.moreCache={};
Util.openMore = function(url, ID) {

  var block = document.getElementById('popup-win');
  if (block==null)
    return true;

  var blk = document.getElementById('ad_'+ID);
  if (blk==null)
    return true;
  var bdy = document.getElementById('popup-body');
  if (bdy!=null) {
    var data = Util.moreCache[ID];
    if (data==null) {
      // Get the more data
      bdy.innerHTML = '<div id="loading"><img src="/images/loading.gif" alt="Loading" title="loading document details"><br>Loading</div>';
      var r = HTTP.newRequest();
      r.docID = ID;
      r.get(url, Util.docComplete, {});
    }
    else
      bdy.innerHTML = data;

    var posx = 0;
    var posy = 0;

    if (blk.pageX!=null || blk.pageY!=null) {
      posx = blk.pageX;
      posy = blk.pageY;
    }
    else if (blk.clientX || blk.clientY) {
      posx = blk.clientX + document.body.scrollLeft;
      posy = blk.clientY + document.body.scrollTop;
    }
    else if (blk.offsetTop!=null || blk.offsetLeft!=null) {
      posx = blk.offsetLeft;
      posy = blk.offsetTop;
    }

    posy += 25;
    posx += 20;

    block.style.top  = ''+posy+'px';
    block.style.left = ''+posx+'px';
  }
  return false;
}

Util.docComplete = function(t, doc) {
  // Select the 'body' part of the data, insert into the target URL
  var idx_s = doc.search(/\<body\>/i), idx_e, res;
  if (idx_s>=0)
    idx_e = doc.search(/\<\/body\>/i);

  if (idx_s>=0 && idx_e>idx_s)
    res = doc.substring(idx_s+6,idx_e);

  if (res==null)
    res = '<p><i>No further information available</i></p>';

  // Times available?
  var times = Util.describeTimes(t.docID);
  
  if (times!=null) {
    times = '<div style="margin:0 auto 3em auto">'+times+'</div>';
    res = res.replace(/\<span\ id\=\"INSTIMES\"\>\<\/span\>/,times);
  }

  var bdy = document.getElementById('popup-body');
  if (bdy!=null)
    Util.moreCache[t.docID] = bdy.innerHTML = res;
}


/**** GENERAL SEARCH ROUTINES ****/
function showAdvanced(yes) {
  var shd  = document.getElementById('search-shade');
  var adv  = document.getElementById('search-adv');

  if (shd!=null && adv!=null) {
    if (yes) {
      shd.style.display = 'block';
      adv.style.top  = '150px';
      adv.style.left = '200px';
    }
    else {
      shd.style.display = 'none';
      adv.style.top  = '-5000px';
      adv.style.left = 0;
    }
  }
}
function testShowMonthAndDays() {
  var el = document.getElementById('srch-type');
  var mnth = document.getElementById('month_sel');
  var days = document.getElementById('day_sel');
  if (el!=null && mnth!=null && days!=null) {
    mnth.style.display=(el.value=='events'?'inline':'none');
    days.style.display=(el.value=='periodic'?'inline':'none');
  }
}
function changeCats() {
  var catEls = document.getElementsByName('cat');
  var dstr = [];
  for (var i=0;i<catEls.length;i++) {
    if (catEls[i].checked)
      dstr.push(catEls[i].title);
  }
  var grp = document.getElementById('grp_now');
  if (grp!=null)
    dstr.push(grp.title);

  var desc = document.getElementById('search-catsel');
  var hint = document.getElementById('advopt');

  if (desc!=null && hint!=null) {
    if (dstr.length>0) {
      desc.innerHTML = "Search categories : "+dstr.join(', ');
      hint.innerHTML = "refine your search";
    }
    else
      hint.innerHTML = "advanced options";
  }
  // Show months and days?
  testShowMonthAndDays();
}
function clearAllCats() {
  var catEls = document.getElementsByName('cat');
  var dstr = [];
  for (var i=0;i<catEls.length;i++)
    catEls[i].checked = false;

  var desc = document.getElementById('search-catsel');
  var hint = document.getElementById('advopt');
  if (desc!=null && hint!=null) {
    desc.innerHTML = "";
    hint.innerHTML = "advanced options";
  }
}

