// JavaScript Document


//declare the global variables
var alarmStatus=false;
var alarmTime=0;
var hour=0;
var minute=0;

//populate the minutes listbox
function populateMinutes(){

for (i=0;i<60;i++){
	 var optn = document.createElement("OPTION");
	 if (i<10){
	optn.text="0"+i;
	 }else{
	 optn.text=i;
	 }
	optn.value=i;
document.alarm.minute.options.add(optn);
}
}


// Function to generate and return a random number from 0 to 7
function randNum() {
	var screenNumber = Math.floor((7)*Math.random());
	return screenNumber;
}


// Function to return the lacal time zone
function getLocalTimeZone() {

	var d = new Date()
	var gmtHours = -d.getTimezoneOffset()/60;
	return gmtHours;
}

//Function returns a date object. The 'offset' parameter is the UTC offset for a specific time zone 

function timeZone(offset){
	date = new Date();
	localOffset = date.getTimezoneOffset() * 60000;	
	localTime=date.getTime();
	utc =localTime+localOffset;
	nd = new Date(utc + (3600000*offset));
	return nd;
}


// function to change the color of the screen behind the numbers
function changeScreen(newScreen,isRandom) {
	
	if (isRandom) {
	newScreen = randNum(); // get a random number from 0 to 7
	
	// check to make sure the new random number is not equal to 0 or to the old screen number, if it is either, get a new random number
	while (newScreen == oldScreen || newScreen == 0){ 
		newScreen = randNum();
	}
	
	// swap the current screen image with a different one, using random number or paramater
	}
	var screenChange = document.getElementById("screen");
	screenChange.src ='images/screen'+newScreen+'.png';
	oldScreen = newScreen;	
}

// generic function to return the time based on the time unit (hour, minute, second, month, or day) passed into the functions parameter (timeUnit)
function gettime(timeUnit) {
	currTime=timeZone(offset);
//	currTime = new Date();
	if (timeUnit == "hour") {
		display = currTime.getHours();
	} else if (timeUnit == "minute") {
		display = currTime.getMinutes();
	} else if (timeUnit == "second") {
		display = currTime.getSeconds();
	} else if (timeUnit == "month") {
		display = currTime.getMonth();
	} else if (timeUnit == "year") {
		display = currTime.getFullYear();
	}
	else {
		display = currTime.getDate() ;
	}
	return display;
}


/* This function takes a two digit number (hours, minutes, or seconds) and separates it into two separate numbers which are then used to display the correct image for each digit */
function separateNums(num, sourceID) {

// determine if the time is AM or PM
	if (num>12 && sourceID=="hours") {
		ampm = "pm"; // set time of day to 'pm'
		num=num-12; // change 24 hour numbers into 12 hour numbers
		}
		
		else if (num<12 && sourceID=="hours"){ 
		 ampm = "am"; // set time of day to 'am'
		}
		else {
			ampm=ampm;
		}		
		// if the hour is "00" change it to 12
		if (num == 0) {
			num=12;
		} else {
			num=num;
		}
	
	// get the hundreths part of the 2 digit number alone on left side of decimal
	var hundredths = Math.round(num)/10;  
	
	// convert this number into an integer
	var roundHundredths = parseInt(hundredths);  
	
	// get the tenths part of the number by subtracting the integer portion from the value of the "hundredths" variable and multiply by 10
	var tenths = Math.round((hundredths-roundHundredths)*10);
	
	// swap the image for the hundredths portion
	var photo = document.getElementById(sourceID+"1");
	photo.src ='images/'+roundHundredths+'.png';
	
	// swap the image for the tenths portion
	photo = document.getElementById(sourceID+"2");
	photo.src ='images/'+tenths+'.png';

	// swap the image for am or pm
	var halfOfDay = document.getElementById("am_pm");
	halfOfDay.src='images/'+ampm+'.png';
			
	}

// return the current date as a string
function date() {
	var day = gettime("day");
	var month = gettime("month");
	var year = gettime("year");
	var todaysDate = "Today's date is "+month+"/"+day+"/"+year;
	return todaysDate;
}


