var CountryPage
var CountryName
var CountryID
var MetaName
var MetaData
var ProcName
var ProcData
var NumScenes
var QLPage
var SelectedQLnum = 0
var HelpPage
var Arr = new Array(200)
var ProcArr = new Array(200)
var RemarkString

function developer()
{
	var developer ="<HTML><HEAD><TITLE>Image 2000</TITLE></HEAD>"
	    developer+="<BODY BGCOLOR='#C0C0FF'>"
	    developer+="<B>Developer information</B><BR>"
	    developer+="Image 2000 viewer (ver. 1.02) by Per Syrén<BR>" 
	    developer+="Cataloque system and maps, by Lars Nilsson<BR>" 
	    developer+="Satellus AB Kiruna, Sweden, Nov. 2000<BR>"
	    developer+="within the Image 2000 project<BR>"
	    developer+="<BR><BR><A HREF='javascript:opener.CreateHelpPage()'><B>Back to help</B></A><BR>".small()
	    developer+="</BODY></HTML>"

	HelpPage.document.write(developer)
	HelpPage.document.close()
}

function help()
{
	if(HelpPage && !HelpPage.closed)
		HelpPage.close()

	HelpPage=window.open("", "help", "width=350,height=250, alwaysRaised")
	
	CreateHelpPage()
}

function CreateHelpPage()
{	
	var helpfile ="<HTML><HEAD><TITLE>HELP Image 2000</TITLE></HEAD>"
	    helpfile+="<BODY BGCOLOR='#C0C0FF'>"
	    helpfile+="<B>HELP on Image 2000</B><BR><BR>"
	    helpfile+="Step 1".bold()+"<BR>" 
	    helpfile+="Select a country from the list to the right<BR>"
	    helpfile+="to open a specific country map<BR>"
	    helpfile+="Step 2".bold()+"<BR>"  
	    helpfile+="Point to a image node on the country map<BR>"
	    helpfile+="to open a specific image and the related metadata"
	    helpfile+="<BR><BR><A HREF='javascript:void(0)' onClick='window.close()'><B>Close</B></A><BR>".small()
	    helpfile+="<A HREF='javascript:opener.developer()'><B>Develop info</B></A><BR>".small()
	    helpfile+="</BODY></HTML>"

	HelpPage.document.write(helpfile)
	HelpPage.document.close()

}

function CheckOnClose(type)
{
	if(type == 1 && HelpPage && !HelpPage.closed){ 
		HelpPage.close()	
	}
	if(type == 1 && QLPage && !QLPage.closed){
		QLPage.close()
	}
	if(type == 1 && CountryPage && !CountryPage.closed){
		CountryPage.close()
	}
	if(type == 2 && QLPage && !QLPage.closed){
		QLPage.close()
	}
}

function show(s)
{
	window.status = s
	return true
}
function clear()
{
	window.status = ""
	return true
}

