#!/cygdrive/c/strawberry/perl/bin/perl -w

# Need to specify the full path of Perl above, e.g. for Windows something like
#!C:\\Perl32\\bin\\perl -w

use strict;
my $iis6_mode = 0;

if($iis6_mode){
	#Change into cgi-bin directory
	chdir("cgi-bin");
}

eval("require \"gsdlCGI.pm\"");
if ($@)
{
    print STDOUT "Content-type:text/plain\n\n";
    print STDOUT "ERROR: $@\n";
    exit 0;
}

sub display_output
{
	my($docID,$c,$assocfilepath,$site) = @_;
	
	my $iframeURL = "library/collection/$c/document/$docID"."?p.showAssocFilePath=1";
	
	#Render an html page to display and export item.
	my $html = <<EOT;
<html>
<head>
	<title>Export Document to Expeditee</title>
	<base href=".."/>

	<link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet"/>
	<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
	<script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
	<script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
	<script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
	
	<style type="text/css">
		body{
			font-family: Arial;
			font-size: 16px;
			color: #000;
	}
	</style>
	<script type="text/javascript">
		function load(){
	
			var iframe = document.getElementById("iframe");
			
			if(iframe.style.display != "block"){
				iframe.height = "90%";
				iframe.style.display = "block";
			}
			
			var iframeDoc;
			
			if(iframe.contentDocument){
				/* Firefox and Chrome */
				iframeDoc = iframe.contentDocument;
			}else if(iframe.contentWindow){
				/* IE */
				iframeDoc = iframe.contentWindow.document;
			}
			
			var gsContent = iframeDoc.getElementById("gs_content");
			var xmlUrl = iframe.src + "&o=xml";
			
			var frameNum = getMetadata(xmlUrl,'frameID');
			
			if(frameNum === null){
				//use -1 as default, to change later.
				frameNum = -1;
			}
			
			//Check for assocfilepath
			var assocElem = iframeDoc.getElementById('assocfilepath');
			var assoc = null;
						
			if(assocElem === undefined || assocElem === null){
				assoc = getMetadata(xmlUrl,'assocfilepath');
			}
			
			var expFrameTree = htmlToExpeditee(gsContent);
			var expFrame = JSON.stringify(expFrameTree);
			
			var site = "$site";
			var url = "cgi-bin/export-individual-expeditee.pl";
			var params = "c=" + "$c";
			
			if(site.match(/\\w/)){
				params += "&site=" + site;
			}
			
			params += "&a=generate-frame&fn=" + frameNum;
			params += "&json=" + escape(expFrame);
			
			//Add an assocfilepath param 
			if(assoc !== null){
				params += "&assoc=" + assoc;
			}
			
			var request = urlPostSync(url,params);
			
			alert("Document has been successfully exported to Expeditee. The corresponding Expeditee frame can be found in the collection's export directory at: " + frameNum + ".exp");
			
		}
		
		function getMetadata(xmlUrl,nameValue){
	
			var metadata = null;
		
			\$.ajax({
				type: "GET",
				async: false,
				url: xmlUrl,
				dataType: "xml",
				success: function(xml){
								
				\$(xml).find('metadata').each(function(){
					
					var name = \$(this).attr('name');
					
					if(name === nameValue){
						
						if(metadata === null){
							metadata = \$(this).text();
							//console.log(metadata);
						}
					
					}	
				});
				
			}
			
		});
		
		return metadata;
		}
	</script>
	
	<body>
	
		<p id="msg">Exporting following document to Expeditee... </p>
	
	<hr style="margin: 10px;"/>
	
	<!-- Load up an iframe to display URL in-->
	<div align="center"><iframe id="iframe" width="1000px" height="90%" src="$iframeURL" onload="load();"></iframe></div>
	</body>
	</html>
EOT
	print "Content-type:text/html\n\n";
	print $html;
}

sub main
{
	my $gsdl_cgi = new gsdlCGI();
	
	#Load needed GSDL modules
	$gsdl_cgi->setup_gsdl();
	
	my $gsdlhome = $ENV{'GSDLHOME'};
	$gsdl_cgi->checked_chdir($gsdlhome);

	require cgiactions::ExpediteeExportAction;
	$gsdl_cgi->parse_cgi_args();
	
	$gsdl_cgi->{'xml'} = 0;
	
	my $fn = $gsdl_cgi->param("fn");
	
	if(defined $fn){
		
		my $site = $gsdl_cgi->clean_param("site");
		my $c = $gsdl_cgi->clean_param("c");
		my $a = $gsdl_cgi->clean_param("a");	#assocfilepath
		
		my $json = $gsdl_cgi->clean_param("json");
		
		my $action = new ExpediteeExportAction($gsdl_cgi,$iis6_mode);
		$action->do_action();
		
	}else{
		
		my $docID = $gsdl_cgi->clean_param("docID");
		my $c = $gsdl_cgi->clean_param("c");
		my $assocfilepath = $gsdl_cgi->clean_param("assocfilepath");
		my $site = $gsdl_cgi->clean_param("site");

		if(!defined $site){
			$gsdl_cgi->generate_error("No site specified.");
		}
		
		display_output($docID,$c,$assocfilepath,$site);
	}
}

&main();