
document.observe("dom:loaded", loadFirstNotice );
document.observe("dom:loaded", attachNoticeEvents );

// Global Variables
var ajaxBusyNotice = null;
var ajaxResponse = null;
var currentNoticeId = null;
var cNotice = 0;
var nNotices = 2;
var noticeArray = new Array();
var noticeDirection = null;


/**
 * Load First Notice
 */
function loadFirstNotice()
{
  if( $('noticeWindow') && $('fNoticeImg') )
  {
    $('noticeWindow').setStyle( {opacity:0} );
    
    var newImage = new Image;
    newImage.onload = function()
    {
      setTimeout( "new Effect.Fade( $('noticeWindow'), { from:0, to:1, duration:0.4} );", 200);
      loadNextNotice();
    }
    newImage.src=$('fNoticeImg').src;
  }
}


/**
 * Attach Notice Events
 */
function attachNoticeEvents()
{
  if( $('noticeWindow') && $('viewNotices') )
  {
    $('viewNotices').setStyle( {display:'none'} );
    
    var newI = new Element( 'img', { 'id':'noticesLeft', 'alt':'', 'src':'/images/misc/slideLeftOff.gif' } );
    $('viewNotices').insert({ 'after' : newI} );
    $('noticesLeft').setStyle({ marginRight:'2px'} );
      
    var newI = new Element( 'img', { 'id':'noticesRight', 'alt':'', 'src':'/images/misc/slideRightOn.gif' } );
    $('noticesLeft').insert({ 'after' : newI} );

    Event.observe( $('noticesLeft'), 'mouseover',  noticesLeftOver,  false );
    Event.observe( $('noticesLeft'), 'mouseout',   noticesLeftOut,   false );
    Event.observe( $('noticesLeft'), 'click',      noticesLeftClick, false );
    
    Event.observe( $('noticesRight'), 'mouseover', noticesRightOver,  false );
    Event.observe( $('noticesRight'), 'mouseout',  noticesRightOut,   false );
    Event.observe( $('noticesRight'), 'click',     noticesRightClick, false );
        
    var currentNotice = $('noticeInner').childNodes[0];
    $(currentNotice).setStyle( {float:'left', width:'168px' } );
    currentNoticeId = currentNotice.id.replace('notice_', '');
  }
}


/**
 * Notice Left Navigator - Mouse Over
 */
function noticesLeftOver(e)
{
  $('noticesLeft').setStyle( {cursor:'pointer'} );
  if( cNotice != 0 )
  {
    $('noticesLeft').src = '/images/misc/slideLeftOver.gif';
  }
}


/**
 * Notice Left Navigator - Mouse Out
 */
function noticesLeftOut(e)
{
  if( cNotice != 0 )
  {
    $('noticesLeft').src = '/images/misc/slideLeftOn.gif';
  }
  else
  {
    $('noticesLeft').src = '/images/misc/slideLeftOff.gif';
  }
}


/**
 * Notice Left Navigator - Click
 */
function noticesLeftClick(e)
{
  var previousNotice = cNotice-1;
  var path = '/notices/updateSlide/id/'+previousNotice+'.html';
  $('noticesLeft').setStyle( {cursor:'pointer'} );
  
  if( cNotice != 0 && ajaxBusyNotice!=1 )
  {
    $('noticesLeft').src = '/images/misc/slideLeftOver.gif';
    ajaxBusyNotice=1;
    if( noticeArray[previousNotice] )
    {
      eval( noticeArray[previousNotice] );
    }
    else
    {
      new Ajax.Request(path,
      {
        method:'get',
        onComplete: function(transport)
        {
          var ajaxResponse = transport.responseText || null;
          if( ajaxResponse ) 
          {
            eval( ajaxResponse );
            noticeArray[previousNotice] = ajaxResponse;
          }
        }
      });
    }
  }
  
  Event.stop(e); 
}


/**
 * Notice Right Navigator - Mouse Over
 */
function noticesRightOver(e)
{
  $('noticesRight').setStyle( {cursor:'pointer'} );
  if( cNotice != nNotices-1 )
  {
    $('noticesRight').src = '/images/misc/slideRightOver.gif';
  }
}


/**
 * Notice Right Navigator - Mouse Out
 */
function noticesRightOut(e)
{
  if( cNotice != nNotices-1 )
  {
    $('noticesRight').src = '/images/misc/slideRightOn.gif';
  }
  else
  {
    $('noticesRight').src = '/images/misc/slideRightOff.gif';
  }
}


/**
 * Notice Right Navigator - Click
 */
