/**
 * liteZoom magnify javascript
 * Copyleft (C) 2010 BohwaZ, licensed under the GNU AGPL v3.
 * http://bohwaz.net/
 **

    liteZoom is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License.

    liteZoom is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with liteZoom.  If not, see <http://www.gnu.org/licenses/>.
*/

var liteZoom = {
    magnify: 2,
    currentImage: null,
    currentZoom: null,

    load: function()
    {
        var elements = document.getElementsByTagName("a");
        var elements_length = elements.length;

        for (i = 0; i < elements_length; i++)
        {
            if (elements[i].rel && elements[i].rel.match(/litezoom/))
            {
                liteZoom.initElement(elements[i]);
            }
        }
    },

    initElement: function(elm)
    {
        var img = elm.getElementsByTagName("img");

        if (img.length != 1)
            return false;

        img[0].onmouseover = liteZoom.hover;
        img[0].onmouseout = liteZoom.out;
    },

    hover: function()
    {
        var img = this;
        var parent = img.parentNode;

        var zoom = document.createElement('span');
        zoom.id = 'liteZoomMagnifier';
        zoom.style.display = 'block';
        zoom.style.width = zoom.style.left = img.width + 'px';
        zoom.style.height = img.height + 'px';
        zoom.style.overflow = 'hidden';

        var magnify =
            (match = parent.rel.match(/litezoom\[([0-9.]+)\]/))
            ? parseFloat(match[1])
            : liteZoom.magnify;

        var zoomImg = img.cloneNode(true);
        zoomImg.src = parent.href;
        zoomImg.width = zoomImg.width * magnify;

        zoom.appendChild(zoomImg);

        parent.appendChild(zoom);

        liteZoom.currentImage = img;
        liteZoom.currentZoom = zoom;

        document.onmousemove = liteZoom.move;
    },

    move: function(e)
    {
        e = e ? e : window.event;

        var x = e.pageX ? e.pageX : e.x;
        var y = e.pageY ? e.pageY : e.y;
        var img_x = img_y = 0;

        // Get mouse position relative to element
        var elm = liteZoom.currentImage;

        if (elm.offsetParent)
        {
            do
            {
                img_x += elm.offsetLeft;
                img_y += elm.offsetTop;
            }
            while (elm = elm.offsetParent);
        }

        img_x = x - img_x;
        img_y = y - img_y;

        var img = liteZoom.currentImage;
        var zoomImg = liteZoom.currentZoom.firstChild;

        var ratio = zoomImg.width / img.width;
        var pos_x = Math.round((ratio * img_x) - (img.width / 2));
        var pos_y = Math.round((ratio * img_y) - (img.height / 2));

        if (pos_x > (zoomImg.width - img.width))
            pos_x = zoomImg.width - img.width;

        if (pos_y > (zoomImg.height - img.height))
            pos_y = zoomImg.height - img.height;

        zoomImg.style.marginLeft = '-' + pos_x + 'px';
        zoomImg.style.marginTop = '-' + pos_y + 'px';
    },

    out: function()
    {
        if (document.getElementById('liteZoomMagnifier'))
        {
            document.getElementById('liteZoomMagnifier').parentNode.removeChild(document.getElementById('liteZoomMagnifier'));
            document.onmousemove = null;
            liteZoom.currentImage = null;
            liteZoom.currentZoom = null;
        }
    }
};

if (window.addEventListener)
{
    window.addEventListener('load', liteZoom.load, false);
}
else if (window.attachEvent)
{
    window.attachEvent('onload', liteZoom.load);
}
else
{
    window.onload = liteZoom.load;
}