// popupPDF.js
// 4/30/2010
// used to pop up a PDF in a Google Viewer, anywhere on the site.
//
// example usage:
// openPDF(this,'http://www.datafoundations.com/example.pdf');
//
function GetWidth()
{
	var x = 0;
	if (self.innerHeight)
	{
			x = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
			x = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
			x = document.body.clientWidth;
	}
	return x;
}
 
function GetHeight()
{
	var y = 0;
	if (self.innerHeight)
	{
			y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
			y = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
			y = document.body.clientHeight;
	}
	return y;
}
function openPDF2(PDFlocation){
	
}
function openPDF(PDFlocation){	
	if($.browser.msie && parseInt($.browser.version) <7) {
		window.location = PDFlocation;
		return;
	}
	var myExistingPDFdiv=document.getElementById('dfiPDFshade');
	if(myExistingPDFdiv==null) {
		var docWidth = GetWidth();
		var docHeight = GetHeight();
	
		var shade=document.createElement('div');
		shade.setAttribute('id','dfiPDFshade');
		shade.setAttribute('style','z-index: 999999;position:fixed; top:0; left:0; width:'+docWidth+'px; height: '+docHeight+'px;display:none;background:black;background:rgba(0,0,0,0.5);filter:alpha(opacity=50)');
		
		var myPDFdiv=document.createElement('div');
		myPDFdiv.setAttribute('id', 'dfiPDFdiv');
		myPDFdiv.setAttribute('style',' display:none;position: fixed; top:0; left:0; margin: 50px;  width: '+docWidth+'px; height: '+docHeight+'px;');
		
		var myPDFborder=document.createElement('div');
		myPDFborder.setAttribute('id', 'dfiPDFborder');
		myPDFborder.setAttribute('style', 'display:none;background: #444444; width: 100%; height:100%; border: solid 10px #444444; -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px;');
		
		var myPDFframeHolder=document.createElement('div');
		myPDFframeHolder.setAttribute('id','dfiPDFframeHolder');		
		myPDFframeHolder.setAttribute('style','display:none;width:'+(docWidth-120)+'px; height:' + (docHeight-145) + 'px;clear: both;border: none;');
		
		var myPDFframe=document.createElement('iframe');
		myPDFframe.setAttribute('id','dfiPDFframe');
		myPDFframe.setAttribute('style','width:100%; height:100%;border: none;');
		
		var myPDFcontrol=document.createElement('div');
		myPDFcontrol.setAttribute('id','dfiPDFcontrol');
		myPDFcontrol.setAttribute('style','display:none;height:25px; font-family: Arial; text-decoration:underline; color:white;');
		
		var myPDFnodeCloser=document.createElement('span');
		myPDFnodeCloser.setAttribute('style','display: block;float: right; color: white; font-weight: bold; margin: 0 0px;cursor:pointer;');
		myPDFnodeCloser.setAttribute('onclick','closePDF();');
		myPDFnodeCloser.innerHTML='&laquo; Return to DataFoundations.com';
		
		var myPDFDownloader=document.createElement('span');
		myPDFDownloader.setAttribute('style','display: block;float: left; color: white; font-weight: bold; margin: 0 0px;cursor:pointer;');
		myPDFDownloader.setAttribute('onclick','downloadPDF("'+PDFlocation+'");');
		myPDFDownloader.innerHTML='Save this document';
			
		myPDFcontrol.appendChild(myPDFDownloader);
		myPDFcontrol.appendChild(myPDFnodeCloser);
		myPDFborder.appendChild(myPDFcontrol);
		myPDFframeHolder.appendChild(myPDFframe);
		myPDFborder.appendChild(myPDFframeHolder);
		myPDFdiv.appendChild(myPDFborder);
		shade.appendChild(myPDFdiv);
		document.getElementsByTagName('body')[0].appendChild(shade);
		
		$('#dfiPDFdiv').css({'left':((docWidth-120)/2)+'px'});
		$('#dfiPDFdiv').css('display','none');
		$('#dfiPDFborder').css({'display':'none','width':'40px','height':'40px'});
		$('#dfiPDFcontrol').css('display','none');
		$('#dfiPDFframeHolder').css('display','none');
		$('#dfiPDFshade').fadeIn( 500, function () {
			$('#dfiPDFdiv').fadeIn(1000);
			$('#dfiPDFborder').fadeIn(1000);
			$('#dfiPDFborder').animate({
				width: (docWidth-120)+"px"
				}, 1000);				
			$('#dfiPDFdiv').animate({
					left: "0px"
				},1000,"swing",function () {
					$('#dfiPDFdiv').animate({
						height: (docHeight-100)+"px"
					},1000);
					$('#dfiPDFborder').animate({
						height: (docHeight-120)+"px"
					},1000,"swing",function() {	
						$('#dfiPDFcontrol').fadeIn(1000);
						$('#dfiPDFframeHolder').fadeIn(1000);
						myPDFframe.setAttribute('src','http://docs.google.com/viewer?url=' + encodeURIComponent(PDFlocation) + '&embedded=true');
					});
				});
		});
	} else {
		$('#dfiPDFshade').fadeIn(500);
	}	
}
function closePDF(){
	$('#dfiPDFshade').fadeOut(500);
}
function downloadPDF(PDFlocation){
	window.open(PDFlocation);
}