// JavaScript Document

var map;
var markermgr;
var geocoder;
var gdir;
var directions;
var addressMarker;
var num = 1;
var zoom = 13;

// Call this function when the page has been loaded
function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("mapLanding"));

		map.addControl(new GLargeMapControl());
		map.addControl(new GHierarchicalMapTypeControl());
		map.addControl(new GOverviewMapControl());

		geocoder = new GClientGeocoder();
		
		var latitude = document.getElementById('latitude').value;
		var longitude = document.getElementById('longitude').value;
		var address = document.getElementById('resortAddress').innerHTML;
		
		if (latitude != 0 && longitude != 0)
		{
			var point = new GLatLng(latitude, longitude);
			map.setCenter(point, zoom);
			placeMarker(point, getInfo() )
		}
		else
		{
			showAddress(address, getInfo() );
		}
	}
}

function showAddress(address, info)
{
	var city = $('city').value;
	var state = $('state').value;
	
	if (geocoder)
	{
		geocoder.getLatLng(
			address,
			function(point)
			{
				if (!point)
				{
					showAddress(city, state);
				}
				else
				{
					map.setCenter(point, zoom);
					placeMarker(point, info);
				}
			}
		);
	}
}

function getGeoCodeOLD(address, city, state, zip)
{
	var address = address + ',' + city + ',' + state + ' ' + zip;
	
	geocoder.getLatLng(
		address,
		function(point)
		{
			if (point)
			{
				$('latitude').value = point.lat();
				$('longitude').value = point.lng();
				$('geostatus').selectedIndex = 1;
			}
			else
			{
				alert ("Sorry, could not find a GeoCode for the supplied address: " + address);
			}
		}
	);
}

function getGeoCode(address, city, state, zip, apiKey)
{
	var address = address + ' ' + city + ' ' + state + ' ' + zip;
	var url = '/admin/ajax/geocoder.php';

	/*
	| If the latitude or longitude are blank geocode them else just return false
	*/
	if ( $('latitude').value == '' || $('longitude').value == '' )
	{
		new Ajax.Request(url, {
			method: 'post',
			asynchronous: false,
			parameters: {
				address: address,
				apiKey: apiKey
			},
			onFailure: function (transport)
			{
				alert(transport.statusText);
			},
			onComplete: function (transport)
			{
				var data = transport.responseText.evalJSON();

				if (data)
				{
					$('latitude').value = data.latitude;
					$('longitude').value = data.longitude;
					$('geostatus').selectedIndex = 1;
				}
				else
				{
					alert ("Sorry, could not find a GeoCode for the supplied address: " + address);
				}
			}
		});
	}
	else
	{
		return false;
	}
}

