﻿function imageMapScaler() {
    var iWidth = 0, imageId = '', mapId = '', initialAreas = [], isScaled = false;
    function realignImage() {
        if (isScaled) {
            return;
        }
        var coords = [], scale = +($('#' + imageId).width() / iWidth);
        for (var i = 0, len = initialAreas.length; i < len; i = i + 1) {
            coords = initialAreas[i].slice(0);
            for (var j = 0, jlen = coords.length; j < jlen; j = j + 1) { coords[j] = +coords[j] * scale; }
            $('#' + mapId + ' area').eq(i).attr('coords', coords.join(','));
        }
        isScaled = true;
    }
    return {
        init: function (config) {
            iWidth = config.nativeImageWidth; imageId = config.imageId; mapId = config.mapId; isScaled = false;
            $('#' + mapId + ' area').each(function (idx, el) { initialAreas.push(el.coords.split(',')); });
            $('#' + imageId).mouseenter(function () { realignImage(); }); $(window).resize(function () { isScaled = false; });
        }
    }
}
