﻿// stopwatch constructor
var s = new Stopwatch(function(runtime) {
  // format time as m:ss.d
  var minutes = Math.floor(runtime / 60000);
  var seconds = Math.floor(runtime % 60000 / 1000);
  var decimals = Math.floor(runtime % 1000 / 100);
  var displayText = minutes + ":" + (seconds < 10 ? "0" : "") + seconds + "." + decimals;
  jQuery("#time").html(displayText);
});

// countdown constructor
var c = new Stopwatch(function(runtime) {
  // format time as m:ss.d
  var minutes = Math.floor(runtime / 60000);
  var seconds = Math.floor(runtime % 60000 / 1000);
  var decimals = Math.floor(runtime % 1000 / 100);
  var displayText = minutes + ":" + (seconds < 10 ? "0" : "") + seconds + "." + decimals;
  //c.starttime = 60000;
  jQuery("#countdown_time").html(displayText);
});

function updateClock() {
	// Set current date/time
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var part = 'am';
	if (hours >= 12) {
		part = 'pm'
		if (hours > 12) {
			hours = hours - 12;
		}
	}
	
	//set time
	var minutesString = '';
	if (minutes < 10){
		 minutesString = '0' + minutes;
	}
	else {
		minutesString = minutes;
	}
	var outStr = hours + ':' + minutesString + part;
	document.getElementById('current_time').innerHTML = outStr;
}

jQuery(document).ready(function() {
	//set time
	updateClock();
	setInterval('updateClock()', 1000);
	
	//set date
	var now = new Date();
	document.getElementById('date').innerHTML = now.toLocaleDateString();
});

// Stopwatch dialog
jQuery(document).ready(function() {	
	// Dialog			
	jQuery('#timer_display').dialog({
		autoOpen: false
	});
	
	// Dialog Link
	jQuery('#timer_link').click(function(){
		jQuery('#timer_display').dialog('open');
		return false;
	});
	
	//Stopwatch
	jQuery("#timer_start").bind("click", function(){ 
	s.startStop(); 
	//alert('Start!');
	});
	jQuery("#timer_reset").bind("click", function(){ s.resetLap(); });
	s.doDisplay();

	
	//Countdown
	jQuery("#countdown_start").bind("click", function(){ 
	// validate start time
	var hours = document.getElementById('countdown_hours').value;
	var minutes = document.getElementById('countdown_minutes').value;
	if (hours != parseInt(hours)){
		alert('Hours: ' + hours + ' is not a whole number!');
		return false;
	}
	if (minutes != parseInt(minutes)){
		alert('Minutes: ' + minutes + ' is not a whole number!');
		return false;
	}
	
	// set start time
	var startMS = parseInt(minutes) * 60000; //change minutes into milliseconds
	startMS += parseInt(hours) * 3600000; //change hours into milliseconds
	c.starttime = startMS;
	
	c.startStopCountdown(); 
	});
	
	jQuery("#countdown_reset").bind("click", function(){ c.resetLap(); });
	c.doDisplay();
});

// calculator dialog
jQuery(document).ready(function() {	
	// Dialog			
	jQuery('#calculator_wrapper').dialog({
		autoOpen: false,
		width: 500
	});
	
	// Dialog Link
	jQuery('#calculator_link').click(function(){
		jQuery('#calculator_wrapper').dialog('open');
		return false;
	});
});

// todo list dialog
jQuery(document).ready(function() {	
	// Dialog			
	jQuery('#notes_wrapper').dialog({
		autoOpen: false
	});
	
	// Dialog Link
	jQuery('#notes_link').click(function(){
		jQuery('#notes_wrapper').dialog('open');
		GetNotes();
		return false;
	});
});

function SaveNotes()
{
	var value = document.getElementById('tpNotes').value;
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + 60);
	document.cookie= "tpNotes=" + escape(value) + ";expires="+exdate.toUTCString();
	alert('Your notes have been saved!');
	jQuery('#notes_wrapper').dialog('close');
}

function GetNotes() {
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf("tpNotes=");
		if (c_start != -1)
		{
			c_start = c_start + 8;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			var notes = unescape(document.cookie.substring(c_start,c_end));
			document.getElementById('tpNotes').value = notes;
		}
		return false;
	}
	return false;
}

// colorbox for cooliris
jQuery(document).ready(function () {
    jQuery(".cooliris").colorbox();
});

