/**********************************************************************
 * HH400 Google Maps API Project
 * @File: hh400.map.helper.js
 * Core JS functions utilized by the HH400 Google Map
 * These functions handle a lot of the functionality for
 * Sidebar map elements (legends, layers, etc) and map-centric
 * elements (markers, overlays, etc)
 * Basic map setup is done via hh400.map.js
 * Additional HTML/DOM/Effects are handled via hh400.map.utilities.js
 * Version 1.0 | 05-04-2009
 * Developed by Cartosoft, LLC
 * http://www.cartosoft.com | info@cartosoft.com
 * Copyright 2009 Google, Inc. | http://www.google.com
**********************************************************************/

/***
Populate the Info Window for a Tab
***/

var _tab, _theme_id, _id

function tabWindow(a,b,c) {

	if (!map.getInfoWindow()) {
		return false;
	}
	
	_tab = a;
	_theme_id = b;
	_id = c;
	
	maxWindow();
	
}


/***
Maximize the InfoWindow
***/

function maxWindow() {
	var iw = map.getInfoWindow();
	iw.maximize();
}


/***
Hash Function: Used to track multiple points with the same coordinates
***/
function hash(lng, lat)	{
	return(lng + ":" + lat);
}


/***
Create a marker; returns a marker
***/
var max_content_div;
var tab_indexes = new Array();
var max_state;

