(function($){
	$.fn.BDFCenterObject = function(opts){
		var defaults = {
			tX: 0,
			tY: 0
			},
			opts = $.extend(defaults, opts);
			
		return this.each(function(){
			var $obj = $(this);
			
			$(window).bind({
				resize: function(e){
					$obj.css({
						marginLeft: $(window).width()/2 - $obj.width()/2 + opts.tX,
						marginTop: $(window).height()/2 - $obj.height()/2 + opts.tY
					});
				}
			}).trigger('resize');
		});
	};
	
	$.fn.BDFShadowBoxAd = function(opts){
		var defaults = {},
			opts = $.extend(defaults, opts),
			$screen = $('<div/>',{
				id: 'shadowScreen',
				css: {
					width: $(window).width(),
					height: $(window).height(),
					backgroundColor: '#000000',
					opacity: .55,
					position: 'fixed',
					top: 0,
					left: 0,
					zIndex: 1000,
					opacity: 0,
					cursor: 'pointer'
				}
			}).prependTo('body').delay(500).animate({
				opacity: .75
			}, 500);
			
			$(window).bind({
				resize: function(e){
					$screen.css({
						width: $(window).width(),
						height: $(window).height()
					});
				}
			});
		
		return this.each(function(){
			var $ad = $(this).css({
					display: 'block',
					position: 'fixed',
					border: '6px solid #ffffff',
					zIndex: 1001,
					opacity: 0,
					top: 0,
					left: 0
				}).BDFCenterObject().delay(1000).animate({
					opacity: 1
				}, 750, function(){
					$screen.bind({
						click: function(e){
							$ad.remove();
							$screen.remove();
							$(window).unbind('resize');
							$('#shadowClose').remove();
						}
					});
				});
				
				var $cB = $('<span/>',{
					text: 'close',
					id: 'shadowClose',
					css: {
						position: 'fixed',
						background: '#ffffff',
						padding: '5px 10px 5px 10px',
						zIndex: 1002,
						fontSize: 12,
						fontWeight: 'bolder',
						cursor: 'pointer',
						opacity: 0,
						top: 0,
						left: 0
					},
					click: function(){
						$ad.remove();
						$screen.remove();
						$(window).unbind('resize');
						$cB.remove();
					}
				}).prependTo('body');
				
				$(window).bind({
					keydown: function(e){
						if(e.keyCode==27){
							$ad.remove();
							$screen.remove();
							$(window).unbind('resize');
							$cB.remove();
						}
					}
				});
				
				$cB.BDFCenterObject({tX: $ad.outerWidth()/2-$cB.width()-3, tY: -$ad.outerHeight()/2+$cB.height()+3}).delay(1250).animate({
					opacity: 1
				});

		});
	};
})(jQuery);

$(function(){
	if($('.popupItem').length>0){

			$(window).load(function(){
				$('.popupItem').BDFShadowBoxAd();
			});
	}
});