// set the alarm clock
function setAlarm() {
	// get the selected values from the form
	var checkTime=gettime("hour")*60+gettime("minute");
	var object=document.getElementById("alarm_light");
	var setAlarmButton=document.alarmSetBtn.button; //the button that turns the alarm on of off

	if (alarmStatus==true){
		alarmStatus=false; 
		object.src="images/alarm_off.png"; // switch the alarm light to off
		setAlarmButton.value="turn alarm on";
	} else {
		turnAlarmOn();
		var hourIndex=document.alarm.hour.selectedIndex;
		var minuteIndex=document.alarm.minute.selectedIndex;
		hour=parseInt(document.alarm.hour.options[hourIndex].value);
		minute=parseInt(document.alarm.minute.options[minuteIndex].value);
		alarmTime=hour*60+minute; // set the global variable alarmTime
		// alarmCookie();
	}
	alarmCookie();
	
}
function turnAlarmOn(){
		var object=document.getElementById("alarm_light");
		alarmStatus=true;
		object.src="images/alarm_on.png"; //switch the alarm light to on
		var setAlarmButton=document.alarmSetBtn.button; //the button that turns the alarm on of off
		setAlarmButton.value="turn alarm off";
		
}

// return the number of days for each month of the year
function daysInMonth(currMonth) {
var dayCount = new Array(31, 28, 31, 30, 30, 30, 31, 31, 30, 31, 30, 31);
return dayCount[currMonth];

}

// clear the 'setAlarmDay' listbox before adding new days
function clearDayList() {
document.getElementById("setAlarmDay").options.length=0;

}

// clear the 'setAlarmMonth' listbox before adding new months
function clearMonthList() {
document.getElementById("setAlarmMonth").options.length=0;

}

// return the current month as a string
function months(theMonth) {
	var monthsInYear =  new Array("January", "February", "March", "April", "May", "June", "JUly", "August", "September", "October", "November", "December");
	return monthsInYear[theMonth];
	}

// populate the first listbox with the months remaining in the year, including the current month

function populateMonth() { 
	clearMonthList();
	popMonth=gettime("month");

//	popMonth=thisMonth();
	for (i=popMonth;i<12;i++) {
		var opt = document.createElement("option");
		document.getElementById("setAlarmMonth").options.add(opt);
		month=months(i);
		opt.text = month;
        opt.value = i+1;
		
	}
	populateDay();// Once the months have been populated in to the listbox, run the 'populateDay()' function
	
	}
	
// based on the selected month, populate the drop list with the remaining days in that month
function populateDay() {
	
	clearDayList();// clear the listbox
	var selectedMonth=document.getElementById("setAlarmMonth").value;// get the selected month
	currMonth=gettime("month")+1;
	
	/* Check to see if the selected month is the current month. 
	If that is true, set i to the current day to begin populating 
	the listbox starting with the current day.
	Otherwise, set i to 0 to begin populating with the first day of the month. */
	if (selectedMonth==currMonth) {
		i=gettime("day")-1; 
	} else {
		i=0;
		}
	// call 'daysInMonth' function to return the number of days in selected month
	days=daysInMonth(selectedMonth-1); 
	
	// populate the 'setAlarmDay' listbox
	for (i;i<days;i++) {
	
		var opt = document.createElement("option");
		document.getElementById("setAlarmDay").options.add(opt);
		
		opt.text = i+1;
        opt.value = i+1;

		}
}

/*function background_x(pos){
	obj= document.getElementById('container');
	obj.style.left = pos+"px";
	
}
*/

// get the x coordinate of an object
function xCoord(id) {
	object=document.getElementById(id);
	xc=parseInt(object.style.left);
	return xc;
}



