// JavaScript Document

/*******************************
*
* InfoLayers.js
* 
* DHTML Hidden Layers with additional information that position to a triggering element and Show / Hide on said elements rollover.
* This does not handle the rollover of the element
* Ver = 2.0 - Utilizes prototype.js - /js/extern/prototype.js
*
* Author: John Hutcheson, control.option
*
*******************************/
var InfoLayers = Class.create();


InfoLayers.prototype = {

	//NavMenus Object Class Constructor
	initialize: function(anchorSuffix, layerSuffix, xOffSet, yOffSet) {
		if (document.images) {
			//Create empty extended Aarray to hold info layers
			this.layers = $A(new Array());
			
			this.anchorSuffix = (anchorSuffix && typeof anchorSuffix == "string") ? anchorSuffix : "_infoAnchor";
			this.layerSuffix = (layerSuffix && typeof layerSuffix == "string") ? layerSuffix : "_infoLayer";
			this.xOffSet = xOffSet;
			this.yOffSet = yOffSet;
			
			//Initialize timer variable used to hold set Timeout function in request hide method
			this.timer;
			
			//Set up event handlers
			this.setHandlers();
		}
	},
	
	addLayerSet: function(triggerElem, xOffSet, yOffSet) {
		
		var idRoot = this.getIdRoot(triggerElem);
		var layer = $(idRoot + this.layerSuffix);
		var anchor = $(idRoot + this.anchorSuffix);
		var xOS = (xOffSet == undefined) ? 0 : parseInt(xOffSet);
		var yOS = (yOffSet == undefined) ? 0 : parseInt(yOffSet);
		if (typeof this.xOffSet == "string") {  
			if (this.xOffSet == "left") {
				xOS = -(layer.getWidth());
			} else if (this.xOffSet == "right") {
				xOS = anchor.getWidth() + layer.getWidth();
			}
		}
		
		if (typeof this.yOffSet == "string") {  
			if (this.yOffSet == "top") {
				yOS = -(layer.getHeight());
			} else if (this.yOffSet == "bottom") {
				yOS = anchor.getHeight() + layer.getHeight();
			}
		}
		
		
		var triggerLayerSet = { trigger: $(triggerElem), infoLayer: layer, posAnchor: anchor, shiftX: xOS , shiftY: yOS };
		
		
		if (this.layers.indexOf(triggerLayerSet) != -1) {
			return;
		}
		this.layers.push(triggerLayerSet);
		
	},
	
	getIdRoot: function (id) {
		return id.replace(id.substring(id.indexOf('_')), '');
	},
	
	getLayerSet: function (elt) {
		var layerSet;
		this.layers.each(function(l) {
			if (l.trigger.id == elt.id) {
				layerSet = l;
				$break;
			}
		});
		return layerSet;	
	},
	
	setHandlers: function () {
		this.boundOver = this.over.bindAsEventListener(this);
		this.boundOut = this.out.bindAsEventListener(this);
		Event.observe (document, 'mouseover', this.boundOver);
		Event.observe (document, 'mouseout', this.boundOut);
	},
	
	over: function (e) {
		Event.stop(e);
		var elt = $(Event.element(e));
		if (elt.hasClassName("mppDescTrigger")) {
			this.showLayer(this.getLayerSet(elt));
		}
	},
	
	out: function (e) {
		Event.stop(e);
		var elt = $(Event.element(e));
		if (elt.hasClassName("mppDescTrigger")) {
			this.requestHide();
		}
	},
	
	
	
	
	requestHide: function() {
		var boundcall = this.hideLayers.bind(this);
		this.timer = setTimeout(boundcall, 100);
	},
	
	hideLayers: function() {
		this.layers.each(function (l) {
			l.infoLayer.setHidden();									  
		});
	},
	
	showLayer: function(layerSet) {
		this.keepLayer();
		this.hideLayers();
		
		var offSet = Position.cumulativeOffset(layerSet.posAnchor);
		
		layerSet.infoLayer.setStyle({left: (offSet[0] + layerSet.shiftX) + 'px', top: (offSet[1] + layerSet.shiftY) + 'px'});
		layerSet.infoLayer.setVisible();
	},
	
	keepLayer: function () {
		clearTimeout(this.timer);
	}
	
	
};
