/*
 * tools.overlay "Apple Effect" 1.0.1
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/overlay.html#apple
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Since  : July 2009
 * Date: ${date}
 * Revision: ${revision} 
 */
(
function(d) {
    var b = d.tools.overlay;
    b.effects = b.effects || {};
    b.effects.apple = { version: "1.0.1" };
    d.extend(
    b.conf, {
        start: {
            absolute: true,
            top: null,
            left: null
        },
        fadeInSpeed: "fast",
        zIndex: 9999
    });

    function c(f) {
        var g = f.offset();
        return [g.top + f.height() / 2, g.left + f.width() / 2]
    }

    var e = function(n) {
        var k = this.getOverlay(),
            f = this.getConf(),
            i = this.getTrigger(),
            q = this,
            r = k.outerWidth({ margin: true }),
            m = k.data("img");

        if (!m) {
            var l = k.css("backgroundImage");
            if (!l) { throw "background-image CSS property not set for overlay" }
            l = l.substring(l.indexOf("(") + 1, l.indexOf(")")).replace(/\"/g, "");
            k.css("backgroundImage", "none");
            m = d('<img src="' + l + '"/>');
            m.css({ border: 0, position: "absolute", display: "none" }).width(r);
            d("body").append(m);
            k.data("img", m);
        }

        var o = d(window),
            j = f.start.top || Math.round(o.height() / 2),
            h = f.start.left || Math.round(o.width() / 2);
        if (i) {
            var g = c(i);
            j = g[0];
            h = g[1];
        }
        if (!f.start.absolute) {
            j += o.scrollTop();
            h += o.scrollLeft();
        }

        m.css({
            top: j,
            left: h,
            width: 0,
            zIndex: f.zIndex
        }).show();

        m.animate
        (
            {
                top: k.css("top"),
                left: k.css("left"),
                width: r
            },
            f.speed,
            function()
            {
                k.css("zIndex", f.zIndex + 1).fadeIn(f.fadeInSpeed,
                function()
                {
                    if (q.isOpened() && !d(this).index(k)) {
                        n.call()
                    } else {
                        k.hide()
                    }
                }
        )
            })
        };

    var a = function(f) {
        var h = this.getOverlay(),
        i = this.getConf(),
        g = this.getTrigger(),
        l = i.start.top,
        k = i.start.left;
        h.hide();
        if (g) {
            var j = c(g);
            l = j[0];
            k = j[1]
        }
        h.data("img").animate({
            top: l,
            left: k,
            width: 0
        },
        i.closeSpeed,
        f
        )
    };
     b.addEffect("apple", e, a)
})(jQuery);