// JavaScript Document

function ImageryGroup()
{
	this.selected=false;
}

ImageryGroup.prototype.select=function(imagery)
{
	if(this.selected)
	{
		this.selected.rectangle.showUnselect();
	}
	
	this.selected=imagery;
	
	this.selected.rectangle.showSelect();
}

function DCIImagery(name,sw,ne,acquisitionTime,oMap)
{
	this.name=name;
	this.sw=sw;
	this.ne=ne;
	this.acquisitionTime=acquisitionTime;
	this.oMap=oMap;
	this.visible=true;
}

DCIImagery.prototype.fit=function()
{
	var bounds=new GLatLngBounds(this.sw,this.ne);
	
	this.oMap.setCenter(bounds.getCenter(),this.oMap.getBoundsZoomLevel(bounds));
}

DCIImagery.prototype.center=function()
{
	this.oMap.panTo(new GLatLng(this.ne.lat(),this.sw.lng()));
}

DCIImagery.prototype.showMarker=function()
{
	if(this.marker)
	{
		this.marker.show();
	}
	else
	{
		this.marker=new GMarker(new GLatLng(this.ne.lat(),this.sw.lng()));
		
		var oThis=this;
		
		GEvent.addListener(this.marker,'click',function()
		{
			oThis.openInfoWindow();
			
			if(oThis.menuItem)
			{
				menu.selectItem(oThis.menuItem);
			}
			
			imageryGroup.select(oThis);
		});
		
		GEvent.addListener(this.marker,'mouseover',function()
		{
			oThis.rectangle.highlight();
			
			if(oThis.menuItem)
			{
				// IE bug workaround: Take away style properties before scrolling to prevent entire page from shifting.
				var tmp=oThis.menuItem.className;

				oThis.menuItem.className='';
				
				oThis.menuItem.scrollIntoView(false);
				
				oThis.menuItem.className=tmp;
				
				menu.highlightItem(oThis.menuItem);
			}
			
		});
		
		GEvent.addListener(this.marker,'mouseout',function()
		{
			oThis.rectangle.lowlight();
			
			if(oThis.menuItem)
			{
				menu.lowlightItem(oThis.menuItem);
			}
		});
		
		this.oMap.addOverlay(this.marker);
	}
}

DCIImagery.prototype.hideMarker=function()
{
	if(this.marker)
	{
		this.marker.hide();
	}
}

DCIImagery.prototype.showRectangle=function()
{
	if(this.rectangle)
	{
		this.rectangle.show();
	}
	else
	{
		this.rectangle=new ImageOutline(this.sw,this.ne,'imageOutline_Normal','imageOutline_Highlighted','imageOutline_Selected');
		
		this.oMap.addOverlay(this.rectangle);
	}
}

DCIImagery.prototype.hideRectangle=function()
{
	if(this.rectangle)
	{
		this.rectangle.hide();
	}
}