// Rollovers
if (document.images) {
	var icon_stopwatch_up = new Image();
	var icon_stopwatch_ro = new Image();
	var icon_calculator_up = new Image();
	var icon_calculator_ro = new Image();
	var icon_todo_up = new Image();
	var icon_todo_ro = new Image();
	var icon_cooliris_up = new Image();
	var icon_cooliris_ro = new Image();
	var icon_twitter_up = new Image();
	var icon_twitter_ro = new Image();
	var icon_facebook_up = new Image();
	var icon_facebook_ro = new Image();
	var lpGenerator_up = new Image();
	var lpGenerator_ro = new Image();
	var hp_link_doe_up = new Image();
	var hp_link_doe_ro = new Image();
	var hp_link_photo_up = new Image();
	var hp_link_photo_ro = new Image();
	var hp_link_onion_up = new Image();
	var hp_link_onion_ro = new Image();
	var hp_link_wiki_up = new Image();
	var hp_link_wiki_ro = new Image();


	icon_stopwatch_up.src = "/wp-content/themes/teachingpost/images/icon_stopwatch.png";
    icon_stopwatch_ro.src = "/wp-content/themes/teachingpost/images/icon_stopwatch_ro.png";
    icon_calculator_up.src = "/wp-content/themes/teachingpost/images/icon_calculator.png";
    icon_calculator_ro.src = "/wp-content/themes/teachingpost/images/icon_calculator_ro.png";
	icon_todo_up.src = "/wp-content/themes/teachingpost/images/icon_notepad.png";
	icon_todo_ro.src = "/wp-content/themes/teachingpost/images/icon_notepad_ro.png";
	icon_cooliris_up.src = "/wp-content/themes/teachingpost/images/icon_cooliris.png";
	icon_cooliris_ro.src = "/wp-content/themes/teachingpost/images/icon_cooliris_ro.png";
	icon_twitter_up.src = "/wp-content/themes/teachingpost/images/icon_twitter.png";
	icon_twitter_ro.src = "/wp-content/themes/teachingpost/images/icon_twitter_ro.png";
	icon_facebook_up.src = "/wp-content/themes/teachingpost/images/icon_facebook.png";
	icon_facebook_ro.src = "/wp-content/themes/teachingpost/images/icon_facebook_ro.png";
	lpGenerator_up.src = "/wp-content/themes/teachingpost/images/hp_btn_lp.png";
	lpGenerator_ro.src = "/wp-content/themes/teachingpost/images/hp_btn_lp_ro.png";
	hp_link_doe_up.src = "/wp-content/themes/teachingpost/images/hp_btn_doe.png";
	hp_link_doe_ro.src = "/wp-content/themes/teachingpost/images/hp_btn_doe_ro.png";
	hp_link_photo_up.src = "/wp-content/themes/teachingpost/images/hp_btn_photo.png";
	hp_link_photo_ro.src = "/wp-content/themes/teachingpost/images/hp_btn_photo_ro.png";
	hp_link_onion_up.src = "/wp-content/themes/teachingpost/images/hp_btn_onion.png";
	hp_link_onion_ro.src = "/wp-content/themes/teachingpost/images/hp_btn_onion_ro.png";
	hp_link_wiki_up.src = "/wp-content/themes/teachingpost/images/hp_btn_wiki.png";
	hp_link_wiki_ro.src = "/wp-content/themes/teachingpost/images/hp_btn_wiki_ro.png";
}


function setTimerMode(mode) {
	switch(mode)
	{
		case 'countdown':
		  document.getElementById('countdown_wrapper').style.display = 'block';
		  document.getElementById('stopwatch_wrapper').style.display = 'none';
		  break;
		case 'stopwatch':
		  document.getElementById('stopwatch_wrapper').style.display = 'block';
		  document.getElementById('countdown_wrapper').style.display = 'none';
		  break;
		default:
		  document.getElementById('stopwatch_wrapper').style.display = 'none';
		  document.getElementById('countdown_wrapper').style.display = 'none';
		  alert('Timer mode not found!');
	}
}

// nav hover states persist
jQuery(document).ready(function () {
    // get current page
    var sPath = window.location.pathname;
    var lastChar = sPath.substring(sPath.length - 1);
    if (lastChar == '/')
    	sPath = sPath.slice(0, -1);
    
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    
    var sPageScrubbed = sPage.replace(new RegExp( "/", "gi" ), '');
    
    // choose page and take action
    switch (sPageScrubbed) {
        case 'activity':
            document.getElementById('nav_wall').className = 'nav_selected';
            break;
        case 'groups':
            document.getElementById('nav_groups').className = 'nav_selected';            
            break;
	    case 'members':
	        document.getElementById('nav_members').className = 'nav_selected';        
	        break;
		case 'gallery-splash':
		    document.getElementById('nav_gallery').className = 'nav_selected';		    
		    break;
		case 'links':
		  	document.getElementById('nav_links').className = 'nav_selected';			
		  	break;
		case 'store':
		  	document.getElementById('nav_shop').className = 'nav_selected';			
		  	break;

		default:
			document.getElementById('nav_home').className = 'nav_selected';
			break;
    }
});

// home page tabs
jQuery(document).ready(function () {
	var sPath = window.location.pathname;
	if (sPath == '/') {
		document.getElementById('content_wrapper').style.padding = '25px 10px 25px 20px';
	}
});
