(function($)
{
    var $this = $(this),
        s = {
            f_speed: 350,
            f_easing: 'swing',
            c_type: 'html',
            c_content: {
                html: '<p>No content to show!</p>',
                file: '',
                width: 0,
                height: 0,
                flashvars: {},
                params: {},
                attributes: {}
            }
        },
        p_top = 0,
        p_left = 0,
        active = false;
        locked = false;
    
    var methods = {
        init:function(options)
        {
            return this.each(function()
            {                                
                // combine passed options with defaults
                if(options) $.extend(s, options);
                
                // rebind links
                $('.jPop_html')
                    .unbind('click')
                    .click(function()
                    {
                        $(this).jPop({
                            'c_type': 'html',
                            'c_content': {
                                'html': $(this).attr('rel')
                            }
                        });
                        return false;
                    });
                    
                $('.jPop_id')
                    .unbind('click')
                    .click(function()
                    {
                        $(this).jPop(
                        {
                            'c_type': 'id',
                            'c_content': {
                                'html': $('#'+$(this).attr('rel')).html()
                            }
                        });
                        return false;
                    });
                    
                $('.jPop_hide')
                    .unbind('click')
                    .click(function()
                    {
                        $(this).jPop('hide');
                        return false;
                    });
                
                // ensure body has relative position    
                $('body').css('position', 'relative');
                
                // create jQuery background object     
                var background = $("<div></div>")
                    .attr("id", "jPop_background")
                    .width($(document).width())
                    .height($(document).height())
                    .css({
                        'position': 'absolute',
                        'top': 0,
                        'left': 0,
                        'background-color': '#000',
                        'z-index': 1,
                        'filter': 'alpha(opacity=60)',
                        'opacity': 0.6,
                        'display': 'none'
                    })
                    .click(function()
                    {
                        $(this).jPop('hide');   
                    });
                    
                // create jQuery container object
                var container = $("<div></div>")
                    .attr("id", "jPop_container")
                    .css({
                        'position': 'absolute',
                        'z-index': 2,
                        'display': 'none'
                    });    
                    
                // append objects to body
                $('body').append(background).append(container);
                
                // handle window scroll
                $(window).scroll(function()
                {
                    $(this).jPop('scroll');
                });
                
                // handle screen resize
                $(window).resize(function()
                {    
                    $(this).jPop('resize');                 
                });
                
                // handle keyups
                $(window).keyup(function(e)
                {
                    if((!locked) && (e.keyCode == 27)) $(this).jPop('hide');;       
                });
            });    
        },
        show:function(options)
        {
            return this.each(function()
            {
                if(locked)
                {
                    return false;
                }
                else
                {
                    // combine passed options with defaults
                    if(options) $.extend(s, options);                    
                    
                    active = true;
                    locked = true;

                    // insert content
                    switch(s.c_type)
                    {
                        case 'swf':
                            // add container for swfobject to DOM then let swfobject work its magic
                            $('#jPop_container')
                                .html($("<div></div>")
                                .attr('id', 'swf_container')); 
                            swfobject.embedSWF(s.c_content.file, 'swf_container', s.c_content.width, s.c_content.height, "10.0.0", "/flash/expressInstall.swf", s.c_content.flashvars, s.c_content.params, s.c_content.attributes);
                        break;
                            
                        case 'html':
                        case 'id':
                        default:
                            $('#jPop_container').html(s.c_content.html);
                        break;
                    }
                                        
                    // fade in background
                    $('#jPop_background').fadeIn(s.f_speed, s.f_easing, function()
                    {                                            
                        // set popup size and position then show it
                        $('#jPop_container')
                            .jPop('resize')
                            .fadeIn(s.f_speed, s.f_easing);
                                                
                        // unlock jPop
                        locked = false;
                    });
                }
            });
        },
        hide:function(options)
        {
            return this.each(function()
            {
                // combine passed options with defaults
                if(options) $.extend(s, options);
                
                $('#jPop_container').fadeOut(s.f_speed, s.f_easing); 
                $('#jPop_background').fadeOut(s.f_speed, s.f_easing, function()
                {
                    active = false;
                });    
            });  
        },
        resize:function(options)
        {
            return this.each(function()
            {
                if(active)
                {
                    // combine passed options with defaults
                    if(options) $.extend(s, options);

                    $('#jPop_background')
                        .width('100%')
                        .height('100%');
                        
                    $('#jPop_container')
                        .css({
                            'left': ($(window).width() / 2) - ($('#jPop_container').width() / 2) + parseInt($(document).scrollLeft()),
                            'top': ($(window).height() / 2) - ($('#jPop_container').height() / 2) + parseInt($(document).scrollTop())
                        });
                        
                    $('#jPop_background')
                        .width($(document).width())
                        .height($(document).height());
                }
                else
                {
                    return false;
                }
            });
        },
        scroll:function(options)
        {
            return this.each(function()
            {
                if(active)
                {
                    // combine passed options with defaults
                    if(options) $.extend(s, options);
                          
                    // swfobject fix 
                    if(s.c_type == 'swf') jQuery.noData= {};
                                    
                    $('#jPop_container')
                        .animate({
                            'left': ($(window).width() / 2) - ($('#jPop_container').width() / 2) + parseInt($(document).scrollLeft()), 
                            'top': ($(window).height() / 2) - ($('#jPop_container').height() / 2) + parseInt($(document).scrollTop())
                        }, {
                            queue: false,
                            duration: s.f_speed,
                            easing: s.f_easing
                        });
                }
                else
                {
                    return false;
                }
            });
        }
    } 
    
    $.fn.jPop = function(method)
    {
        if(methods[method])
        {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }  
        else if(typeof method === 'object' || !method)
        {
            return methods.show.apply(this, arguments);
        }
        else
        {
            $.error('Method '+method+' does not exist in jPop');
        }
    };    
})(jQuery);
