// variables have to be defined on a global level to be used
var menu_arrow_open = new Image(14,14);
var menu_arrow_closed = new Image(14,14);

menu_arrow_open.src   = '/images/formatting/menu_arrow_open.gif';
menu_arrow_closed.src = '/images/formatting/menu_arrow_closed.gif';

// advertisement javascript
var QuoteShow = Class.create();
QuoteShow.prototype = {
  initialize: function(element, options) {
    this.element = $(element);
    this.options = Object.extend({className: 'advertisement', duration: 5}, options);
    this.quotes = document.getElementsByClassName(this.options.className, this.element);

    this.prepareQuotes();
    this.registerCallback();
  },
  prepareQuotes: function() {
    this.currentQuote = this.quotes.first();
    this.element.style.height = this.quotes.max(function(quote) {
      var visible = Element.visible(quote), height;
      Element.setStyle(quote, {position: 'absolute', width: '384px', left: '290px', top: '0px'});
      if (!visible) Element.show(quote);
      height = Element.getHeight(quote);
      if (!visible) Element.hide(quote);
      return height;
    }).toString() + 'px';
  },
  nextQuote: function() {
    return this.quotes[(this.quotes.indexOf(this.currentQuote) + 1) % this.quotes.length];
  },
  registerCallback: function() {
    window.setTimeout(this.tick.bind(this), this.options.duration * 1000);
  },
  tick: function() {
    var currentQuote = this.currentQuote, nextQuote = this.nextQuote();

    new Effect.Parallel([
      new Effect.Fade(currentQuote, {sync: true}),
      new Effect.Appear(nextQuote, {sync: true})
    ], {
      duration: 1 ,
      afterFinish: (function(effect) {
        this.currentQuote = nextQuote;
        this.registerCallback();
      }).bind(this)
    })
  }
}
Event.observe(window, 'load', function() {
  new QuoteShow('advertisements');
})   

// time display
function show_time(){
   if (!document.layers&&!document.all&&!document.getElementById) {
      return;
   }
   var Digital=new Date();
   var hours=Digital.getHours();
   var minutes=Digital.getMinutes();
   var seconds=Digital.getSeconds();
   var dn="am";
   
   if (hours>12){
      dn="pm";
      hours=hours-12;
   }
   if (hours==0) {
      hours=12;
   }
   if (minutes<=9) {
      minutes="0"+minutes;
   }
   if (seconds<=9) {
      seconds="0"+seconds;
   }

   myclock=hours+":"+minutes+":"+seconds+" "+dn;

   if (document.layers){
      document.layers.liveclock.document.write(myclock);
      document.layers.liveclock.document.close();
   } else if (document.all) {
      liveclock.innerHTML=myclock;
   } else if (document.getElementById) {
      $('liveclock').innerHTML=myclock;
   }
   
   setTimeout("show_time()",1000);
}

// IE menu initiator
function startList() 
{
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			// disable the adding of the over class so that the CSS doenst work for current menu items
			if (node.nodeName=="LI" && node.className != "current has_children arrow")	{
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

// make so that the first input variable is automatically focused
function setFocus() 
{                         
   if (document.forms.length > 0) { 
      var field = document.forms[0]; 
      for (i = 0; i < field.length; i++) { 
         if ((field.elements[i].type == "text") || 
            (field.elements[i].type == "textarea") || 
            (field.elements[i].type.toString().charAt(0) == "s")) { 
            document.forms[0].elements[i].focus(); 
            break; 
         } 
      } 
   } 
} 

// show/hide a div
/*function toggle (id){}
if (getElementById(id).style.display == 'none') {
   getElementById(id).style.display == 'block'
} else {
   getElementById(id).style.display == 'none'   
}*/

// change class, pass in class & new name
function switch_class(curr, className)
{
	curr.className = className
}

// menu collapsing feature
function toggle_menu(curr,menu) 
{
	if ($(curr).style.display == 'none') {
		$(curr).style.display = 'block';
    document[menu].src = menu_arrow_open.src;
		document[menu].alt = 'Collapse Menu';
		document[menu].title = 'Collapse Menu';
		document[menu].width = 14;
		document[menu].height = 14;
	} else {
		$(curr).style.display = 'none';
		document[menu].src = menu_arrow_closed.src;		
		document[menu].alt = 'Expand Menu';
		document[menu].title = 'Expand Menu';
		document[menu].width = 14;
		document[menu].height = 14;
	}
}

// used to disable submit buttons to avoid double submit
function disable(button) 
{
	button.value = "Loading ...";
	button.blur();
	button.disabled = true;
}

// initiator
function init() 
{   
   show_time();
   startList();
   setFocus();
} 

