
// start TextareaMaxlength
var TextareaMaxlength = Class.create (
{
    initialize: function(textarea, maxlength)
    {
        this.textarea = textarea;
        this.maxlength = maxlength;
        // attach event handlers
        Event.observe(this.textarea, 'keyup', this.__Key.bindAsEventListener(this));
    },

    // Enforce textarea maxlength
    enforce: function()
    {
        if ($F(this.textarea).length >= this.maxlength)
        {
            this.textarea.value = $F(this.textarea).substring(0, this.maxlength);
        }
    },

    // Deal with event handler
    __Key: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        this.enforce();
    }

});
// end TextareaMaxlength

// start PopOver
var PopOver = Class.create (
{
    initialize: function(trigger, popover, closer)
    {
        this.trigger = trigger;
        this.popover = popover;
        this.closer = closer;
        this.trigger.style.display = 'block';

        // attach a click handler to trigger element
        Event.observe(this.trigger, 'click', this.__Click1.bindAsEventListener(this));

        // attach a click handler to close link
        Event.observe(this.closer, 'click', this.__Click2.bindAsEventListener(this));
    },

    // Deal with a clicked link
    __Click1: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        this.popover.style.display = 'block';
    },

    // Deal with a clicked link
    __Click2: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        this.popover.style.display = 'none';
    }
});
// end PopOver

// start MultiPopOvers
var MultiPopOvers = Class.create (
{
    initialize: function(aTriggers, aPopovers, aClosers)
    {
        this.links = aTriggers;
        this.items = aPopovers;
        this.closers = aClosers;

        // Loop through each and attach a click handler to trigger element
        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'click', this.__Click1.bindAsEventListener(this));
        }

        // Loop through each and attach a click handler to close link
        for (var i=0; i<this.closers.length; i++)
        {
	        Event.observe(this.closers[i], 'click', this.__Click2.bindAsEventListener(this));
        }

    },

    // Show a specific item, hide the rest
    doShowHide: function(iWhich)
    {
        for (var i=0; i<this.items.length; i++)
        {
	        if (i == iWhich)
	        {
		        this.items[i].style.display = 'block';
	        } else {
	            this.items[i].style.display = 'none';
	        }
        }
    },

    // Hide all items
    doHide: function()
    {
        for (var i=0; i<this.items.length; i++)
        {
	        this.items[i].style.display = 'none';
        }
    },

    // Deal with a clicked link
    __Click1: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    },

    // Deal with a clicked link
    __Click2: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        this.doHide();
    }

});
// end MultiPopOvers

// start ImageGallery
var ImageGallery = Class.create (
{
    initialize: function(aLinks, ssPhoto, rotate)
    {
        this.links = aLinks;
        this.photo = ssPhoto;
        this.rotate = rotate || false;
        this.num = 0;

        // show different item every 5 seconds
        if (rotate)
        {     
            this.interval = setInterval(function()
            {
                this.doRotate();
            }.bind(this), 5000);
        }

        // Loop through each and attach a click handler
        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'click', this.__Click.bindAsEventListener(this));
        }

    },

    // Rotate thru items
    doRotate: function()
    {
        this.num++;
        if (this.num == this.links.length)
        {
            this.num = 0;
        }
        this.doPhotoSwap(this.num);
    },

    // Show a specific item, hide the rest
    doPhotoSwap: function(iWhich)
    {
        for (var i=0; i<this.links.length; i++)
        {
	        if (i == iWhich)
	        {
		        this.links[i].className = 'in';
		        this.photo.src = this.links[i].href;
		        this.photo.alt = this.links[i].title;
	        } else {
	            this.links[i].className = '';
	        }
        }
    },

    // Deal with a clicked link
    __Click: function(e)
    {
        clearInterval(this.interval);   // clear timer once link is clicked
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.doPhotoSwap(i);
		        break;
	        }
        }
    }
});
// end ImageGallery

