$(document).ready(function(){
	var timeout = 600000; //10 minutes
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
  	 return this.animate({opacity: 'toggle'}, speed, easing, callback);
	}; 
	
	$.clipboardReady(function(){
		$('#copy_to_clip').click(function(){
			$.clipboard($('textarea.bigGreenBox').val());
			$('#copy_to_clip').fadeTo(250, .05);
			$('#copy_ack').fadeToggle(250).fadeTo(2500, 1, function(){$('#copy_to_clip').fadeTo(250, 1);}).fadeToggle(250);
			$(window).bind("beforeunload", function() { 
		    $.clipboard('');
		  });
		  $(window).unload(function() { 
		    $.clipboard('');
		  });
			return false;
		});
	}, { swfpath: "js/jquery.clipboard.swf", debug: false }); 
	
	$('.generateText').click(function(){
		t = '';
		t = 'Emergency Plan information for ';
		t += $('#plan_first_name').val()+' '+$('#plan_last_name').val()+'\n';
		t += $('#plan_phone').val()+'\n';
		t += $('#plan_email').val()+'\n\n';
		
		t += 'My Emergency Contact \n';
		t += $('#emergency_first_name').val()+' '+$('#emergency_last_name').val()+'\n';
		t += $('#emergency_phone').val()+'\n';
		t += $('#emergency_email').val()+'\n\n';
		
		t += 'My Out-of-Town Contact \n';
		t += $('#oot_first_name').val()+' '+$('#oot_last_name').val()+'\n';
		t += $('#oot_phone').val()+'\n';
		t += $('#oot_email').val()+'\n\n';
		
		t += 'My Neighborhood Meeting Place \n';
		t += $('#meet_street1').val()+'\n';
		if ($('#meet_street2').val() != '') t += $('#meet_street2').val()+'\n';
		t += $('#meet_city').val()+', '+$('#meet_state').val()+' '+$('#meet_zip').val()+'\n';
		t += $('#meet_phone').val();
		
		$('textarea.bigGreenBox').val('');
		$('textarea.bigGreenBox').val(t);
		
		$('#plan_form_container').slideUp('slow', function(){
			$('#plan_result_container').slideDown('slow');	
		});
	});
	
	$('#plan_result_back').click(function(){
		$('#plan_result_container').slideUp('slow', function(){
			$('#plan_form_container').slideDown('slow');	
		});
	});
	
	$(document).keyup(function(event){
		$.idle(function() { 
			$.clipboard('');
			$('input').val('');
			$('textarea').val('');
			alert('You have been idle for 10 minutes.\n\nFor security purposes, the information you entered into this form has been erased.'); 
		}, timeout);	
	});
	
	//force blank field when leaving page
	$(window).bind("beforeunload", function() { 
    clearAllFields()
  });
  $(window).unload(function() { 
    clearAllFields()
  });
	
  function clearAllFields()
  {
  	$('input').val('');
  	$('textarea').val('');
  	$('textarea').text('');
  	$('textarea').html('');
  }
	
});

(function($) {

	if (typeof $.timeout != "undefined") return; 

	$.extend({
	  timeout : function (func,delay) {
			// init
			if (typeof $.timeout.count == "undefined") $.timeout.count = 0; 	
			if (typeof $.timeout.funcs == "undefined") $.timeout.funcs = new Array(); 
			// set timeout
			if (typeof func =='string') return setTimeout(func, delay); 
			if (typeof func =='function') {
				$.timeout.count++;
				$.timeout.funcs[$.timeout.count] = func;
				return setTimeout("$.timeout.funcs['"+$.timeout.count+"']();", delay);
			}
		},
	  interval : function (func,delay) {
			// init
			if (typeof $.interval.count == "undefined") $.interval.count = 0; 	
			if (typeof $.interval.funcs == "undefined") $.interval.funcs = new Array(); 
			// set interval
			if (typeof func =='string') return setInterval(func, delay); 
			if (typeof func =='function') {
				$.interval.count++;
				$.interval.funcs[$.interval.count] = func;
				return setInterval("$.interval.funcs['"+$.interval.count+"']();", delay);
			}
		},
	  idle : function (func,delay) {
			// init
			if (typeof $.idle.lasttimeout == "undefined") $.idle.lasttimeout = null;
			if (typeof $.idle.lastfunc == "undefined") $.idle.lastfunc = null;
			// set idle timeout
			if ($.idle.timeout) { clearTimeout($.idle.timeout); $.idle.timeout = null; $.idle.lastfunc = null; }
			if (typeof(func)=='string') { 
				$.idle.timeout = setTimeout(func, delay); 
				return $.idle.timeout;
			}		
			if (typeof(func)=='function') { 
				$.idle.lastfunc = func;
				$.idle.timeout = setTimeout("$.idle.lastfunc();", delay);
				return $.idle.timeout;
			}
		},
	  clear : function (countdown) {
		clearInterval(countdown);
		clearTimeout(countdown);
	  }	
	});
})(jQuery);