// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Call this function when the page has been loaded
function initialize()
{
	lat = 32.207911;
	lng = -110.966614;
	map = new google.maps.Map2(document.getElementById("map"));
	gdir = new GDirections(map, document.getElementById("directions"));
	map.setCenter(new google.maps.LatLng(lat-0.1, lng-0.1), 14);
	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	address = "Iglesia Central de Tucson<br/>1101 South 5th Avenue<br/>Tucson Arizona 85701<br/>(520) 622-1441<br/><br/> Horario:<br/>Miércoles 7-9pm<br/>Viernes 7-9pm<br/>Sábados 9am-12pm<br/><br/>Jóvenes - Sábados 6-9pm"

	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point);

	GEvent.addListener(marker, "click",
		function() {
			marker.openInfoWindowHtml(address);
		});

	map.addOverlay(marker);
	marker.openInfoWindowHtml(address);
		
	//window.resizeBy(1024-window.innerWidth, 768-window.innerHeight);
	
	get_today_bible_verse();
		
	setActiveButton('location');
	setActiveWindow('location');
}

function setDirections(fromAddress, toAddress, locale)
{
	inst = document.getElementById('instructions');
	inst.style.display = 'none';
	
	inst = document.getElementById('directions');
	inst.style.backgroundColor = '#bbddee';
	
	gdir.load("from: " + fromAddress + " to: " + toAddress,
	                { "locale": locale });
}

// Translate errors to spanish
function handleErrors() 
{
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     	alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

   //else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  //<--- Doc bug... this is either not defined, or Doc is wrong
//		 alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
   else alert("Ocurrio un error desconocido.");

}

function onGDirectionsLoad()
{ 
	      // Use this function to access information about the latest load()
	      // results.

	      // e.g.
	      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
		  // and yada yada yada...
}

function showBibleVerse()
{
	window.open('/daily_bible/link');
}

function showNews(id)
{
	window.open(id, '');
}

function showCalendar()
{
	window.open('http://ical.iglesiacentraldetucson.org');
}

function get_today_bible_verse()
{
	new Ajax.Updater('bibleyear-button', '/daily_bible/verse', 
		{ method: 'get'});
}

function get_beliefs_list()
{
	new Ajax.Updater('belief-container', '/beliefs/view', 
		{ method: 'get',
		  onComplete: function(transport)
			{
				setActiveWindow('belief');
				showBelief(1);
			}
		});
}

function getContainerView(divId)
{
	new Ajax.Updater(divId + '-container', divId + '/view', { method: 'get'});
}

function showBelief(id)
{
	var item = document.getElementById("belief-item-"+id);
	
	if(activeItem != null)
	{
		activeItem.style.backgroundColor = "#bbddee";
	}
	
	if(item != null)
	{
		item.style.backgroundColor = "#ccc900";
		activeItem = item;
	}
	
	new Ajax.Updater('belief-description', '/beliefs/item/'+id, 
		{ method: 'get'});
}

function showArchive(id)
{
	var item = document.getElementById("video-item-"+id);
	
	if(activeItem != null)
	{
		activeItem.style.backgroundColor = "#bbddee";
	}
	
	if(item != null)
	{
		item.style.backgroundColor = "#ccc900";
		activeItem = item;
	}
	
	new Ajax.Updater('archive-description', '/archive/description/'+id, 
		{ method: 'get'});
}

function showVideo(id)
{
	new Ajax.Updater('video-container', '/archive/item/'+id, 
		{ method: 'get'});
}

function showSelection()
{
	new Ajax.Updater('archive-selection', '/archive/selection/', 
		{ method: 'get'});
}

function showLocalNews()
{
	new Ajax.Updater('lnews-container', '/news/local', 
		{ method: 'get'});
}

function showWindow(divId, options)
{
	if(divId != activeWindow)
	{
		switch(divId)
		{
			case 'belief':
				//setActiveWindow(divId);
				setActiveButton(divId);
				get_beliefs_list();
				break;
				
			case 'video':
				setActiveWindow(divId);
				if(options != "undefined")
				{
					showVideo(options);
				}
				break;
				
			case 'contact':
				setActiveWindow(divId);
				setActiveButton(divId);
				break;
				
			case 'pastor':
				setActiveWindow(divId);
				setActiveButton(divId);
				break;
				
			case 'archive':
				setActiveWindow(divId);
				setActiveButton(divId);
				getContainerView(divId);
				showSelection();
				break;
				
			case 'lnews':
				setActiveWindow(divId);
				setActiveButton(divId);
				showLocalNews();
				break;
				
			default:
				setActiveWindow(divId);
				setActiveButton(divId);
				getContainerView(divId);
				break;
		}
	}
}

function showSpeaker()
{
	speakerList = document.getElementById('speaker-list');
	selIndex = speakerList.selectedIndex;
	
	new Ajax.Updater('archive-container', 'archive/speaker/?name=' + speakerList[selIndex].value,
		{ method: 'get'});
}

function closeWindow(divId)
{
	if(divId != null)
	{
		Effect.Fade(divId+'-window', {duration: 0.3});
		setActiveButton(null);
		setActiveWindow(null);
	}
}

function setActiveButton(divId)
{
	var button = null;
	var active = null;
	
	if(divId != null)
	{
		button = document.getElementById(divId+'-button');
	}
	
	if(activeButton != null)
	{
		active = document.getElementById(activeButton+'-button');
		active.style.borderStyle = "outset";
		activeButton = null;
	}
	
	if(activeButton != button && divId != null)
	{
		button.style.borderStyle = "inset";
		activeButton = divId;
	}
}

function setActiveWindow(divId)
{
	if(divId != null)
	{
		closeWindow(activeWindow);
		activeWindow = divId;
		Effect.Appear(divId+'-window', { queue: 'end', duration: 0.3});
	}
	else
	{
		activeWindow = null;
	}
}

function changeBorder(divId, type)
{
	if(divId != null)
	{
		item = document.getElementById(divId+'-button');
		if(divId != activeButton && item != null)
		{
			if(type != null)
			{
				item.style.borderStyle = type;
			}
		}
	}
}

function setItemColor(divId, type)
{
	item = document.getElementById(divId);
	if(type == 'out')
	{
		if(item != activeItem)
		{
			item.style.backgroundColor = '#bbddee';
		}
	}
	else if(type == 'in' && item != activeItem)
	{
		item.style.backgroundColor = '#88aabb';
	}
}