var mapHeight=434;
var mapWidth=1000;
var photoWidth=300;
var photoHeight=420;
var panUnit=20;
var searchPushpin=null;
var currentPushpin=null;
var currentPreview=null;

VE_MapControl.emptyTile="http://virtualearth.msn.com/i/spacer.gif";

function fnValidateShowOblique()
{
	if(document.getElementById("showOblique").checked)
	{
		fnLoadOblique();		
		document.getElementById("previewPanel").style.display="block";
	}
	else
	{
		//Clear pins
		map.ClearPushpins();
		currentPushpin=null;
		fnAddSearchPushpin();
		//Close overview
		document.getElementById("previewPanel").style.display="none";
		currentPreview=null;
	}
}

function fnGetOtherObject(selectedId)
{
	var theOtherObj = null;
	if(document.getElementById("movetogether").checked)
	{
		if(selectedId == "before")
		{
			theOtherObj=getRawObject("after");
		}
		else if(selectedId == "after")
		{
			theOtherObj=getRawObject("before");	
		}
	}
	
	return theOtherObj;
}

function fnPan(direction, eid)
{
	var theObj = getRawObject(eid);
	var theOtherObj=fnGetOtherObject(eid);
	if(direction == "up")
	{
		shiftBy(theObj, 0, -1*panUnit);
		if(theOtherObj)
		{
			shiftBy(theOtherObj, 0, -1*panUnit);
		}
	} else if(direction == "down")
	{
		shiftBy(theObj, 0, panUnit);
		if(theOtherObj)
		{
			shiftBy(theOtherObj, 0, panUnit);
		}
	} else if(direction == "left")
	{
		shiftBy(theObj, panUnit, 0);
		if(theOtherObj)
		{
			shiftBy(theOtherObj, panUnit, 0);
		}
	} else if(direction == "right")
	{
		shiftBy(theObj, -1*panUnit, 0);
		if(theOtherObj)
		{
			shiftBy(theOtherObj, -1*panUnit, 0);
		}
	}
}

function fnGetMouseX()
{
	return window.event.pageX?window.event.pageX:window.event.clientX+document.body.scrollLeft;
}
function fnGetMouseY()
{
	return window.event.pageY?window.event.pageY:window.event.clientY+document.body.scrollTop;
}
function fnInitPictoPan()
{
	document.getElementById("photopopupPanel").style.cursor="move";
// 	window.status=panEnabled;
	lastMouseX=fnGetMouseX();
	lastMouseY=fnGetMouseY();
//	window.status=lastMouseX + ", " + lastMouseY;
	panEnabled = true;
}

function fnEndPictoPan()
{
	panEnabled = false;
//	window.status=panEnabled;
	document.getElementById("photopopupPanel").style.cursor="hand";
}

function fnTrackPictoPan()
{
	if(panEnabled)
	{
//		window.status=panEnabled;
		//Get x, y
		var tempX=fnGetMouseX();
		var tempY=fnGetMouseY();

		//Now drag the image
		var currentTop=document.getElementById("before").style.top;
		var currentLeft=document.getElementById("before").style.left;

		var deltaTop =currentTop-tempY;
		var deltaLeft=currentLeft-tempX;

//		window.status=deltaLeft + ", " + deltaTop;

		fnShiftBy(deltaLeft, deltaTop);
	}

}

function fnGoFullScreen()
{
	var theObj = document.getElementById("photopopupPanel");
	theObj.style.top=15;
	theObj.style.left=15;
	theObj.style.width=fnGetWindowWidth() - 30;
	theObj.style.height=fnGetWindowHeight() - 30;

	photoWidth=fnGetWindowWidth()/2-120;
	photoHeight=fnGetWindowHeight()-100;
	
	document.getElementById("photopopupContent_Before").style.width=photoWidth;
	document.getElementById("photopopupContent_Before").style.height=photoHeight;
	document.getElementById("photopopupContent_After").style.width=photoWidth;
	document.getElementById("photopopupContent_After").style.height=photoHeight;
	
	document.getElementById("after").style.width=2000;
	document.getElementById("before").style.width=2000;
}

function fnWindowRestore()
{
	var theObj = document.getElementById("photopopupPanel");
	
	theObj.style.left=10;
	theObj.style.top=48;
	theObj.style.width=790;
	theObj.style.height=480;
	
	photoWidth=320;
	photoHeight=420;
	
	document.getElementById("photopopupContent_Before").style.width=photoWidth;
	document.getElementById("photopopupContent_Before").style.height=photoHeight;
	document.getElementById("photopopupContent_After").style.width=photoWidth;
	document.getElementById("photopopupContent_After").style.height=photoHeight;
}

