//import "earth.idl";
google.load("earth", "1");

var ge = null;

//**************************************************************
function init() {
	ge=google.earth.createInstance("map3d", initCallback, failureCallback);
}

//**************************************************************
function initCallback(object) 
{
	ge = object;
	ge.getWindow().setVisibility(true);
  
	setPlaceMark();
}
//**************************************************************
function failureCallback(object) {
}
//**************************************************************
//function that creates request object for Ajax
function createRequest()
{	//Mozilla & Safari
	var req = null;
	try
	{	
		req = new XMLHttpRequest();
		return req;
	}
	catch (trymicrosoft)
	{	//Internet Explorer
		try
		{
			req = new ActiveXObject("Msx12.XMLHTTP");
			return req;
		}
		catch (othermicrosoft)
		{	//other internet Expolrer
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
				return req;
			}
				catch(failed)
				{
					req = null;
					return req;
				}
		}
	}
	if(req == null)
		alert("Error creating request object");		
}

//*************************************************************************
function drawClipSection()
{
	//a string of the url , passing as arguments the fields from the input form  (  min (freq )  max (freq )  and  s ( number of results )
	var url = "clipSection.php";
		
	//create an XMLHttpRequest object for the asynchronous call
	var layOutReq = createRequest();
	//alert(layOutReq);
	//try connection
	layOutReq.open("GET", url, true);
				
	//inline definition of the callback function that handles the document when the connnection is OK
	layOutReq.onreadystatechange = function()
	{
		//check ready state
		if (layOutReq.readyState == 4)
		{
			if (layOutReq.status == 200) 
			{
				// get the response from the asynchronous call
				var response  = layOutReq.responseText;
				//alert(response);
				document.getElementById("clipSection").innerHTML = response ;
			}
		}
	}
		
	layOutReq.send(null);
}
//*************************************************************************************
function setPlaceMark(){
	
	var placemark = ge.createPlacemark('');
	
	placemark.setName("You are at ARBIMON UPRRP");
	ge.getFeatures().appendChild(placemark);
	
	// Create style map for placemark
	var normal = ge.createIcon('');
	normal.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
	var iconNormal = ge.createStyle('');
	iconNormal.getIconStyle().setIcon(normal);
	var highlight = ge.createIcon('');
	highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
	var iconHighlight = ge.createStyle('');
	iconHighlight.getIconStyle().setIcon(highlight);
	var styleMap = ge.createStyleMap('');
	styleMap.setNormalStyle(iconNormal);
	styleMap.setHighlightStyle(iconHighlight);
	placemark.setStyleSelector(styleMap);
	
	// Create point
	var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	var point = ge.createPoint('');
	point.setLatitude(la.getLatitude());
	point.setLongitude(la.getLongitude());
	placemark.setGeometry(point);
	
 }
 //**********************************************************************************
 function pausecomp(millis)
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); }
while(curDate-date < millis);
} 
//*************************************************************************************
function takeSnapShot(){

	var url = "takeSnapShot.php";
		
	//create an XMLHttpRequest object for the asynchronous call
	var snapReq = createRequest();
	//alert(layOutReq);
	//try connection
	snapReq.open("GET", url, true);
				
	//inline definition of the callback function that handles the document when the connnection is OK
	snapReq.onreadystatechange = function()
	{
		//check ready state
		if (snapReq.readyState == 4)
		{
			if (snapReq.status == 200) 
			{
				// get the response from the asynchronous call
				alert(snapReq.responseText);
				
				//document.getElementById("clipSection").innerHTML = response ;
			}
		}
	}
		
	snapReq.send(null);
	
}
//**************************************************************************************
function myKMLParse(){
	
  	
  	
  	//parser=new DOMParser();

  	var xmlDoc=document.implementation.createDocument("","",null);
	xmlDoc.async=false;	
	xmlDoc.load("doc.xml");
	var tit=xmlDoc.getElementsByTagName("Folder")[1].childNodes;
	//document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
	//document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

  	
  	
  	alert(tit);
  
	//while(int i = 0; i < nodes.length(); i ++) {
 	//	Element titleElem = (Element)nodes.item(i);
	//	Node childNode = titleElem.getFirstChild();
	//	if (childNode instanceof Text) {
	//		System.out.println("Book title is: " + childNode.getNodeValue());
	//}
	
}

//**************************************************************************************
 function moveCameraTo(lat, lon)
 {
	var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	var currentTime = new Date();
	
	// for(var i=0;i<5;i++){
// 		lookAt.setLatitude(lat*Math.pow(-1,i)+(i*3));
// 		lookAt.setLongitude(lon*Math.pow(-1,i)-(i*3));
// 		ge.getView().setAbstractView(lookAt);
// 	
// 		pausecomp(3000);
// 		takeSnapShot();
// 		alert("done with snapshot");
// 		pausecomp(3000);
// 		//Write Ajax that executes PrintScreen command
// 		
// 	}
		
  	myKMLParse();
  	return;	
  var studySite = ge.parseKml(
  '<?xml version="1.0" encoding="UTF-8"?>' +
  '<kml xmlns="http://earth.google.com/kml/2.1">' +
  '  <Placemark>' +
  '    <name>A Place We want to Study</name>' +
  '    <Polygon>' +
  '      <extrude>1</extrude>' +
  '      <altitudeMode>relativeToGround</altitudeMode>' +
  '      <outerBoundaryIs>' +
  '        <LinearRing>' +
  '          <coordinates>' +
  '            -77.05788457660967,38.87253259892824,100 ' +
  '            -77.05465973756702,38.87291016281703,100 ' +
  '            -77.05315536854791,38.87053267794386,100 ' +
  '            -77.05552622493516,38.868757801256,100 ' +
  '            -77.05844056290393,38.86996206506943,100 ' +
  '            -77.05788457660967,38.87253259892824,100' +
  '          </coordinates>' +
  '        </LinearRing>' +
  '      </outerBoundaryIs>' +
  '      <innerBoundaryIs>' +
  '        <LinearRing>' +
  '          <coordinates>' +
  '            -77.05668055019126,38.87154239798456,100 ' +
  '            -77.05542625960818,38.87167890344077,100 ' +
  '            -77.05485125901024,38.87076535397792,100 ' +
  '            -77.05577677433152,38.87008686581446,100 ' +
  '            -77.05691162017543,38.87054446963351,100 ' +
  '            -77.05668055019126,38.87154239798456,100' +
  '          </coordinates>' +
  '        </LinearRing>' +
  '      </innerBoundaryIs>' +
  '    </Polygon>' +
  '  </Placemark>' +
  '</kml>');

ge.getFeatures().appendChild(studySite);

// Fly to the Pentagon
var la = ge.createLookAt('');
la.set(38.867, -77.0565, 500, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 45, 900);
ge.getView().setAbstractView(la);

takeSnapShot();
	//alert("Capturing Screen latest!");
	//var myAs=new ActiveXObject("ActiveScreen.Capturer");
	
	//alert("as: "+myAs);
	//myAs.CaptureScreen();
	//var fs=myAs.SaveToFile("test1.jpg");
	//WScript.Echo("Image Size = " + fs.toString() + "\r\n");

 }
 //**********************************************************************************
 