(function () {
        "use strict";

        var utils = (function() {
            var LABEL_OFFSET = {0};
            
            function displayElementText(centerX, centerY, id) {
                var labelContainer = document.getElementsByClassName("himc-label")[0];
                var label = document.createElement("himc-label");
                label.className = "himc-label";
                label.innerHTML = labels[id];
                labelContainer.appendChild(label);
                styleElement(label, centerX - (label.offsetWidth / 2), centerY);
            }
            
            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(maxY, 10) + LABEL_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 {displayElementText: displayElementText,
                    getAreaCenter: getAreaCenter};
        })();
        
        var controller = (function() {
        
            document.getElementById("himc-map-container").onload = function () {
                var areas = document.querySelectorAll("area");
                for (var i = 0; i < areas.length; i++) {
                    var centroid = utils.getAreaCenter(areas[i].getAttribute("coords"));
                    utils.displayElementText(centroid.x, centroid.y, i);
                }
            };
            
            return { };
        })();