//++
//
//  JavaScript:
//	calendar.js
//
//  Provides:
//	lcl_get_element
//	lcl_get_month
//	lcl_month_highlight
//	lcl_month_normal
//	lcl_month_pos
//	lcl_notice_display
//	lcl_notice_initialize
//	lcl_notice_popup
//	lcl_notice_remove
//
//  Notes:
//
//  History:
//	2010-07-30 by Simon L Jackson
//	    Initial version
//
//--

romber_include_once ( '/common/romber_form.js' );

gbl_months = [
  'jan', 'feb', 'mar', 'apr', 'may', 'jun',
  'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ];

//
//  Function:
//	lcl_get_element
//
//  Purpose:
//	Find the element associated with an event
//
//  Notes:
//	Tries to handle different browsers.
//	Prototype's Event.element didn't work when there is
//	no actual event so we have to have our own function.
//

function lcl_get_element ( arg_event ) {

  var lcl_element;

// In IE we may not be passed an event, so use the global event
  if ( ! arg_event )
    var arg_event = window.event;

// Try various properties to determine the element
  if ( arg_event.currentTarget )
    lcl_element = arg_event.currentTarget;
  else if ( arg_event.target )
    lcl_element = arg_event.target;
  else if ( arg_event.srcElement )
    lcl_element = arg_event.srcElement;

// Handle Safari issue
  if ( lcl_element.nodeType == 3 )
    lcl_element = lcl_element.parentNode;

// Very nasty kludge to convert trigger element (target)
// to handler element (currentTarget)
  if ( lcl_element.tagName == 'B'
   || lcl_element.tagName == 'SPAN'
   || lcl_element.tagName == 'STRONG' ) {
    lcl_element = lcl_element.parentNode;
  }

  return ( lcl_element );

}


//
//  Function:
//	lcl_get_month
//
//  Purpose:
//	Determines the month associated with an element
//
//  Notes:
//	This function relines on the element having or being contained within another
//	element which has an ID attribute ending in a 3 letter month code.
//

function lcl_get_month ( arg_element ) {

  var lcl_element = arg_element;

  while ( lcl_element.id == '' ) {
    lcl_element = lcl_element.parentNode;
  }
////alert (lcl_element.id.substr ( lcl_element.id.length - 3 ));

  return ( lcl_element.id.substr ( lcl_element.id.length - 3 ));

}


//
//  Functions:
//	lcl_month_highlight
//	lcl_month_normal
//
//  Purpose:
//	Add or remove highlighting for a month heading
//
//  Notes:
//	Force background images to be preloaded
//

var lcl_image_off = new Image;
var lcl_image_on = new Image;
lcl_image_off.src = '/images/menus/month_rollover_off.jpg';
lcl_image_on.src = '/images/menus/month_rollover_on.jpg';

function lcl_month_highlight ( arg_anchor ) {

  arg_anchor.style.backgroundRepeat = 'no-repeat';
  arg_anchor.style.backgroundPosition = lcl_month_pos ( lcl_get_month ( arg_anchor ));
  arg_anchor.style.backgroundImage = "url('/images/menus/month_rollover_on.jpg')";

}

function lcl_month_normal ( arg_anchor ) {

  var lcl_month = arg_anchor.parentNode.id.substr(14);
  arg_anchor.style.backgroundRepeat = 'no-repeat';
  arg_anchor.style.backgroundPosition = lcl_month_pos ( lcl_month );
  arg_anchor.style.backgroundImage = "url('/images/menus/month_rollover_off.jpg')";

}


//
//  Function
//	lcl_month_pos
//
//  Purpsoe:
//	Return the image starting position pair for a given month
//

function lcl_month_pos ( arg_month ) {

  for ( var lcl_month = 0 ; lcl_month < 12 ; lcl_month++ )
    if ( gbl_months[lcl_month] == arg_month )
      break;

  return ( -332 * lcl_month + 'px 0px' );

}


//
//  Function:
//	lcl_notice_initialize
//
//  Purpose:
//	Initialize the month headings with the appropriate event handlers
//

function lcl_notice_initialize () {

  var lcl_anchor;
  var lcl_list_element;
  var lcl_id;

  for ( var lcl_index = 0 ; lcl_index < 12 ; lcl_index++ ) {
    lcl_month = gbl_months[lcl_index];
    lcl_id = 'menu_calendar_' + lcl_month;
    lcl_list_element = $(lcl_id);
    lcl_anchor = lcl_list_element.firstChild;
    lcl_anchor.onclick = function ( arg_event ) {
      var lcl_anchor = lcl_get_element ( arg_event );
      lcl_notice_popup ( lcl_anchor );
    }
    lcl_anchor.onmouseout = function ( arg_event ) {
      var lcl_anchor = lcl_get_element ( arg_event );
      lcl_month_normal ( lcl_anchor );
    }
    lcl_anchor.onmouseover = function ( arg_event ) {
      var lcl_anchor = lcl_get_element ( arg_event );
      lcl_month_highlight ( lcl_anchor );
    }
    lcl_month_normal( lcl_anchor );
  }

}

romber_onload_add ( lcl_notice_initialize );


//
//  Functions:
//	lcl_notice_display
//	lcl_notice_popup
//
//  Purpose:
//	Display the details for a specific month
//

function lcl_notice_display ( arg_anchor ) {

  var lcl_month = lcl_get_month ( arg_anchor );

  romber_get_window_details ();

  arg_anchor.onmouseout = function () {};
  lcl_month_highlight ( arg_anchor );

  lcl_noticeouter = $('noticeouter');
  lcl_noticeouter.style.height = ( gbl_window_height - 10 ) + 'px';
  lcl_noticeouter.style.width = ( gbl_window_width - 10 ) + 'px';
  lcl_noticeouter.style.top = document.body.style.top + 5 + 'px';
  lcl_noticeouter.style.left = document.body.style.left + 5 +'px';
  lcl_noticeouter.style.display = 'inline';

  lcl_noticeinner = $('noticeinner_'+lcl_month);
  lcl_noticeinner.style.top = (( gbl_window_height - 402 ) / 2 ) + 'px';
  lcl_noticeinner.style.left = (( gbl_window_width - 342 ) / 2 ) + 'px';
  lcl_noticeinner.style.display = 'inline';

}

function lcl_notice_popup ( arg_anchor  ) {

  window.onresize = function () {
    lcl_notice_display ( arg_anchor );
  }

  lcl_notice_display ( arg_anchor );

  return ( true );

}


//
//  Function:
//	lcl_notice_remove
//
//  Purpose:
//	Remove the popup display of some months details
//

function lcl_notice_remove ( arg_event ) {

  var lcl_div = lcl_get_element ( arg_event );
  var lcl_month = lcl_get_month ( lcl_div );

  $('noticeinner_'+lcl_month).style.display = 'none';

  $('noticeouter').style.display = 'none';

  var lcl_anchor = $('menu_calendar_'+lcl_month).firstChild;
  lcl_anchor.onmouseout = function ( arg_event ) {
    var lcl_anchor = lcl_get_element ( arg_event );
    lcl_month_normal ( lcl_anchor );
  }

  lcl_month_normal ( lcl_anchor );

}

