(function($){
	
    $.fn.showPopup = function(options){
        var defaults = {
            zIndex: 1080,
            opacity: 0.7,
            closeButtonClass: ['close'],
            overlayDuration: 250,
            popupDuration: 500,
            autoClose: false,
            noOverlay: false,
            removeOnHide: false,
            confirmPopup: false,
            confirmOK: '',
            dockPanel: false, //1 = top left, 2 = top right, 3 = bottom left, 4 = bottom right, default = center
            offset: {
                x: 0,
                y: 0
            },
            dockOffset: 15,			
            onShow: null,
            onHide: null,
            onDestroy: null,
            onConfirm: null			
        };
		
        return this.each(function(){
            
            var jpopup = $(this),
            jwindow = $(window),
            jhtml = $('body'),
            initScrollTop = jwindow.scrollTop(),
            opt = $.extend(defaults, options);
			
            if(!opt.noOverlay){
                var overlay = $('<div></div>').css({
                    'display': 'block',
                    'visibility': 'visible',
                    'position': 'absolute',
                    'top': 0,
                    'left': 0,
                    'width': Math.max(jwindow.width(), jhtml.innerWidth()),
                    'height': Math.max(jwindow.height(), jhtml.innerHeight()),
                    'zIndex': opt.zIndex,
                    'backgroundColor': '#000',
                    'opacity': 0
                }).appendTo(document.body);	
				
                jpopup.insertAfter(overlay);
            }	
			
            var rePosition = function(){
                var winWidth = jwindow.width(),
                winHeight = jwindow.height(),
                popupWidth = jpopup.width(),
                popupHeight = jpopup.height(),
                top = Math.max(0, (winHeight - popupHeight)/2),
                left = Math.max(0, (winWidth - popupWidth)/2);	
					
                if(opt.dockPanel){
                    switch (opt.dockPanel){
                        case 1: //top left
                            top = left = opt.dockOffset;
                            break;
						
                        case 2: //top right
                            top = opt.dockOffset;
                            left = Math.max(0, winWidth - popupWidth - opt.dockOffset);
                            break;
						
                        case 3: //bottom left
                            top = Math.max(0, winHeight - popupHeight - opt.dockOffset);
                            left = opt.dockOffset;
                            break;
						
                        case 4: //bottom right
                            top = Math.max(0, winHeight - popupHeight - opt.dockOffset);
                            left = Math.max(0, winWidth - popupWidth - opt.dockOffset);
                            break;
						
                        default: //same as center
                            break;
                    }
                }
				
                return {
                    top: top, 
                    left: left
                };
            }, windowScroll = function(){
                if(opt.el){
                    var newpos = rePosition();					
                    jpopup.css({
                        'top': newpos.top,
                        'left': newpos.left
                    });
                }
                else{
                    if($.browser.msie && parseInt($.browser.version) < 7){
                        var newpos = rePosition();
                        jpopup.css({
                            'position': 'absolute',
                            'top': newpos.top + $(window).scrollTop()
                        });
						
                    }else if(jwindow.height() < jpopup.outerHeight(true) || jwindow.width() < jpopup.outerWidth(true)){
                        jpopup.css({
                            'position': 'absolute',
                            'top': initScrollTop
                        });
                    }else{
                        if(jpopup.css('position') != 'fixed'){
                            var newpos = rePosition();
							
                            jpopup.css({
                                'position': 'fixed',
                                'top': newpos.top
                            });
                        }
                    }
                }
            }, windowResize = function(){
                var newpos = rePosition();
				
                if($.browser.msie && parseInt($.browser.version) < 7){
                    jpopup.css({
                        'position': 'absolute',
                        'top': newpos.top + $(window).scrollTop()
                    });
                }else if(opt.el){
                    jpopup.css({
                        'top': newpos.top,
                        'left': newpos.left
                    });
                }
                else{
                    jpopup.css({
                        'position': ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) ? 'absolute' : 'fixed',
                        'top': newpos.top,
                        'left': newpos.left
                    });	
                }
				
				
				
                if(overlay){
                    overlay.css({	
                        'width': jhtml.innerWidth(),
                        'height': jhtml.innerHeight()
                    });
                }
            }, closePopup = function(){
					
                if(opt.onHide){
                    opt.onHidecall(jpopup);
                }
				
                if($.browser.msie && parseFloat($.browser.version) < 9){
                    setTimeout(function(){
                        if(opt.removeOnHide){
                            jpopup.remove();
                        }else{
                            jpopup.css('top', -7000);
                        }
                    }, opt.popupDuration / 3);
					
                }else{
                    jpopup.stop(true).fadeTo(opt.popupDuration, 0, function(){
                        if(opt.removeOnHide){
                            jpopup.remove();
                        }else{
                            jpopup.css('top', -7000);
                        }
                    });
                }
				
                if(overlay){
                    overlay.stop(true).fadeTo(opt.overlayDuration, 0, function(){
                        overlay.remove();
						
                        if(opt.onDestroy){
                            opt.onDestroy.call(jpopup);
                        }
                    });
                }
				
                jwindow.unbind('scroll.jpopupev', windowScroll);
                jwindow.unbind('resize.jpopupev', windowResize);
				
                return false;
            };
			
            if(opt.dockPanel != 5){
                jwindow.bind('scroll.jpopupev', windowScroll);
                jwindow.bind('resize.jpopupev', windowResize);
            }
			
            var closeBtn = jpopup.find('.' + opt.closeButtonClass[0]);
            if(closeBtn.length){
                closeBtn.unbind('click.jpopupev').bind('click.jpopupev', closePopup);
				
                if(opt.closeButtonClass.length > 1){
                    jpopup.find('.' + opt.closeButtonClass.slice(1).join(',.')).click(function(){
                        closeBtn.trigger('click');
                        return false;
                    });
                }
            }
			
            if(overlay && !opt.confirmPopup){
                overlay.unbind('click.jpopupev').bind('click.jpopupev', closePopup);
            }
			
            var pos = rePosition();
			
            jpopup.css({
                'position': ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) || opt.dockPanel == 5? 'absolute' : 'fixed',
                'top': pos.top,
                'left': pos.left,
                'zIndex': opt.zIndex + 1
            });
			
            if(overlay){
                overlay.stop(true).fadeTo(opt.overlayDuration, opt.opacity);
            }
			
            if(opt.onShow){
                opt.onShow.call(jpopup);
            }
			
            if($.browser.msie && parseFloat($.browser.version) < 9){
                setTimeout(function(){
                    jpopup.css('display', 'block');
                }, opt.popupDuration / 3);
            }else{
                jpopup.css('opacity', 0).stop(true).fadeTo(opt.popupDuration, 1);
            }
        });
    };
    
})(jQuery);