// start ContentSwitcher
var ContentSwitcher = Class.create (
{
    initialize: function(aLinks, aItems, rotate, showfirst)
    {
        this.links = aLinks;
        this.items = aItems;
        this.rotate = rotate || false;
        this.showfirst = showfirst || false;
        this.num = 0;

        // show different item every 8 seconds
        if (rotate)
        {     
            this.interval = setInterval(function()
            {
                this.doRotate();
            }.bind(this), 8000);
        }

        // Loop through each and attach a click handler
        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'click', this.__Click.bindAsEventListener(this));
        }

		// Show the first content item
        if (showfirst)
        {   
		    this.doShowHide(0);
        }
    },

    // Rotate thru items
    doRotate: function()
    {
        this.num++;
        if (this.num == this.links.length)
        {
            this.num = 0;
        }
        this.doShowHide(this.num);
    },

    // Show a specific item, hide the rest
    doShowHide: function(iWhich)
    {
        for (var i=0; i<this.items.length; i++)
        {
	        if (i == iWhich)
	        {
		        this.links[i].parentNode.className = 'in';
		        this.items[i].style.display = 'block';
	        } else {
	            this.links[i].parentNode.className = '';
	            this.items[i].style.display = 'none';
	        }
        }
    },
    // Deal with a clicked link
    __Click: function(e)
    {
        clearInterval(this.interval);   // clear timer once link is clicked
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    }
});
// end ContentSwitcher

// start SifrContentSwitcher
// sifr text has issues in IE with standard show / hide techniques,
// this technique uses absolute positioning, rotate has been removed
var SifrContentSwitcher = Class.create (
{
    initialize: function(aLinks, aItems, showfirst)
    {
        this.links = aLinks;
        this.items = aItems;
        this.showfirst = showfirst || false;

        // Loop through each and attach a click handler
        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'click', this.__Click.bindAsEventListener(this));
        }

		// Show the first content item
        if (showfirst)
        {   
		    this.doShowHide(0);
        }
    },

    // Show a specific item, hide the rest
    doShowHide: function(iWhich)
    {
        for (var i=0; i<this.items.length; i++)
        {
	        if (i == iWhich)
	        {
		        this.links[i].parentNode.className = 'in';
		        this.items[i].style.position = 'static';
		        this.items[i].style.top = '0px';
	        } else {
	            this.links[i].parentNode.className = '';
	            this.items[i].style.position = 'absolute';
	            this.items[i].style.top = '-50001px';
	        }
        }
    },

    // Deal with a clicked link
    __Click: function(e)
    {
        clearInterval(this.interval);   // clear timer once link is clicked
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    }
});
// end SifrContentSwitcher

// start TabSwitcher
var TabSwitcher = Class.create (
{
    initialize: function(aLinks, aItems)
    {
        this.links = aLinks;
        this.items = aItems;
        this.timeout;

        // Loop through each and attach an event handler
        for (var i=0; i<this.links.length; i++)
        {
	        Event.observe(this.links[i], 'mouseover', this.__linkOver.bindAsEventListener(this));
	        Event.observe(this.links[i], 'mouseout', this.__linkOut.bindAsEventListener(this));
        }
        for (var i=0; i<this.items.length; i++)
        {
	        
	        Event.observe(this.items[i], 'mouseover', this.__itemOver.bindAsEventListener(this));
	        Event.observe(this.items[i], 'mouseout', this.__itemOut.bindAsEventListener(this));
        }
    },

    // Show a specific item, hide the rest
    doShowHide: function(iWhich)
    {
        for (var i=0; i<this.items.length; i++)
        {
	        if (i == iWhich)
	        {
		        clearTimeout(this.timeout);
		        this.items[i].style.display = 'block';
	        } else {
	            this.items[i].style.display = 'none';
	        }
        }
    },

    // Deal with link mouseover
    __linkOver: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    },

    // Deal with link mouseout
    __linkOut: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.links.length; i++)
        {
	        if (this.links[i] == element)
	        {
                this.timeout = setTimeout(function()
                {
                    this.items[i].style.display = 'none';
                }.bind(this), 1500);
		        break;
	        }
        }
    },

    // Deal with item mouseover
    __itemOver: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        for (var i=0; i<this.items.length; i++)
        {
	        if (this.items[i] == element)
	        {
		        this.doShowHide(i);
		        break;
	        }
        }
    },

    // Deal with item mouseout
    __itemOut: function(e)
    {
        var e = e || window.event;
        Event.stop(e);

        var element = Event.element(e);
        for (var i=0; i<this.items.length; i++)
        {
			  /*note: 
					e.relatedTarget is the node to which the pointer went
					e.currentTarget is the node to which the event is attached
			  */
	        if (this.items[i] == element && !(e.relatedTarget.descendantOf(element)))
	        {
                this.timeout = setTimeout(function()
                {
                    this.items[i].style.display = 'none';
                }.bind(this), 1500);
		        break;
	        }
        }
    }

});
// end TabSwitcher

