/* To do

- BF under instructions
- Click on a virtue opens up an index of all virtues with descriptions (div)
- fix title clipping (same height for all header images/ different div)
- make CSS styles JS vars
- clarify instructions
- write history/ inspiration? 
			- incl pictures?
			- incl links to autobio on Powells
			- incl link to http://www.flamebright.com/PTPages/Benjamin.asp
			- incl links to http://www.diyplanner.com/templates/official/hpda/addons/franklin
			- incl link to http://www2.fi.edu/ ?
- about page
- page titles based on virtue?
- landscape?

*/

var quote1  = "Eat not to dullness; drink not to elevation.";
var quote2  = "Speak not but what may benefit others or yourself; avoid trifling conversation.";
var quote3  = "Let all your things have their places; let each part of your business have its time.";
var quote4  = "Resolve to perform what you ought; perform without fail what you resolve.";
var quote5  = "Make no expense but to do good to others or yourself; i.e., waste nothing.";
var quote6  = "Lose no time; be always employ'd in something useful; cut off all unnecessary actions.";
var quote7  = "Use no hurtful deceit; think innocently and justly, and, if you speak, speak accordingly.";
var quote8  = "Wrong none by doing injuries, or omitting the benefits that are your duty.";
var quote9  = "Avoid extremes; forbear resenting injuries so much as you think they deserve.";
var quote10 = "Tolerate no uncleanliness in body, clothes, or habitation.";
var quote11 = "Be not disturbed at trifles, or at accidents common or unavoidable.";
var quote12 = "Rarely use venery but for health or offspring, never to dulness, weakness, or injury of peace or reputation.";
var quote13 = "Imitate Jesus and Socrates.";

//var newuser = "false";

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var difference;

function countup(yr,m,d){
	var today   = new Date()
	var todayy  = today.getYear()
	if (todayy < 1000)
	todayy += 1900
	var todaym  = today.getMonth()
	var todayd  = today.getDate()
	var todaystring = montharray[todaym] + " " + todayd + ", " + todayy
	var paststring  = montharray[m-1] + " " + d + ", " + yr
	difference = (Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1)
	difference = Math.abs(difference);
	//difference+=" days"
	//document.write("It\'s been "+difference+" since the launch of JavaScript Kit!")
	//alert(difference);
}

function clearcookie() {
	obj.remove("cStartDate");
	obj.remove("cVirtueArray");
	var arr = new Array('"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"');
	obj.set("cVirtueArray",arr);
	//alert(obj.get("cVirtueArray"));
	//location.reload(true);
	location.href = "./"
	//alert("Reset!");
}

function confirmreset() {
	var answer = confirm ("Are you sure you wish to reset?  All data will be lost.")
	if (answer){
		clearcookie();
		//location.reload(true);
		location.href = "./"
	}
}

function settestdate() {
	var setdate = new Date(2009, 0, 1);  // actual month is +1			88 days max...
	var answer = confirm ("Are you sure you wish to set the start date to " + setdate + "?")
	if (answer){
		//obj.set("cStartDate", new Date(2008, 4, 10));
		obj.set("cStartDate", setdate);
		//alert("Start date set to " + setdate + ". " + (Math.abs(setdate.getDate() - TodaysDate.getDate())) + " days ago.");
		alert("Start date set to " + setdate);
		//clearcookie();
		//location.reload(true);
		location.href = "./"
	}
}

function toggle(cell) {
	tempbg = document.getElementById(cell).style.background;
	if (cell >= (StartDateDay) && cell <= (total_days+StartDateDay)) {
		//alert(document.getElementById(cell).style.background );
		//alert(cell + " " + (total_days+StartDateDay));
		if (document.getElementById(cell).style.background == 'repeat-x rgb(246, 246, 246)' ||
			document.getElementById(cell).style.background == 'rgb(246, 246, 246)' ||
			document.getElementById(cell).style.background == 'rgb(19, 91, 251) url(null) repeat scroll 0% 0%' ||
			document.getElementById(cell).style.background == 'url(http://reasoninteractive.com/tools/benfranklin/null) initial initial rgb(19, 91, 251)' ||
			document.getElementById(cell).style.background == 'url(http://reasoninteractive.com/tools/benfranklin/null) no-repeat rgb(19, 91, 251)' ||
			document.getElementById(cell).style.background == 'url(http://reasoninteractive.com/tools/benfranklin/null) no-repeat initial rgb(19, 91, 251)' ||
			document.getElementById(cell).style.background == 'url(http://reasoninteractive.com/tools/benfranklin/null) rgb(19, 91, 251)') {
			// asterisk: url(http://reasoninteractive.com/tools/bf13/img/asterisk-white.png) no-repeat initial rgb(19, 91, 251)
			// alert("no asterisk");
			document.getElementById(cell).style.background        = 'url(img/asterisk-white.png) no-repeat';
			document.getElementById(cell).style.backgroundColor   ="#135bfb";
			document.getElementById(cell).style.borderColor       = '#145bf7';
			document.getElementById(cell).style.borderRightColor  = '#145bf7';
			document.getElementById(cell).style.borderTopColor    = '#145bf7';
			document.getElementById(cell).style.borderBottomColor = '#145bf7';
			mySplitResult.splice(cell,1,'"t"');
		} else {
			//alert("asterisk");
			document.getElementById(cell).style.background        = 'url(' + null + ') no-repeat';
			//document.getElementById(cell).style.background      = 'url(img/bg-td-active.png) repeat-x';
			document.getElementById(cell).style.backgroundColor   ="#135bfb";
			document.getElementById(cell).style.borderColor       = '#145bf7';
			document.getElementById(cell).style.borderRightColor  = '#145bf7';
			document.getElementById(cell).style.borderTopColor    = '#145bf7';
			document.getElementById(cell).style.borderBottomColor = '#145bf7';
			mySplitResult.splice(cell,1,'"f"');
		}
	} else {
		alert("You may only edit days between the date you started tracking your virtues and today (the blue squares).");
	}	
	obj.set("cVirtueArray",mySplitResult);
}