function moveScreen(nav) {
	var old_x=xCoord('container');
	alert(old_x);
//	var new_x=pos;
	
	if (nav=="nav1") {
		new_x=0;
	}
	else if (nav=="nav2") {
		new_x=-1000;
	}
	else if (nav=="nav3") {
		new_x = -2000;
	}
	else if (nav=="nav4") {
		new_x = -3000;
	}
	else if (nav=="nav5") {
		new_x = -4000;
	}
	if (new_x < old_x){
		while(new_x !== old_x){
//		alert(old_x+" "+ new_x);			
		shiftIt('container',-10,0)
		setTimeout(old_x=old_x-10,30);
		//old_x=old_x-10;
		}
	} else if (new_x > old_x){
		while(new_x !== old_x){
//		alert(old_x+" "+ new_x);
		shiftIt('container',10,0)
		setTimeout(old_x=old_x+10,30);
//		setTimeout("shiftIt('container',10,0)",300);
//		old_x=old_x+10;
		}
	}
}
/*
function changePos(toPos){
	var object = document.getElementById('container');
	var pos = parseInt(object.style.left);
	var change = toPos-pos;
	var total = pos+Math.ceil((change/2));
	object.style.left=total+'px';
	function c() {
		changePos(toPos);
	}
	if(change==0) {
		clearTimeout(timer);
		return;
	}
	timer=setTimeout(c,30);
	alert("hi");
}
*/
function changePos(toPos){
	var object = document.getElementById('container');
	var pos = parseInt(object.style.left);
	var change = toPos-pos;
//	alert("change= "+change);
	var total = pos+Math.ceil((change/2));
//	object.style.left=total+'px';
	if (change != -1) {
		total=total;
		shiftIt('container',total);
		number=toPos;
		setTimeout("changePos(number)",100);
	} 
}





function shiftIt(id, dx) {
	object=document.getElementById(id);
	object.style.left=dx+'px';
/*	object.style.top=yCoord(id)+dy+"px"; */
}

function alarmCookie(){
	var thisday = new Date();
	var expdate = new Date(thisday.getTime() + 7 * 24 * 60 * 60 * 1000);
	expdate.toGMTString();
	document.cookie ="time = "+alarmTime+"; expires = "+expdate;
	document.cookie = "alarmStatus = "+alarmStatus+"; expires = "+expdate;
	document.cookie = "hour = "+hour+"; expires = "+expdate;
	document.cookie = "minute ="+minute+";expires = "+expdate;
}

function getAlarmCookie(val){
	var cookie = null;
	var findVal = val + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		var start = dc.indexOf(findVal);
		if (start >= 0) {
			start += findVal.length;
			lastVal = dc.indexOf(";", start);
			if (lastVal == -1) {
				lastVal = dc.length;
			}
			cookie = (dc.substring(start, lastVal));
		}else {
			return cookie;
		}
	}
	return cookie;
}

function setAlarmByCookie(){
	alarmStatus= getAlarmCookie("alarmStatus");
	alarmTime= parseInt(getAlarmCookie("alarmTime"));
	hour= parseInt(getAlarmCookie("hour"));
	minute= parseInt(getAlarmCookie("minute"));
	
	var hourValue = document.alarm.hour;
	var minuteValue = document.alarm.minute;
	hourValue.selectedIndex=hour;
	minuteValue.selectedIndex=minute;
	if (alarmStatus=="true"){
		alarmStatus=true;
		turnAlarmOn();
		alert("Your alarm is ON");
	}
	else{
		alert("Your alarm is OFF");
	}
	
}

function checkAlarmStatus(){
	alert(alarmTime);
}

//	set local timezone when page loads
	var offset = getLocalTimeZone();	
	
var oldScreen = 1;

// preload button bar images
if (document.images) {
	var ImgOver = new Array();
	ImgOver [0] = new Image();
	ImgOver [1] = new Image();
	ImgOver [2] = new Image();
	ImgOver [3] = new Image();
	ImgOver [4] = new Image();
	ImgOver [5] = new Image();

	ImgOver[0].src = "images/nav_02_over.png";
	ImgOver[1].src = "images/nav_02_over.png";
	ImgOver[2].src = "images/nav_03_over.png";
	ImgOver[3].src = "images/nav_04_over.png";
	ImgOver[4].src = "images/nav_05_over.png";
	ImgOver[5].src = "images/nav_06_over.png";


	var ImgOut = new Array();
	ImgOut[0] = new Image();
	ImgOut[1] = new Image();
	ImgOut[2] = new Image();
	ImgOut[3] = new Image();
	ImgOut[4] = new Image();
	ImgOut[5] = new Image();

	ImgOut[0].src = "images/nav_02.png";
	ImgOut[1].src = "images/nav_02.png";
	ImgOut[2].src = "images/nav_03.png";
	ImgOut[3].src = "images/nav_04.png";
	ImgOut[4].src = "images/nav_05.png";
	ImgOut[5].src = "images/nav_06.png";

}

