var activeMenu;

function createMenu(name){
	var el = Ext.get(name + '-link');
	var tid = 0, menu, doc = Ext.getDoc();
	
	var handleOver = function(e, t){
		if(t != el.dom && t != menu.dom && !e.within(el) && !e.within(menu)){
			hideMenu();
		}	
	};
			
	var hideMenu = function(){
		if(menu){
			menu.hide();
			//el.setStyle('text-decoration', '');
			doc.un('mouseover', handleOver);
			doc.un('mousedown', handleDown);
		}
	}
	
	var handleDown = function(e){
		if(!e.within(menu)){
			hideMenu();
		}
	}
	
	var showMenu = function(){
		clearTimeout(tid);
		tid = 0;
		
		if (!menu) {
			menu = new Ext.Layer({shadow:'sides', hideMode: 'display'}, name + '-menu');
		}
		menu.hideMenu = hideMenu;
			
		menu.el = el;
		if(activeMenu && menu != activeMenu){
			activeMenu.hideMenu();
		}
		activeMenu = menu;
		
		if (!menu.isVisible()) {
			menu.show();
			menu.alignTo(el, 'tl-bl?');
			menu.sync();
			//el.setStyle('text-decoration', 'underline');
			
			doc.on('mouseover', handleOver, null, {buffer:150});
			doc.on('mousedown', handleDown);
		}
	}
	
	el.on('mouseover', function(e){
		if(!tid){
			tid = showMenu.defer(150);				
		}
	});
	
	el.on('mouseout', function(e){
		if(tid && !e.within(el, true)){
			clearTimeout(tid);
			tid = 0;				
		}
	});
}

function anchor_scroll(element) {
	if (element == "top") {
		Ext.get(document.documentElement).scrollTo('top', 0, true);
	} else {
		var el = Ext.get(element);
		Ext.get(document.documentElement).scroll('down', el.getTop(), true);
	}
}

var records_vault_popup;
var city_timeline_popup;

Ext.onReady(function() {
	// Create a popup for the records vault page
	records_vault_popup = new Ext.Window({
		title: 'Records Vault',
		width: 400,
		height: 440,
		resizable: false,
		closeAction: 'hide',
		items: [{
			xtype: 'panel',
			bodyStyle: 'padding: 5px;',
			border: false,
			autoLoad: '/_pages/records_vault/index.php'
		}],
		tbar: [{
			text: 'Enter Records Vault',
			icon: '/images/list_item_red.png',
			cls: 'x-btn-text-icon',
			handler: function() {
				records_vault_popup.hide();
				window.open('http://weblink.ranchomirageca.gov/WebLink8');
			}
		}]
	});
	
	// Create a popup for the city timeline
	city_timeline_popup = new Ext.Window({
		title: 'City Timeline',
		width: 943,
		modal: true,
		resizable: false,
		closeAction: 'hide',
		contentEl: 'city_timeline'
	});
});