function thisweek() {
	document.getElementById("v1").style.background        = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("1").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("2").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("3").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("4").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("5").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("6").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("7").style.background         = 'url(img/bg-td-active.png) repeat-x';
	document.getElementById("v1").style.color             = '#fff';
	document.getElementById("v1").style.borderLeftColor   = '#0973f2';
	document.getElementById("v1").style.borderColor       = '#0973f2';
	//document.getElementById("11").style.background      = 'url(img/bg-td-active.png) repeat-x';
	
	for (i=1;i<=7;i++) {
		document.getElementById(i).style.borderColor        = '#0973f2';
		document.getElementById(i).style.borderRightColor   = '#0973f2';
		document.getElementById(i).style.borderTopColor     = '#037afe';
		document.getElementById(i).style.borderBottomColor  = '#037afe';
	}
}

function init() {
  //alert(new Date());
	pic1      = new Image(); 
	pic1.src  = "img/asterisk-white.png";
	
	//currentTime = new Date();
	//month = currentTime.getMonth();
	//day = currentTime.getDate();
	//year = currentTime.getFullYear();

	TodaysDate = new Date();
	//StartDate = new Date(year, month, day);
	//StartDate = new Date(2008, 5, 14); //Month is 0-11 in JavaScript   <-- force date for testing
	
	if (obj.get("cStartDate") != null) {
		newuser       = false;
		StartDate     = new Date(obj.get("cStartDate"));
		StartDateDay  = StartDate.getDay()+2;  //------
		//alert("Welcome back! " + StartDate);
		loadContent('chart');
	} else {
		newuser = true;
		//alert("Welcome!");
		obj.set("cStartDate", TodaysDate);
		StartDate     = new Date();
		StartDateDay  = StartDate.getDay()+1;
		loadContent('intro');
	}
	
	//alert("TodaysDate "  + TodaysDate);
	//alert("StartDate" + StartDate);
	//alert(StartDate.getDay());
	//alert(total_days + " = " + Math.abs(StartDate) + " - " + TodaysDate);
	//alert(StartDate.getUTCMonth() + "/" +  StartDate.getUTCDate() + "/" +  StartDate.getFullYear());
	//countup(2008,7,1);
	
	//countup(StartDate.getFullYear(),StartDate.getUTCMonth(),StartDate.getUTCDate());
	//alert(difference);
}

Date.prototype.getWeek = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
}

function calcDays(){
  var d = new Date();
  today = (d.getUTCMonth() + "/" + d.getUTCDate() + "/" + d.getFullYear())

	var weekday   = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	var monthname = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
	
	today = new Date();
	// one day in milliseconds
	var one_day = 1000*60*60*24;
	
	//total_days = Math.abs(StartDate.getDate() - TodaysDate.getDate());
	countup(StartDate.getFullYear(),StartDate.getUTCMonth()+1,StartDate.getUTCDate());
	total_days = difference;

	// full course
	//alert("Total Days: " + (total_days + StartDateDay));
	if ((total_days + StartDateDay) > 91) {
	    alert("Congratulations.  You have completed a full course.  Repeat this four times in a year and make Ben proud.  You've made me proud, anyway.");
	    //confirmreset();
	    clearcookie();
	}
	
	
	//week = Math.ceil(total_days/7);
	week = Math.ceil((total_days + StartDateDay)/7);

 	document.getElementById("header_img").src                 = "img/headers/" + week + ".png";
 	document.getElementById("header_explaination").innerHTML  = "&quot;" +  eval("quote" + week) + "&quot;";
	document.getElementById('header_date').innerHTML          = weekday[d.getDay()]+ " " + monthname[d.getMonth()]+ " " + d.getDate() + ", " + d.getFullYear() + " (Week " + week + ", Day " + (total_days+1) + ")";
	
	//alert(StartDateDay + " " + total_days);
	//alert("ffff" + StartDate);
	//alert(StartDate + " " + d)
	
	for (i=(StartDateDay);i<=(total_days+StartDateDay);i++) {
		// set active
		document.getElementById(i).style.background         = 'url('+null+')';
		//document.getElementById(i).style.background       = 'url(img/bg-td-active.png) repeat-x';
		document.getElementById(i).style.backgroundColor    = "#135bfb";
		document.getElementById(i).style.borderColor        = '#145bf7';
		document.getElementById(i).style.borderRightColor   = '#145bf7';
		document.getElementById(i).style.borderTopColor     = '#145bf7';
		document.getElementById(i).style.borderBottomColor  = '#145bf7';
	}
}


