	
	if(!window.console) {
		window.console = {
			log:function(){alert(arguments[0]);}
		};
	}
	
	/*
	Stages and Targets
	*/
	
	var stages = {
		1: {
			spring: 280,
			summer: 390,
			autumn: 280,
			winter: 210
		},
		2: {
			spring: 230,
			summer: 300,
			autumn: 230,
			winter: 190
		},
		3: {
			spring: 200,
			summer: 250,
			autumn: 200,
			winter: 180
		},
		4: {
			spring: 150,
			summer: 150,
			autumn: 150,
			winter: 150
		}
	};
	
	/*
	Basic Config
	*/
	
	var calcpeople = 0; //default startpoint
	var weekly = 0; //total
	var daily = 0;
	var target = 0;
	var stage = 3; //current stage
	
	function seasonTarget()
	{
		var now = new Date();
		var month = now.getMonth() + 1;
		var date = now.getDate();
		var year = now.getYear();
		if (month == 12) target = stages[stage]['summer'];
		if (month > 1 && month <= 3) target = stages[stage]['summer'];
		if (month >= 3 && month <= 6) target = stages[stage]['autumn'];
		if (month >= 6 && month <= 9) target = stages[stage]['winter'];
		if (month >=9 && month <= 12) target = stages[stage]['spring'];
		//possible intercept for greywater values here
		
		return target;
	}
	
	function weekly_target()
	{
		//daily target * 7 days * people
		var target = seasonTarget();
		target = target * 7;
		target = target * (calcpeople + 1);
		$("#target_weekly").html(pad(target,6));
	}
	
	function daily_target()
	{
		//daily target * people
		var target = seasonTarget();
		target = target * (calcpeople + 1);
		$("#target_daily").html(pad(target,6));
	}
	
	/*
	Numbers to English
	*/
	
	var engnum = {
		0: "Household Use",
		1: "Person One",
		2: "Person Two",
		3: "Person Three",
		4: "Person Four",
		5: "Person Five",
		6: "Person Six",
		7: "Person Seven",
		8: "Person Eight",
		9: "Person Nine",
		10: "Person Ten"
	};
	
	/*
	Averages Array
	*/
	
	var pplavg = {};
	
	/*
	Helper function to pad targets
	*/
	
	function pad(number,length)
	{
		var str = '' + Math.round(number);
		while (str.length < length) {
			str = '0' + str;
		}
		return str;
	}
	
	function resizeBlack()
	{
		var winx = getPageSize();
		$("#launch_frame").css("height",winx[1]);
		$("#launch_frame").css("width",winx[0]);
	}

	function init_launcher()
	{
		$("#calc_people").slider({ //make slider
			maxValue: 6,
			minValue: 1,
			startValue: 2,
			slide: function(ele,ul){
				var val = ul.slider.curValue;
				$('#calc_people_val').val(val);
				var tmpval = val + 1;
				$('#people_add .num').html(tmpval);
			}
		});
		
		$("#calc_launch_form").click(function(){ //based on slider
			$("#calc_launch_form").addClass("launching");
			$.ajax({
				type: "GET",
				url: "/savewaterforlife/calc_launch.aspx",
				success: function(result){
					$("#launch_center").html(result);
					
					$(".option_block:odd").css("margin","5px 0 0 10px").css("width","49%");
					
					calcpeople = parseInt($('#calc_people_val').val());
					$("#calc_launch_form").removeClass("launching");
					$("#option_box").hide();
					$("#calculator_box").hide();
					$("#averages_box").hide();
					$("#help_box").hide();
					resizeBlack();					
					$("#launch_frame").css("display","block").each(function(){
						water_calc(true);
						reset_inputs();
						$("#launch_center").show();
						$("#option_box").fadeIn("fast",function(){
							$("#calculator_box").fadeIn("fast",function(){
								$("#help_box").fadeIn("fast",function(){
									clone_inputs(0,true);
								});
							});
						});
					});
					$("a.close").click(function(){
						$("#launch_center").fadeOut("slow",function(){
							$("#launch_frame").hide();
						});
						return false;
					});
				}
			});
		});
	}
	
	function reset_inputs() //nuke old ones
	{
		$("div.calc_calc").slice(2).remove();
	}
	
	function clone_inputs(xx,opt) //chained cloning
	{	
		if (xx < calcpeople) {
			xx++;
			$("div.calc_calc:eq(1)").clone(true).insertAfter("div.calc_calc:last").css("display","none").fadeIn("normal",function(){
				water_calc(opt);
				clone_inputs(xx,opt);
				resizeBlack();
			});
		}
		$("div.calc_calc:eq(2)").css("marginRight",0); //ie6 fix
		$("div.calc_calc:eq(5)").css("marginRight",0); //ie6 fix
	}
	
	function water_calc(startup)
	{
		weekly = 0; //reset totals
		daily = 0;
		$("div.calc_calc").each(function(i){
			var box = $(this).children(".calc_block:visible").children(".option").children("select");
			var avg = $(this).children(".calc_avrg:visible").children(".avg");
			var nam = $(this).children(".personname").children(".name");
			var edi = $(this).children(".personname").children(".edit");
			var val = calc_block(box,i);
			var fin = 0;
			for(x=0;x<val.length;x++) {
				fin += val[x];
			}
			pplavg[i] = fin;
			
			if ($(this).attr("rel") != "weekly") {
				weekly += fin * 7; //daily
				daily += fin;
			} else {
				weekly += fin; //weekly
				daily += fin / 7;
			}
			if (startup) {
				$(nam).html(engnum[i]); //name
				$(edi).val(engnum[i]);
			}
			
			$(avg).html(pad(pplavg[i],3)); //avg
		});
		//$("#calc_weekly").html(pad(weekly,6)); //REMOVED requested
		$("#calc_daily").html(pad(daily,6));
		
		//weekly_target(); //re-eval target amount  //REMOVED requested
		daily_target();
	}
	
	function calc_block(blk,box)
	{
		var ret = new Array();
		$(blk).each(function(){
			var rel = $(this).attr("rel");
			var val = parseInt($(this).val());
			var parent = parseInt($("#"+rel).val());
			var result = parent * val;
			
			if ($(this).hasClass("personal_weekly")) {
				result = result / 7;
			}
			ret.push(result);
		});
		return ret;
	}	
	
	/*
	-------------------------------------------------------------
	Toggles and Switches
	-------------------------------------------------------------
	*/
	
	function toggle_help()
	{
		$(".help_toggle").slideToggle("slow",function(){
			resizeBlack();
		});
		$(".help_note").toggle();
	}
	
	function tooltip(tip)
	{
		if (tip) {
			$("#tooltipx").html(tip).show();
			var nw = $("#tooltipx")[0].offsetWidth;
			var nh = $("#tooltipx")[0].offsetHeight;
			$("#tooltipx").after("<iframe src='about:blank' id='blkframe' style='display: none; top: -1000; left: -1000; position: absolute; z-index: 2000; width: "+nw+"px; height: "+nh+"px;' frameborder='0'></iframe>");			
		} else {
			$("#tooltipx").html('').hide();
			$("#tooltipfix").hide();
			$('#blkframe').remove();
		}
	}
	
	$(document).ready(function(){
		$(document).mousemove(function(e){
			$("#tooltipx").css("top",e.pageY+20).css("left",e.pageX+20);
			$("#blkframe").css("top",e.pageY+20).css("left",e.pageX+20).show();
		});
		$(window).resize(function(){
			resizeBlack();
		});
	});
	
	function calc_custom_name(ele)
	{
		$(ele).children(".name").hide();
		$(ele).children(".edit").show().focus();
	}
	
	function calc_custom_save(ele)
	{
		$(ele).prev(".name").html($(ele).val()).show();
		$(ele).hide();
	}
	
	function calc_custom_enter(event,ele)
	{
		if (event.which) {
			keyCode = event.which;
		} else if (event.keyCode) {
			keyCode = event.keyCode;
		}
		if(keyCode == 13) {
			calc_custom_save(ele);
		}
	}
	
	/*
	-------------------------------------------------------------
	Write the calculator to the page
	-------------------------------------------------------------
	*/	
	
	function output_calc()
	{
		var output = ' \
			<div id="tooltipx"></div> \
			<div id="launch_frame"></div> \
			<div id="calculator"> \
				<a name="calculator"></a> \
				<form action="?" method="post" onsubmit="return false"> \
					<div id="platform"> \
						<a href="#top" id="calc_launch_form"><span>Launch</span></a> \
						<div class="slider_box"> \
							<strong>How many people in your household?</strong> \
							<div id="calc_people" class="ui-slider-2"> \
								<div class="ui-slider-handle"></div> \
							</div> \
						</div> \
						<div id="people_add">People: <span class="num">1</span></div> \
						<input type="hidden" id="calc_people_val"/> \
					</div> \
					<div id="launch_center"></div> \
				</form> \
			</div>';
		document.write(output);
	}
	
	/*
	-------------------------------------------------------------
	Page Size Functions
	-------------------------------------------------------------
	*/
	
	function getPageSize()
	{
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
	
	function getPageScroll()
	{
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll) 
		return arrayPageScroll;
	}
	
	