// Prototype triggers for IP Handbook
// Created by Henry Woodbury, 2007

// Handle Show / Hide Abstracts
function initHiders() {
	$$('a.show-all').each(function(value, index) {
		var ref = "." + value.getAttribute('href').replace(/^.*\#/,'');
		Element.observe(value, 'click', function(e) {
			if (value.hasClassName('op')) {
				$$('a.show-all').each(function(v, i) {
					v.removeClassName('op');
					v.innerHTML = '<img src="/aa_images/icon_abstract_show_all.gif" width="16" height="13" alt="Show All Abstracts" border="0">Show All Abstracts';
				});
				$$(ref).each(function(v, i) {
					Element.hide(v);
				});
			} else {
				$$('a.show-all').each(function(v, i) {
					v.addClassName('op');
					v.innerHTML = '<img src="/aa_images/icon_abstract_show_all.gif" width="16" height="13" alt="Hide All Abstracts" border="0">Hide All Abstracts';
				});
				$$(ref).each(function(v, i) {
					Element.show(v);
				});
			}
			Event.stop(e);
		});
	});
}

// Initialize elements that toggle "open" and "closed"
function initShowHide(selector) {
	$$(selector).each(function(v) {
		Element.observe(v, 'click', function(e) {
			ref = v.getAttribute('href').replace(/^.*\#/,'');
			if ($(ref)) {
				$(ref).show();
				$(v).up(1).hide();
			}
			Event.stop(e);
		});
	});
}

// Document Menus
function initMenus(callers) {
	var obj = {
		callers: callers,
		currentMenu: 'na',
		targetMenu: 'na',
		x1: 0,
		y1: 0,
		showMenu: function() {
			$(this.currentMenu).show()
			obj.x1 = Position.cumulativeOffset($(this.currentMenu))[0] - 10;
			obj.x2 = this.x1 + $(this.currentMenu).getWidth() + 20;
			obj.y1 = Position.cumulativeOffset($(this.currentMenu))[1] - 40;
			obj.y2 = this.y1 + $(this.currentMenu).getHeight() + 50;
		}
	};
	$$(obj.callers).each(function(v) {
		Element.observe(v, 'mouseover', function(e) {
			var ref = v.getAttribute('title').replace(/\s/,'');
			obj.targetMenu = 'menu' + ref;
			if (obj.targetMenu != obj.currentMenu) {
				if ($(obj.currentMenu)) $(obj.currentMenu).hide();
				obj.currentMenu = obj.targetMenu;
				if ($(obj.currentMenu)) obj.showMenu();
			}
		});
	});
	Element.observe(document.body, 'mousemove', function(e) {
		if ($(obj.currentMenu)) {
			if (((Event.pointerX(e) < obj.x1) || Event.pointerY(e) < obj.y1) || (Event.pointerX(e) > obj.x2) || (Event.pointerY(e) > obj.y2)) {
				$(obj.currentMenu).hide();
				obj.currentMenu = 'na';
			}
		}
	});
}


// Floating Glossary (Info) Boxes
function initInfo() {
	var currentItem = false;
	$$('a.show-info').each(function(value, index) {
		Element.observe(value, 'click', function(e) {
			var ref = value.getAttribute('href').replace(/^.*\#/,'');
			if (ref == currentItem) {
				$(ref).hide();
				currentItem = false;
			} else {
				if ($(currentItem) && $(currentItem).visible()) $(currentItem).hide();
				currentItem = ref;
				$(ref).style.top = Position.cumulativeOffset(value)[1] + value.getHeight() + 4 + 'px';				
				$(ref).show();
			}
			Event.stop(e);
		});	
	});
}

function initShadows() {
	if (/MSIE/.test(navigator.userAgent)) {
		$$('.shadow-left').each(function(value, index) {
			Element.addClassName(value, 'shadow-left-ie');
		});
		$$('.shadow-center').each(function(value, index) {
			Element.addClassName(value, 'shadow-center-ie');
		});
		$$('.shadow-right').each(function(value, index) {
			Element.addClassName(value, 'shadow-right-ie');
		});
	}
}

/* Handle floater */
function initFloater() {
	if (!$('floater')) return;
//	if (parseInt($('floater').style.top) > ($('content-layout').getHeight() + 100)) {
//		$('floater').hide();
//		return;
//	}
	var flo = {
		id: 'floater',
		startX: 0,
		startY: 0,
		offsetX: 0,
		offsetY: 10,

		floatY: function() {
//			new Effect.Move(this.id, { x: this.startX, y: this.moveY(), duration: 0.25, mode: 'absolute' });			
			$(this.id).style.top = this.moveY() + "px";
		},

		moveY: function() {
			Position.prepare();
			return Math.max(this.startY - Position.deltaY, this.offsetY) + Position.deltaY;
		}
	}

	flo.floats = new PeriodicalExecuter(floatY, 0.1);
	Position.absolutize(flo.id);
	if (flo.startY == 0) {	flo.startY = parseInt($(flo.id).style.top); }
	if (flo.startX == 0) {	flo.startX = parseInt($(flo.id).style.left); }
	function floatY() { flo.floatY(); }
}

function init() {
	initMenus();
	initHiders();
	initShadows();
	initInfo();
	initFloater();
}

// The direct action
Event.observe(window,'load',init);