function createMarker(point, icon, title, description, id, theme_id, num_images, num_videos, num_links, num_threed_images, m_html) {
	
	var marker = new GMarker(point, {icon:icon, title:title, maxWidth: 335});
	
	//Attach an event listener to the marker.  Show information in the info window.
	GEvent.addListener(marker, "click", function() {
	
		if($('fullscreen-tip-fullscreen')) {
			$('fullscreen-tip-fullscreen').setStyle({display:'none'});
		}
		
		_tab = 'info';
		_theme_id = theme_id;
		_id = id;
		
		var back_html = '';
		if (sel_cluster_marker == marker) {
			back_html = '<div style="font-size: 10px;padding-bottom: 3px;"><a href="javascript:void(0);" onClick="map.returnToSavedPosition();clusterInfo(idx_cluster);" class="cluster-more-link" style="font-weight:normal;">&laquo; Back to List of Points of Interest at This Location</a></div>';
			m_html = '';
		}
		else {
			if (!multiple_links_ids[theme_id+'-'+id] && m_html.length > 0) {
			m_html = '<div id="multiple-links-wrapper-'+theme_id+'-'+id+'" class="multiple-links-wrapper" onMouseOver="javascript:$(\'multiple-links-wrapper-'+theme_id+'-'+id+'\').setStyle({background:\'#ffffff\',color: \'#333333\'});" onMouseOut="javascript:$(\'multiple-links-wrapper-'+theme_id+'-'+id+'\').setStyle({background:\'#f5f5f5\',color: \'#888888\'});">' + m_html + '</div>';
			multiple_links_ids[theme_id+'-'+id] = true;
			}
		}
		
		max_content_div = document.createElement('div');
		
		var max_title = title;
		
		if (max_title.length > 45) {
			max_title = max_title.substr(0,45) + '...';
		}
		
		var desc_html = description;
		
		if (description.length > 550) {
			var desc_html = description.substr(0,550) + '...' + '<br/><br/><a href="javascript:void(0);" onClick="javascript:tabWindow(\'info\','+theme_id+','+id+'); return false;" class="more-link">More</a><br/><br/>';
		}
		
		// Add the UGC teaser link to the description:
// 		desc_html = desc_html + '<p><a class="cluster-link" onclick="return GB_showCenter(\'' +
// 									'Contribute Your Information to Our Origins Map Layer\'' +
// 									', this.href, 550, 775)" href="origins-ugc.php">' +
// 									'<img align="absmiddle" style="padding-bottom: 4px;"' +
// 									'alt="Add Your Own Information!"' + 'src="images/library/icons/icon-new-note.gif"/>' +
// 									'Add Your Own Information!</a></p>';
		
		var html_title = '<div class="infotitle">'+title+'</div>';
		
		if (num_images < 1 && num_videos < 1 && num_links < 1) {
			var html = m_html + '<div class="infowindow">' + back_html + html_title + desc_html + '</div>';
			max_content_div.innerHTML = '<div class="infowindow">' + back_html + description + '</div>';
		}
		else {
			var html = createTabbedWindow(title, description, id, theme_id, num_images, num_videos, num_links, num_threed_images);
			max_content_div.innerHTML = '<div class="infowindow">' + back_html + html + '</div>';
			html = m_html + '<div class="infowindow">' + back_html + html_title + html  + '</div>';
		}
		
		marker.openInfoWindowHtml(html, {maxContent: max_content_div, maxTitle: max_title, maxWidth: 410});
		
		var iw = map.getInfoWindow();
        GEvent.addListener(iw, "restoreend", function() {
        	document.getElementById('streetview-control').style.display = 'block';
        	document.getElementById('shortcuts-control').style.display = 'block';
        	document.getElementById('topten-control').style.display = 'block';
			
			if (sidebar_status == 'max' && !$('sidebar').visible()) {
				toggleSidebar('max');
			}
			
			$$('.infowindow').each(function(s){ s.setStyle({width: '320px'}); })
			$$('.infowindow-content').each(function(s){ s.setStyle({width: '310px'}); })
			
			$$('.infowindow-tab').each(function(s){ s.removeClassName('infowindow-tab'); })
			$$('.infowindow-html-content').each(function(s){ s.setStyle({ display:'none', height: '230px' }) })
			
			if ($('tab-info' + '-' + _theme_id + '-' + id)) {
				$('tab-info-' + theme_id + '-' + id).removeClassName('infowindow-tab-inactive');
				$('tab-info-' + theme_id + '-' + id).addClassName('infowindow-tab');
				
				$('info-' + theme_id + '-' + id).setStyle({ display:'block' });
				$('long-description-' + theme_id + '-' + id).setStyle({ display:'none' });
				$('short-description-' + theme_id + '-' + id).setStyle({ display:'block' });
			}
			
          });
          
          
        GEvent.addListener(iw, "maximizeend", function() {
			document.getElementById('streetview-control').style.display = 'none';
			document.getElementById('shortcuts-control').style.display = 'none';
			document.getElementById('topten-control').style.display = 'none';
			
			if (!$('sidebar').visible()) {
				map.panBy(new GSize(114,0));
			}
			
			if (fullscreen == 1 || _theme_id != 3) {
				$$('.infowindow').each(function(s){ var width = (s.ancestors()[0].getWidth()-15); s.setStyle({width: width+'px', marginLeft: '5px'}); });
				$$('.infowindow-content').each(function(s){ var width = (s.ancestors()[0].getWidth()); s.setStyle({width: (width-10)+'px'}); });
			}
			else {
				$$('.infowindow').each(function(s){ var width = s.setStyle({width: '400px', marginLeft: '0px'}); });
				$$('.infowindow-content').each(function(s){ s.setStyle({width: '400px'}); });
				iw.disableMaximize();
			}
			
			if (_tab != 'info' && $(_tab + '-' + _theme_id + '-' + _id)) {
				var url = 'utilities/hh400.'+_tab+'s.php?size=thmb&idx='+_theme_id+'&rec='+_id;
				
				GDownloadUrl(url, function(data, responseCode) {
				
					if (responseCode == 200) {
						if ($(_tab + '-' + _theme_id + '-' + _id)) {
							$(_tab + '-' + _theme_id + '-' + _id).innerHTML = data;
						}
					}
					
				});
			}
			
			if (_tab == 'info') {
				if ($('long-description-' + _theme_id + '-' + _id)) {
					$('long-description-' + _theme_id + '-' + _id).setStyle({ display:'block' });
					$('short-description-' + _theme_id + '-' + _id).setStyle({ display:'none' });
				}
			}
			
			$$('.infowindow-tab').each(function(s){ s.removeClassName('infowindow-tab'); })
			$$('.infowindow-html-content').each(function(s){ s.setStyle({ display:'none', height: '300px' }) })
			
			if ($('tab-' + _tab + '-' + _theme_id + '-' + _id)) {
				$('tab-' + _tab + '-' + _theme_id + '-' + _id).removeClassName('infowindow-tab-inactive');
				$('tab-' + _tab + '-' + _theme_id + '-' + _id).addClassName('infowindow-tab');
				
				$(_tab + '-' + _theme_id + '-' + _id).setStyle({ display:'block' });
			}
			
        });
          
          
        GEvent.addListener(iw, "closeclick", function() {
			document.getElementById('streetview-control').style.display = 'block';
			document.getElementById('shortcuts-control').style.display = 'block';
			document.getElementById('topten-control').style.display = 'block';
			
			if (sidebar_status == 'max' && !$('sidebar').visible()) {
				toggleSidebar('max');
			}
			
			$$('.infowindow').each(function(s){ s.setStyle({width: '320px', marginLeft: '0px'}); })
			$$('.infowindow-content').each(function(s){ s.setStyle({width: '310px'}); })
			
          });
	});
	
	return marker

}