function fnLoadOblique()
{
	if(!document.getElementById("showOblique").checked)
		return;
	if(obdatReady)
	{
		fnAddObliquePushpins("N");
	}
	else
	{
		window.setTimeout("fnLoadOblique();", 500);
	}
}

function fnCloseObliqueImage()
{
	var displaypanelname="photopopupPanel";

	if(displaypanelname && document.getElementById(displaypanelname))
	{
		if(document.getElementById(displaypanelname).style)
		{
			document.getElementById(displaypanelname).style.display='none';
		}
	}
}

function fnShowObliqueImage(photoid)
{
	fnWindowRestore();
	currentPhoto = photoid;
	var displaypanelname="photopopupPanel";
	var imgdiv = document.getElementById(displaypanelname);
	
	document.getElementById("movetogether").checked = false;
	
	if(imgdiv && imgdiv.style)
	{	
		//set content
		var html = "<img style='position:absolute; left:0; top:0; z-index:0; cursor:move;' id='before' name='before' src='" + baseimageurl + "before/" + photoid + "' ondblclick='fnZoomHere()' onmousewheel='fnZoomHere()'>";
		document.getElementById("photopopupContent_Before").innerHTML=html;
		html = "<img style='position:absolute; left:0; top:0; z-index:0; cursor:move;' id='after' name='after' src='" + baseimageurl + "after/" + photoid + "' ondblclick='fnZoomHere()' onmousewheel='fnZoomHere()'>";
		document.getElementById("photopopupContent_After").innerHTML=html;

		imgdiv.style.display='block';			
		
		fnZoomStart('in', 'before');
		fnZoomStart('in', 'after');		
	}	
}


function fnPreviewObliqueImage(photoid, lat, lon)
{
	var previewpanelname="previewPanel";
	
	if(photoid==currentPreview)
		return;
	currentPreview=photoid;

	if(previewpanelname && document.getElementById(previewpanelname))
	{
		if(document.getElementById(previewpanelname).style)
		{
			//set content
			var img = document.getElementById("previewImg");
			img.photoid=photoid;
			img.src=(baseimageurl+'after/'+photoid).replace('/i/','/j/').replace('.jpg','.106;200;7;70.jpg');

			//set current image
			if(currentPushpin!=null)
				currentPushpin.innerHTML=currentPushpin.innerHTML.replace('icon_cameraOver.gif','icon_camera.gif');
			var newPushpin=document.getElementById("'"+photoid+"'");
			newPushpin.innerHTML=newPushpin.innerHTML.replace('icon_camera.gif','icon_cameraOver.gif');
			currentPushpin=newPushpin;
		}
	}
}

function fnGetWindowWidth(){var width=0;if(typeof(window.innerWidth)=='number'){width=window.innerWidth;}else if(document.documentElement&&document.documentElement.clientWidth){width=document.documentElement.clientWidth;}else if(document.body&&document.body.clientWidth){width=document.body.clientWidth;}if(!width||width<100){width=100;}return width;}
function fnGetWindowHeight(){var height=0;if(typeof(window.innerHeight)=='number'){height=window.innerHeight;}else if(document.documentElement&&document.documentElement.clientHeight){height=document.documentElement.clientHeight;}else if(document.body&&document.body.clientHeight){height=document.body.clientHeight;}if(!height||height<100){height=100;}return height;}
function fnCreateCompass()
{
  var el=document.createElement("div");
  el.id="VE_Compass";
  el.style.background="url(images/compass.gif)";
  el.onmousedown=VE_Compass._MouseDown;
  el.onmouseup=VE_Compass._MouseUp;
  el.onmousemove=VE_Compass._MouseMove;
  el.style.position="absolute";
  el.style.top = 52;
  el.style.left = 25;
  el.style.zIndex=31;
  el.style.width = 87;
  el.style.height = 87;
  VE_Compass.element=el;
  document.body.appendChild(el);
}

function fnOnMapChange(e)
{
	fnClosePopup();
	fnCloseObliqueImage();
	fnLoadOblique();
	VE_ZoomControl.SetZoomLevel(map.GetZoomLevel());
}

function fnOnMouseClick(e)
{
	fnClosePopup();
	fnCloseObliqueImage();
}

