/*
 * popbox (for jQuery)
 * version: 1.1 (03/01/2008)
 * @requires jQuery v1.2 or later
 *
 * Usage:
 *  
 *  jQuery(document).ready(function() {
 *    jQuery('a[rel*=popbox]').popbox() 
 *  })
 *
 *  <a href="#popudiv" rel="popbox">popup</a>
 *    Loads the #popudiv div in the box
 *
 *  <a href="popup.html" rel="popbox">popup file</a>
 *    Loads the popup.html page in the box
 *
 *  <a href="popup.png" rel="popbox">popup image</a>
 *    Loads the popup.png image in the box
 *
 *
 *   programmatically:
 * 
 *    jQuery.popbox('html')
 *
 *  This will open a popbox with "html" as the content.
 *    
 *    jQuery.popbox(function() { ajax })
 *
 *  This will show a loading screen before the passed function is called,
 *  allowing for a better ajax experience.
 *
 */

// global variables //
var popboxMask = true;
var popboxWidth = 0; //auto size=0

function openPopup(val, type) {

    switch (type) {
        case 0: //'div' 
            var dv = $get(val);
            jQuery.popbox(dv.innerHTML, null);
            break;
        case 1: //text-html
            jQuery.popbox(val, null);
            break;
        case 2: //file
            jQuery.popbox.openFile(val, null);
            break;
        default: //text-html
            jQuery.popbox(val, null);
            break;
    }
    return false;
}

function closePopup() {
    jQuery.popbox.close();
}

function getPopupWidth () {
    if (popboxWidth == 0) return "";
    return 'style ="width:' + popboxWidth + 'px;"';
}

(function($) {
    $.popbox = function(data, cls) {
        $.popbox.init()
        $.popbox.loading()
        $.isFunction(data) ? data.call($) : $.popbox.showDialog(data, cls)
    }
    $.popbox.settings = {
        image_types: ['png', 'jpg', 'jpeg', 'gif'],
        popbox_html: '<div id="popbox" style="display:none;"><div class="popup"><table><tbody><tr><td class="tr"/><td class="b"/><td class="tl"/></tr><tr><td class="b"/><td class="body" ' + getPopupWidth() + ' ><div class="notitle"></div><div class="content" ></div><div class="footer"><span class="caption"></span><a id="close" href="#" class="close"></a></div></td><td class="b"/></tr><tr><td class="br"/><td class="b"/><td class="bl"/></tr></tbody></table></div></div>'
        //popbox_html: '<div id="popbox" style="display: none;"><div class="popup"><table><tbody><tr><td class="tr" /><td class="b" /><td class="tl" /></tr><tr><td class="b" /><td class="body" ' + getPopupWidth() + '><div class="notitle"></div><div class="content"></div></td><td class="b" /></tr><tr><td class="b" /><td class="tfoot" ><div><a id="close" href="#" class="close"></a></div></td><td class="b" /></tr><tr><td class="br" /><td class="b" /><td class="bl" /></tr></tbody></table></div></div>'
    }
    $.popbox.loading = function() {
        if ($('#popbox .loading').length == 1) return true

        $('#popbox .content').empty()
        $('#popbox .body').children().hide().end().
      append('<div class="loading"><div class="popbox_loading"></div></div>')

        var pageScroll = $.popbox.getPageScroll()
        $('#popbox').css({
            top: pageScroll[1] + ($.popbox.getPageHeight() / 10),
            left: pageScroll[0]
        }).show()

        $(document).bind('keydown.popbox', function(e) {
            if (e.keyCode == 27) $.popbox.close()
        })
        if (popboxMask) {
            $('#backgroundPopup').css({
            "opacity": "0.7"
            });
            $('#backgroundPopup').fadeIn("slow");
        }
    }

    $.popbox.showDialog = function(data, cls) {
        if (cls) $('#popbox .content').addClass(cls)
        $('#popbox .content').append(data)
        $('#popbox .loading').remove()
        $('#popbox .body').children().fadeIn('normal')
    }

    // open file into dialog
    $.popbox.openFile = function(url, cls) {
        //$.popbox.init(settings)
        $.popbox.loading(true)
        this.href = url;
        $.get(this.href, function(data) { $.popbox.showDialog(data, cls) })

        return false
    }

    $.popbox.close = function() {
        if (popboxMask) {
            $("#backgroundPopup").fadeOut("slow");
        }

        $(document).trigger('close.popbox')
        return false
    }

    $(document).bind('close.popbox', function() {
        $(document).unbind('keydown.popbox')
        $('#popbox').fadeOut(function() {
            $('#popbox .content').removeClass().addClass('content')
        })
    })

    $.fn.popbox = function(settings) {
        $.popbox.init(settings)

        var image_types = $.popbox.settings.image_types.join('|')
        image_types = new RegExp('\.' + image_types + '$', 'i')

        function click_handler() {
            $.popbox.loading(true)


            // support for rel="popbox[.inline_popup]" syntax, to add a class
            var cls = this.rel.match(/popbox\[\.(\w+)\]/)
            if (cls) cls = cls[1]

            // div
            if (this.href.match(/#/)) {
                var url = window.location.href.split('#')[0]
                var target = this.href.replace(url, '')
                $.popbox.showDialog($(target).clone().show(), cls)

                // image
            } else if (this.href.match(image_types)) {
                var image = new Image()
                image.onload = function() {
                    $.popbox.showDialog('<div class="image"><img src="' + image.src + '" /></div>', cls)
                }
                image.src = this.href

                // ajax
            } else {
                $.get(this.href, function(data) { $.popbox.showDialog(data, cls) })
            }

            return false
        }

        this.click(click_handler)
        return this
    }

    $.popbox.init = function(settings) {

        if ($.popbox.settings.inited) {
            return true
        } else {
            $.popbox.settings.inited = true
        }

        if (settings) $.extend($.popbox.settings, settings)
        $('body').append($.popbox.settings.popbox_html)

        var preload = [new Image(), new Image()]
        preload[0].src = $.popbox.settings.close_image
        preload[1].src = $.popbox.settings.loading_image

        $('#popbox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
            preload.push(new Image())
            preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
        })

        $('#popbox .close').click($.popbox.close)
        //$('#popbox .close_image').attr('src', $.popbox.settings.close_image)
    }

    // getPageScroll() 
    $.popbox.getPageScroll = function() {
        var xScroll, yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
        } else if (document.body) {// all other Explorers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft;
        }
        return new Array(xScroll, yScroll)
    }

    // adapter from getPageSize() 
    $.popbox.getPageHeight = function() {
        var windowHeight
        if (self.innerHeight) {	// all except Explorer
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowHeight = document.body.clientHeight;
        }
        return windowHeight
    }
})(jQuery);