function createTabbedWindow(title, description, id, theme_id, num_images, num_videos, num_links, num_threed_images) {

	var main_div = new Element('div');
	
	var wrapper_div = new Element('div', {'class':'infowindow-sidebar-wrapper'});
	wrapper_div.setStyle({ position: 'relative', marginTop: '5px' });
	
	var window_content = new Element('div', {'class': 'infowindow-content'});
	wrapper_div.insert(window_content);
	
	var window_sb_w = new Element('div', {'class': 'infowindow-sidebar-tabs-wrapper'});
	window_content.insert(window_sb_w);
	
	var window_html = new Element('div', {'class': 'infowindow-html'});
	window_content.insert(window_html);
	
	var ul_sidebar = new Element('ul', {'class': 'infowindow-sidebar'});
	window_sb_w.insert(ul_sidebar);
	
	var li_info = new Element('li');
	li_info.innerHTML = '<a href="javascript:void(0);" onClick="javascript:tabWindow(\'info\','+theme_id+','+id+'); return false;" class="infowindow-tab" id="tab-info-'+theme_id+'-'+id+'">' + 
						'<img src="images/library/icons/icon-help-theme-header.gif" alt="Info" align="absmiddle" />'+
						' Info</a>';
	ul_sidebar.insert(li_info);
	
	var info_content = new Element('div', {'id': 'info-'+theme_id+'-'+id, 'class': 'infowindow-html-content'});
	
	var long_desc = new Element('div', {'id': 'long-description-'+theme_id+'-'+id});
	long_desc.setStyle({display:'none', textAlign: 'left'});
	
	var short_desc = new Element('div', {'id': 'short-description-'+theme_id+'-'+id});
	short_desc.setStyle({textAlign: 'left'});
	
	long_desc.insert(description);
	
	if (description.length > 550) {
		var desc_html = description.substr(0,550) + '...' + '<br/><br/><a href="javascript:void(0);" onClick="javascript:tabWindow(\'info\','+theme_id+','+id+'); return false;" class="more-link">More</a><br/><br/>';
		short_desc.insert(desc_html);
	}
	else {
		short_desc.insert(description);
	}
	
	info_content.insert(long_desc);
	info_content.insert(short_desc);
	window_html.insert({top:info_content});
	
	if (num_images > 0) {
		var li_image = new Element('li');
		li_image.innerHTML = '<a href="javascript:void(0);" onClick="javascript:tabWindow(\'image\','+theme_id+','+id+'); return false;" class="infowindow-tab-inactive" id="tab-image-'+theme_id+'-'+id+'">' + 
							'<img src="images/library/icons/icon-image.gif" alt="Image(s)" align="absmiddle" />'+
							' Image(s)</a>';
		ul_sidebar.insert({bottom:li_image});
		
		var image_content = new Element('div', {'id': 'image-'+theme_id+'-'+id, 'class': 'infowindow-html-content'});
		image_content.setStyle ( { display: 'none', textAlign: 'center' } );
		image_content.innerHTML = "Loading...";
		window_html.insert({top:image_content});
	}
	
	if (num_videos > 0) {
		var li_video = new Element('li');
		li_video.innerHTML = '<a href="javascript:void(0);" onClick="javascript:tabWindow(\'video\','+theme_id+','+id+'); return false;" class="infowindow-tab-inactive" id="tab-video-'+theme_id+'-'+id+'">' + 
							'<img src="images/library/icons/icon-movie.gif" alt="Video(s)" align="absmiddle" />'+
							' Video(s)</a>';
		ul_sidebar.insert({bottom:li_video});
		
		var video_content = new Element('div', {'id': 'video-'+theme_id+'-'+id, 'class': 'infowindow-html-content'});
		video_content.setStyle ( { display: 'none', textAlign: 'center'} );
		video_content.innerHTML = "Loading...";
		window_html.insert({top:video_content});
	}
	
	if (num_links > 0) {
		var li_links = new Element('li');
		li_links.innerHTML = '<a href="javascript:void(0);" onClick="javascript:tabWindow(\'link\','+theme_id+','+id+'); return false;" class="infowindow-tab-inactive" id="tab-link-'+theme_id+'-'+id+'">' + 
							'<img src="images/library/icons/icon-link.gif" alt="Link(s)" align="absmiddle" />'+
							' Link(s)</a>';
		ul_sidebar.insert({bottom:li_links});
		
		var link_content = new Element('div', {'id': 'link-'+theme_id+'-'+id, 'class': 'infowindow-html-content'});
		link_content.setStyle ( { display: 'none'} );
		window_html.insert({top:link_content});
	}
	
	if (num_threed_images > 0) {
		var threed_links = new Element('li');
		threed_links.innerHTML = '<a href="javascript:void(0);" onClick="javascript:tabWindow(\'3d\','+theme_id+','+id+'); return false;" class="infowindow-tab-inactive" id="tab-3d-'+theme_id+'-'+id+'">' + 
							'<img src="images/library/icons/icon-3d.gif" alt="3D Models" align="absmiddle" />'+
							' 3D</a>';
		ul_sidebar.insert({bottom:threed_links});
		
		var threed_content = new Element('div', {'id': '3d-'+theme_id+'-'+id, 'class': 'infowindow-html-content'});
		threed_content.setStyle ( { display: 'none', overflow: 'hidden'} );
		window_html.insert({top:threed_content});
	}
	
	wrapper_div.setStyle ( { display: 'block' });
	
	main_div.insert(wrapper_div);
	
	return main_div.innerHTML;

}


