
// Add a class name to the body as a global flag that the user has javascript.
document.observe('dom:loaded', function(){
	var elBody = $(document.body);
	if (elBody) {elBody.addClassName('js-enabled');}
});
// end

/*
 * EnterClick listens on a containing element for the enter/return key being pressed.
 * When the enter key is pressed within a child input element that will submit the form, 
 * it will prevent the default form submittal and fire the click event of a designated button.
 *  
 * Usage:
 * $('fieldset_forgot').onEnterClick($('btn_Forgot'));
 * currently used on /account/login.aspx
 */
var EnterClick = Class.create({
	relevantTypes: $w('text password checkbox radio'),
	initialize: function(element, button)
	{
		this.element = $(element).addClassName("enterclick");
		this.button = $(button);
		this.element.observe('keypress', this.__keyPress.bindAsEventListener(this));
	},
	__keyPress: function(e)
	{
		var elSrc = e.element();
		if((e.keyCode == Event.KEY_RETURN) 
			&& (elSrc.nodeName == 'INPUT') 
			&& (this.relevantTypes.include(elSrc.type)))
			{
				// prevent default form submittal
				e.stop(); 
				// fire associated button's click event
				this.button.click();
		}
	}
});
// Extend Element with enterclick
Element.addMethods({
	onEnterClick: function(element, button)
	{
		new EnterClick(element, button);
		return element;
	}
});

// end EnterClick

// start PrintPage
var PrintPage = Class.create ({
	initialize: function(link)
	{
		this.link = link;
		this.link.observe('click', this.__Click.bindAsEventListener(this));
	},
	__Click: function(e)
	{
		e.stop();
		window.print();
	}
});
// end PrintPage

// start TextareaMaxlength
var TextareaMaxlength = Class.create ({
	initialize: function(textarea, maxlength)
	{
		this.textarea = textarea;
		this.maxlength = maxlength;
		this.textarea.observe('keyup', this.__Key.bindAsEventListener(this));
	},
	__Key: function(e)
	{
		e.stop();
		if ($F(this.textarea).length >= this.maxlength)
		{
			this.textarea.value = $F(this.textarea).substring(0, this.maxlength);
		}
	}
});
// end TextareaMaxlength

// start PopupWindow
var PopupWindow = Class.create ({
    initialize: function(link, width, height, xtras)
    {
        this.popName = 'popup';
        this.link = link;
        this.url = this.link.href;
        this.altW = 640;
        this.altH = 480;
        this.altX = 'location=no,menubar=no,statusbar=no,toolbar=no,scrollbars=no,resizable=yes';
        this.popupW = width || this.altW;
        this.popupH = height || this.altH;
        this.popupX = xtras || this.altX;
        this.availH = screen.availHeight;
        this.availW = screen.availWidth;
        this.topPos = (this.availH - this.popupH)/2;
        this.leftPos = (this.availW - this.popupW)/2;
        this.features = 'width=' + this.popupW + ',height=' + this.popupH + ',' + this.popupX + ',top=' + this.topPos + ',left=' + this.leftPos;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        var win = window.open(this.url, this.popName, this.features);
        win.focus();
    }
});
// end PopupWindow

// start MultiPopupWindows
var MultiPopupWindows = Class.create ({
    initialize: function(links, width, height, xtras)
    {
        this.popName = 'popup';
        this.links = links;
        this.length = this.links.size();
        this.altW = 640;
        this.altH = 480;
        this.altX = 'location=yes,menubar=yes,statusbar=yes,toolbar=yes,scrollbars=yes,resizable=yes';
        this.popupW = width || this.altW;
        this.popupH = height || this.altH;
        this.popupX = xtras || this.altX;
        this.availH = screen.availHeight;
        this.availW = screen.availWidth;
        this.topPos = (this.availH - this.popupH)/2;
        this.leftPos = (this.availW - this.popupW)/2;
        this.features = 'width=' + this.popupW + ',height=' + this.popupH + ',' + this.popupX + ',top=' + this.topPos + ',left=' + this.leftPos;
		var boundLinkClick = this.__Click.bindAsEventListener(this);
        this.links.invoke('observe', 'click', boundLinkClick);
    },
    __Click: function(e)
    {
        e.stop();
        var element = e.element();
        for (var i=0; i<this.length; i++)
        {
	        if (this.links[i] == element)
	        {
                var url = this.links[i].href;
                var win = window.open(url, this.popName, this.features);
                win.focus();
                break;
	        }
        }
    }
});
// end MultiPopupWindows

// start ClosePopup
var ClosePopup = Class.create ({
    initialize: function(link)
    {
        this.link = link;
        this.link.observe('click', this.__Click.bindAsEventListener(this));
    },
    __Click: function(e)
    {
        e.stop();
        window.close();
    }
});
// end ClosePopup

// start FlyoutNav
var FlyoutNav = Class.create ({
    initialize: function(nav)
    {
        this.nav = nav;
        this.navnodes = this.nav.childElements();
        this.length = this.navnodes.size();
        for(var i=0; i<this.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');
    }
});
// only needed for IE6, place in head inside IE6 conditional comment
/*
document.observe('dom:loaded', function()
{
    doSiteNavFlyout = new FlyoutNav( $('mainnav') );
});
*/
// end FlyoutNav
