(function () {
        "use strict";

        var utils = (function() {
            var INFO_OFFSET = {0};

            function getOffsets() {
                var mapContainer = document.getElementById("himc-map-container");
                return {top: mapContainer.offsetTop, left: mapContainer.offsetLeft};
            }
            
            function styleElement(element, left, top) {
                element.style.left = left + "px";
                element.style.top = top + "px";
            }
            
            function getBoundingCenter(minX, maxX, minY, maxY) {
                var x = parseInt((minX + maxX) / 2, 10);
                var y = parseInt((minY + maxY) / 2, 10) + INFO_OFFSET;
                return {x: x, y: y};
            }
            
            function getAreaCenter(coords) {
                var coordsArray = coords.split(","),
                    center = [];
                var coord, maxX, maxY, minX = maxX = parseInt(coordsArray[0], 10),
                    minY = maxY = parseInt(coordsArray[1], 10);
                for (var i = 0; i < coordsArray.length; i++) {
                    coord = parseInt(coordsArray[i], 10);
                    if (i % 2 === 0) {
                        if (coord < minX) {
                            minX = coord;
                        } else if (coord > maxX) {
                            maxX = coord;
                        }
                    } else {
                        if (coord < minY) {
                            minY = coord;
                        } else if (coord > maxY) {
                            maxY = coord;
                        }
                    }
                }
                return getBoundingCenter(minX, maxX, minY, maxY);
            }
  
            return {getOffsets: getOffsets,
                    styleElement: styleElement,
                    getAreaCenter: getAreaCenter};
        })();
        
        var box = (function() {
            var INFOBOX_POINTER_HEIGHT = 15;
            
            function displayBox(centerX, centerY, id) {
                var infoBox = document.getElementById("himc-info-box");
                infoBox.innerHTML = infoBoxes[id];
                infoBox.className = "himc-visible";
                utils.styleElement(infoBox, centerX - (infoBox.offsetWidth / 2), centerY - infoBox.offsetHeight - INFOBOX_POINTER_HEIGHT);
            }

            function hideBox() {
                document.getElementById("himc-info-box").className = "himc-hidden";
            }
            
            return {display: displayBox, hide: hideBox};
        })();

        var controller = (function() {
            
            document.addEventListener("click", function (e) {
                if (e.target.tagName === "AREA") {
                    var centroid = utils.getAreaCenter(e.target.getAttribute("coords"));
                    var offsets = utils.getOffsets();
                    var computedX = centroid.x + offsets.left;
                    var computedY = centroid.y + offsets.top;
                    box.display(computedX, computedY, Number(e.target.getAttribute("data-info-id")));
                } else {
                    box.hide();
                }
            });
            
            return { };
        })();