// Prototype triggers for TropIKA
// Created by Henry Woodbury, Spring 2007

// Handle Show / Hide Elements
function initHiders() {
	$$('.hider').each(function(value, index) {
		Element.observe(value, 'click', function(e) {
			ref = "." + value.getAttribute('href').replace(/^.*\#/,'');
			$$(ref).each(function(value, index) {
				if (Element.hasClassName(value, 'op')) {
					Element.removeClassName(value, 'op');
					Element.hide(value);
				} else {
					Element.addClassName(value, 'op');
					Element.show(value);
				}
			});
			Event.stop(e);
		});
	});
}

// Handle Show / Hide Elements where Ref is not from an Anchor
function commentHider(ref) {
	$$(ref).each(function(value, index) {
		if (Element.hasClassName(value, 'op')) {
			Element.removeClassName(value, 'op');
			Element.hide(value);
		} else {
			Element.addClassName(value, 'op');
			Element.show(value);
		}
	});
}

// Handle Checkbox Show / Hide Elements
function initFormHiders() {
	$$('.form-hider').each(function(value, index) {
		Element.observe(value, 'click', function(e) {
			ref = value.getAttribute('href').replace(/^.*\#/,'');
			refi = ref + '-image';
			if ($(ref).visible()) {
				Element.hide(ref);
				$(refi).src = "/images/icon_open.gif";
			} else {
				Element.show(ref);
				$(refi).src = "/images/icon_close.gif";
			}
			Event.stop(e);
		});
	});
}

// Handle downlinks and references
function initOptions() {
	var currentSection = 'section-page-lead';
	var refSection = 'page-references';
	if ($(currentSection)) {
		var ref = 'section-' + location.href.split('#')[1];
		if ($(ref) && ref != currentSection) {
			Element.hide(currentSection);
			currentSection = ref;
			Element.show(currentSection);
			sectionHider(ref);
		}
		$$('div.section-navigator a').each(function(value, index) {
			Element.observe(value, 'click', function(e) {		
				var ref = 'section-' + value.getAttribute('href').replace(/^.*\#/,'');
				if ($(ref)) {
					Element.hide(currentSection);
					currentSection = ref;
					Element.show(currentSection);
					sectionHider(ref);
				}
			});
		});
	}
	if ($('hider-' + refSection)) {
		$$('a.ref').each(function(value, index) {
			Element.observe(value, 'click', function(e) {
				sectionHider('section-' + refSection);
			});
		});
	}
}

/* Show / Hide 'Hider' Sections */
function sectionHider(ref) {
	var hiderRef = ref.replace(/section/, 'hider');
	var hiderClass = ref.replace(/section-/, '');
	if ($(hiderRef) && Element.hasClassName(hiderRef, 'op')) {
		$$('.' + hiderClass).each(function(value, index) {
			if (Element.hasClassName(value, 'op')) {
				Element.removeClassName(value, 'op');
				Element.hide(value);
			} else {
				Element.addClassName(value, 'op');
				Element.show(value);
			}
		});						
	}
}

/* Handle "select all" checkboxes */
function initHoover() {
	var checkAll = new Array();
	var checkBack = new Array();
	var elCid;
	$$('input.checkall').each(function(value, index) {
		var obj = {
			id: $(value).id,
			check: $(value).checked,
			targets: '.' + $(value).id
		};
		checkAll.push(obj);
		$$(obj.targets).each(function(val, i) {
			if (!$(val).id) {
				elCid = 'hoover-' + i;
				while ($(elCid)) {
					elCid += 'i';
				}
				$(val).id = elCid;	
			}
			var obi = {
				id: $(val).id,
				checker: index
			}
			checkBack.push(obi);
		});
	});	
	checkAll.each(function(value, index) {
		Element.observe($(value.id), 'click', function(e) {
			if (value.check) {
				value.check = false;
				$(value.id).checked = false;
				$$(value.targets).each(function(val, i) {
					$(val).checked = false;
				});
				
			} else {
				value.check = true;
				$(value.id).checked = true;
				$$(value.targets).each(function(val, i) {
					$(val).checked = true;
				});
				if ($(value.id + '-match')) {
				}
			}
		});
	});
	checkBack.each(function(value, index) {
		Element.observe($(value.id), 'click', function(e) {
			if (!$(value.id).checked) { 
				checkAll[value.checker].check = false;
				$(checkAll[value.checker].id).checked = false;
			}
		});
	});
}

// Alpha transparency for banner PNG
function initAlphaBanner() {
	if (/MSIE/.test(navigator.userAgent)) {
		$('banner-image').addClassName('banner-image-ie');
		$('banner-image').removeClassName('banner-image-ei');
	}
}

// Floating Info Boxes
function initInfo(ajaxURL) {
	var ajaxCaller = 'panel-criteria';
	var ajaxTarget = 'ajaxData'; 
	var currentBox = false;
	var currentItem = false;
	$$('.show-info').each(function(value, index) {
		Element.observe(value, 'click', function(e) {
			var refItem = value.getAttribute('href').replace(/^.*\#/,'');
			var refBox = refItem.split('?')[0];
			var refID =  refItem.split('?')[1];
			if (refItem == currentItem && $(currentBox).visible()) {
				$(refBox).hide();
				currentItem = false;
				currentBox = false;
			} else {
				if ($(currentBox) && $(currentBox).visible()) $(currentBox).hide();
				currentItem = refItem;
				currentBox = refBox;
				var callX = Position.page(value)[0];
				var callY = Position.page(value)[1];
				var callOffX = Position.cumulativeOffset(value)[0];
				var callOffY = Position.cumulativeOffset(value)[1];
				var callW = value.getWidth();
				var callH = value.getHeight();
				var w = $(currentBox).getWidth();
				var h = $(currentBox).getHeight();
				var winH = document.documentElement.clientHeight;
				var winW = document.documentElement.clientWidth;
				var icon = "icon-" + currentBox;
				if (callH + callY + 4 + h < winH) {
					var styleLeft = parseInt(callOffX + callW/2 - w/2);
					var iconLeft = Math.min(styleLeft, 0);
					if (callX + w/2 + 4 > winW) {
						styleLeft = winW - 4 - w;
						iconLeft = callX - styleLeft - parseInt(w/2) + callW/2;
					}
					$(currentBox).style.left = Math.max(styleLeft, 0) + 'px';
					$(currentBox).style.top = callOffY + callH + 4 + 'px';
					$(icon).style.left = parseInt(w/2) - 7 + iconLeft + 'px' ;
					$(icon).style.top = '0px';
					$(icon).removeClassName('icon-criteria-left');
					$(icon).removeClassName('icon-criteria-right');
					$(icon).addClassName('icon-criteria');
					$(icon).src = '/images/icon_arrow_criteria.gif';
				} else {
					var boxOffY = h*2/3;
					if (callY + h - boxOffY > winH) boxOffY = callY + h - winH;
					if (callY - boxOffY < 0) boxOffY = callY;
					$(currentBox).style.top = callOffY - boxOffY + 'px';
					$(icon).style.top = boxOffY + 2 + 'px';
					$(icon).removeClassName('icon-criteria');
					if (callX - w - 4 < 0) {
						$(currentBox).style.left = callOffX + callW + 4 + 'px';
						$(icon).style.left = '0px';
						$(icon).removeClassName('icon-criteria-left');
						$(icon).addClassName('icon-criteria-right');
						$(icon).src = '/images/icon_arrow_criteria_right.gif';
					} else {
						$(currentBox).style.left = callOffX - w - 4 + 'px';
						$(icon).style.left = w - 8 + 'px';
						$(icon).removeClassName('icon-criteria-right');
						$(icon).addClassName('icon-criteria-left');
						$(icon).src = '/images/icon_arrow_criteria_left.gif';

					}
				}
				if (refBox == ajaxCaller && $(ajaxTarget)) {
					$(ajaxTarget).innerHTML = '<p><img src="/images/search_process.gif" alt="loading" title="loading" /></p>';
					Try.these (
						function() { req = new Ajax.Updater(ajaxTarget, ajaxURL, { method: 'get', parameters: refID }); }
					);
				}
				Element.show(currentBox);
			}
			Event.stop(e);
		});
	});
// Handle the Make Private / Make Public
	$$('a.vj-status').each(function(value, index) {
		Element.observe(value, 'click', function(e) {
			if (value.innerHTML == 'Make public') {
				value.previous('span').update('Public');
				value.update('Make private');
// Ajax call here to update database
			} else if (value.innerHTML == 'Make private') {
				value.previous('span').update('Private');
				value.update('Make public');
// Ajax call here to update database
			}
			Event.stop(e);
		});
	});
}

function init() {
	initHoover();
	initHiders();
	initFormHiders();
	initOptions();
	initInfo('http://www.dynamicdiagrams.net/cgi-bin/tropika.v2.pl');
}


// InitSubMenus function
function initSubMenus(selector) {
	$$(selector).each(function(value) {
		Element.observe(value, 'click', function(e) {
			ref = value.getAttribute('href').replace(/^.*\#/,'');
			if ($(value).hasClassName('hider-open')) {
				$(value).removeClassName('hider-open');
				$(value).addClassName('hider-closed');
				$(ref).hide();
			} else {
				$(value).removeClassName('hider-closed');
				$(value).addClassName('hider-open');
				$(ref).show();
			}
			Event.stop(e);
		});
	});
}

// Separate init for the Main Menu to get them functional as quickly as possible
function initMenus() {
	initAlphaBanner();
	initSubMenus('a.menu-hider');
}

// The direct action
Event.observe(window,'load',init);