/***
Cluster Marker InfoWindow Function
***/
var r_address;
function clusterInfo(cluster, theme, title) {

	idx_cluster = cluster;

	var window_content = new Element('div', {'class': 'infowindow', 'id':'cluster-window'});
	var html ="";
	var num_markers = cluster.clusteredMarkers.length;
	
	if (cluster.clusteredMarkers.length > 10) {
	
		var bounds = new GLatLngBounds();
		var m_counter = 0;
		var key = [];
		var keycounts = [];
		var nodes = [];
		var n_count = [];
		var focus_html = "";
		
		function arrContains(a, obj){
		  for(var i = 0; i < a.length; i++) {
		    if(a[i] === obj){
		      return true;
		    }
		  }
		  return false;
		}
				
		for (var i=0; i < cluster.clusteredMarkers.length; i++) {
			var point = cluster.clusteredMarkers[i].getLatLng();
			var f_lat = point.lat();
			var f_lng = point.lng();
			var hash = f_lat+':'+f_lng;
			
			var key = hash;
			keycounts[key] = 0
			
			bounds.extend(point);
		}
		
		for (var i=0; i < cluster.clusteredMarkers.length; i++) {
			var point = cluster.clusteredMarkers[i].getLatLng();
			var f_lat = point.lat();
			var f_lng = point.lng();
			var hash = f_lat+':'+f_lng;
			
			var key = hash;
			if (keycounts[key]) keycounts[key]++;
			else (keycounts[key]) = 1;
			
			if (keycounts[key] > 3 && !arrContains(nodes,key)) {
				nodes.push(key);
			}
			
		}
		
		if (nodes.length > 0) { 
			focus_html += '<br/><br/><span class="infotitle">Take a Shortcut:</span><br/><span>It looks like there are several points close to each other near:</span><br/>';
		}
		
		for (var i=0; i<nodes.length; i++) {
			var h_p = nodes[i];
			var coords = h_p.split(':');
			var f_lat = coords[0];
			var f_lng = coords[1];
			var t_lat = f_lat.replace(/./,'-');
			var t_lng = f_lng.replace(/./,'-');
			getAddress(null, new GLatLng(f_lat,f_lng));
		}
		
			
		cluster_bounds = bounds;
	
		html = '<span class="infotitle">There are too many points of interest to list here</span>' +
				'<br/>'+cluster.clusteredMarkers.length+' points total<br/><br/>' +
				'<a href="javascript:void(0);" onClick="javascript:zoomTo(cluster_bounds);map.closeInfoWindow();" class="cluster-link">' +
				'<img src="images/library/icons/icon-zoom-small.gif" alt="Zoom In" align="absmiddle" /> ' +
				'Click here to zoom in for a closer look</a> on ' + title + focus_html;
	}
	else {
		html = '<span class="infotitle">There are ' + num_markers+ ' points of interest here.</span>' +
				'<br/>Select one from the list below:<br/><ol class="cluster-list">';
		
		for (var i=0; i < cluster.clusteredMarkers.length; i++) {
		
			var cl_marker = cluster.clusteredMarkers[i];
			var title = cl_marker.getTitle(0);
			html += '<li><a href="javascript:void(0);" onClick="javascript:map.savePosition();clWindow('+i+');">'+title+'</a></li>';
		}
		
		html += '</ol>';
		html +=  'Or you can '+
				'<a href="javascript:void(0);" onClick="javascript:map.zoomIn();map.closeInfoWindow();" class="cluster-link">' +
				'<img src="images/library/icons/icon-zoom-small.gif" alt="Zoom In" align="absmiddle" /> zoom in</a> ' +
				'for a closer look.';
	}
	
	html +=  '</div>';
	
	window_content.insert(html);
	
	map.openInfoWindow(cluster.clusterMarker.getLatLng(), window_content);
	
}