function fnOnPageLoad()
{
	window.onresize=function(e){return false;}

	//create map
	map = new VE_MapControl(29.947552606656373,-90.07003111988588,15, 'h', "relative", 0, 0, mapWidth, mapHeight);
	document.getElementById("virtualearthmap").appendChild(map.element);

	//create zoom
	var zm=VE_ZoomControl.Create(810, 48, 15, "absolute");
	document.body.appendChild(zm);

	//create pan
	fnCreateCompass();

	//wireup events
	map.onMapChange=fnOnMapChange;
	map.onMouseClick=fnOnMouseClick;
	map.onEndContinuousPan=fnOnMapChange;
	map.onEndZoom=fnOnMapChange;
	
	//load preview image
	fnLoadOblique();

	//Init drag stuff
	initDHTMLAPI();
	initDrag();
}

function fnDisplayPopup(title, description, link, art, x, y)
{
	var displaypanelname="popupPanel";

	if(displaypanelname && document.getElementById(displaypanelname))
	{
		if(document.getElementById(displaypanelname).style)
		{

			//set content
			var html = "<table width=250><tr valign=top><td colspan=2 valign=top><h2><a class=linkBlack target=_blank href='" + link  + "'>" + unescape(title) + "</a></h2></td></tr>";
			if(art != "")
			{
				html = html + "<tr valign=top><td valign=top><img src=" + art + " width=100 height=100></td><td valign=top><font class=bulletRedSmall>" + unescape(description) + "</font><td></tr><table>";
			}
			else
			{
				html = html + "<tr valign=top><td valign=top colspan=2><font class=bulletRedSmall>" + unescape(description) + "</font><td></tr><table>";
			}
			document.getElementById("popupcontent").innerHTML=html;

			//Display
			var currentX = map.GetX(y) + 20;
			var currentY = map.GetY(x) + 40;

			//document.getElementById(displaypanelname).style.visibility='visible';
			document.getElementById(displaypanelname).style.display='block';
			document.getElementById(displaypanelname).style.top=currentY;
			document.getElementById(displaypanelname).style.left=currentX;
			if(document.getElementById(displaypanelname).filters[0])
			{
				document.getElementById(displaypanelname).filters[0].Apply();
			}
		}
	}
}


function fnAddPushpinsFromXml()
{
	//Initialize the view port
	var lat1 = 0;
	var lon1 = 0;
	var lat2 = 0;
	var lon2 = 0;

	var title= null;
	var description= null;
	var link= null;
	var art= null;
	var icon= null;
	var lat= null;
	var lon= null;
	var x = feedXmlDoc.getElementsByTagName("item");
	var count;
	for (i=0;i<x.length;i++)
	{
		count = i;
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1)
			{
				continue;
			}
			if(x[i].childNodes[j].nodeName == "title")
			{
				title = x[i].childNodes[j].firstChild.nodeValue;
			}
			else if(x[i].childNodes[j].nodeName == "description")
			{
				description = x[i].childNodes[j].firstChild.nodeValue;
			}
			else if(x[i].childNodes[j].nodeName == "link")
			{
				link = x[i].childNodes[j].firstChild.nodeValue;
			}
			else if(x[i].childNodes[j].nodeName == "art")
			{
				if(x[i].childNodes[j].firstChild)
				{
					art = x[i].childNodes[j].firstChild.nodeValue;
				}
				else
				{
					art="";
				}
			}
			else if(x[i].childNodes[j].nodeName == "icon")
			{
				icon = x[i].childNodes[j].firstChild.nodeValue;
			}
			else if(x[i].childNodes[j].nodeName == "lat")
			{
				lat = x[i].childNodes[j].firstChild.nodeValue;
			}
			else if(x[i].childNodes[j].nodeName == "long")
			{
				lon = x[i].childNodes[j].firstChild.nodeValue;
			}
		}

		if(lat1 == 0)
		{
			lat1 = lat;
		}
		else if(lat1 < lat)
		{
			lat1 = lat;
		}

		if(lat2 == 0)
		{
			lat2 = lat;
		}
		else if(lat2 > lat)
		{
			lat2 = lat;
		}
		if(lon1 == 0)
		{
			lon1 = lon;
		}
		else if(lon1 < lon)
		{
			lon1 = lon;
		}
		if(lon2 == 0)
		{
			lon2 = lon;
		}
		else if(lon2 > lon)
		{
			lon2 = lon;
		}

		var onclickfunction = "fnDisplayPopup(\"" + escape(title) + "\", \"" + escape(description) +  "\", \"" +  link + "\", \"" + art + "\", " + lat + ", " + lon + ");";
		//Add pushpin
		map.AddPushpin('pin' + count,
			lat,
			lon,
			24,
			24,
			'null',
			"<img alt='Click to see more' onclick='" + onclickfunction + "' src=\"" + icon + "\">",
			35);
	}

	//Set viewport
	if(lat1 != 0 && lat2 != 0 && lon1 != 0 && lon2 != 0)
	{
		map.SetViewport(1*lat1, 1* lon1, 1*lat2, 1*lon2);
	}
}

