var iitvearth_slideSpeed = 10;	// Higher value = faster
var iitvearth_timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var iitvearth_activeId = false;
var iitvearth_slideInProgress = false;

function showHideContent(e,inputId)
{
	if(iitvearth_slideInProgress)return;
	iitvearth_slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = document.getElementById('iitvearth_a' + numericId);

	objectIdToSlideDown = false;
	
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(iitvearth_activeId &&  iitvearth_activeId!=numericId){			
			objectIdToSlideDown = numericId;
			slideContent(iitvearth_activeId,(iitvearth_slideSpeed*-1));
		}else{
			
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,iitvearth_slideSpeed);
		}
	}else{
		slideContent(numericId,(iitvearth_slideSpeed*-1));
		iitvearth_activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =document.getElementById('iitvearth_a' + inputId);
	var contentObj = document.getElementById('iitvearth_ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',iitvearth_timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				document.getElementById('iitvearth_a' + objectIdToSlideDown).style.display='block';
				document.getElementById('iitvearth_a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,iitvearth_slideSpeed);				
			}else{
				iitvearth_slideInProgress = false;
			}
		}else{
			iitvearth_activeId = inputId;
			iitvearth_slideInProgress = false;
		}
	}
}

function initShowHideDivs()
{
	var divs = document.getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='IITVEARTH_DirectionsLink'){
			divs[no].onclick = showHideContent;
			divs[no].id = 'iitvearth_q'+divCounter;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'iitvearth_a'+divCounter;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='IITVEARTH_LocationSearch_content';
			contentDiv.id = 'iitvearth_ac' + divCounter;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}

function initMapSearchForm()
{
	var geoRssFeed = Request.QueryString("georss");
	if(geoRssFeed != null)
	{
		document.getElementById("txtFrom").value = geoRssFeed;
		SwitchSearchType(4);
	} 
	else
	{
		SwitchSearchType(document.getElementById("SearchType").value);	
	}
}

// Process the querystring
function QSHandler() 
{
	var qs=location.search.substr(1).split("&");
	this.data=[];
	for(var i=0;i<qs.length;i++) this.data[qs[i].split("=")[0]]=qs[i].split("=")[1];
		this.QueryString=function(x) 
	{
		return this.data[x];
	};
} 

//initiate the querystring object
var Request=new QSHandler();


function SwitchFromTo()
{
var txtFrom = document.getElementById("txtFrom").value;
var txtTo = document.getElementById("txtTo").value;
document.getElementById("txtFrom").value = txtTo;
document.getElementById("txtTo").value = txtFrom;
}

function openPopup(winTitle,routeinfo){
	var winDirections = new Window("winDirections", {className: "mac_os_x", width:400, height:400,title: winTitle, showEffectOptions: {duration:1} });
	winDirections.getContent().innerHTML = routeinfo;
	//master.setContent("directions");
	winDirections.setDestroyOnClose();
	winDirections.showCenter();
//master.show();
}

function openPointList(PointsDiv) {
	if (PointsDiv.innerHTML != '') {
		var win = new Window("PointList", {className: "mac_os_x", width:212, height:350,title: "", showEffectOptions: {duration:1} });
		win.getContent().innerHTML = PointsDiv.innerHTML;
		win.setDestroyOnClose();
		win.show();
	}
}

// Map Events
// Currently Virtual Earth doesn't detect the version of Firefox properly. 
// This will get things working
// If the browser is Firefox get the version number
var ffv = 0;
var ffn = "Firefox/"
var ffp = navigator.userAgent.indexOf(ffn);
if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
// If we're using Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
if (ffv >= 1.5) {
	Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
}

function WriteLatLon(lblLat, lblLon, lat, lon) {
	WriteStatus('<span class=IITVEARTH_StatusbarButton_Copy title="Copy Latitude" onclick=ClipBoard("' + lat + '");  onmouseover="showStatus(\'Click to add this latitude to your clipboard...\');" onMouseOut="return showStatus(\'\');">' + lblLat + lat + '</span> <span class=IITVEARTH_StatusbarButton_Copy title="Copy Longitude" onclick=ClipBoard("' + lon +'"); onmouseover="showStatus(\'Click to add this longitude to your clipboard...\');" onMouseOut="return showStatus(\'\');">' + lblLon + lon + '</span>');
}
function ClipBoard(str) 
{
	if (str != "")
	{
		window.clipboardData.setData("Text", str);
		showStatus(str + " is now copied to your clipboard.");
	}
}

function WriteStatus(str)
{
	document.getElementById('IITVEARTH_message').innerHTML = str;
}

function MapOnMouseOverCallback (x, y, title, details)
{
}
function onFindResults(findResults)         
{
		var results="Find Results:\n";
		for (r=0; r < findResults.length; r++)  {
		results += findResults[r].Name + ", ";
		results += findResults[r].Description + ": ";
		results += findResults[r].Phone + "\n";
		}
		//alert(results);
}
// Prints out the route
function onGotRoute(route)         
	{ 
     
	var routeinfo = "";                     
	var steps = "<div class='IITVEARTH_Directions'><ol>";
	var len = route.Itinerary.Segments.length;               
	for(var i = 0; i < len ;i++)               
	{
	steps += "<li>";
			
	steps += route.Itinerary.Segments[i].Instruction + " (";                  
	steps += route.Itinerary.Segments[i].Distance + " ";                  
	steps += route.Itinerary.DistanceUnit + ")";
			
	steps += "</li>";              
}   
steps += "</ol>";
steps += "<br><br><input type='button' value='Print' onclick='togglePrintDisplay();' onkeypress='togglePrintDisplay();' id='printbut_1' class='searchBtn'>";

openPopup("Route Information: Distance " + route.Itinerary.Distance + " " + route.Itinerary.DistanceUnit,steps);                 
}
                   
function GetMap(ctlid, lat, lon, zoom, style, title, pointid)
{            
//	WriteLatLon(lat, lon);
//	map = new VEMap(ctlid);            
//	map.LoadMap(new VELatLong(lat, lon), zoom, style, false);
//	AddPin(title, pointid)
}

function AddPin(title, pointid)         
{
//	//var pinID = 1;
//	//var icon = document.getElementById('point_icon_' + pointid).getAttribute('src');
//	//var pin = new VEPushpin(pinID, map.GetCenter(), icon, title, document.getElementById('desc_' + pointid).innerHTML);     
//	//map.AddPushpin(pin);           
//	//pinID++;         
}

function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
}

function togglePrintDisplay(e)
{
if (window.event) 
	{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
	} 
else if (e) 
	{
	e.stopPropagation();
	e.preventDefault();
	}

window.print();
}