function noticesRightClick(e)
{
  var nextNotice = cNotice+1;
  var path = '/notices/updateSlide/id/'+nextNotice+'.html';
  $('noticesRight').setStyle( {cursor:'pointer'} );

  if( cNotice != nNotices-1 && ajaxBusyNotice!=1 )
  {
    $('noticesRight').src = '/images/misc/slideRightOver.gif';
    ajaxBusyNotice=1;
    if( noticeArray[nextNotice] )
    {
      eval( noticeArray[nextNotice] );
    }
    else
    {
      new Ajax.Request(path,
      {
        method:'get',
        onComplete: function(transport)
        {
          var ajaxResponse = transport.responseText || null;          
          if( transport.responseText) 
          {
            eval( ajaxResponse );
            noticeArray[nextNotice] = ajaxResponse;
          }
        }
      });
    }
  }
  else
  {
    $('noticesRight').src = '/images/misc/slideRightOff.gif';
  }
  
  Event.stop(e); 
}




function loadNewNotice( noticeNo, notices, id, path, noticeTitle, noticeDate )
{
  var newImage = new Image;  
  newImage.src='/images/headings/sideNotices/'+id+'/'+path+'.gif';
  
    noticeDirection = cNotice<noticeNo ? 1 : 0;
    cNotice=noticeNo;
    nNotices=notices;
    
    if( BrowserDetect.browser=='Explorer' && BrowserDetect.version==6)
    {
      $('notice_'+currentNoticeId).remove();
      $('noticeInner').innerHTML = '<div id="notice_'+id+'"></div>';
      $('noticeInner').setStyle( { opacity:0 } );
      $('notice_'+id).innerHTML = '<div class="noticeHeader">' 
    	  + (noticeDate ? '<div class="noticeDate">' + noticeDate + '</div>' : '')
    	  + (noticeTitle ? '<h3 class="noticeTitle">' + noticeTitle + '</h3>' : '')
    	  + "</div>"
//    	  + '<img src="'+newImage.src+'" />'
    	  + '<div class="more"><a href="/notices/'+path+'.html">More</a></div>';
      
      currentNoticeId = id;
      new Effect.Fade( $('noticeInner'), { from:0, to:1, duration:0.4} );
      setTimeout( "ajaxBusyNotice=0;updateNoticeSlideButtons();loadNextNotice();", 400 );
    }
    else
    {
      if( noticeDirection )
      {
        if( !$('notice_'+id) )
        {
          $('noticeInner').setStyle( {width:$('noticeInner').offsetWidth+168+'px'} );
          var newI = new Element( 'div', { 'id':'notice_'+id } ).update( 
        		  '<div class="noticeHeader">'
        		  + (noticeDate ? '<div class="noticeDate">' + noticeDate + '</div>' : '')
            	  + (noticeTitle ? '<h3 class="noticeTitle">' + noticeTitle + '</h3>' : '')
        		  + "</div>"
//            	  + '<img src="'+newImage.src+'" />' 
        		  + '<div class="more"><a href="/notices/'+path+'.html">More</a></div>' 
		  );
          $('notice_'+currentNoticeId).insert ({ 'after' : newI } );
          $('notice_'+id).setStyle( {float:'left', width:'168px'} );
        }
        
        currentNoticeId = id;      
        new Effect.Move( $('noticeInner'), { x:-168, y: 0, mode: 'relative', duration:0.4 } );
        setTimeout( "ajaxBusyNotice=0;updateNoticeSlideButtons();loadNextNotice();", 400 );
      }
      else
      {      
        currentArticleId = id;      
        new Effect.Move( $('noticeInner'), { x:168, y: 0, mode: 'relative', duration:0.4 } );
        setTimeout( "ajaxBusyNotice=0;updateNoticeSlideButtons();", 400 );
      }
    }
}



function updateNoticeSlideButtons()
{
  if( noticeDirection )
  {
    if( cNotice!=0 )
    {
      $('noticesLeft').src = '/images/misc/slideLeftOn.gif';
    }
    else
    {
      $('noticesLeft').src = '/images/misc/slideLeftOff.gif';
    }
  }
  else
  {
    if( nNotices!=cNotice+1 )
    {
      $('noticesRight').src = '/images/misc/slideRightOn.gif';
    }
    else
    {
      $('noticesRight').src = '/images/misc/slideRightOff.gif';
    }
  }
}

/**
 * Load the Next Notice
 */
function loadNextNotice()
{
  var nextNotice = cNotice+1;
  var path = '/notices/updateSlide/id/'+nextNotice+'.html';
  if( !ajaxBusyNotice && nextNotice!=nNotices-1 && !noticeArray[nextNotice] )
  {
    ajaxBusyNotice=1;
    new Ajax.Request(path,
    {
      method:'get',
      onComplete: function(transport)
      {
        var ajaxResponse = transport.responseText || null;
        if( ajaxResponse ) noticeArray[nextNotice] = ajaxResponse;
        ajaxBusyNotice=0;
      }
    });
  }
}