// preload piano key images
if (document.images) {
	var whiteKey = new Array();
	whiteKey [0] = new Image();
	whiteKey [1] = new Image();
	

	whiteKey[0].src = "images/white_key_down.png";
	whiteKey[1].src = "images/white_key_up.png";
	


	var blackKey = new Array();
	blackKey[0] = new Image();
	blackKey[1] = new Image();
	

	blackKey[0].src = "images/black_key_down.png";
	blackKey[1].src = "images/black_key_up.png";
	

}

// preload macro images
if (document.images) {
	var macros = new Array();
	
for (i=0;i<13;i++) {
	macros [i] = new Image();
	macros [i].src = "images/macro-"+(i+1)+".jpg";	
}
	var macro_counter=0; //set counter to 0 for macro photo frame
}

// create array to contain answers to macro photo quiz
var answers = new Array("rice", "guitar", "boot", "match", "colored pencil", "bread", "light bulb", "candy", "paint brush", "toothpaste", "fork", "guitar string", "mushroom");

var scramble = new Array("icer", "triuag", "tobo", "chatm", "doerloc cpilne", "redab", "tighl lubb", "dancy", "tipan shrbu", "phatoosteh", "rofk", "tuirag rtsngi", "shuromom");
var score=0;

function submitAnswer(){
	if (document.form1.answer.value==answers[macro_counter]){
		changeColor('results', 'green');
		document.form1.results.value="Yes, you are correct!";
		score=score+1;
	}else{
		changeColor('results', 'red');
		document.form1.results.value="Sorry, try again.";
		document.form1.answer.value="type your answer here";
	}



}

// function to swap button image when the mouse moves over image
function RollOver(i) {
	if(document.images) document.images[i-1].src = ImgOver[i-1].src;
}

// function to swap button image when the mouse moves over image
function whiteKeyDown(id) {
	var object=document.getElementById(id);
	
	if(document.images) object.src =  whiteKey[0].src;
}

function whiteKeyUp(id) {
	var object=document.getElementById(id);
	
	if(document.images) object.src =  whiteKey[1].src;
}

function blackKeyDown(id) {
	var object=document.getElementById(id);
	
	if(document.images) object.src =  blackKey[0].src;
}

function blackKeyUp(id) {
	var object=document.getElementById(id);
	
	if(document.images) object.src =  blackKey[1].src;
}

// function to swap button image source when the mouse moves off image
function RollOut(i) {
	if(document.images) document.images[i-1].src = ImgOut[i-1].src;
}

// function to change the color of the "choose a new screen color" menu options
function changeColor(id, color) {
	var object=document.getElementById(id);
	object.style.color=color;
}


function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function playFlashMovie(note)
{
	var flashMovie=getFlashMovieObject(note);
	flashMovie.Play();
}

function nextMacro(){
	if(macro_counter < 12){
	macro_counter=macro_counter+1;
	swapMacro(macro_counter);
	}else{
		alert('Sorry, no more pictures!');
	}
}

function prevMacro(){
	if(macro_counter > 0){
	macro_counter=macro_counter-1;
	swapMacro(macro_counter);
	}else{
		macro_counter=0;			
		//document.form1.counter.value=num;

		alert('Ooops, no more pictures that way!');
	}
}

function swapMacro(num) {
	var object=document.getElementById("picture");
	//document.form1.counter.value=num;
	if(document.images) object.src = macros[num].src;
	changeClue(num);
	clearResults();
}
//		alert(macro_counter);
	
function changeClue(clue) {
	document.form1.clue.value=scramble[clue];
}

function clearResults(){
	document.form1.results.value=null;
	document.form1.answer.value=null;
}

