/*
 *  DROP-DOWN MENUS - Based on Prototype JavaScript framework
 *  (c) 2007 MCC Network Services
 *
 *	consult the css file for adjusting width and other styles of the menus
 *
 *	required files:
 *
 *	prototype.js
 *	scriptaculous.js
/*--------------------------------------------------------------------------*/

function startMenus()
{
$$('ul.submenu').each(function(s) {
  s.setStyle({opacity: "0.90"});
});

var main_items = $$('ul.menu > li');
var items_total = main_items.size();

main_items.each(function(e, index)
	{
		//IF OUR MAIN MENU LINK HAS A SUBMENU
		if(e.down(1))
		{
		//OBSERVE 'MOUSEOVER' EVENT
		e.observe('mouseover',function() {
		//GET NEXT TAG INSIDE THE LI TAG WHICH IS THE A TAG
		c = e.immediateDescendants();
				
		bx = c[0].getStyle('background-position-x');
		by = c[0].getStyle('background-position-y');
		bc = c[0].getStyle('color');
					
		//CHANGE THE TAB'S BACKGROUND TO HAVE THE MOUSEOVER 'STICKY' STATE
		c[0].setStyle({'background-position': "0px -42px",'color' : '#ff9c00'});
		truepos = Position.positionedOffset(e);
		bodypos = $$('body')[0].getWidth();
		l = truepos[0];
				//FIX MENU POSITION IF IT GOES OUT OF BOUNDARIES OF THE DOCUMENT.
				if((bodypos <= l + 180 + 14))
				{
				w = e.getWidth();
					//LAST ITEM IN THE MENU NEEDS A SPECIAL FIX
					if(index == (items_total-1)) {
						l = (l - w - 9);}
						else {//everything else
						l = (l - w + 1);}
				}
			// PLACE THE MENU ON THE PAGE
			e.down(1).setStyle({left: l +'px',top: '42px'});
			});

		//OBSERVE 'MOUSEOUT' EVENT
		e.observe('mouseout',function() {
		//GET NEXT TAG INSIDE THE LI TAG WHICH IS THE A TAG
		c = e.immediateDescendants();
		//CHANGE THE TAB'S BACKGROUND BACK TO ORIGINAL BACKGROUND
		//IE ACCEPTS BACKGROUND-POSITION-X & BACKGROUND-POSITION-Y FROM CSS JUST FINE,
		//HOWEVER FIREFOX IS CLULESS WHAT X & Y IS; HENCE THE BRUTE FORCE WITH `UNDEFINED`...ETC.
		orgstyle = (bx == undefined && by == undefined) ? "" : bx + ' ' + by;
		c[0].setStyle({'background-position': orgstyle, 'color' : '#fff'});
		e.down(1).setStyle({left: "-9999px"});
			});
		}
		else //NO SUBMENU
		{
		return false;
		}
	}
);
}

//LOAD UP ON PAGE LOAD
Event.observe(window, 'load', startMenus);

function getDirections()
{
	if($('origin') && $F('origin').length > 0)
	{
		window.open('http://maps.google.com/maps?f=d&hl=en&geocode=&time=&date=&ttype=&saddr='+$F('origin')+'&daddr=1400+College+Dr,+Waco,+TX+76708&sll=31.524703,-97.058029&sspn=0.409128,0.6427&ie=UTF8&z=9&om=1');
	}
	else
	{
		alert('Please enter your address.\nExample: 1234 Elm St, Houston, TX, 70707');
	}
}
