/******************************************************************************************

Description:  MDP site-wide default scripts
Author:  Joseph Murray

Copyright 2008, Memory Disorders Program, Georgetown University

*******************************************************************************************/

var DEBUG_ALL = 4;
var DEBUG_MESSAGE = 3;
var DEBUG_ERROR = 2;
var DEBUG_CRITICAL = 1;
var DEBUG_MODE = DEBUG_MESSAGE;


/***** General *****/

function debug(mode) {
	return (DEBUG_MODE >= mode);
}

function message(text) {
	if (debug(DEBUG_MESSAGE)) {
		alert("MESSAGE: " + text);
	}
}

function error(text) {
	if (debug(DEBUG_ERROR)) {
		alert("ERROR: " + text);
	}	
}

function critical(text) {
	if (debug(DEBUG_CRITICAL)) {
		alert("CRITICAL: " + text)
	}
}

/***** Managing cookies *****/
/*
	The following three functions are borrowed from quirksmode.org
*/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/***** Events ******/

// when the page is loaded
function onReady() {
	
	// set font size based on user preferences
	var fontSize = parseInt(readCookie(FONT_SIZE_KEY));
	if (fontSize) {
		setFontSize(fontSize);
	}
	
	// determine location
	var page = document.location.href;
	setMenus(page);
	
	// load Google Map API
	loadGoogleMapApi();	
}

// when the search box gains focus
function onSearchFocus(searchTextBox) {
	$(searchTextBox).val('').removeClass('disabled');
}


/***** Managing styles *****/

var FONT_SIZE_KEY = 'mdp-font-size';
var MIN_FONT_SIZE = 8;

// get the body font size
function getFontSize() {
	return parseInt($('body').css('font-size'));
}

// set the body font size
function setFontSize(fontSize) {
	if (fontSize && !isNaN(fontSize)) {
		$('body').css('font-size', fontSize);
		createCookie(FONT_SIZE_KEY, fontSize);	
	}
}

// adjust the body font size based on a pixel size change, either positive or negative
function adjustFontSize(byPixels) {
	if (byPixels && !isNaN(byPixels)) {
		var fontSize = getFontSize();
		if ((fontSize + byPixels) >= MIN_FONT_SIZE) {
			setFontSize(fontSize + byPixels);
		}
	}
	
}

// highlights the approriate menu items
function setMenus(page) {
	var splits = page.split('/');
	var menuItem = splits[splits.length - 2];
	var pageItem = splits[splits.length - 1].split('.')[0];
	
	$("#" + menuItem).addClass('current');
	$("#" + pageItem).addClass('current');
	
}


/***** Google Maps API *****/

var map = null;
var gdir = null;
var geocoder = null;
var BASE_ADDRESS = "3800 Reservoir Rd NW, Washington, DC 20007";

function loadGoogleMapApi() {
	
	if (GBrowserIsCompatible()) {
		// load map
		map = new GMap2(document.getElementById("map"));

		// load directions
		gdir = new GDirections(map, document.getElementById("directions"));
		GEvent.addListener(gdir, "load", onGDirectionsLoad);
		GEvent.addListener(gdir, "error", handleErrors);		

		var point = null;
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			BASE_ADDRESS,
			function(point) {
				if (!point) {
					alert(BASE_ADDRESS + " not found!")
				} else {
					map.setCenter(point, 13);
				}
			}
		)
	}
}

function setDirections(fromAddress, toAddress, locale) {
	gdir.load("from: " + fromAddress + " to: " + toAddress, 
		{ "locale": locale });
}

function handleErrors() { 
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) 
		alert("No corresponding geographic location could be found for one of the specified addresses. Please try again.\nError code: " + gdir.getStatus().code);

 	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) 
		alert("Your request for directions could not be completed. Please try again at a later time.\n Error code: " + gdir.getStatus().code);

 	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) 
		alert("Please provide a starting and ending address\n Error code: " + gdir.getStatus().code);

	else
		alert("An unknown error occurred.");
}

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...
}
