function l(l) {
	if(window.console) {
		window.console.log(l);
	} else {
		alert(l);
	}
}

/* previous/next keys (P/left arrow, N/right arrow) */
document.observe('keydown', keyboardAction); 
function keyboardAction(event) {

	//make sure we aren't in an input or text area
	var tag = event.srcElement || event.target;
	if (tag.tagName && tag.tagName != "INPUT" && tag.tagName != "TEXTAREA" && tag.tagName != "SELECT") {
		var keycode = event.keyCode;
		var key = String.fromCharCode(keycode).toLowerCase();
		
		/*
		var context = 'item';
		if ($$('div[rel=calendar]')) { context = 'date' }
		else if ($('context')) { context = 'photo' }
		else if ($('pages')) { context = 'page' }
		*/
		
		if ((key == 'p') || (keycode == 37)){
			if ($('previous')) { window.location = $('previous').href  } else { notice('You\'re at the beginning'); }
		} else if ((key == 'n') || (keycode == 39)){ 
			if ($('next')) { window.location = $('next').href } else { notice('You\'re at the end'); }
		}
	}
	//if there's no previous/next pop a notice.
}

function validateComment(theForm) {
	if(theForm.name.value == '') {
		alert('What\'s your name?  Seriously.');
		return false;
	}
	if(theForm.comment.value == '') {
		alert('Why would you leave a blank comment?  Fill that thing out.');
		return false;
	}	
	return true;
}

function notice(message) {
	$('notice').update('<p>' + message + '</p>');
	if ($('notice').visible()) {
		new Effect.Highlight($('notice'), { startcolor:'#004080', endcolor: '#0066CC' });
	} else {
		Effect.BlindDown($('notice'), { duration: 0.3 });
	}
}

function makeMapDynamic(coords,zoom) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2($('map'));
		map.setCenter(new GLatLng(coords[0], coords[1]), zoom);
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		var point = new GLatLng(coords[0], coords[1]);
		map.addOverlay(new GMarker(point,new GIcon(G_DEFAULT_ICON)));
	}
}

function showDerekAt(coords,zoom,time) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2($('map'));
		var point = new GLatLng(coords[0], coords[1]);

		map.setCenter(new GLatLng((coords[0]-0+0.000350), coords[1]), zoom);
		map.setMapType(G_SATELLITE_MAP);
		
		//map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		
/*
		var baseIcon = new GIcon();
		baseIcon.iconSize = new GSize(44,56);
		baseIcon.iconAnchor = new GPoint(22,56);
		baseIcon.infoWindowAnchor = new GPoint(0,0);
		var derekIcon = (new GIcon(baseIcon, "images/faceMarker.png", null, ""));
*/
		
		var derekIcon = new GIcon();								
		derekIcon.image = "images/faceMarker.png";
		derekIcon.iconSize = new GSize(44,56);
		derekIcon.iconAnchor = new GPoint(22,56);
		derekIcon.infoWindowAnchor = new GPoint(0,0);
		derekIcon.infoWindowAnchor = new GPoint(0,0);
		
		var marker = new GMarker(point,{ icon:derekIcon });
		map.addOverlay(marker);
		
		GEvent.addListener(marker, "click", function() {
			//tweet or link to photo
			marker.openInfoWindowHtml(time, {pixelOffset:new GSize(100,1)});
		});
		//map.openInfoWindow(point, time, { pixelOffset:new GSize(0,0), maxWidth: 100 });
		return marker;

	}
}

function grabTweets() {
	//if($('twitter')) { 
	new Ajax.Request('/grabNewTweets.php', {
		onSuccess: function(newTweets) {
			if($('statusUpdates')) { 
				if (newTweets.responseText.length > 0) {
					//$('oldTweets').insert({ top: $$('#firstTweet div').first() });
					//$('firstTweet').insert({ top: newTweets.responseText });
					//new Effect.Highlight($$('#firstTweet div').first(), { startcolor:'#0066CC', endcolor: '#DEDEDE' });					
					$('statusUpdates').insert({top: newTweets.responseText });
					new Effect.Highlight($$('#statusUpdates div').first(), { startcolor:'#0066CC', endcolor: '#DEDEDE' });					
				}
			}
		}
	});
}
function grabPhoneLocation() {
	new Ajax.Request('/grabPhoneLocation.php', {});
}
document.observe('dom:loaded', grabTweets);
document.observe('dom:loaded', grabPhoneLocation);