function loadcountry(ID)
{
	if(CountryPage && !CountryPage.closed )
		CountryPage.close()

	CountryID   = ID;
	CountryName = get_CountryName(ID)
	MetaName    = ID+"/"+ID+"_rawqlmeta.txt"
	ProcName    = ID+"/"+ID+"_processmeta.txt"
	PosName     = ID+"/"+ID+"_mapposition.txt"

	//Collect the mapposition data
	document.readerApplet.setFileName(PosName)
	var posdata = document.readerApplet.fetchPosdata("HREF")

	CountryPage=window.open("", ID, "width=711,height=580, resizable=yes, menubar")//540+171 =  711

	//Define HTML Code that generate the side
	var thepage=""
	thepage+="<HTML>\n<HEAD>\n<TITLE>"+CountryName+" IMAGE 2000</TITLE>\n"
	thepage+="</HEAD>\n<BODY BGCOLOR='#C0C0FF' onLoad='moveTo(0,0)' onUnload='opener.CheckOnClose(2)'>\n"
	thepage+="<FORM NAME = 'reader'>\n"
	thepage+="<TABLE BORDER=1 WIDTH='671' HEIGHT='500'>\n"
	thepage+="<TR><TD WIDTH='80%' BGCOLOR='silver'>\n"
	thepage+="<P ALIGN='CENTER'><B>Image 2000 - "+CountryName+"</B></TD>\n"
	thepage+="<TD ALIGN='RIGHT' WIDTH='20%' BGCOLOR='silver'><INPUT TYPE='button' VALUE='Close' onClick='window.close()'></TD>\n"
	//thepage+="<TR><TD COLSPAN=2>\n"
	thepage+="<TR><TD>\n"
	//thepage+="<IMG SRC="+CountryID+"/"+CountryID+"_coverage.gif WIDTH=500 HEIGHT=500 BORDER=0 ISMAP USEMAP='#NOMSCENEMAP'>\n"
	thepage+="<IMG SRC="+CountryID+"/"+CountryID+".gif WIDTH=500 HEIGHT=500 BORDER=0 ISMAP USEMAP='#NOMSCENEMAP'>\n"
	thepage+="<TD>\n"
	thepage+="<IMG SRC=legend/legend.gif WIDTH=171 HEIGHT=500 BORDER=0>\n"
	thepage+="</TD></TR></TABLE>\n"
	
//Gintaras
//	thepage+="<A HREF="+ID+"/"+ID+"_info.html target='_blank'>Additional information about "+CountryName+"</A><BR>"
	thepage+="<A HREF='info/"+ID+"_info.html' target='_blank'>Additional information about "+CountryName+"</A><BR>"
	
	thepage+="<MAP NAME='NOMSCENEMAP'>\n"
	thepage+= posdata
	thepage+="</MAP>\n"
	thepage+="</FORM>\n"
	thepage+="</BODY>\n"
	thepage+="</HTML>"

	CountryPage.document.write(thepage)
	CountryPage.document.close()
}


function get_CountryName(ID)
{
	var country=document.readerApplet.getCountryName(ID)
	//alert(country);
	return country
}



function qlinfo(sceneID)
{

	if(QLPage && !QLPage.closed )
		QLPage.close()

	QLPage=window.open("", "sceneID", "width=580,height=550,menubar")
	
	//alert(MetaName.toLowerCase() +" : "+sceneID)

	document.readerApplet.setFileName(MetaName);
	MetaData=""
	MetaData+= document.readerApplet.fetchMetadata(sceneID)
	NumScenes= document.readerApplet.getNumItems()

	var separator = ";"
	Arr = MetaData.split(separator)

	//alert(MetaData);
	//alert(NumScenes);
	
	SelectedQLnum=0	
	CreateQLPage(SelectedQLnum)
}

