(function($) {                
	$.fn.popup = function(options) {
		
  		var jQueryMatchedObj = this;
  
		function _initialize() {
		    _start(this,jQueryMatchedObj);
		    return false;
		}
  
		function _start(objClicked, jQueryMatchedOb) {
			
			var imageLink = objClicked.getAttribute('href');
			
			$('embed, object, select, .pointer').css({ 'visibility' : 'hidden' });
			$('body').append('<div id="popup-background"></div><div id="popup-frame-container"><div id="popup-frame"><div id="popup-frame-controlbox"><img id="popup-frame-close" class="right" src="/pub/images/_close.gif" /><img id="popup-frame-print" class="right" src="/pub/images/_printer.gif" /></div><img id="popup-frame-image" src="/pub/images/_loading.gif" width="32" height="32" /></div></div>');
		
			var arrPageSizes = ___getPageSize();
			$('#popup-background').css({
				position:			'absolute',
				backgroundColor:	'#000',
				opacity:			0.75,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1],
				left:				0,
				top:				0,
				zIndex:				990
			}).fadeIn();
						
			var arrPageScroll = ___getPageScroll();
			$('#popup-frame-container').css({
				position:			'absolute',
				top:				arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:				arrPageScroll[0],
				width: 				'100%',
				zIndex: 			999,
				'text-align':		'center'
			});
				
			$('#popup-frame-controlbox').css({
				height:				'26px',
				backgroundColor:	'#fff'
			});
				
			$('#popup-frame-print, #popup-frame-close').css({
				cursor:				'pointer',
				margin:				'12px 11px 0 0'
			});
				
			$('#popup-frame').css({
				backgroundColor:	'#fff',
				margin:				'0 auto',
				width:				375,
				height:				300
			});
				
			$('#popup-frame-image').css({
				width:				32,
				height:				32,
				margin:				'115px 0 0 0'
			});
				
			
			var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
				
				//alert( objImagePreloader.src );
				
				$('#popup-frame-image').css({ margin: '11px 11px 11px 11px' });								
				$('#popup-frame-image').attr('src', objImagePreloader.src );
				$('#popup-frame').animate({ width: objImagePreloader.width + 22 , height: objImagePreloader.height + 48 }, 'fast');
				$('#popup-frame-image').animate({ width: objImagePreloader.width , height: objImagePreloader.height }, 'fast');
				//$('#popup-frame-image').width( objImagePreloader.width ).height( objImagePreloader.height );
				objImagePreloader.onload=function(){};
			};
			objImagePreloader.src = imageLink;
				
			$('#popup-background,#popup-frame-container,#popup-frame-close').click(function() {
				_finish();									
			});
			$('#popup-frame-print').click(function(){
				$('#popup-frame').print();
				return false;	
			});
			
			$(window).resize(function() {
				var arrPageSizes = ___getPageSize();
				$('#popup-background').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				var arrPageScroll = ___getPageScroll();
				$('#popup-frame-container').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});	
		}
		
		function _finish() {
			$('#popup-frame-container').remove();
			$('#popup-background').fadeOut(function() { $('#popup-background').remove(); });
			$('embed, object, select, .pointer').css({ 'visibility' : 'visible' });
		}
				
		function ___getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		function ___getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
	
	return this.unbind('click').click(_initialize);
	};
})(jQuery);

// Create a jquery plugin that prints the given element.
jQuery.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
 
	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.
 
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
 
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);
 
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( "<style> #popup-frame-controlbox{ display: none; } </style>" );
	objDoc.write( "</head>" );
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
 
	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}
	
$(document).ready( function(){
   	$('.popup').popup();    	
});
