/*

		When I've gotta eat
		wouldn't it be neat
		if I could know
		where my friends go?
		
		Now I can.  It's Fodabo.
			
			by Jeff Carpenter
			Javascript advice from
				"Web Programming" Stepp, Miller, Kirst
				http://www.quirksmode.org/js/events_properties.html#target
				http://www.w3schools.com/js/default.asp
				http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element
				http://www.hscripts.com/tutorials/javascript/dom/select-events.php
				
*/

window.onload = initialize;

/*------------------------Declare-somma-them-glow-bull-variables---------*/
var choice = ''; 

var id = 'default';

/*--------------------------------------------------------------Initialize------*/
function initialize() {
	// Hall Choices
	$('fosschoice').observe("click", away_message );
	$('danachoice').observe("click", away_message );	
	$('bobschoice').observe("click", away_message );
	// Meal Choices	
	$('lunch').observe("click", away_message );		
	$('dinner').observe("click", away_message );
	
	// fStatus
	$('V').observe("click", show_fstatus );	

	// Determine default meal
	defaultmeal();
	
	away_update()
}


function away_update() {
	birth("Send a <span class='meal'>message</span> to <a href='mailto:hello@jeffcarp.com' class='meal' style='color:#000'>hello@jeffcarp.com</span>.");
	birth("<span class='meal'>If</span> you need <span class='meal'>somebody</span> to <span class='meal'>talk</span> to,");
	birth("Sorry, <span class='meal'>Fodabo</span> is <span class='meal'>closed</span> for the <span class='meal'>summer</span>.");
}

function away_message() {
	alert("sorry, we're away.");
}



function option_meal(option) {
	
	var timearray = new Array();
	timearray[0] = "00";
	timearray[1] = "15";
	timearray[2] = "30";
	timearray[3] = "45";
	
	if (option == 'lunch') var noption = 'dinner';
	if (option == 'dinner') var noption = 'lunch';

	// Change variable "meal"
	meal = option;
	
	// Option style is active
	$(option).setAttribute('class', 'meal_active');
	// Not-Option style is inactive
	$(noption).setAttribute('class', 'meal_inactive');
	fstatus('Meal Choice is '+meal);

	// Get the time selection box
	var timeselect = $("option_select_time");
	
	// Clear all options from timeselect
	if (timeselect.hasChildNodes())	{
	    while (timeselect.childNodes.length >= 1) {
	        timeselect.removeChild(timeselect.firstChild);       
	    } 
	}

	var newOption = document.createElement('option');
	newOption.innerHTML = "When?";
	$("option_select_time").appendChild(newOption);

	if (option == 'lunch') {
		// Populate timeselect with lunch times
		for (hrs=11;hrs<=14;hrs++) {
			for (min=0;min<=3;min++) {
				var newOption = document.createElement('option');
				//newOption.setAttribute('value', '1700');
				if (hrs == 13) hrs = "1";	
				if (hrs == 14) hrs = "2";	
				newOption.innerHTML = hrs+":"+timearray[min];
				if (hrs == "1") hrs = 13;
				if (hrs == "2") hrs = 14;					
				$("option_select_time").appendChild(newOption);
			}
		}
	}

	if (option == 'dinner') {
		// Populate timeselect with dinner times
		for (hrs=5;hrs<=8;hrs++) {
			for (min=0;min<=3;min++) {
				var newOption = document.createElement('option');
				newOption.setAttribute('value', hrs+":"+timearray[min]);	
				newOption.innerHTML = hrs+":"+timearray[min];
				$("option_select_time").appendChild(newOption);
			}
		}
	}
}

function birth(message) {


	// Create outer update container (need 2 divs for anim to work properly)
	var newEntry = document.createElement('div');
	// Set display to none (animation)
	newEntry.setAttribute('style', 'display:none;');

	var innerdiv = document.createElement('div');
	
	// Set a random box color
	var randint = Math.floor(Math.random()*3)
	if (randint == 0) { innerdiv.setAttribute('class', 'updateFoss'); }
	if (randint == 1) { innerdiv.setAttribute('class', 'updateDana'); }
	if (randint == 2) { innerdiv.setAttribute('class', 'updateBobs'); }	


	// Insert text
	innerdiv.innerHTML = message;
	
	newEntry.appendChild(innerdiv);

	// Add to timeline
	$('updatebox').insertBefore(newEntry, $('updatebox').firstChild);		

	new Effect.BlindDown(newEntry);
}

function defaultmeal() {
		var currentTime = new Date()
		var hours = currentTime.getHours()
		var minutes = currentTime.getMinutes()
		if (minutes < 10){
			minutes = "0" + minutes;
		}
		else {
			minutes = "" + minutes;
		}
//		alert(hours + minutes);
		if(hours + minutes > 1430){
			option_meal('dinner');
		} 
		else {
			//alert('lunchtime');			
			option_meal('lunch');
		}
}

function fstatus(message){
	var msg = document.createElement('p');
	msg.setAttribute('class', 'statusmessage');
	$("statusbox").appendChild(msg);
	msg.innerHTML = message;
}
	
function show_fstatus(){
	Effect.toggle('statusbox', 'slide');
}