/***
Reverse Geocoder
***/
function getAddress(overlay, latlng) {
  if (latlng != null) {
    address = latlng;
    geocoder.getLocations(latlng, showAddress);
  }
}

/***
Process the reverse geocoded address
***/
function showAddress(response) {

	var focus_html = "";

  if (response && response.Status.code == 200) {
  	var add_els = new Array();
    var place = response.Placemark[0];
	try { locality = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName; add_els.push(locality); }
	catch (e) {
		try { locality = place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName; add_els.push(locality); }
		catch (e) { locality = null;} 
	}
	try { region = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName; add_els.push(region); }
	catch (e) { region = null;} 
	country = place.AddressDetails.Country.CountryNameCode; add_els.push(country);

	var r_address = add_els.join(', ');
	
    var f_lat = place.Point.coordinates[1];
    var f_lng = place.Point.coordinates[0];
    focus_html += '<a href="javascript:void(0);" class="cluster-link" onClick="javascript:map.setCenter(new GLatLng('+f_lat+','+f_lng+'), 16);">'+r_address+'</a><br/>';
	if ($('cluster-window')) {
		$('cluster-window').insert({bottom:focus_html});
		map.updateInfoWindow();
	}
  }
}



function clWindow(idx) {
	var marker = idx_cluster.clusteredMarkers[idx];
	
	idx_cluster.refreshEnabled=false;
	map.removeOverlay(marker._parentCluster);
	map.addOverlay(marker);
	//marker._isActive=true;
	//marker._isVisible=true;
	
	sel_cluster_marker = marker;
	
	GEvent.trigger(marker, 'click');
	
	var k=GEvent.addListener(map, 'infowindowclose', function(){
		GEvent.removeListener(k);
		map.removeOverlay(marker);
		idx_cluster.refreshEnabled=true;
		map.addOverlay(sel_cluster_marker._parentCluster);
	});

	
}



/***
Zoom to the extent of an array of points
***/
function zoomToBounds(bnds, pt, ofst) {

	if (!bnds) {
		return false;
	}
	
	if (bnds.length < 2) {
		return false;
	}
	
	var zoom_offset = -1;
	
	if (ofst) {
		zoom_offset = ofst;
	}
	
	var tmp_bnds = new GLatLngBounds();
	
	for (var i=0; i < bnds.length; i++) {
		if (!pt) {
			var point = bnds[i].getLatLng();
		}
		else {
			var point = bnds[i];
		}
		tmp_bnds.extend(point);
	}
	
	
	map.setZoom(map.getBoundsZoomLevel(tmp_bnds) + parseInt(zoom_offset));
	var clat = (tmp_bnds.getNorthEast().lat() + tmp_bnds.getSouthWest().lat()) /2;
	var clng = (tmp_bnds.getNorthEast().lng() + tmp_bnds.getSouthWest().lng()) /2;
	map.setCenter(new GLatLng(clat,clng));

}


/***
Zoom to the extent of a GBounds
***/
function zoomTo(bounds) {

	//Zoom to the bounds of the markers
	//Set the map bounds based on the data that have been added...this guarantees that all points and the appropriate zoom are displayed every time
	map.setZoom(map.getBoundsZoomLevel(bounds));
	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	map.setCenter(new GLatLng(clat,clng));
						
}


/***
Add a top-ten marker
***/
function addTopMarker(theme, active_id) {

	var track_ids, theme_topten;

	switch (theme) {	
		case 'illustrations':
		aMarker(illustrations_overlay_ids, illustrations_topten, active_id);
		break;
		case 'origins':
		aMarker(origins_overlay_ids, origins_topten, active_id);
		break;
	}
	
	function aMarker(track_ids, theme_topten, active_id) {
		var a_marker = track_ids[active_id]; 
		map.addOverlay(a_marker);
		theme_topten.push(a_marker);
		GEvent.trigger(a_marker, "click");
		GEvent.addListener(a_marker, "infowindowclose", function() {
			map.removeOverlay(a_marker);
		});
	}
}


/***
Open the info window for a marker using the Click Event trigger
***/
function clickMarker(a_marker) {

	GEvent.trigger(a_marker, "click");
	
}