(function(f) {
    var b, c, j, m, l, i, g, e, h, a, k;
    b = document.namespaces;
    has_canvas = !! document.createElement("canvas").getContext;
    if (!(has_canvas || b)) {
        f.fn.maphilight = function() {
            return this
        };
        return
    }
    if (has_canvas) {
        g = function(p, o, n) {
            if (o <= 1) {
                p.style.opacity = o;
                window.setTimeout(g, 10, p, o + 0.1, 10)
            }
        };
        e = function(n) {
            return Math.max(0, Math.min(parseInt(n, 16), 255))
        };
        h = function(n, o) {
            return "rgba(" + e(n.substr(0, 2)) + "," + e(n.substr(2, 2)) + "," + e(n.substr(4, 2)) + "," + o + ")"
        };
        c = function(n) {
            var o = f('<canvas style="width:' + n.width + "px;height:" + n.height + 'px;"></canvas>').get(0);
            o.getContext("2d").clearRect(0, 0, o.width, o.height);
            return o
        };
        j = function(q, n, t, p, o) {
            var s, r = q.getContext("2d");
            r.beginPath();
            if (n == "rect") {
                r.rect(t[0], t[1], t[2] - t[0], t[3] - t[1])
            } else {
                if (n == "poly") {
                    r.moveTo(t[0], t[1]);
                    for (s = 2; s < t.length; s += 2) {
                        r.lineTo(t[s], t[s + 1])
                    }
                } else {
                    if (n == "circ") {
                        r.arc(t[0], t[1], t[2], 0, Math.PI * 2, false)
                    }
                }
            }
            r.closePath();
            if (p.fill) {
                r.fillStyle = h(p.fillColor, p.fillOpacity);
                r.fill()
            }
            if (p.stroke) {
                r.strokeStyle = h(p.strokeColor, p.strokeOpacity);
                r.lineWidth = p.strokeWidth;
                r.stroke()
            }
            if (p.fade) {
                g(q, 0)
            }
        };
        m = function(n, o) {
            n.getContext("2d").clearRect(0, 0, n.width, n.height)
        }
    } else {
        c = function(n) {
            return f('<var style="zoom:1;overflow:hidden;display:block;width:' + n.width + "px;height:" + n.height + 'px;"></var>').get(0)
        };
        j = function(o, r, s, v, n) {
            var t, u, p, q;
            t = '<v:fill color="#' + v.fillColor + '" opacity="' + (v.fill ? v.fillOpacity : 0) + '" />';
            u = (v.stroke ? 'strokeweight="' + v.strokeWidth + '" stroked="t" strokecolor="#' + v.strokeColor + '"' : 'stroked="f"');
            p = '<v:stroke opacity="' + v.strokeOpacity + '"/>';
            if (r == "rect") {
                q = f('<v:rect name="' + n + '" filled="t" ' + u + ' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:' + s[0] + "px;top:" + s[1] + "px;width:" + (s[2] - s[0]) + "px;height:" + (s[3] - s[1]) + 'px;"></v:rect>')
            } else {
                if (r == "poly") {
                    q = f('<v:shape name="' + n + '" filled="t" ' + u + ' coordorigin="0,0" coordsize="' + o.width + "," + o.height + '" path="m ' + s[0] + "," + s[1] + " l " + s.join(",") + ' x e" style="zoom:1;margin:0;padding:0;display:block;position:absolute;top:0px;left:0px;width:' + o.width + "px;height:" + o.height + 'px;"></v:shape>')
                } else {
                    if (r == "circ") {
                        q = f('<v:oval name="' + n + '" filled="t" ' + u + ' style="zoom:1;margin:0;padding:0;display:block;position:absolute;left:' + (s[0] - s[2]) + "px;top:" + (s[1] - s[2]) + "px;width:" + (s[2] * 2) + "px;height:" + (s[2] * 2) + 'px;"></v:oval>')
                    }
                }
            }
            q.get(0).innerHTML = t + p;
            f(o).append(q)
        };
        m = function(n) {
            f(n).find("[name=highlighted]").remove()
        }
    }
    l = function(o) {
        var n, p = o.getAttribute("coords").split(",");
        for (n = 0; n < p.length; n++) {
            p[n] = parseFloat(p[n])
        }
        return [o.getAttribute("shape").toLowerCase().substr(0, 4), p]
    };
    k = function(p, o) {
        var n = f(p);
        return f.extend({}, o, f.metadata ? n.metadata() : false, n.data("maphilight"))
    };
    a = function(n) {
        if (!n.complete) {
            return false
        }
        if (typeof n.naturalWidth != "undefined" && n.naturalWidth == 0) {
            return false
        }
        return true
    };
    i = {
        position: "absolute",
        left: 0,
        top: 0,
        padding: 0,
        border: 0
    };
    var d = false;
    f.fn.maphilight = function(p) {
        p = f.extend({}, f.fn.maphilight.defaults, p);
        if (f.browser.msie && !d) {
            document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
            var o = document.createStyleSheet();
            var n = ["shape", "rect", "oval", "circ", "fill", "stroke", "imagedata", "group", "textbox"];
            f.each(n, function() {
                o.addRule("v\\:" + this, "behavior: url(#default#VML); antialias:true")
            });
            d = true
        }
        return this.each(function() {
            var v, s, z, r, u, w, y, t, x;
            v = f(this);
            if (!a(this)) {
                return window.setTimeout(function() {
                    v.maphilight(p)
                }, 200)
            }
            z = f.extend({}, p, f.metadata ? v.metadata() : false, v.data("maphilight"));
            x = v.get(0).getAttribute("usemap");
            r = f('map[name="' + x.substr(1) + '"]');
            if (!(v.is("img") && x && r.size() > 0)) {
                return
            }
            if (v.hasClass("maphilighted")) {
                var q = v.parent();
                v.insertBefore(q);
                q.remove()
            }
            s = f("<div></div>").css({
                display: "block",
                background: "url(" + this.src + ")",
                position: "relative",
                padding: 0,
                width: this.width,
                height: this.height
            });
            if (z.wrapClass) {
                if (z.wrapClass === true) {
                    s.addClass(f(this).attr("class"))
                } else {
                    s.addClass(z.wrapClass)
                }
            }
            v.before(s).css("opacity", 0).css(i).remove();
            if (f.browser.msie) {
                v.css("filter", "Alpha(opacity=0)")
            }
            s.append(v);
            u = c(this);
            f(u).css(i);
            u.height = this.height;
            u.width = this.width;
            y = function(C) {
                var A, B;
                B = k(this, z);
                if (!B.neverOn && !B.alwaysOn) {
                    A = l(this);
                    j(u, A[0], A[1], B, "highlighted");
                    if (B.groupBy && f(this).attr(B.groupBy)) {
                        var D = this;
                        r.find("area[" + B.groupBy + "=" + f(this).attr(B.groupBy) + "]").each(function() {
                            if (this != D) {
                                var F = k(this, z);
                                if (!F.neverOn && !F.alwaysOn) {
                                    var E = l(this);
                                    j(u, E[0], E[1], F, "highlighted")
                                }
                            }
                        })
                    }
                }
            };
            if (z.alwaysOn) {
                f(r).find("area[coords]").each(y)
            } else {
                f(r).find("area[coords]").each(function() {
                    var A, B;
                    B = k(this, z);
                    if (B.alwaysOn) {
                        if (!w) {
                            w = c(v.get());
                            f(w).css(i);
                            w.width = v.width();
                            w.height = v.height();
                            v.before(w)
                        }
                        A = l(this);
                        if (f.browser.msie) {
                            j(u, A[0], A[1], B, "")
                        } else {
                            j(w, A[0], A[1], B, "")
                        }
                    }
                });
                f(r).find("area[coords]").mouseover(y).mouseout(function(A) {
                    m(u)
                })
            }
            v.before(u);
            v.addClass("maphilighted")
        })
    };
    f.fn.maphilight.defaults = {
        fill: true,
        fillColor: "8f2c2d",
        fillOpacity: 0.3,
        stroke: false,
        strokeColor: "ff0000",
        strokeOpacity: 1,
        strokeWidth: 1,
        fade: true,
        alwaysOn: false,
        neverOn: false,
        groupBy: false,
        wrapClass: true
    }
})(jQuery);

