$j(document).ready(function() {
	//logic to remove back to tops / mio index when unessecary
	var mit = $j('#moreInfoTab'),
		miti = $j('#moreInfoTabIndex'),
		mita = $j('#moreInfoTab a.top');
	//show the mio tab before load so we can get accurate height data, we'll hide it when we're done
	mit.css({ position:'relative' }).show();
	//store the original height
	var mioOriginalHeight = parseInt(mit.css('height'));
	//make the mio tab full height (no scroll)
	mit.css({'height':'auto'});
	//hide all of the back to top links and mio index
	miti.hide();
	mita.hide();
	//now get the height and see if the tab is short, ie. non-scrolling
	if (mit.height() > mioOriginalHeight) {
		//if it's still tall, ie. scrolling, then turn the index/top links back on
		miti.show();
		mita.show();	
	}
	//we're done, reset the tab height and hide it
	mit.css({'height':mioOriginalHeight+'px'}).hide();
	
	var embedPDF = false,
		embedType = 'google';

	if (Prototype.Browser.IE) {
		if (pipwerks.pdfUTILS.detect.pluginFound()) {
			embedPDF = true;
			embedType = 'plugin';
		}
	} else {
		embedPDF = true;
	}

	if (embedPDF) {
		//create the overlay div if it doesn't exist, else reuse
		if ($j('#pdf_wrap').length == 0) {
			var wrapper = $j('<div />')
				.attr({ id: 'pdf_wrap' })
				.addClass('simple_overlay')
				.css({
					top: '50px',
					width:'850px', 
					height:'510px' 
				});
			var content = $j('<div />')
				.attr({ id: 'pdf_content' });
			var close = $j('<div />')
				.css({
					position:'relative', 
					textAlign:'center' 
				});
			var closeLink = $j('<a/>')
				.attr({ href: 'javascript://' })
				.addClass('btmClose')
				.click(function() {
					$j('a.pdfviewer').each(function() {
						$j(this).overlay().close();
					});
					return false;
				})
				.html('Close Window');
				
			closeLink.appendTo(close);
			content.appendTo(wrapper);
			close.appendTo(wrapper);
			wrapper.appendTo('body');
		}
	
		$j('a.pdfviewer')
			.css({
				background: 'url(/_img/pdf.gif) no-repeat',
				paddingLeft: '25px'
			})
			.overlay({
				top: 50,
				target: '#pdf_wrap',
				onBeforeLoad: function(e) {
					$j('#pdf_wrap').hide();
					$j('#pdf_content').html('');

					var w = $j(window).width();
					var h = $j(window).height();					
					var url = $j(this.getTrigger()).attr('href');
					
					if (url.match(/.pdf$/)) {
						if (embedType == 'plugin') {
							var newEmbed = $j('<embed />')
								.attr({
									'src':url,
									'width':'820px', //(w-130)+
									'height':(h-150)+'px'
								})
								.css({
									border:'1px solid #333'
								});
							newEmbed.appendTo($j('#pdf_content'));
						} else {
							var newDiv = $j('<div />')
								.css({
									position:'relative' 
								});
							var newImg = $j('<img />')
								.attr('src', '/_img/doc_viewer_logo.jpg')
								.css({
									position:'absolute', 
									top:'1px',
									left:'1px'
								});
							var newImg2 = $j('<img />')
								.attr('src', '/_img/doc_viewer_cover.jpg')
								.css({
									position:'absolute', 
									top:'1px',
									right:'1px'
								});
							var newIFrame = $j('<iframe />')
								.attr({
									'src':'http://docs.google.com/viewer?url='+escape(url)+'&embedded=true',
									'width':'100%',
									'height':(h-150)+'px'
								})
								.css({
									border:'1px solid #666' 
								});			
							newImg.appendTo(newDiv);
							newImg2.appendTo(newDiv);
							newIFrame.appendTo(newDiv);
							newDiv.appendTo($j('#pdf_content'));
						}
					}
					
					$j('#pdf_wrap').css({ width:'850px', height:(h - 120) + 'px' }).show(); //(w - 100) + 
				}
			});
	} else {
		$j('a.pdfviewer').attr('target', '_blank');
	}
});

var pipwerks = pipwerks || {};

pipwerks.pdfUTILS = {
	termFound: function (strToSearch, term){
		return (strToSearch.indexOf(term) !== -1);
	},	

	detect: {		
		hasReaderActiveX: function (){
			var axObj = null;
			
			if (window.ActiveXObject) {	
				try {
					axObj = new ActiveXObject("AcroPDF.PDF");
				} catch(err) {}
					
				if(!axObj){					
					try {
						axObj = new ActiveXObject("PDF.PdfCtrl");			
					} catch(err) {}
				}
				
				if (axObj !== null) { 					
					return true;					
				}		
			}
			
			return false;			
		},

		hasReader: function (){		
			var termFound = pipwerks.pdfUTILS.termFound;
			var n = navigator.plugins;
			
			for(var i=0; i < n.length; i++){				
				var name = n[i].name;
				
				if(termFound(name, "Adobe Reader") || termFound(name, "Adobe PDF") || termFound(name, "Acrobat")){					
					return true;	
				}
			}
			
			return false;
		},

		//Detects PDF plugin, regardless of manufacturer
		hasGeneric: function (){			
			var plugin = navigator.mimeTypes["application/pdf"];
			return (plugin && plugin.enabledPlugin);		
		},

		pluginFound: function (){
			var type = null;
			var detect = pipwerks.pdfUTILS.detect;
			
			if(detect.hasReader() || detect.hasReaderActiveX()){				
				type = "Adobe";			
			} else if(detect.hasGeneric()) {			
				type = "generic";			
			}
			
			return type;
		}
	}	
};