function loadContent(div, virtue) {
  switch(div)
  {
  case "intro":
   	document.getElementById("intro").style.display        = 'block';
		document.getElementById("chart").style.display        = 'none';
		document.getElementById("instructions").style.display = 'none';
		document.getElementById("about").style.display        = 'none';
		document.getElementById("virtues").style.display      = 'none';
		document.getElementById("header-intro").style.display = 'block';
		document.getElementById("header").style.display       = 'none';
		document.getElementById("footer").style.display       = 'block';
   break;  
	case "instructions":
    document.getElementById("intro").style.display        = 'none';
		document.getElementById("chart").style.display        = 'none';
		document.getElementById("instructions").style.display = 'block';
		document.getElementById("about").style.display        = 'none';
		document.getElementById("virtues").style.display      = 'none';
		document.getElementById("header-intro").style.display = 'block';
		document.getElementById("header").style.display       = 'none';
		document.getElementById("footer").style.display       = 'block';
  break;
  case "about":
    document.getElementById("intro").style.display        = 'none';
		document.getElementById("chart").style.display        = 'none';
		document.getElementById("instructions").style.display = 'none';
		document.getElementById("about").style.display        = 'block';
		document.getElementById("virtues").style.display      = 'none';
		document.getElementById("header-intro").style.display = 'block';
		document.getElementById("header").style.display       = 'none';
		document.getElementById("footer").style.display       = 'block';
  break;
  case "virtues":
		document.getElementById("intro").style.display        = 'none';
   	document.getElementById("chart").style.display        = 'none';
		document.getElementById("instructions").style.display = 'none';
		document.getElementById("about").style.display        = 'none';
		document.getElementById("virtues").style.display      = 'block';
		document.getElementById("header-intro").style.display = 'block';
		document.getElementById("header").style.display       = 'none';
		document.getElementById("footer").style.display       = 'block';
		//window.scrollBy(0, 200);
		//window.location.hash = virtue;
		//alert(virtue);
  break;
  case "chart":
		calcDays();
		document.getElementById("intro").style.display        = 'none';
   	document.getElementById("chart").style.display        = 'block';
		document.getElementById("instructions").style.display = 'none';
		document.getElementById("about").style.display        = 'none';
		document.getElementById("virtues").style.display      = 'none';
		document.getElementById("header-intro").style.display = 'none';
		document.getElementById("header").style.display       = 'block';
		document.getElementById("footer").style.display       = 'block';
  
	  //document.getElementById("header_img").style.display='block';

		// SET DEFAULT COOKIE
		//var arr = new Array('"f"','"f"','"f"','"f"','"f"','"f"','"t"','"f"','"f"','"f"','"t"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"t"','"t"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"','"f"');
		//obj.set("cVirtueArray",arr);

		myvar = obj.get("cVirtueArray");
		mySplitResult = myvar.split(",");

		for (i=1;i<=91;i++) {
			if (mySplitResult[i] == '"t"') {
			// set asterisks
			document.getElementById(i).style.background         = 'url(img/asterisk-white.png) no-repeat';
			document.getElementById(i).style.backgroundColor    = "#135bfb";
			document.getElementById(i).style.borderColor        = '#145bf7';
			document.getElementById(i).style.borderRightColor   = '#145bf7';
			document.getElementById(i).style.borderTopColor     = '#145bf7';
			document.getElementById(i).style.borderBottomColor  = '#145bf7';
			}
			//document.getElementById(i).style.background = 'url(img/asterisk.png) no-repeat';
		}
  break;  
  default:
  }

  /*
  //window.location.hash = virtue;
  if (div == "virtues") {
    //setTimeout("window.scrollTo(0, 200);",100);
  }
  */
  
}
