
var map = null;
var movingDiv = '';
var statList = new Array();    
var isAbleToGetData = true;

var awsHttp;
var awsIsRandomParamEnabled = true;

function AwsInit()
{
	if( window.XMLHttpRequest )
		awsHttp = new XMLHttpRequest();
	else
		awsHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function AwsSendSyncGetReq(url) 
{
	return AwsSendReq('get',url,null);
}

function AwsSendSyncPostReq(url) 
{
	return AwsSendReq('post',url,null);
}

function AwsSendAsyncGetReq(url,callBackFunc) 
{
	AwsSendReq('get',url,callBackFunc);
}

function AwsSendAsyncPostReq(url,callBackFunc) 
{
	AwsSendReq('post',url,callBackFunc);
}

function AwsSendReq(method,url,callBackFunc) 
{
	if(awsIsRandomParamEnabled) url += '&r='+ Math.random();
	awsHttp.open(method,url,(callBackFunc != null));

	if(callBackFunc) awsHttp.onreadystatechange = callBackFunc;
	awsHttp.send(null);

	if(callBackFunc)
		return;
	else
		return awsHttp.responseText;
}

function CopyPermaLinkToClipBoard()
{
	var clipboardArea = document.getElementById('clipboardArea');
	clipboardArea.value = CreatePermalink();
	clipboardText = clipboardArea.createTextRange();
	clipboardText.select();
	clipboardText.execCommand('Copy');
	
	ShowMsgBox(false);
}

function CreatePermalink()
{
	return CreateMapTypeLink('0');
}

function DisplayEmptySearchResults()
{
	var searchListResults = document.getElementById('searchListResultsDiv');
	var searchStr = document.getElementById('ss');
	searchListResults.innerHTML = '<b>No locations found for:</b><br>"' + searchStr.value + '"';
	ShowSearchResults(true);
}

function DisplayPermalink()
{
	var results = 'Use the following link at any time to return to WeatherBug Maps with your current map view just as it is now.<br><br>';
	results += '<table align="center"><tr><td class="msgBoxText">' + CreatePermalink() + '</td></tr></table>';
	if(document.all) results += '<br><br><table align="center"><tr><td width="100"><a class="msgBoxText" href="JavaScript:EmailPermaLink();">Email It</a></td>';
	if(document.all) results += '<td><a class="msgBoxText" href="JavaScript:CopyPermaLinkToClipBoard();">Copy to clipboard</a></td>';
	results += '</tr></table>';	
	
	var msgBoxText = document.getElementById('msgBoxTextDiv');
	msgBoxText.innerHTML = results;
	
	ShowMsgBox(true);
}

function DisplaySearchResults(locationUS,locationIntl)
{
	var results = '';
	if( locationUS.length > 0 )
	{
		results += '<b>U.S.</b> ('+ locationUS.length +' cities with weather stations)';
		for(i = 0; i < locationUS.length; i++)	
		{
			results += '<br>  - <a href="JavaScript:PositionMap(' + locationUS[i].getAttribute("lon") +','+ locationUS[i].getAttribute("lat") +',11);">' + locationUS[i].getAttribute("city") + ', ' + locationUS[i].getAttribute("state") + '</a>';
		}
	}
	
	if( locationIntl.length > 0 )
	{
		if( results.length > 0 ) results += '<br>';
		results += '<b>International</b> ('+ locationIntl.length +' cities with weather stations)';
		for(i = 0; i < locationIntl.length; i++)	
		{
			results += '<br>  - <a href="JavaScript:PositionMap(' + locationIntl[i].getAttribute("lon") +','+ locationIntl[i].getAttribute("lat") +',9);">' + locationIntl[i].getAttribute("city-name") + ', ' + locationIntl[i].getAttribute("country") + '</a>';
		}	
	}	
	
	var searchListResults = document.getElementById('searchListResultsDiv');
	searchListResults.innerHTML = results;
	
	ShowSearchResults(true);
}

function EmailPermaLink()
{
	window.location = 'mailto:?subject='+escape('WeatherBug Maps')+ '&body=' + escape('WeatherBug Maps:\n' + CreatePermalink());
	ShowMsgBox(false);
}

function InitPage()
{
	AwsInit();
	window.onresize = InitScreen;
	InitScreen();
	InitInfoWindowAddingStations();
	InitInfoWindowZoomStatus();
	InitInfoWindowSearchLocation();
	InitMap();

	
	MarkMap();
}

function InitScreen()
{
	var mapDiv = document.getElementById('myMap');

	if( document.all )
	{
		mapDiv.style.width = document.body.clientWidth - 30 + 'px';
		mapDiv.style.height = document.body.clientHeight - 140 + 'px';    	
	}
	else
	{
		mapDiv.style.width = window.innerWidth - 30 + 'px';
		mapDiv.style.height = window.innerHeight - 140 + 'px';    	
	}
}

function LocationResponse()
{
	if( awsHttp.readyState != 4 ) return;
	
	var xmlDoc = awsHttp.responseXML;
	var locationUS = xmlDoc.getElementsByTagName('aws:location-us');	
	if( locationUS.length == 0 ) locationUS = xmlDoc.getElementsByTagName('location-us');
	
	var locationIntl = xmlDoc.getElementsByTagName('aws:location-intl');
	if( locationIntl.length == 0 ) locationIntl = xmlDoc.getElementsByTagName('location-intl');
	
	var total = locationUS.length + locationIntl.length;
	
		
	
	ShowSearchLocation(false);
	
	if( total == 1 )
	{
		if( locationUS.length == 1 )
			PositionMap(locationUS[0].getAttribute("lon"),locationUS[0].getAttribute("lat"),11);
		else
			PositionMap(locationIntl[0].getAttribute("lon"),locationIntl[0].getAttribute("lat"),9);
	}
	else if( total == 0 )
		DisplayEmptySearchResults();
	else
		DisplaySearchResults(locationUS,locationIntl);
}

function MouseDown(event, div)
{
	movingDiv = div;
}

function MouseMove(event)
{
	if( movingDiv.length > 0 )
	{
		var div = document.getElementById( movingDiv );
		div.style.top = event.clientY - 15;
		div.style.left = event.clientX - 110;
   }
}

function MouseUp(event)
{
	movingDiv = '';
}


function InitInfoWindow(id, text)
{
	var infoWindow = document.createElement('div');
	infoWindow.setAttribute('id', id);
	infoWindow.style.color = '#10238C';
	infoWindow.style.visibility = 'hidden';
	infoWindow.style.backgroundColor = '#FFFF99';
	infoWindow.style.height = '50px';
	infoWindow.style.top = '125px';
	infoWindow.style.left = '290px';
	infoWindow.style.zindex = '1';
	infoWindow.style.position = 'absolute';
	infoWindow.style.fontFamily = 'Tahoma, Arial, sans-serif';
	infoWindow.style.borderStyle = 'solid';
	infoWindow.style.borderWidth = '2px';
	infoWindow.style.fontSize = '12px';
	infoWindow.style.fontWeight = 'bold';
	infoWindow.innerHTML = '<br>&nbsp;' + text + '&nbsp;&nbsp;';
	
	document.body.appendChild(infoWindow);
}

function InitInfoWindowAddingStations()
{
	InitInfoWindow('addingStations', 'Adding additional stations...');
}

function ShowAddingStations(show)
{
	addingStations.style.visibility = (show ? 'visible' : 'hidden');
}

function InitInfoWindowZoomStatus()
{
	InitInfoWindow('zoomStatus', 'Zoom in closer to view stations');
}

function ShowZoomStatus(show)
{
	zoomStatus.style.visibility = (show ? 'visible' : 'hidden');
}

function InitInfoWindowSearchLocation()
{
	InitInfoWindow('searchLocation', 'Searching locations...');
}

function ShowSearchLocation(show)
{
	searchLocation.style.visibility = (show ? 'visible' : 'hidden');
}

function PositionMap(lon,lat,zoomLevel)
{
		
		
	CenterAndZoom(lon,lat,zoomLevel);
	ShowSearchResults(false);
}

function ShowMsgBox(show)
{
	var msgBox = document.getElementById('msgBoxDiv');
	msgBox.style.visibility = (show ? 'visible' : 'hidden');
}


function ShowSearchResults(show)
{
	var searchList = document.getElementById('searchListDiv');
	searchList.style.visibility = (show ? 'visible' : 'hidden');
}

function GetSelectedUnits()
{
    window.location = CreatePermalink();
}