(function( $ ){
	var overlay;
	var box;
	var close;
	var overlayHeight;
	var o;
	var image = new Image();
	var image_append;
	var swf_append;
	
	$.teaserpop = function(opts){ $.fn.teaserpop(opts); };
	
	$.teaserpop.show = function() {
		
		if ($.browser.msie && !$.support.opacity && $.browser.version < 7) {
			$('select').css({'visibility': 'hidden'});
		}
		overlayHeight = $(document).height();
		
		overlay = $('<div id="teaserpop_overlay"></div>');
		overlay.css({
			'width':$(window).width(),
			'height':overlayHeight,
			'background-color':o.overlay_color
		}).hide().appendTo('body');
		
		close = $('<div class="teaserpop_close"><a href="javascript:void(0)"></a></div>');
		
		box = $('<div id="teaserpop_box"><div class="teaserpop_content"></div></div>');
		box.css({
			'width': '50px',
			'height': '50px',
			'background-color':o.box_bg_color,
			'padding':o.box_padding,
			'display':'none',
			'MozBorderRadius': o.box_border_radius, 
			'WebkitBorderRadius': o.box_border_radius,
			'BorderRadius': o.box_border_radius,
			'border-radius': o.box_border_radius,
			'z-index':'99999997'
		}).appendTo('body').addClass('teaserpop_spinner');
		box.append(close);
		
		$.teaserpop.center(box);
		
		$('.teaserpop_close').click($.teaserpop.close);
		overlay.click($.teaserpop.close);

		overlay.css({'display':'block'}).fadeTo(o.overlay_fade_speed, o.overlay_opacity, function() {
			box.fadeIn('fast', function() {
				$.teaserpop.load();
			});
		});
	};
	
	$.teaserpop.load = function () {
		
		switch (o.type) {
				case 'img' :
					$(image).one("load",function(){
						$.teaserpop.fade_img();
					}).error(function(){
						$(box).removeClass('teaserpop_spinner').animate({ 
								'width' : "200px", 
								'height' : "35px",
								'top' : (Math.max(( $(window).height() - 35 ) / 2+$(window).scrollTop())) + "px",
								'left' : (Math.max(( $(window).width() - 200 ) / 2+$(window).scrollLeft())) + "px"
								}, 500, function() {
									if ($.browser.msie){close.show();}else{close.fadeIn('fast');}
									$( '.teaserpop_content' ).html('<p class="teaserpop_error">'+o.error_message+'</p>');
						}).css('overflow', 'visible');
					});
					image.src = o.src;				
				break;
				case 'swf' :
					swf_append = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'+ o.swf_width +'" height="'+ o.swf_height +'"><param name="movie" value="'+o.src+'" /><param name="wmode" value="transparent" /><param name="menu" value="false" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="'+o.src+'" menu="false" wmode="transparent" width="'+ o.swf_width +'" height="'+ o.swf_height +'"><!--<![endif]--><p><a href="http://www.adobe.com/go/getflashplayer">You need Flash Player. Download it here.</a></p><!--[if !IE]>--></object><!--<![endif]--></object>';

					box.removeClass('teaserpop_spinner').animate({ 
						'width' : o.swf_width + "px", 
						'height' : o.swf_height + "px",
						'top' : (Math.max(( $(window).height() - o.swf_height ) / 2+$(window).scrollTop())) + "px",
						'left' : (Math.max(( $(window).width() - o.swf_width ) / 2+$(window).scrollLeft())) + "px"
						}, 500, function() {
					
						if ($.browser.msie){close.show();}else{close.fadeIn('fast');}
						$( '.teaserpop_content' ).html(swf_append);
					}).css('overflow', 'visible');
				break;
		}
		
	};
	$.teaserpop.fade_img = function () {
			
			image_append = $("<img />").attr({
				'id' : 'teaserpop_img',
				'border':'0',
				'src' : o.src
			}).hide().appendTo( '.teaserpop_content' );
			
			if (o.href!=="") {
				image_append.wrap('<a href="' + o.href + '" target="' + o.target + '"></a>');
			}

			
			var img_width = $( image_append ).width();
			var img_height = $( image_append ).height();
			
			box.removeClass('teaserpop_spinner').animate({ 
					'width' : (Math.max(img_width)) + "px", 
					'height' : (Math.max(img_height)) + "px",
					'top' : (Math.max(( $(window).height() - img_height ) / 2+$(window).scrollTop())) + "px",
					'left' : (Math.max(( $(window).width() - img_width ) / 2+$(window).scrollLeft())) + "px"
					}, 500, function() {
				if ($.browser.msie){close.show();}else{close.fadeIn('fast');}
				image_append.fadeIn(500);
			}).css('overflow', 'visible');
					
					
	};
	$.teaserpop.center = function (element) {
		$(element).css({ 
					'top' : ( $(window).height() - $(element).height() ) / 2+$(window).scrollTop() + "px", 
					'left' : ( $(window).width() - $(element).width() ) / 2+$(window).scrollLeft() + "px"
					});
	};
	
	$.teaserpop.center_animate = function (element) {
		$(element).stop(true,true).animate({ 
							'top' : (Math.max(( $(window).height() - $(element).height() ) / 2+$(window).scrollTop())) + "px", 
							'left' : (Math.max(( $(window).width() - $(element).width() ) / 2+$(window).scrollLeft())) + "px"
							}, 200);
	};

	$.teaserpop.close = function() {
		close.hide();
		box.fadeOut('fast', function() {
		  $(this).remove();
			if ($.browser.msie && !$.support.opacity && $.browser.version < 7) {
				$('select').css({'visibility': 'visible'});
			}
		  $(window).unbind("resize");
				overlay.fadeOut('fast', function() {
				  $(this).remove();
				});
		});
	};

	$.teaserpop.resize = function() {
			 if (overlay.is(':visible')) {
				overlay.css({
					'width':$(window).width(),
					'height':$(document).height(), 
					'top':'0', 
					'left':'0'
				});
				$.teaserpop.center_animate(box);
			}
	};
	
	
	$.fn.teaserpop = function( options ) { 

		var defaults = {
				src: 'teaserpop/uploads/3462661544_579ea2e9f3.jpg',
				type: 'img',
				href: '',
				target: '_blank',
				swf_width: 0,
				swf_height: 0,
				overlay_color: '#000000',
				overlay_opacity: 0.8,
				overlay_fade_speed: 500,
				box_padding: 20, // no px
				box_bg_color: '#FFFFFF',
				box_border_radius: 0,
				error_message: "Error... Can't find the file..."
		};
		o = $.extend(defaults, options);
		
		if ($.browser.msie && !$.support.opacity && $.browser.version < 7) {			
			$(window).scroll(function() {
				if (overlay.is(':visible')) {
					overlay.css({
						width: $(window).width(), 
						height: $(window).height(), 
						top: $(window).scrollTop(), 
						left: $(window).scrollLeft()
					});
				}
			});
		}
		$(window).resize(function() { $.teaserpop.resize(); });
		$.teaserpop.show();		
  };
})(jQuery);