function fnAddObliquePushpins(shotType)
{
	//Clear all existing pins
	map.ClearPushpins();
	currentPushpin=null;
	fnAddSearchPushpin();

	if(map.GetZoomLevel()<15)
	{
		fnShowPreviewContent('previewBodyZoom');
		return;
	}

	//Initialize the view port
	var lat1 = Math.round(map.GetLatitude(mapHeight)*10000);
	var lat2 = Math.round(map.GetLatitude(0)*10000);
	var latC = Math.round(map.GetLatitude(mapHeight/2)*10000);
	var lon1 = Math.round(map.GetLongitude(mapWidth)*10000);
	var lon2 = Math.round(map.GetLongitude(0)*10000);
	var lonC = Math.round(map.GetLongitude(mapWidth/2)*10000);
	
	// If the viewport is small enough that we might miss an image that shows us, expand enough to include nearest image
	if(Math.abs(lat1-lat2)<200)
	{
		lat1=latC-100;
		lat2=latC+100;
	}
	if(Math.abs(lon1-lon2)<200)
	{	
		lon1=lonC+100;
		lon2=lonC-100;
	}

	var lat=null;
	var lon=null;
	var name=null;
	var shortname=null;
	var cAdded=0;
	var dist=0;
	var nearestDist=-1;
	var nearest=null;
	for (i=0;i<cObdat;i++)
	{
		lat=obdat[i][0];
		if(lat<lat1)
			continue;
		if(lat2<lat)	// Since sorted by lat
			break;
		lon=obdat[i][1];
		if((lon<lon2)||(lon1<lon))
			continue;
			
		dist=(latC-lat)*(latC-lat)+(lonC-lon)*(lonC-lon);
		if ((dist<nearestDist) || (nearestDist==-1))
		{
			nearestDist=dist;
			nearest=obdat[i];
		}
			
		lat/=10000;
		lon/=10000;
		
		shortname=obdat[i][2];
		name=shortname.substring(0,12)+'NeighObliq'+shortname.substring(12,22)+'-000.jpg';
		var innerdiv = "<img onclick=\"fnShowObliqueImage('" + name + "');\" onmouseover=\"fnPreviewObliqueImage('" + name + "', " + lat + ", " + lon + ");\" src='icons/icon_camera.gif'></img>";
		//Add pushpin
		map.AddPushpin("'" + name + "'", lat, lon, 24, 24, 'null', innerdiv, 35);
	}
	
	if(nearest==null)
	{
		fnShowPreviewContent('previewBodyMove');
	}
	else
	{
		shortname=nearest[2];
		name=shortname.substring(0,12)+'NeighObliq'+shortname.substring(12,22)+'-000.jpg';
		fnPreviewObliqueImage(name);
		fnShowPreviewContent('previewBodyImg');
	}
}

function fnShowPreviewContent(s)
{
	document.getElementById('previewBodyImg').style.display='none';
	document.getElementById('previewBodyZoom').style.display='none';
	document.getElementById('previewBodyMove').style.display='none';
	document.getElementById(s).style.display='block';
	document.getElementById('previewPanel').style.display='block';
}

function fnClosePopup()
{
	var displaypanelname="popupPanel";

	if(displaypanelname && document.getElementById(displaypanelname))
	{
		if(document.getElementById(displaypanelname).style)
		{
			//document.getElementById(displaypanelname).style.visibility='hidden';
			document.getElementById(displaypanelname).style.display='none';
		}
	}
}