// start PopupWindow
var PopupWindow = Class.create (
{
    initialize: function(link, width, height, extras)
    {
        this.winName = 'popup';
        this.altWidth = 640;
        this.altHeight = 480;
        this.altExtras = 'location=yes,menubar=yes,statusbar=yes,toolbar=yes,scrollbars=yes,resizable=yes';
        this.link = link;
        this.url = link.getAttribute('href');
        this.w = width || this.altWidth;
        this.h = height || this.altHeight;
        this.x = extras || this.altExtras;
        this.features = 'width=' + this.w + ',height=' + this.h + ',' + this.x;
        Event.observe(this.link, 'click', this.__Click.bindAsEventListener(this));
    },
    // Deal with a clicked link
    __Click: function(e)
    {
        var e = e || window.event;
        Event.stop(e);
        var element = Event.element(e);
        var win = window.open(this.url, this.winName, this.features);
        win.focus();
    }
});
// end PopupWindow

// start FlyoutNav
var FlyoutNav = Class.create (
{
    initialize: function(nav)
    {
        this.nav = nav;
        this.navnodes = nav.childElements();
        // Loop through each and attach event handlers
        for(var i=0; i<this.navnodes.length; i++)
        {
            Event.observe(this.navnodes[i], 'mouseover', function()
            {
                this.addClassName('over');
            });
            Event.observe(this.navnodes[i], 'mouseout', function()
            {
                this.removeClassName('over');
            });
            //Event.observe(this.navnodes[i], 'mouseover', this.__Mouseover.bindAsEventListener(this));
            //Event.observe(this.navnodes[i], 'mouseout', this.__Mouseout.bindAsEventListener(this));
        }
    },
    __Mouseover: function(e)
    {
        var e = e || window.event;
        var element = Event.findElement(e, 'li');
        element.addClassName('over');
    },
    __Mouseout: function(e)
    {
        var e = e || window.event;
        var element = Event.findElement(e, 'li');
        element.removeClassName('over');
    }
});
// note: not needed for SFO
// end FlyoutNav



// ****** start: misc ******

function printPage()
{
    if (window.print){window.print();}
}

function hide(element)
{
	document.getElementById(element).style.display = 'none';
	return;
}
function show(element)
{
	document.getElementById(element).style.display = 'block';
	return;		
}

// ****** end: misc ******




// NO LONGER IN USE, USE INSTEAD: var PopupWindow
// start: popup window
// usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])
// basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>
// advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>
// site-wide defaults:
popup_w = 400;
popup_h = 300;
popup_scroll = true;
popup_extras = 'location=0,statusbar=0,menubar=0';
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? popup_scroll : args[i++];
	var features = 'width=' + (w || popup_w)
				 + ',height=' + (h || popup_h)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + (args[i] || popup_extras);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}
// end: popup window
