/*
 * Date picker plugin for jQuery
 * http://binarypusher.com
 *
 * Copyright (c) 2006 Steve Bramley (binarypusher.com)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * $LastChangedDate: 2006-08-23 12:39:59 +0800 (Wed, 23 Aug 2006) $
 * $Rev: 0 $
 */


var cal = new function() {

    $.fn.calendar = function(o, d, m, y){ 

			

			if(typeof(o._day == "undefined")){

				init(o);

			}

			$(o).hover(function(){},function(){}).css({cursor:"pointer"});

			$(o).focus(function(){ $(this).attr("disabled",true);popCal(this, this._today)});

		};

		

		function init(o) {

			o._days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30, 31);

			o._day_names = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

			o._month_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December");

			o._raw_date_string = o.value;

			o._day = Number(o._raw_date_string.split("/")[0]);

			o._month = Number(o._raw_date_string.split("/")[1]);

			o._year = Number(o._raw_date_string.split("/")[2]);

			o._today = null;

			

			o.target_elem = o;

			

			if (o._year == null || o._month == null || isNaN(o._year) || isNaN(o._month)){

				o._today = new Date();

				o._year = o._today.getYear();

				o._month = o._today.getMonth();

			} else {

				o._today = new Date()

				o._today.setFullYear(o._year, Number(o._month-1),o._day);

			}

			

			if ((o._year % 4 == 0) && (o._year % 100 != 0)){

				o._days[1] = 29;

			}

		}

		

		function shutCal(o) {

			$('#' + o.id).attr("disabled",false);
			$('#' + o.id).parent().find(".picker").remove();
			
			date_value = $('#' + o.id).val();
			
			if (date_value != ''){
				$.post("/chart/get", { start_date: date_value }, function(json){
											
					eval("var args = " + json);
						$("#edit-id").val(args.id);
						$("#edit-temperature").val(args.temperature);
						$("#edit-time").val(args.time);
						$("#edit-cervical_fluid_item_length").val(args.fluid);
						$("#edit-intercourse_item_length").val(args.intercourse);
						$("#edit-tests_item_length").val(args.tests);
						$("#edit-meds_item_length").val(args.meds);
						$("#edit-symptoms_item_length").val(args.symptoms);
						$("#edit-symptom_notes").val(args.notes);
				});
			}

		}

		

		function popCal(o,d) {

			shutCal(o);

			

			var current_date = d;

			var year = null;

			current_date.setDate(1);

			if ((Number(d.getYear())+1900) > 3000){

				year = d.getYear();

			} else {

				year = (Number(d.getYear())+1900);

			}

			var first_of_month = current_date.getDay();

			var _minus_month = new Date(Number(year),Number(d.getMonth()-1),1);

			var _plus_month = new Date(Number(year),Number(d.getMonth()+1),1);//((current_date.getMonth()+1) == 13) ? 1 : (current_date.getMonth()+1);	

			var _minus_year = new Date(Number(year-1),d.getMonth(),1,0,0,0,0);//(Number(current_date.getYear())+1899);			

			var _plus_year = new Date(Number(year+1),d.getMonth(),1,0,0,0,0);//(Number(current_date.getYear())+1901);	

			var target = $('#' + o.id).parent().append('<span class="picker"></span>');

			teststring = '<ul class="head"><li class="test yearminus">&laquo;</li><li class="label">' + year + '</li><li class="test yearplus">&raquo;</li></ul>';

			teststring += '<ul class="head"><li class="test monthminus">&laquo;</li><li class="label none">' +  o._month_names[current_date.getMonth()] + '</li><li class="test monthplus">&raquo;</li></ul>';

			teststring += '<ul class="days_heading"><li>s</li><li>m</li><li>t</li><li>w</li><li>t</li><li>f</li><li>s</li></ul>';



				column = 0;

				teststring += '<ul class="days">';

	

				for (i=0; i<first_of_month; i++) {

					teststring += '<li class="none">&nbsp;</li>';

					column++;

				}

			

				o._month++;

			

				for (i=1; i<=o._days[current_date.getMonth()]; i++) {

					if (column % 7 == 0)

						teststring += '<ul class="days">';
						
					teststring += '<li>';

			

					if (i < 10){

						teststring += "0"+i;

					} else {

						teststring += i;

					}

					teststring += "</li>";

					column++;

					if (column == 7) {

						teststring += '</ul>';

						column = 0;

					}

					


				}

			$(".picker").html('');

			$(".picker").append(teststring + '<ul class="tail"><li class="close">close</li></ul>');

			

			$(".days li,.head .test").not(".none").hover(function(){},function(){}).css({cursor:"pointer"});

			$(".close").hover(function(){},function(){}).css({cursor:"pointer"});

			$(".monthminus").click(function(){popCal(o,_minus_month)});

			$(".monthplus").click(function(){popCal(o,_plus_month)});

			$(".yearminus").click(function(){popCal(o,_minus_year)});

			$(".yearplus").click(function(){popCal(o,_plus_year)});

			$(".close").click(function(){shutCal(o)});

			$(".days li").not(".none").not(".close").click(function(){o.value = $(this).html()+'/'+String(100+current_date.getMonth()+1).substring(1)+'/'+year;init(o);shutCal(o);});

	}

	

}