function fnGetXmlHttpRequest()
{
  var xmlhttpreq = null;
  try
  {
	xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
	try
	{
	  xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (ex)
	{
	  xmlhttpreq = null;
	}
  }

  if(!xmlhttpreq && typeof XMLHttpRequest!='undefined')
  {
	xmlhttpreq = new XMLHttpRequest();
  }

  if(xmlhttpreq == null)
  {
	alert("XmlHTTP is not supported.");
	return;
  }

 return xmlhttpreq;


}

function fnFindOnEnter(e)
{
	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
	{
		fnDoFind();
		return false;
	}
}

function fnDoFind()
{
  var where = document.getElementById("find").value;
  if(where == null || where == "")
  {
	return;
  }

  addressFinder = fnGetXmlHttpRequest();
  addressFinder.onreadystatechange=fnSearchHandler;
  addressFinder.open("GET", baseurl + "search.aspx?place="+where + "&random=" + Math.random(),true);
  addressFinder.send(null);
}

function fnSearchHandler()
{
  if(addressFinder == null)
  {
	return;
  }
  if (addressFinder.readyState==4 && addressFinder.responseText)
  {
	eval(addressFinder.responseText);
  }
}

function fnSearchGood(lat1,lon1,lat2,lon2)
{
	var lat=(lat1+lat2)/2;
	var lon=(lon1+lon2)/2;
	
	if (Math.abs(lat1-lat2)<0.05)
		map.SetCenterAndZoom(lat,lon,16);
	else
		map.SetViewport(lat1,lon1,lat2,lon2);
	
	map.RemovePushpin('myPushpin');
	searchPushpin=new Array(lat,lon);
	fnAddSearchPushpin();
}

function fnAddSearchPushpin()
{
	if(searchPushpin!=null)
		map.AddPushpin('myPushpin',searchPushpin[0],searchPushpin[1],24,24,'bluepin',"<img src='icons/icon_address.gif'>",35);
}

function fnSearchBad()
{
	alert('Unable to find the input location.');
}

//Zoom level functions
function fnZoom(action, id)
{
	var zoomingimage = document.getElementById(id);

	zoomPrefix= (action == "in")? 1 : -1
	if (zoomingimage.style.width=="" || action=="reset")
	{
		zoomingimage.style.width=photoWidth;
	}
	else
	{
		if(action == "out")
		{
			if(parseInt(zoomingimage.style.width)+parseInt(zoomingimage.style.width)*0.20*zoomPrefix < photoWidth)
			{
				zoomingimage.style.width=photoWidth;
				return;
			}
		}

		if (parseInt(zoomingimage.style.width)>10)
		{
			zoomingimage.style.width=parseInt(zoomingimage.style.width)+parseInt(zoomingimage.style.width)*0.20*zoomPrefix;
			if (action == "in")
			{
				zoomingimage.style.left=(parseInt(zoomingimage.style.left)*1.20)-(photoWidth*0.1);
				zoomingimage.style.top=(parseInt(zoomingimage.style.top)*1.20)-(photoHeight*0.1);
			}
			else
			{
				zoomingimage.style.left=(parseInt(zoomingimage.style.left)*0.80)+(photoWidth*0.1);
				zoomingimage.style.top=(parseInt(zoomingimage.style.top)*0.80)+(photoHeight*0.1);
			}
		}
	}
	fnKeepVisible(zoomingimage);
}

function fnZoomStart(action, id)
{
	fnZoom(action, id);
	var theOtherObject=fnGetOtherObject(id);
	if(theOtherObject)
	{
		fnZoom(action, theOtherObject.name);
	}
}

function fnZoomHere(e)
{
	if(!e)
		e=window.event;
	var me=null;
	if(e.srcElement)
		me=e.srcElement;
	else if(e.target)
		me=e.target;
	if(me!=null)
	{
		if(e.type=="mousewheel")
		{
			fnZoomStart((e.wheelDelta>0)?"in":"out",me.id);
			event.cancelBubble=true;
		}
		else
		{
			me.style.left=(photoWidth/2)-e.offsetX;
			me.style.top=(photoHeight/2)-e.offsetY;
			fnZoomStart("in", me.id);
		}
	}
}

function fnKeepVisible(o)
{
	var edge=50;
	if(parseInt(o.style.left)+parseInt(o.style.width) < edge)
		o.style.left=(edge-parseInt(o.style.width))+'px';
	if(parseInt(o.style.left)>photoWidth-edge)
		o.style.left=(photoWidth-edge)+'px';
	if(parseInt(o.style.top)+parseInt(o.height) < edge)
		o.style.top=(edge-parseInt(o.height))+'px';
	if(parseInt(o.style.top)>photoHeight-edge)
		o.style.top=(photoHeight-edge)+'px';
}

var obdat,cObdat=0,minLat,minLon,obdatReady=false;

function fnStartObliqueData(minLatIn,minLonIn,count)
{
	obdat=new Array(count);
	minLat=minLatIn;
	minLon=minLonIn;
}

function fnI(lat,lon,shortid)
{
	obdat[cObdat++]=new Array(minLat+lat,minLon+lon,shortid);
}

function fnEndObliqueData()
{
	obdatReady=true;
	
	var cx=0;
	for (var i=0; i<cObdat; i++)
	{
		if (obdat[i][0] < obdat[i][1])
			cx++;
	}
}
