Centrica.Implementation.InfoBox = {
	infoBoxClass: "infoBox",
	infoBoxInnerClass: "infoBoxWrap",
	infoBoxCloseClass: "infoBoxClose",
	infoBoxFloatedClass: "infoBoxFloat",
	infoBoxPointClass: "infoBoxPoint",

	findInfoBoxes: function () {
		var triggersHover = Centrica.Common.getElementsByTagNameClass(document, "a", "infoBoxHover");
		Centrica.Implementation.InfoBox.prepareTriggers(triggersHover, "hover");
		var triggersImg = Centrica.Common.getElementsByTagNameSimilarClass(document, "img", "teaser");
		Centrica.Implementation.InfoBox.prepareTriggers(triggersImg, "hover");
	},

	prepareTriggers: function (triggerArray, eventType) {
		for(_ta=0; _ta<triggerArray.length; _ta++) {
			if (triggerArray[_ta].nodeName == "IMG") {
				var _infoBox = document.getElementById(triggerArray[_ta].relatedInfoBox);
				if (_infoBox != null) {
					switch (eventType) {
						case "hover":
							triggerArray[_ta].onmouseover = Centrica.Implementation.InfoBox.showInfoBoxHover;
							triggerArray[_ta].onmouseout = Centrica.Implementation.InfoBox.hideInfoBoxHover;
							break;
					}
					Centrica.Implementation.InfoBox.buildInfoBox(_infoBox, eventType);
				}
			}
			else {
				if (triggerArray[_ta].href.indexOf("#") >= 0 ) {
					var _infoBoxID = triggerArray[_ta].href.substring(triggerArray[_ta].href.indexOf("#")+1);
					var _infoBox = document.getElementById(_infoBoxID);
					if (_infoBox != null) {
						triggerArray[_ta].relatedInfoBox = _infoBoxID;
						switch (eventType) {
							case "hover":
								triggerArray[_ta].onmouseover = Centrica.Implementation.InfoBox.showInfoBoxHover;
								triggerArray[_ta].onmouseout = Centrica.Implementation.InfoBox.hideInfoBoxHover;
								break;
						}
						Centrica.Implementation.InfoBox.buildInfoBox(_infoBox, eventType);
					}
				}
			}
		}
	},

	buildInfoBox: function(_infoBox, eventType) {
		Centrica.Common.removeCssClass(_infoBox, Centrica.Implementation.InfoBox.infoBoxClass);
		Centrica.Common.addCssClass(_infoBox, Centrica.Implementation.InfoBox.infoBoxFloatedClass);
		var infoBoxWrapper = Centrica.Common.getElementsByTagNameClass(_infoBox, "div", Centrica.Implementation.InfoBox.infoBoxInnerClass);

		// insert pointer element
		var boxPointer = document.createElement("div");
		boxPointer.className = Centrica.Implementation.InfoBox.infoBoxPointClass;
		infoBoxWrapper[0].appendChild(boxPointer);
	},

	showInfoBoxHover: function(e) {
		return Centrica.Implementation.InfoBox.toggleInfoBoxes(e, true, "hover");
	},

	hideInfoBoxHover: function(e) {
		return Centrica.Implementation.InfoBox.toggleInfoBoxes(e, false, "hover");
	},

	toggleInfoBoxes: function(e, active, eventType) {
		if (!e) var e = window.event;

		// Determine our target
		var _target;
		if (e.target) {
			_target = e.target;
		}
		else if (e.srcElement) {
			_target = e.srcElement;
		}
		if (_target.nodeType == 3) _target = _target.parentNode; // defeat Safari bug

		var _targetInfoBox = document.getElementById(_target.relatedInfoBox);

		//console.log("Target: " + _target);

		if (!active) {
			_targetInfoBox.style.display = "none";
			_targetInfoBox.infoBoxViewClass = null;
			Centrica.Common.removeCssClass(_targetInfoBox, "ibfTL");
			Centrica.Common.removeCssClass(_targetInfoBox, "ibfBL");
		}
		else {
			// Set a flag if we're in hover mode
			if (eventType == "hover") {
				_targetInfoBox.active = true;
				//_target.onmouseout = null;
			}

			// Determine the event position in relation to the page
			var _clickDocPosX = 0;
			var _clickDocPosY = 0;
			if (e.pageX || e.pageY) {
				_clickDocPosX = e.pageX;
				_clickDocPosY = e.pageY;
			}
			else if (e.clientX || e.clientY) {
				_clickDocPosX = e.clientX + document.body.scrollLeft
					+ document.documentElement.scrollLeft;
				_clickDocPosY = e.clientY + document.body.scrollTop
					+ document.documentElement.scrollTop;
			}

			//console.log("Captured event at doc position: [ x: " + _clickDocPosX + " ; y: " + _clickDocPosY + " ]");

			// Determine the event position in relation to the screen
			var _clickScreenPosX = 0;
			var _clickScreenPosY = 0;
			if (e.clientX || e.clientY) {
				_clickScreenPosX = e.clientX;
				_clickScreenPosY = e.clientY;
			}
			else {
				// Keep Safari happy!
				_clickScreenPosX = e.x;
				_clickScreenPosY = e.y;
			}

			//console.log("Captured event at screen position: [ x: " + _clickScreenPosX + " ; y: " + _clickScreenPosY + " ]");

			// Determine the client size
			var _clientWidth = 0;
			var _clientHeight = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				_clientWidth = window.innerWidth;
				_clientHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				_clientWidth = document.documentElement.clientWidth;
				_clientHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				_clientWidth = document.body.clientWidth;
				_clientHeight = document.body.clientHeight;
			}

			//console.log("Detected client dimensions: [ w: " + _clientWidth + " ; h: " + _clientHeight + " ]");

			// Initial boot of info box
			var infoBoxPointer = "ibfTL";
			var _targetPosX = -10000;
			var _targetPosY = 0;

			_targetInfoBox.style.display = "block";
			_targetInfoBox.style.top = _targetPosY + "px";
			_targetInfoBox.style.left = _targetPosX + "px";
			_targetInfoBox.infoBoxViewClass = infoBoxPointer;

			// Determine height of infobox
			var _infoBoxHeight = 0;
			var _infoBoxWidth = 0;
			_infoBoxHeight = _targetInfoBox.offsetHeight;
			_infoBoxWidth = _targetInfoBox.offsetWidth;

			//console.log("Detected info box dimensions of: [ w: " + _infoBoxWidth + " ; h: " + _infoBoxHeight + " ]");

			// Determine actual target position
			//console.log("Target half: " + (_clickScreenPosY>=(_clientHeight/2)?"bottom":"top"));

			_targetPosX = _clickDocPosX;
			if (_clickScreenPosY>=(_clientHeight/2)) {
				infoBoxPointer = "ibfBL";
				_targetPosY = _clickDocPosY - _infoBoxHeight;
			}
			else {
				infoBoxPointer = "ibfTL";
				_targetPosY = _clickDocPosY;
			}

			// Quick fix for flickering
			_targetPosX += 5;

			//console.log("Current target position: [ x: " + _targetInfoBox.style.left + " ; y: " + _targetInfoBox.style.top + " ]");

			if (_targetInfoBox.infoBoxViewClass != null) {
				//console.log("Made it into reposition");
				Centrica.Common.addCssClass(_targetInfoBox, infoBoxPointer);
				_targetInfoBox.style.top = _targetPosY + "px";
				_targetInfoBox.style.left = _targetPosX + "px";
			}

			//console.log("Final target position: [ x: " + _targetPosX + " ; y: " + _targetPosY + " ]");
			//console.log("Actual final target position: [ x: " + _targetInfoBox.style.left + " ; y: " + _targetInfoBox.style.top + " ]");
			//console.dir(_targetInfoBox);
		}

		return false;
	}
};