﻿// Google Analytics Helper
function track(a){if(typeof(tracker)=='object')tracker._trackPageview(a.href.match('^[^:]+://[^/]+(.*)$')[1])};


// JScript File
Event.observe(window, 'load', function() {
  ScrollInit();
  searchInit();
});

// ****************************************************************************
// MESSAGE SCROLLER
// ****************************************************************************
var container;
var messages;
var currentMessageIndex = -1;

function ScrollInit() {
  container = $('messages');
  messages = $$('.message', 'messages');
  
  if (messages.length > 0) {
    var height = container.getHeight();
    
    messages.each(function(e, i) {
      if (i != 0)
        e.hide();
      
      e.setStyle({
        position: "absolute",
        width: parseInt(e.up().getWidth()) -
               parseInt(e.getStyle('margin-left')) -
               parseInt(e.getStyle('margin-right')) + 'px'
      });
    });
    
    if (messages.length > 1) {
      // Move the first message only 1/2 of the animation upward, as
      // to avoid the major shift when first loading the page...
      currentMessageIndex++;
      var message = messages[currentMessageIndex];
      var height = message.up().getHeight();
      var moveDist = (height +
                      parseInt(message.getHeight()) +
                      parseInt(message.getStyle('margin-top')) +
                      parseInt(message.getStyle('margin-bottom'))) / -2.0;
      
      new Effect.Event(
        { duration: 4.0,
          afterFinish: function() {
            new Effect.Parallel(
              [ new Effect.MoveBy(message, moveDist, 0, { sync: true }), 
                new Effect.Opacity(message,  { sync: true, from: 1.0, to: 0.0 } ) ],
              { duration: 2.0,
                afterFinish: function() {
                  message.hide();
                }
              }
            );
            ScrollNextMessage();
          }
        }
      );
    }
  }
}

function ScrollNextMessage() {
  currentMessageIndex++;
  if (currentMessageIndex >= messages.length)
    currentMessageIndex = 0;
  
	ScrollToNextMessage();
}

function ScrollToNextMessage() {
  var message = messages[currentMessageIndex];
  var height = message.up().getHeight();
  var moveDist = (height +
                  parseInt(message.getHeight()) +
                  parseInt(message.getStyle('margin-top')) +
                  parseInt(message.getStyle('margin-bottom'))) / -2.0;
  
  message.setStyle({ top: height + 'px' });
  message.setOpacity(0.0);
  message.show();
  
  new Effect.Parallel(
    [ new Effect.MoveBy(message, moveDist, 0, { sync: true }), 
      new Effect.Opacity(message,  { sync: true, from: 0.0, to: 1.0 } ) ],
    { duration: 2.0,
      afterFinish: function() {
        new Effect.Event(
          { duration: 4.0,
            afterFinish: function() {
              new Effect.Parallel(
                [ new Effect.MoveBy(message, moveDist, 0, { sync: true }), 
                  new Effect.Opacity(message,  { sync: true, from: 1.0, to: 0.0 } ) ],
                { duration: 2.0,
                  afterFinish: function() {
                    message.hide();
                  }
                }
              );
              ScrollNextMessage();
            }
          }
        );
      }
    }
  );
}

// ****************************************************************************
// ALTERNATE REPLACABLE SEARCH TEXTBOX
// ****************************************************************************
function searchSubmit(e) {
  f = $('searchfor');
  alt = e.previous('input[type=hidden]').value
  if (alt == '1') {
    f.value = '';
  } else {
    f.value = e.previous('input[name=for]').value;
  }
  
  f.form.submit();
  return false;
}


function searchInit() {
  var altTextFields = $$('.altTextField');
  
  altTextFields.each(function (e) {
    new Insertion.After(e, '<input type="hidden" value="0" />');
    var status = e.next('input[type=hidden]');
    
    if (e.value == '') {
      status.value = '1';
      e.value = 'Search';
    }
    
    Event.observe(e, 'focus', function() {
      if (status.value == '1') {
        e.value = '';
      }
      e.activate();
    });
    
    Event.observe(e, 'blur', function() {
      if (e.value == '') {
        status.value = '1';
        e.value = 'Search';
      } else {
        status.value = '0';
      }
    });
    
    Event.observe(e, 'keypress', function() {
      if (e.value == '') {
        status.value = '1';
      } else {
        status.value = '0';
      }
    });
  });
}
