var CorePulldownNav = {
	create: function(aId, ulId) {
		this.aId = aId;
		this.ulId = ulId;
	},

	init: function() {
		this.listNav = $(this.ulId);
		if (this.listNav) {
			this.pulldown = $(this.aId);
			Event.observe(this.pulldown, 'click', this.showNav.bindAsEventListener(this));
			Event.observe(this.pulldown, 'mouseout', this.delayedHideNav.bindAsEventListener(this));
			Event.observe(this.pulldown, 'mouseover', this.clearHideNav.bindAsEventListener(this));
			Event.observe(this.listNav, 'mouseout', this.delayedHideNav.bindAsEventListener(this));
			Event.observe(this.listNav, 'mouseover', this.clearHideNav.bindAsEventListener(this));
		}
	},

	showNav: function(e) {
		Event.stop(e);
		
		var pulledElement = Event.element(e);
		pulledElement.blur();
		
		this.clearHideNav();
		if (this.visible) {
			this.hideNav();
		}
		else {
			var leftPos = Position.positionedOffset(pulledElement)[0];
			var topPos = Position.positionedOffset(pulledElement)[1];
			var topPosOff = pulledElement.getHeight();
			this.listNav.style.left = leftPos+'px';
			this.listNav.style.top = (topPos+topPosOff)+'px';
			try {
				Effect.Appear(this.listNav, {duration:0.3});
			} catch(e) {
				Element.show(this.listNav);
			}
			this.visible = true;
		}
	},

	hideNav: function(){
		if (this.visible) {
			try {
				Effect.Fade(this.listNav, {duration:0.3});
			} catch(e) {
				Element.hide(this.listNav);
			}
			this.visible = false;
		}
	},

	delayedHideNav: function(e) {
		if (this.visible){
			this.clearHideNav();
			this.delayID = setTimeout(this.hideNav.bind(this), 250);
		}
	},

	clearHideNav: function(){
		if (this.delayID) {
			clearTimeout(this.delayID);
			this.delayID = null;
		}
	}
};

function newCorePulldownNav(obj) {
	obj = {
		create: CorePulldownNav.create,
		init: CorePulldownNav.init,
		showNav: CorePulldownNav.showNav,
		hideNav: CorePulldownNav.hideNav,
		delayedHideNav: CorePulldownNav.delayedHideNav,
		clearHideNav: CorePulldownNav.clearHideNav
	}
	return obj;
}
