/* ----------------------------
*  ACMU, Menu Management
*  Version 0.3
*  (c) 2009 Atogy Corporation
*  http://www.atogy.com/
*------------------------------ */

var RootMenu='nav';

var BarMenuDef={"Home":"/", 
"Services":"/services", 
"Flight Training":"/training", 
"Company":"/company", 
"Airports":"/airports", 
"Links":"/links.html"};

var TrainingMenuDef={
"Training Home":"/training", 
//"Intro Flight":"/training/intro.html", 
//"Private":"/training/private.html",
//"Instrument":"/training/instrument.html",
//"Commercial":"/training/commercial.html",
//"CFI":"/training/cfi.html",
//"CFII":"/training/cfii.html",
//"BFRs &amp; IPCs":"/training/bfr_ipc.html" };
"Airplane Training":"/training/asel.html",
"Helicopter Training":"/training/heli.html",
"Instructros":"/training/contactCFI.html" };

var ServiceMenuDef={"Aircraft Rental":"/aircraft", 
//"Financing":"/services/finance.html", 
"Pilot Supplies":"/sales", 
"Other Services":"/services"};

var AboutMenuDef={"About Us":"/company", 
"Contact Us":"/company/contact.html",
"Careers":"/careers" };

var AirportMenuDef={"About Benson Airport":"/airports", 
"Benson Airport News":"/airports/news.html" };

var DropMap={
"nav2":ServiceMenuDef,
"nav3":TrainingMenuDef,
"nav4":AboutMenuDef,
"nav5":AirportMenuDef};

var NavDrop = Class.create();

var barClass ='navBar';
var barID ='barID';
var barParent ='header';

var btnClass ='navBtn';
var btnDropClass ='navDropBtn';

var dropClass='navDrop';
var dropParent ='top';

var sepStart =' |&nbsp;';
var sepEnd   ='&nbsp;| ';
var sepClass ='navSep';

var selClass = 'navSel';

NavDrop.VERSION = '0.2';

NavDrop.addSubMenu = function(event){
	var el	    = Event.element(event);
//alert('addSubMenu');
	NavDrop.makeDrop(el.id,dropClass,DropMap[el.id]);
	return Event.stop(event);
};

NavDrop.eatMe = function(event){
	return Event.stop(event);
};

NavDrop.makeBar = function(pid, did, items){
	var bar = new NavDrop();
//alert('makeBar');
	bar.buildBar(pid, did, items);

	for(var item in DropMap) { 
		if (DropMap.hasOwnProperty(item)) {
			Event.observe(item, 'mousedown', NavDrop.addSubMenu);
			Event.observe(item, 'click', NavDrop.eatMe);
		}
	}
	return bar;
};

NavDrop.makeDrop = function(sid, did, items){
	if (window._NavDrop){
		window._NavDrop.hide();
	}
	var drop = new NavDrop();
	drop.buildDrop(sid, did, items);
	return drop;
};

NavDrop.checkIsDrop = function(event) {
	var el	    = Event.element(event);
//alert(el.id);

	if (!window._NavDrop){
	    return false;
	}
	if (Element.descendantOf(el, window._NavDrop.dID)){
	    return;
	}
	window._NavDrop.hide();
	return Event.stop(event);
};

NavDrop.prototype = {

	// NavDrop instance ID
	dID : null ,

	// Parent 'containing' element
	pID : null, 

	// Selected bar menu ID (parent of sub menu)
	sELEM : null ,

////////////////////////   Constructors  //////////////////////////

	initialize: function() {
	},


///////////////////////////////////////////////////////////////////
//  ProWin Display Functions
///////////////////////////////////////////////////////////////////

  // Shows the ProWin
  show: function()
  {
    $(dropParent).insert(this.dID);
    this.dID.show();
    window._NavDrop = this;
    Event.observe(document, 'mousedown', NavDrop.checkIsDrop);
  },

  // Shows the proWin at the given absolute position
  showAt: function (x, y)
  {
    this.dID.setStyle({ left: x + 'px', top: y + 'px' });
    this.show();
  },

  // Shows the ProWin at the coordinates of the provided element
  showAtElement: function(element)
  {
    var pos = Position.cumulativeOffset(element);
    this.showAt(pos[0], pos[1]+element.getHeight());
  },

  // Hides the ProWin
  hide: function()
  {
//alert('hide');
	Event.stopObserving(document, 'mousedown', NavDrop.checkIsDrop);
	this.dID.remove();
	this.sELEM.className='navBtn';
	window._NavDrop=undefined;
//    this.dID.hide();
  },


///////////////////// HTML Menu Generation SCRIPTs ///////////////

	buildBar : function(parentID,barID,items){
//alert('buildBar');
		this.dID = new Element('DIV', { 'class': barClass, 'id': barID });
		this.pID = parentID;
		var cNav = 0;
		var idNav;
		for(var item in items) { 
   			if (items.hasOwnProperty(item)) {
				cNav += 1;
				idNav='nav'+cNav;
				var elemF1 = new Element('FONT', { 'class': sepClass }).update(sepStart);
				var elemA  = new Element('A', { 'class': btnClass, 'id':idNav, 'href': items[item] }).update(item);
				var elemF2 = new Element('FONT', { 'class': sepClass }).update(sepEnd);
				this.dID.appendChild( elemF1 );
				this.dID.appendChild( elemA );
				this.dID.appendChild( elemF2 );
			}
		}
		$(parentID).insert(this.dID);

		this.dID.show();
	},

	buildDrop : function(selID,dropID,items){
//alert('buildDrop');
		this.dID = new Element('DIV', { 'class': dropClass, 'id': dropID });
		this.sELEM = $(selID);

		//alert("dropID="+dropID+" did="+dID);
		for(var item in items) { 
   			if (items.hasOwnProperty(item)) {
				var elemP = new Element('DIV', {} );
				var elemA = new Element('A', { 'class': btnDropClass, 'href': items[item] }).update(item);
				elemP.appendChild( elemA );
				this.dID.appendChild( elemP );
//				if (item.startsWith("-")){
//					var elemS = new Element('HR', {'style':'width: 100%'} );
//					elemP.appendChild( elemS );
//				}
			}
		}

		this.sELEM.className=selClass;
		this.showAtElement($(selID));
	}
};


//////////////////////////////////////////////////////////////////////////////////////////////
// Initiate Menu Loading
///////////////////////////////////////////////////////////////////////////////////////////////

Event.observe(window, 'load', function() {

//	NavDrop.makeBar(barParent,barClass,BarMenuDef);

	getClipAndModify('/header.clip',barParent, function() {
		NavDrop.makeBar(barParent,barID,BarMenuDef);
	});

	$$('div.loadClip').each(function(elem) {

		var clip = elem.readAttribute('clip');
		if (clip != '') {
//	 		alert(clip+" == "+elem.identify());
			getClip(clip,elem.identify()); 
		}
	});
});

/* ******
Event.observe(window, 'load', function() {

	var right = 'right';
	if ($(right) != undefined) {
		getClip('right.clip','right'); 
	}
});
**** */