function placeMarker(point, info)
{
	var details = ' ';
	var email = ' ';
	var photo = ' ';
	
	if (!zoom) 
	{
		var zoom = 13;
	}

	// Create our "tiny" marker icon
	var ralIcon = new GIcon(G_DEFAULT_ICON);
	ralIcon.image = info.icon;
	ralIcon.iconSize = new GSize(47, 40);

	// Set up our GMarkerOptions object
	markerOptions = { icon:ralIcon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(
		marker, 
		'click', 
		function()
		{
			if (info.details)
			{
				details = '<div class="info-details"><a href="' + info.details + '">See Details</a> > </div>';
			}
			
			if (info.email)
			{
				email = '<div class="info-email"><a href="' + info.email + '">Send Email</a> > </div>';
			}
			
			if (info.photo)
			{
				photo = "<div class='info-image'><img src='" + info.photo + "' width='97' height='75'></div>";
			}
			
			marker.openExtInfoWindow(
				map,
				"custom_info_window_" + info.color,
				"<div class='info-title'>" + info.name + "</div>"+
				photo +
				"<div class='info-address'>"+
				"	" + info.address + "<br>" +
				"	" + info.city + ", " + info.state + " " + info.zip + "<br>" +
				"	"+
				"</div>"+
				" " + details +
				" " + email + 
				"<div class='info-phone'> " + info.phone + "</div>",
				{beakOffset: 16}
			 ); 
		  }
	);
	map.addOverlay(marker);
}

function printMapHREF()
{
	var latitude = $('latitude').value;
	var longitude = $('longitude').value;
	var address = $('resortAddress').innerHTML;

	if (latitude && longitude)
	{
		window.open("http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=" + escape($('fromDirections').value) + "&daddr=" + escape($('toDirections').value) + "&sll=" + latitude + ", " + longitude + "&ie=UTF8", 'Google_Maps', 'width=800,height=600,scrollbars=1,resizable=1');
	}
	else
	{
		window.open("http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=" + escape($('fromDirections').value) + "&daddr=" + escape($('toDirections').value) + "&ie=UTF8", 'Google_Maps', 'width=800,height=600,scrollbars=1,resizable=1');
	}
}

/*
| Show the correct map options box
*/
function toggleMapBox(element)
{
	var elements = $('landingMapArea').childElements();
	for (var ctr =0; ctr < elements.length; ++ctr)
	{
		elementID = elements[ctr].id;
		
		if (elementID && elementID != element && elementID != 'mapLanding')
		{
			elementUp(elementID);
		}
	}
	
	toggleElement(element);
}

function changeDisplayOptions(type)
{
	// Change the selected segment
	if ($('displayLodging') )
	{
		$('displayLodging').removeClassName('landing-text-dark');
	}
	
	if ($('displayAttractions') )
	{
		$('displayAttractions').removeClassName('landing-text-dark');
	}
	
	if ($('displayRestaurants') )
	{
		$('displayRestaurants').removeClassName('landing-text-dark');
	}
	
	$('display' + type).addClassName('landing-text-dark');
	
	// Change the type selector box
	var elements = $('displayTypes').childElements();
	for (var ctr = 0; ctr < elements.length; ++ctr)
	{
		elementID = elements[ctr].id;
		
		if (elementID && elementID != 'display' + type + 'Types')
		{
			$(elementID).style.display = 'none';
		}
	}
	
	$('display' + type + 'Types').style.display = '';
}

function changeDisplayType(node, selected)
{
	var elements = $(node + 'Scroller').childElements();
	
	for (var ctr = 0; ctr < elements.length; ++ctr)
	{
		element = elements[ctr];
		linkElement = element.firstDescendant();
		linkElement.removeClassName('landing-text-dark');
	}
	
	$(selected).addClassName('landing-text-dark');
	
	/* Get the listings by AJAX */
	var url = "/util/ajax/googleMaps.php";
	var boundary = map.getBounds();
	
	new Ajax.Request(url, {
		method: 'post',
		asynchronous: false,
		parameters: {
			command	: 'getMarkers',
			id			: $('id').value,
			node		: node,
			type		: selected,
			limit		: 15,
			bounds	: boundary
		},
		onFailure:function(transport)
		{
			alert('Sorry, there was an error: ' + transport.statusText);
		},
		onSuccess:function(transport)
		{
			//alert(transport.responseText);
			var result = transport.responseText;
			
			jsonResponse = eval('(' + result + ')');
			for (ctr = 0; ctr < jsonResponse.listings.length; ++ctr)
			{
				//alert(jsonResponse.listings[ctr].name);
				propInfo = new Object();
				propInfo.color		= 'yellow';
				propInfo.icon		= 'http://www.resortsandlodges.com/images/googleMaps/map-icon-' + node + '.png';
				propInfo.name		= jsonResponse.listings[ctr].name;
				propInfo.address	= jsonResponse.listings[ctr].address;
				propInfo.city		= jsonResponse.listings[ctr].city;
				propInfo.state		= jsonResponse.listings[ctr].state;
				propInfo.zip		= jsonResponse.listings[ctr].zip;
				propInfo.email		= '/apps/inquiries/request.php?id=' + jsonResponse.listings[ctr].id + '&amp;product=' + node + '&amp;pg=&amp;r=' + jsonResponse.listings[ctr].region + '&amp;fromLanding=Y&amp;currentTab=3';
				propInfo.phone		= jsonResponse.listings[ctr].phone;
				
				var point = new GLatLng(jsonResponse.listings[ctr].latitude, jsonResponse.listings[ctr].longitude);

				placeMarker(point, propInfo);
			}
		}
	});
}

/* Scrolling javascript code */
var scrollStep = 2;

var timerLeft = '';
var timerRight = '';

function toLeft(id)
{
  $(id).scrollLeft=0;
}

function scrollDivLeft(id)
{
	clearTimeout(timerRight);
	$(id).scrollLeft += scrollStep;
	timerRight = setTimeout("scrollDivLeft('"+id+"')",10);
}

function toRight(id)
{
	alert ($(id).scrollWidth);
	$(id).scrollLeft = $(id).scrollWidth;
}

function scrollDivRight(id)
{
	clearTimeout(timerLeft);
	$(id).scrollLeft-=scrollStep;
	timerLeft = setTimeout("scrollDivRight('"+id+"')",10);
}

function stopMe()
{
	clearTimeout(timerRight);
	clearTimeout(timerLeft);
}

// For IE6 and IE7, make sure to load maps after page is fully loaded.
function WindowOnload(f)
{
	var prev=window.onload;
	window.onload=function()
	{
		if (prev) prev(); f();
	}
}
