/* $Rev: 3634 $ */
if (jQuery) (function($) {
	$.extend($.fn, {
		popup:function(c, p) {
			var popupobj = {
				openPopup:function() {
					if (!propeties.scroll) {
						this.hiddenBodyScroll();
					}
					if (propeties.modal) {
						this.lockScreen();
					} else {
						this.showPopup();
					}
				},
				closePopup:function() {
					if (!propeties.scroll) {
						this.showBodyScroll();
					}
					if (propeties.modal) {
						this.unLockScreen();
					} else {
						this.hiddenPopup();
					}
				},
				stopScroll:function(event) {
					event = event || window.event; 
					var target = $(event.target);
					var elid = elobj.attr('id');
					if ((target.attr('id') == elid) || (target.parents('#' + elid).length != 0)) {
					    event.returnValue = false;
					    return true;
					}
					if (event.preventDefault) {
						event.preventDefault();
					}
				    event.returnValue = false;
				    return false;
				},
				showBodyScroll:function() {
					$(window).unbind('DOMMouseScroll');
					$(window).unbind('mousewheel');
					$(document).unbind('mousewheel');
					$(document).unbind('keypress');
					$('html').css('overflow', 'auto');
				},
				hiddenBodyScroll:function() {
					$(window).bind('DOMMouseScroll', this.stopScroll);
					$(window).bind('mousewheel', this.stopScroll);
					$(document).bind('mousewheel', this.stopScroll);
					$(document).bind('keypress', this.stopScroll);
					$('html').css('overflow', 'hidden');
				},
				lockScreen:function() {
					var screenHeight = this.getDocumentHeight();
					var ls = $('#popup-lockscreen:first');
					var ils = $('#popup-ilockscreen:first');
					if (ls.length == 0) {
						ls = $('<div id="popup-lockscreen"></div>').css('display', 'none').appendTo('body');
						ils = $('<iframe id="popup-ilockscreen"></iframe>').css('display', 'none').appendTo('body');
					}
					ls.css(this.getCSSLock(screenHeight, propeties.color, propeties.zIndex + 5, propeties.opacity * 100));
					ils.css(this.getCSSLock(screenHeight, propeties.color, propeties.zIndex, 0));
					if (!$.browser.msie) {
						ls.css('opacity', '0');
						ils.css('opacity', '0');
					}
					ils.css('display', 'block');
					this.showPopup();
					if ($.browser.msie) {
						ls.css('display', 'block');
					} else {
						ls.css('display', 'block').fadeTo(propeties.speed, propeties.opacity);
					}
				},
				unLockScreen:function() {
					var ls = $('#popup-lockscreen:first');
					var ils = $('#popup-ilockscreen:first');
					var pthis = this;
					if ($.browser.msie) {
						pthis.hiddenPopup();
						ls.css('display', 'none');
						ils.css('display', 'none');
					} else {
						ls.fadeTo(propeties.speed, 0, function() {
							pthis.hiddenPopup();
							ls.css('display', 'none');
							ils.css('display', 'none');
						});
					}
				},
				showPopup:function() {
					var wnd = $(window), doc = $(document);
					var postype = 'fixed';
					var top = ((wnd.height() - elobj.height()) / 2);
					if ($.browser.msie && $.browser.version < 7) {
						postype = 'absolute';
						top = top + doc.scrollTop();
					}
					elobj.css({
						'position':postype,
						'top':top + 'px',
						'left':((wnd.width() - elobj.width()) / 2) + 'px',
						'z-index':propeties.zIndex + 10
					});
					elobj.css('display', 'block');
				},
				hiddenPopup:function() {
					elobj.css('display', 'none');
				},
				getDocumentHeight:function() {
					var height = 0;
					if ($.browser.msie && $.browser.version < 7) {
						var scrollHeight = Math.max(
							document.documentElement.scrollHeight,
							document.body.scrollHeight
						);
						var offsetHeight = Math.max(
							document.documentElement.offsetHeight,
							document.body.offsetHeight
						);
						if (scrollHeight < offsetHeight) {
							height = $(window).height();
						} else {
							height = scrollHeight;
						}
					} else {
						height = $(document).height();
					}
					return height + 'px';
				},
				getCSSLock:function(height, color, zindex, opacity) {
					return {
						'position':'absolute',
						'top':'0',
						'left':'0',
						'right':'0',
						'bottom':'0',
						'border':'none',
						'width':'100%',
						'height':height,
						'background-color':color,
						'-ms-filter':'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + opacity + ')',
						'filter':'progid:DXImageTransform.Microsoft.Alpha(opacity = ' + opacity + ')',
						'filter':'alpha(opacity=' + opacity + ')',
						'moz-opacity':'0',
						'-khtml-opacity':'0',
						'z-index':zindex
					};
				}
			};
			var propeties = this.data('popup_propeties');
			if ((null == propeties) || ('undefined' == typeof propeties)) {
				propeties = {
					modal:true,
					color:'#000000',
					opacity:0.5,
					speed:'fast',
					scroll:false,
					zIndex:1000
				};
			}
			if ((typeof c == 'object') && (typeof p == 'undefined')) {
				$.extend(propeties, c);
			}
			if ((typeof c == 'string') && (typeof p == 'object')) {
				$.extend(propeties, p);
			}
			this.data('popup_propeties', propeties);
			if (typeof c != 'string') {
				return true;
			}
			elobj = this;
			switch (c) {
				case 'open':
					popupobj.openPopup();
					break;
				case 'close':
					popupobj.closePopup();
					break;
			}
			return false;
		}
	});
})(jQuery);
