/* Copyright 2009 Monsternett AS */

jQuery.fn.imgEnhance = function(providedOptions)
{
    var opts = $.extend({}, jQuery.fn.imgEnhance.defaults, providedOptions);

    if (opts.images.lenght < 1)
        return;

    var cur = 0,
        max = opts.images.length,
        isZoomed = false,
        busy_zooming = false;

    function getPosition(obj, dir) {
        var pos = (dir == 'x') ? obj.offsetLeft : obj.offsetTop;
        var tmp = obj.offsetParent;

        while(tmp != null) {
            pos += (dir == 'x') ? tmp.offsetLeft : tmp.offsetTop;
            tmp = tmp.offsetParent;
        }

        return pos;
    }

    function zoom_image(pressed_magnify)
    {
        if(busy_zooming)
            return;

        if(pressed_magnify && isZoomed)
        {
            return unzoom_image();
        }

        busy_zooming = true;

        var overlay = $("#dark-overlay");
        var wrapper = $("#imagepane-div");
        var pic = $("#news-picture img");
        var body = $("body");
        var wrap = $("#wrapper");

        overlay
            .css('width', Math.max(body.width(), wrap.width()) + 'px')
            .css('height', Math.max(body.height(), wrap.height()) + 'px');


        pic.css('width', pic[0].width + "px");
        pic.css('height', pic[0].height + "px");

        wrapper.css('left', getPosition(wrapper[0], 'x'));
        wrapper.css('top', getPosition(wrapper[0], 'y'));

        var tmp_pic = document.createElement('img');

        tmp_pic.onload = function() {
            var img_w_after = tmp_pic.width;
            var img_h_after = tmp_pic.height;

            wrapper.css('position', 'absolute');
            $("#imagepane-dupe").show();

            pic[0].src = tmp_pic.src;

            pic.animate({
                'width': img_w_after + "px",
                'height': img_h_after + "px"
            }, 1000, 'swing', function() {
                pic.css('width', '');    
                pic.css('height', '');    
                busy_zooming = false;
            });

            wrapper.animate({
                'height': (opts.zoom_max_h + opts.padding + 50) + 'px',
                'width': '850px'
            }, 1000, 'swing');

            if(!isZoomed) {
                overlay.css('opacity', '0');
                overlay.show();
                overlay.animate({
                    'opacity': 0.6,
                    '-moz-opacity': 0.6
                }, 1000, 'swing');
            }

            $("#zoom-type-img")[0].src = "/graphics/icons/shrink.png";
            isZoomed = true;
        };

        tmp_pic.src = "/thumbnail.php?max_w=" + opts.zoom_max_w + "&max_h=" + opts.zoom_max_h + "&img=/"+  opts.images[cur].url;

        return false;
    }

    function unzoom_image()
    {
        if(busy_zooming)
            return false;

        busy_zooming = true;

        var overlay = $("#dark-overlay");
        var wrapper = $("#imagepane-div");
        var pic = $("#news-picture img");

        pic.css('width', pic[0].width);
        pic.css('height', pic[0].height);

        var tmp_pic = document.createElement('img');

        tmp_pic.onload = function() {
            var img_w_after = tmp_pic.width;
            var img_h_after = tmp_pic.height;

            pic[0].src = tmp_pic.src;

            pic.animate({
                'width': img_w_after + "px",
                'height': img_h_after + "px"
            }, 1000, "swing", function() {
                pic.css('width', '');
                pic.css('height', '');
                busy_zooming = false;
            });

            wrapper.animate({
                'height': (opts.unzoomed_max_h + opts.padding + 50) + 'px',
                'width': '470px'
            }, 1000, 'swing', function() {
                wrapper.css('position', 'static');
                $("#imagepane-dupe").hide();
            });

            overlay.animate({
                'opacity': 0,
                '-moz-opacity': 0
                }, 1000, 'swing',
                function() {
                    overlay.hide();
                }
            );

            $("#zoom-type-img")[0].src = "/graphics/icons/magnify.png";

            isZoomed = false;
        };

        tmp_pic.src = "/thumbnail.php?max_w=" + opts.unzoomed_max_w + "&max_h=" + opts.unzoomed_max_h + "&img=/"+  opts.images[cur].url;
        return false;
    }

    function prev_image()
    {
        cur -= 1;
        if(cur < 0)
            cur = opts.images.length-1;
        show_image(cur);

        return false;
    }
    function next_image()
    {
        cur += 1;
        if(cur >= opts.images.length)
            cur = 0;
        show_image(cur);

        return false;
    }

    function show_image(id)
    {
        if(typeof(id) != typeof(undefined))
            cur = id;

        $('#curpic').html(cur+1);
        var img = $("#news-picture img");
        if(isZoomed) {
            img[0].src = "/thumbnail.php?max_w=" + opts.zoom_max_w + "&max_h=" + opts.zoom_max_h + "&img=/" + opts.images[cur].url;
        } else {
            img[0].src = "/thumbnail.php?max_w=" + opts.unzoomed_max_w + "&max_h=" + opts.unzoomed_max_h + "&img=/" + opts.images[cur].url;
        }


        var next = (cur+1 < opts.images.length) ? cur+1 : 0,
            prev = (cur-1 >= 0) ? cur-1 : opts.images.length-1,
            tmp = document.createElement('img'),
            tmp2 = document.createElement('img');

        if(isZoomed) {
            tmp.src  = "/thumbnail.php?max_w=" + opts.zoom_max_w + "&max_h=" + opts.zoom_max_h + "&img=/" + opts.images[next].url;
            tmp2.src = "/thumbnail.php?max_w=" + opts.zoom_max_w + "&max_h=" + opts.zoom_max_h + "&img=/" + opts.images[prev].url;
        } else {
            tmp.src = "/thumbnail.php?max_w=" + opts.unzoomed_max_w + "&max_h=" + opts.unzoomed_max_h + "&img=/" + opts.images[next].url;
            tmp2.src = "/thumbnail.php?max_w=" + opts.unzoomed_max_w + "&max_h=" + opts.unzoomed_max_h + "&img=/" + opts.images[prev].url;
        }

        $("#image-title").html(opts.images[cur].title);
        $("#image-descr").html(opts.images[cur].descr);

        return false;
    }

    var elements = this;
    return elements.each(function()
    {
        var control = $("#picture-control");
        if(!control.length)
            return;

        var a_prev = document.createElement('a'),
            a_next = document.createElement('a'),
            a_zoom = document.createElement('a');

        $(a_prev)
            .click(prev_image)
            .attr('href', '#')
            .html('<img src="/graphics/icons/previous.png" alt="Forrige bilde">');
        $(a_next)
            .click(next_image)
            .attr('href', '#')
            .html('<img src="/graphics/icons/next.png" alt="Neste bilde">');
        $(a_zoom)
            .click(function() { zoom_image(true); return false })
            .attr('href', '#')
            .html('<img id="zoom-type-img" src="/graphics/icons/magnify.png" alt="Forstørr bilde">');

        $("#picture-control")
            .html('<div id="bilde-x-av-y">Bilde <span id="curpic">1</span> av ' + opts.images.length + '</div>')
            .append(a_prev)
            .append(a_zoom)
            .append(a_next);

        var i = 0;
        $(opts.image_group).each(function() {
            $(this).click((function(i) { return function() { show_image(i); return false; } })(i++));
        });
        show_image();
        control.show();
    });

};

jQuery.fn.imgEnhance.defaults = {
    images: [],
    zoom_max_h: 450,
    zoom_max_w: 850,
    unzoomed_max_h: 205,
    unzoomed_max_w: 470,
    image_group: '',
    padding: 4
};

