$(document).ready(function () {
    var fade_speed = 60;
    
    /**
     * Get the hook object defined in the iframe window, if it exists
     */
    function get_iframe_hook(frame)
    {
        var frame = frame.get(0);
        var fw = frame.contentWindow;
        if (!frame.contentWindow)
        {
            return false;
        }
        if (!fw.Tolcher)
        {
            return false;
        }
        if (!fw.Tolcher.popup)
        {
            return false;
        }
        return fw.Tolcher.popup;
    }
    
    $('a[rel="popup"]').each(function () {
        var dims = { width:48, height:48 };
        var first_time = true;
        var frame_attrs = {
            src: $(this).attr('href'),
            width: dims.width,
            height: dims.height,
            "frameBorder": "0",
            "allowtransparency": "allowtransparency"
        };
        $(this).data('popup_url', $(this).attr('href')).qtip({
            content: {
                text: $('<iframe />', frame_attrs).bind('load', function () {
                        if ($(this).data('resizing'))
                        {
                            return;
                        }
                        $(this).animate({opacity:1.0}, fade_speed, function () {
                            $(this).closest('.ui-tooltip-popup').removeClass('ui-tooltip-iframe-loading');
                        });
                    }),
                title: {
                    text: $(this).attr('title'),
                    button: true
                }
            },
            position: {
                my: 'center',
                at: 'center',
                target: $(window),
                viewport: $(window),
                adjust: {
                    y: -50
                }
            },
            show: {
                event: 'click',
                solo: true,
                modal: {
                    on: true,
                    effect: false
                }
            },
            hide: false,
            style: {
                classes: "ui-tooltip-light ui-tooltip-rounded ui-tooltip-popup"
            },
            events: {
                show: function (event, api)
                {
                    // reload the iframe each time
                    api.elements.tooltip.addClass('ui-tooltip-iframe-loading ui-tooltip-iframe-showing');
                    api.elements.tooltip.css({width:(dims.width+20)+'px'}).qtip('reposition');
                    var frame = api.elements.content.find('iframe');
                    frame.css({opacity:0, width:dims.width+'px'});
                    if (dims.height)
                    {
                        frame.css("height", dims.height+'px');
                    }
                    if (!first_time)
                    {
                        frame.get(0).src = api.elements.target.data('popup_url');
                    }
                    else
                    {
                        first_time = false;
                    }
                    window.Tolcher.popup.current = api.elements.tooltip;
                },
                hide: function (event, api) {
                    var hooks = get_iframe_hook(api.elements.content.find('iframe'));
                    if (hooks && typeof hooks.close == 'function')
                    {
                        hooks.close();
                    }
                    window.Tolcher.popup.current = false;
                    api.elements.tooltip.removeClass('ui-tooltip-iframe-loading ui-tooltip-iframe-showing');
                }
            }
        }).click(function (e) {
            return false;
        });
    });

    window.Tolcher.popup = {
        current:false,
        resize: function (w, h, callback) {
            var p = window.Tolcher.popup;
            if (!p.current)
            {
                return;
            }
            if (typeof h == 'function')
            {
                callback = h;
                h = null;
            }
            var css = {width:w+'px'};
            if (h)
            {
                css.height = h+'px';
            }
            p.current.addClass('ui-tooltip-iframe-loading');
            var frame = p.current.find('iframe');
            frame.data('resizing', true);
            frame.animate({opacity:0}, fade_speed, function () {
                frame.css(css);
                p.current.css({width:(w+20)+"px"}).qtip('reposition');
                p.current.removeClass('ui-tooltip-iframe-showing');
                frame.animate({opacity: 1.0}, fade_speed, function () {
                    frame.data('resizing', false);
                    p.current.removeClass('ui-tooltip-iframe-loading');
                    if (typeof callback == "function")
                    {
                        callback();
                    }
                });
            });
        },
        refresh: function (url, callback) {
            var p = window.Tolcher.popup;
            if (!p.current)
            {
                return;
            }
            p.current.addClass('ui-tooltip-iframe-loading');
            var frame = p.current.find('iframe');
            frame.animate({opacity:0}, fade_speed, function () {
                var cb = function ()
                {
                    frame.unbind('load', cb);
                };
                frame.bind('load', cb);
                frame.get(0).src = url;
            });
        },
        fade_out: function (url, callback) {
            var p = window.Tolcher.popup;
            if (!p.current)
            {
                return;
            }
            p.current.addClass('ui-tooltip-iframe-loading');
            var frame = p.current.find('iframe');
            frame.animate({opacity:0}, fade_speed);
        },
        retitle: function (title) {
            var p = window.Tolcher.popup;
            if (!title || !p.current)
            {
                return;
            }
            p.current.find('.ui-tooltip-titlebar .ui-tooltip-title').text(title);
            
        },
        close: function () {
            var p = window.Tolcher.popup;
            if (!p.current)
            {
                return;
            }
            p.current.qtip('hide');
        }
    };
});