function CreateQLPage(n)
{
	RemarkString=""
	var status = 0
	
	//alert(Arr[0+n])
	//alert(Arr[23+n])
	
	//See if the current scene is selected
	if((Arr[0+n].toUpperCase().indexOf("available".toUpperCase())) == 0)
	{
		status = 0
		
	}
	if((Arr[0+n].toUpperCase().indexOf("rejected".toUpperCase())) == 0)
	{
		status = 0
		
	}
	
	else if((Arr[0+n].toUpperCase().indexOf("selected".toUpperCase())) == 0)
	{
		status = 1
		
		if(((Arr[23+n].toUpperCase().indexOf("processed".toUpperCase())) == 0)||
		   ((Arr[23+n].toUpperCase().indexOf("rejected".toUpperCase())) == 0)  )
		{
	//	alert(ProcName)
	//	alert(Arr[3+n])
		status = 2
		document.readerApplet.setFileName(ProcName)
		ProcData=""
		ProcData+= document.readerApplet.fetchMetadata(Arr[3+n])
		ProcArr = ProcData.split(";")
	//	alert(ProcArr)
		}	
		
	}
	
	
	
	
	
	//Define HTML Code that generate the html page
	var thepage=""
	var buttons=""

	thepage+="<HTML>\n<HEAD>\n<TITLE>"+CountryName+" IMAGE 2000</TITLE>\n"
	thepage+="<SCRIPT LANGUAGE='javascript'>\n"
	thepage+="</SCRIPT>"
	thepage+="<STYLE TYPE='text/css'>\n"
	thepage+=".clsLarge {font-size:large;font-weight:bold}\n"
	thepage+=".clsSmall {font-size:10pt}\n"
	thepage+=".clsBoldSmall {font-size:10pt;font-weight:bold}\n"
	thepage+=".clsBoldSmallBlue {font-size:10pt;color:blue;font-weight:bold}\n"
	thepage+=".clsRed {color:red;font-weight:bold}\n"		
	thepage+="</STYLE>\n"
	thepage+="</HEAD>\n<BODY BGCOLOR='#C0C0FF'>\n"

	if(parseInt(NumScenes) > 1)
	{
	thepage+="<SPAN CLASS='clsRed'>"			
	thepage+="Toggle between images with the buttons 'NEXT' and 'PREVIOUS'\n"	
	thepage+="</SPAN>"
	}	

	thepage+="<FORM>"
	thepage+="<TABLE WIDTH=560 BORDER = 1>\n"
	thepage+="<TR><TD BGCOLOR='silver' WIDTH=360><SPAN CLASS='clsLarge'>"
	thepage+="Information on image "+(SelectedQLnum+1)+"("+NumScenes+")"
	thepage+="</SPAN></TD><TD BGCOLOR='silver' WIDTH=200 ALIGN='right'>\n"

	if(parseInt(NumScenes) > 1)
	{
		thepage+="<INPUT TYPE='button' VALUE='Previous' onClick='opener.prev()'>\n"
		thepage+="<INPUT TYPE='button' VALUE='  Next  ' onClick='opener.next()'>\n"
	}

	thepage+="<INPUT TYPE='button' VALUE='Close'onClick='window.close()'>\n"
	thepage+="</TABLE></TD></TR>\n"

	//var ql     ="<TD><IMG SRC="+CountryID+"/"+Arr[1+n]+" ALT="+Arr[3+n]+" WIDTH=400 HEIGHT=400></TD>"
	var ql     ="<TD><IMG SRC=quicklooks/"+Arr[1+n]+" ALT="+Arr[3+n]+" WIDTH=400 HEIGHT=400></TD>"

	var labels ="<TD VALIGN='TOP' WIDTH=80>"
	    labels+="<SPAN CLASS='clsBoldSmallBlue'>"
	    labels+="Metadata<BR>"
	    labels+="</SPAN>"	
	    labels+="<SPAN CLASS='clsBoldSmall'>"
	    
	    
	    
	    if(status < 2){
			labels+="Mission<BR>"
		}
			
	    labels+="Scene ID<BR>"
	    labels+="Date<BR>"
	    
	    if(status < 2 ){
		    labels+="Time GMT<BR>"
			labels+="Station<BR>"
			}
			
        labels+="Cloud UL<BR>"
        labels+="Cloud UR<BR>"
        labels+="Cloud LL<BR>"
        labels+="Cloud LR<BR>"
        
	    if(status < 2){        
			labels+="Sun Angle<BR>"
			labels+="Sun Azim.<BR>"
			labels+="Geo.datum<BR>"    
		}
		else
			labels+="Proj.<BR>"    	
			

	    labels+="ULX<BR>ULY<BR>URX<BR>URY<BR>LRX<BR>LRY<BR>LLX<BR>LLY"
	    labels+="<SPAN CLASS='clsBoldSmallBlue'>"
		labels+="<BR>Status"
		labels+="</SPAN>"	
		labels+="<SPAN CLASS='clsBoldSmall'>"
	    	
	    	
		//Processed
	    if(status == 2){
		
		labels+="<BR>Ordered"
		labels+="<BR>Recieved"		    
		labels+="<BR>Orthorect"		    
		labels+="<BR>QA"
		labels+="<BR>Rejected_by"	
		labels+="<BR>Delivered"
		labels+="<BR>Accepted"
		labels+="</SPAN>"
		
		labels+="<SPAN CLASS='clsBoldSmallBlue'>"
		labels+="<BR><BR>Details"
		labels+="</SPAN>"			
	    }	

	    

	var data ="<TD VALIGN='TOP' WIDTH=80><SPAN CLASS='clsSmall'>"
	    
	    if(status < 2){
			data+="<BR>"+Arr[2+n]    //Mission
	    }
	    
	    data+="<BR>"+Arr[3+n]        //ID
	    data+="<BR>"+Arr[4+n]        //Date
	    
	    if(status < 2){
			data+="<BR>"+Arr[5+n]    //Time
		    data+="<BR>"+Arr[6+n]    //Station
		}
	    
	    data+="<BR>"+Arr[7+n]+"%"    //Clouds UL
	    data+="<BR>"+Arr[8+n]+"%"    //Clouds UR
	    data+="<BR>"+Arr[9+n]+"%"    //Clouds LL
	    data+="<BR>"+Arr[10+n]+"%"   //Clouds LR
		
		if(status < 2){
		    data+="<BR>"+Arr[13+n]+"\°"  //Sun Angle
			data+="<BR>"+Arr[12+n]+"\°"  //Sun Azimuth
		}
		
		if(status < 2)
		{
			data+="<BR>"+Arr[14+n]       //Geo Datum
			data+="<BR>"+Arr[15+n].slice(0,10)+"\°"  //ULX
			data+="<BR>"+Arr[16+n].slice(0,10)+"\°"  //ULY
			data+="<BR>"+Arr[17+n].slice(0,10)+"\°"  //URX
			data+="<BR>"+Arr[18+n].slice(0,10)+"\°"  //URY
			data+="<BR>"+Arr[19+n].slice(0,10)+"\°"  //LRX
			data+="<BR>"+Arr[20+n].slice(0,10)+"\°"  //LRY
			data+="<BR>"+Arr[21+n].slice(0,10)+"\°"  //LLX
			data+="<BR>"+Arr[22+n].slice(0,10)+"\°"  //LLY			
		}
	    else
	    {
			data+="<BR>"+ProcArr[2]      //Projection
		    data+="<BR>"+ProcArr[3].slice(0,10)+" m"  //ULX
			data+="<BR>"+ProcArr[4].slice(0,10)+" m"  //ULY
			data+="<BR>"+ProcArr[5].slice(0,10)+" m"  //URX
			data+="<BR>"+ProcArr[6].slice(0,10)+" m"  //URY
			data+="<BR>"+ProcArr[7].slice(0,10)+" m"  //LRX
			data+="<BR>"+ProcArr[8].slice(0,10)+" m"  //LRY
			data+="<BR>"+ProcArr[9].slice(0,10)+" m"  //LLX
			data+="<BR>"+ProcArr[10].slice(0,10)+" m"  //LLY
		}

		//The current status
    	data+="<SPAN CLASS='clsBoldSmall'>"
    	if(status < 2)
			data+="<BR>"+Arr[0+n]
		else			
			data+="<BR>"+ProcArr[0]
			
		data+="</SPAN>"	
		data+="<SPAN CLASS='clsSmall'>"

		
		if(status == 2)
		{
		
			data+="<BR>"+ProcArr[12]    //Ordered  date
			data+="<BR>"+ProcArr[13]    //Recieved date	
			data+="<BR>"+ProcArr[14]    //Orthocor date	
			data+="<BR>"+ProcArr[15]    //QA       date	
			data+="<BR>"+ProcArr[11]    //Rejected date
			data+="<BR>"+ProcArr[16]    //Deliver  date	
			data+="<BR>"+ProcArr[17]    //Accepted date	
			data+="<BR><BR><A HREF='info/details.html'>Link</A>"
			RemarkString=ProcArr[18]
	    }	

        data+="</SPAN></TD>"	

	thepage+="<TABLE BORDER = 1>\n"
	thepage+="<TR>\n"
	thepage+=labels+data+ql
	thepage+="</TR>\n"
	thepage+="</TABLE>"
	thepage+="<TABLE BORDER = 0>\n"
	thepage+="<TR>\n"
	thepage+="<TD VALIGN='TOP' WIDTH=160><B>Project Manager's remark: </B></TD>\n"
	thepage+="<TD VALIGN='TOP' WIDTH=400>"+RemarkString+"</TD>\n"
	thepage+="</TR>\n"
	thepage+="</TABLE>"

	thepage+="</FORM>"
	thepage+="</HTML>"

	QLPage.document.write(thepage)
	QLPage.document.close()

}


function next()
{
	if((SelectedQLnum+1) < parseInt(NumScenes)){
		SelectedQLnum++	
		CreateQLPage(SelectedQLnum*24)
	}

}

function prev()
{
	if(SelectedQLnum > 0){
		SelectedQLnum--		
		CreateQLPage(SelectedQLnum*24)
	}
}