DCIImagery.prototype.openInfoWindow=function()
{
	if(this.infoWindowElement)
	{
		this.marker.openInfoWindow(this.infoWindowElement);
	}
	else if(this.infoWindowTemplate)
	{
		var thisObj=this;
		
		this.infoWindowElement=this.infoWindowTemplate.cloneNode(true);
		
		var aSearch=new Array();
		var aReplace=new Array();
		
		aSearch[aSearch.length]='Label';
		aSearch[aSearch.length]='Fit';
		aSearch[aSearch.length]='Center';
		aSearch[aSearch.length]='Preview';
		aSearch[aSearch.length]='acquisitionTime';
		aSearch[aSearch.length]='files';
		
		// Name.
		aReplace[aReplace.length]=createSpanText(this.name);
		
		// Fit button.
		var fitButtonElement=document.createElement('div');
		
		aReplace[aReplace.length]=fitButtonElement;
		
		var oFitButton=new YAHOO.widget.Button({id:this.name+'_fit_button',type:'button',label:'Fit',container:fitButtonElement});
		
		oFitButton.addListener('click',function()
		{
			thisObj.fit();
		});
		
		oFitButton.addListener('onmouseover',function()
		{
			menu.highlightItem(thisObj.menuItem);
			
			thisObj.rectangle.highlight();
		});
		
		oFitButton.addListener('onmouseout',function()
		{
			menu.lowlightItem(thisObj.menuItem);
			
			thisObj.rectangle.lowlight();
		});
		
		// Center button.
		var centerButtonElement=document.createElement('div');
		
		aReplace[aReplace.length]=centerButtonElement;
		
		var oCenterButton=new YAHOO.widget.Button({id:this.name+'_center_button',type:'button',label:'Center',container:centerButtonElement});
		
		oCenterButton.addListener('click',function()
		{
			thisObj.center();
		});
		
		oCenterButton.addListener('onmouseover',function()
		{
			menu.highlightItem(thisObj.menuItem);
			
			thisObj.rectangle.highlight();
		});
		
		oCenterButton.addListener('onmouseout',function()
		{
			menu.lowlightItem(thisObj.menuItem);
			
			thisObj.rectangle.lowlight();
		});
		
		// Preview button.
		var previewButtonElement=document.createElement('div');
		
		aReplace[aReplace.length]=previewButtonElement;
		
		var previewMenuElementID='previewMenu_1';
		
		var counter=2;
		
		while(document.getElementById(previewMenuElementID))
		{
			previewMenuElementID='previewMenu_'+counter;
			
			counter++;
		}
		
		var previewMenu=new YAHOO.widget.Menu(previewMenuElementID);
		
		// Sometimes the preview menu is stranded when the info window is closed.
		// Make sure the preview menu is hidden when the info window is closed.
		var handle;
		
		previewMenu.showEvent.subscribe(function()
		{
			handle=GEvent.addListener(thisObj.oMap,'infowindowclose',function()
			{
				previewMenu.hide();
			});
		});
		
		previewMenu.hideEvent.subscribe(function()
		{
			GEvent.removeListener(handle);
		});
		
		// Add an empty item to the menu as a workaround.
		// Remove it after the other items have been added.
		previewMenu.addItem(' ');
		
		previewMenu.render(document.body);

		var oPreviewButton=new YAHOO.widget.Button({id:this.name+'_preview_button',type:'menu',menu:previewMenu,label:'Preview',container:previewButtonElement});
		
		oPreviewButton.addListener('click',function()
		{
			if(!thisObj.previewExists)
			{
				var handleSuccess=function(o)
				{
					if(o.responseText!==undefined)
					{
						var previews=eval('('+o.responseText+')');
						
						if(previews)
						{
							thisObj.previewExists=true;
							
							for(var index in previews)
							{
								var item=previewMenu.addItem('Preview '+(Number(index)+1));
								
								item.clickEvent.subscribe(function()
								{
									for(var index in previews)
									{
										if(Number(index)+1==this.index)
										{
											var image=document.createElement('img');
											
											image.setAttribute('src','crawler/previews/'+previews[index]['path']);
										
											var previewWindow=new PreviewWindow();
											
											previewWindow.show('Preview',image);
											
											previewWindow.setWH(previews[index]['width'],previews[index]['height']);
											
											break;
										}
									}
								});
							}
							
							// Remove empty menu item.
							previewMenu.getItem(0).destroy();
						}
						else
						{
							previewMenu.hide();
							
							alert('Preview not available.');
						}
					}
				}
				
				var handleFailure=function(o)
				{
					alert('Preview not available.');
				}
				
				YAHOO.util.Connect.asyncRequest('GET','crawler/services/getPreviews.php?productID='+thisObj.name,{success:handleSuccess,failure:handleFailure});
			}
		});
		
		// Acq. time.
		aReplace[aReplace.length]=document.createTextNode((this.acquisitionTime.getMonth()+1)+'/'+this.acquisitionTime.getDate()+'/'+this.acquisitionTime.getFullYear());
		
		// Files button.
		var filesButtonElement=document.createElement('div');
		
		aReplace[aReplace.length]=filesButtonElement;
		
		var oFilesButton=new YAHOO.widget.Button({id:this.name+'_files_button',type:'button',label:'Files',container:filesButtonElement});
		
		oFilesButton.addListener('click',function()
		{
			var treeHolder=document.createElement('div');
			
			var tree=new YAHOO.widget.TreeView(treeHolder);
			
			var handleSuccess=function(o)
			{
				if(o.responseText!==undefined)
				{
					var files=eval('('+o.responseText+')');
					
					if(files)
					{
						tree.subscribe('labelClick',function(node)
						{
							if(!node.hasChildren(false))
							{
								// Do something.
								var networkLocations=document.createElement('div');
								
								networkLocations.appendChild(document.createTextNode('Containing Folder'));
								
								networkLocations.appendChild(document.createElement('br'));
								
								var containingFolder=document.createElement('textarea');
								
								containingFolder.setAttribute('wrap','off');
								
								containingFolder.setAttribute('rows','2');
								
								containingFolder.style.width='590px';
								
								containingFolder.appendChild(document.createTextNode(node.href.replace(node.label,'')));
								
								networkLocations.appendChild(containingFolder);
								
								networkLocations.appendChild(document.createElement('br'));
								
								networkLocations.appendChild(document.createTextNode('File'));
								
								networkLocations.appendChild(document.createElement('br'));
								
								var fileLocation=document.createElement('textarea');
								
								fileLocation.setAttribute('wrap','off');
								
								fileLocation.setAttribute('rows','2');
								
								fileLocation.style.width='590px';
								
								fileLocation.appendChild(document.createTextNode(node.href));
								
								networkLocations.appendChild(fileLocation);
								
								var previewWindow=new PreviewWindow();
								
								previewWindow.show('Network Locations',networkLocations);
								
								previewWindow.setWH(600,200);
								
								return false;
							}
						});
						
						for(var i=0;i<files.length;i++)
						{
							var chunks=files[i].split('/');
							
							var currentNode=tree.getRoot();
							
							for(var j=0;j<chunks.length;j++)
							{
								var nodeExists=false;
								
								for(var k=0;k<currentNode.children.length;k++)
								{
									if(currentNode.children[k].label==chunks[j])
									{
										nodeExists=true;
										
										currentNode=currentNode.children[k];
										
										break;
									}
								}
								
								if(!nodeExists)
								{
									for(var l=j;l<chunks.length;l++)
									{
										if(l==chunks.length-1)
										{
											currentNode=new YAHOO.widget.TextNode({label:chunks[l],href:files[i].replace(/\//g,'\\')},currentNode,false);
										}
										else
										{
											currentNode=new YAHOO.widget.TextNode(chunks[l],currentNode,false);
										}
									}
									
									break;
								}
							}
						}
						
						tree.draw();
					}
					else
					{
					}
				}
			}
			
			var handleFailure=function(o)
			{
				alert('File retrieval failed.');
			}
			
			YAHOO.util.Connect.asyncRequest('GET','crawler/services/getFiles.php?productID='+thisObj.name,{success:handleSuccess,failure:handleFailure});
			
			var previewWindow=new PreviewWindow();
			
			previewWindow.show('Files',treeHolder);
			
			previewWindow.setWH(500,500);
		});
		
		this.parseTemplate(this.infoWindowElement,aSearch,aReplace);
		
		this.openInfoWindow();
	}
	else
	{
		var RPC=new RPCObj();
		
		var thisObj=this;
		
		RPC.call('gui_templates/infoWindow.php',function()
		{
			/* document.importNode is not supported on IE6. */
			if(document.importNode)
			{
				DCIImagery.prototype.infoWindowTemplate=document.importNode(this.getElementById('content'),true);
			}
			else
			{
				/*
				Using innerHTML as part of a workaround for IE6's lack of document.importNode.
				As of 2007.03.15, innerHTML is not actually a part of the W3C DOM specification or any standard.
				Limit the use of innerHTML.
				*/
				DCIImagery.prototype.infoWindowTemplate=document.createElement('div');
				
				DCIImagery.prototype.infoWindowTemplate.innerHTML=this.getElementById('content').innerHTML;
			}
			
			this.removeElement();
			
			thisObj.openInfoWindow();
		});
	}
}

DCIImagery.prototype.getMenuItem=function(className)
{
	var thisObj=this;
	
	this.menuItem=document.createElement('div');
	
	this.menuItem.className=className;
	
	GEvent.addDomListener(this.menuItem,'mouseover',function()
	{
		menu.highlightItem(thisObj.menuItem);
						   
		thisObj.rectangle.highlight();
	});
	
	GEvent.addDomListener(this.menuItem,'mouseout',function()
	{
		menu.lowlightItem(thisObj.menuItem);
						   
		thisObj.rectangle.lowlight();
	});
	
	setTimeout(function(){thisObj.createMenuItem();},1);
	
	return this.menuItem;
}

DCIImagery.prototype.createMenuItem=function()
{
	var thisObj=this;
	
	if(this.menuItemTemplate)
	{
		this.menuItem.innerHTML=this.menuItemTemplate.innerHTML;
		
		var aSearch=new Array();
		var aReplace=new Array();
		
		aSearch[aSearch.length]='Label';
		aSearch[aSearch.length]='Fit';
		aSearch[aSearch.length]='Center';
		aSearch[aSearch.length]='Details';
		aSearch[aSearch.length]='acquisitionTime';
		
		aReplace[aReplace.length]=createSpanText(this.name);
		
		aReplace[aReplace.length]=createSpanLink3('Fit',
		function()
		{
			thisObj.fit();
		},
		function()
		{
			menu.highlightItem(thisObj.menuItem);
			
			thisObj.rectangle.highlight();
		},
		function()
		{
			menu.lowlightItem(thisObj.menuItem);
			
			thisObj.rectangle.lowlight();
		});
		
		aReplace[aReplace.length]=createSpanLink3('Center',
		function()
		{
			thisObj.center();
		},
		function()
		{
			menu.highlightItem(thisObj.menuItem);
			
			thisObj.rectangle.highlight();
		},
		function()
		{
			menu.lowlightItem(thisObj.menuItem);
			
			thisObj.rectangle.lowlight();
		});
		
		aReplace[aReplace.length]=createSpanLink3('Details',
		function()
		{
			menu.selectItem(thisObj.menuItem);
			
			imageryGroup.select(thisObj);
			
			thisObj.openInfoWindow();
		},
		function()
		{
			menu.highlightItem(thisObj.menuItem);
			
			thisObj.rectangle.highlight();
		},
		function()
		{
			menu.lowlightItem(thisObj.menuItem);
			
			thisObj.rectangle.lowlight();
		});
		
		aReplace[aReplace.length]=document.createTextNode((this.acquisitionTime.getMonth()+1)+'/'+this.acquisitionTime.getDate()+'/'+this.acquisitionTime.getFullYear());
		
		this.parseTemplate(this.menuItem,aSearch,aReplace);
	}
	else
	{
		if(this.templateFetched)
		{
			setTimeout(function(){thisObj.createMenuItem();},1);
			
			return;
		}
		
		DCIImagery.prototype.templateFetched=true;
		
		var RPC=new RPCObj();
		
		RPC.call('gui_templates/menuItem.php',function()
		{
			/* document.importNode is not supported on IE6. */
			if(document.importNode)
			{
				DCIImagery.prototype.menuItemTemplate=document.importNode(this.getElementById('content'),true);
			}
			else
			{
				/*
				Using innerHTML as part of a workaround for IE6's lack of document.importNode.
				As of 2007.03.15, innerHTML is not actually a part of the W3C DOM specification or any standard.
				Limit the use of innerHTML.
				*/
				DCIImagery.prototype.menuItemTemplate=document.createElement('div');
				
				DCIImagery.prototype.menuItemTemplate.innerHTML=this.getElementById('content').innerHTML;
			}
			
			this.removeElement();
			
			thisObj.createMenuItem();
		});
	}
}

DCIImagery.prototype.parseTemplate=function(currentElement,aSearch,aReplace)
{
	if(currentElement)
	{
		var i=0;
		
		var currentElementChild=currentElement.childNodes[i];
		
		while(currentElementChild)
		{
			this.parseTemplate(currentElementChild,aSearch,aReplace);
			
			i++;
			
			currentElementChild=currentElement.childNodes[i];
		}
		
		if(currentElement.tagName)
		{
			for(var j=0;j<aSearch.length;j++)
			{
				if(currentElement.id==aSearch[j])
				{
					currentElement.parentNode.replaceChild(aReplace[j],currentElement);
				}
			}
		}
		
		return currentElement;
	}
}

DCIImagery.prototype.show=function()
{
	if(this.menuItem)
	{
		this.menuItem.style.display='';
	}

	this.showRectangle();
	
	this.showMarker();
	
	this.visible=true;
}

DCIImagery.prototype.hide=function()
{
	if(this.menuItem)
	{
		this.menuItem.style.display='none';
	}
	
	this.hideRectangle();
	
	this.hideMarker();
	
	this.visible=false;
}

function ImageOutline(sw,ne,className_Normal,className_Highlighted,className_Selected)
{
	if(!this.className_Normal)
	{
		ImageOutline.prototype.className_Normal=className_Normal;
		ImageOutline.prototype.className_Highlighted=className_Highlighted;
		ImageOutline.prototype.className_Selected=className_Selected;
		
		var monkey=new CSSMonkey();
		
		ImageOutline.prototype.offset_Normal=monkey.get('.'+this.className_Normal,'borderWidth').split(' ');
		ImageOutline.prototype.offset_Normal=this.dimToNum(this.offset_Normal[0]);
		
		ImageOutline.prototype.offset_Highlighted=monkey.get('.'+this.className_Highlighted,'borderWidth').split(' ');
		ImageOutline.prototype.offset_Highlighted=this.dimToNum(this.offset_Highlighted[0]);
		
		ImageOutline.prototype.offset_Selected=monkey.get('.'+this.className_Selected,'borderWidth').split(' ');
		ImageOutline.prototype.offset_Selected=this.dimToNum(this.offset_Selected[0]);
	}
	
	this.sw=sw;
	this.ne=ne;
	
	this.selected=false;
}

ImageOutline.prototype=new GOverlay();

ImageOutline.prototype.initialize=function(map)
{
	this.map=map;
	
	this.div=document.createElement('div');
	
	this.div.style.position='absolute';
	
	this.div.className=this.className_Normal;
	
	this.map.getPane(G_MAP_MAP_PANE).appendChild(this.div);
}

ImageOutline.prototype.highlight=function()
{
	if(!this.selected&&this.div.className==this.className_Normal)
	{
		this.div.className=this.className_Highlighted;
		
		this.redraw(true);
	}
}

ImageOutline.prototype.lowlight=function()
{
	if(!this.selected&&this.div.className==this.className_Highlighted)
	{
		this.div.className=this.className_Normal;
		
		this.redraw(true);
	}
}

ImageOutline.prototype.showSelect=function()
{
	this.selected=true;
	
	this.div.className=this.className_Selected;
		
	this.redraw(true);
}

ImageOutline.prototype.showUnselect=function()
{
	this.selected=false;
	
	this.div.className=this.className_Normal;
	
	this.redraw(true);
}

ImageOutline.prototype.remove=function()
{
	this.div.parentNode.removeChild(this.div);
}

ImageOutline.prototype.copy=function()
{
	return new Rectangle(this.bounds,this.className_Normal,this.className_Highlighted,this.className_Selected);
}

ImageOutline.prototype.redraw=function(force)
{
	if(!force)
	{
		return;
	}
	
	var c1=this.map.fromLatLngToDivPixel(this.sw);
	var c2=this.map.fromLatLngToDivPixel(this.ne);

	this.div.style.width=Math.abs(c2.x-c1.x)+'px';
	this.div.style.height=Math.abs(c2.y-c1.y)+'px';
	
	this.div.style.left=(Math.min(c2.x,c1.x))+'px';
	this.div.style.top=(Math.min(c2.y,c1.y))+'px';
	
	switch(this.div.className)
	{
		case this.className_Normal:
			this.div.style.left=(this.dimToNum(this.div.style.left)-this.offset_Normal)+'px';
			this.div.style.top=(this.dimToNum(this.div.style.top)-this.offset_Normal)+'px';
		break;
		case this.className_Highlighted:
			this.div.style.left=(this.dimToNum(this.div.style.left)-this.offset_Highlighted)+'px';
			this.div.style.top=(this.dimToNum(this.div.style.top)-this.offset_Highlighted)+'px';
		break;
		case this.className_Selected:
			this.div.style.left=(this.dimToNum(this.div.style.left)-this.offset_Selected)+'px';
			this.div.style.top=(this.dimToNum(this.div.style.top)-this.offset_Selected)+'px';
		break;
	}
}

ImageOutline.prototype.dimToNum=function(dim)
{
	return Number(dim.replace(/px/,''));
}

ImageOutline.prototype.show=function()
{
	this.div.style.display='';
}

ImageOutline.prototype.hide=function()
{
	this.div.style.display='none';
}

