
// Thanks to http://www.kelvinluck.com/2009/02/progressive-enhancement-with-jquery-example/

function dce(elname){ return document.createElement(elname); }
function jdce(elname){ return $(dce(elname)); }

$(document).ready(function()
{
	// Set up date pickers
	$(".dateSpan").each(function()
		{
			var $this = $(this);

			var bits = this.id.split("_");
			bits.pop();
			var name = bits.join("_");
			
			var pn = this.parentNode;
			while(pn.localName != "form")
			{
				pn = pn.parentNode;
			}
			var f = pn; // the form
			
			
			
			var currDate = new Date(f["review_date_year"].value, f["review_date_month"].value, f["review_date_day"].value);
			
			$this.empty().
				append(jdce("input")
				.attr
				({
					id: name, value: $.datepicker.formatDate("d MM yy", currDate) 
				})
				.datepicker({
					autoSize: true,
					constrainInput: true,
					dateFormat: "d MM yy",
					onSelect: function(dateStr, picker)
					{
						var name = this.id;
						$("#" + name + "_dayhid")[0].value = picker.currentDay;
						$("#" + name + "_monthhid")[0].value = picker.currentMonth;
						$("#" + name + "_yearhid")[0].value = picker.currentYear;
					}
				})).
				append(jdce("input").attr({id: name + "_dayhid", type: "hidden", name: name + "_day"})).			
				append(jdce("input").attr({id: name + "_monthhid", type: "hidden", name: name + "_month"})).
				append(jdce("input").attr({id: name + "_yearhid", type: "hidden", name: name + "_year"}))
			;
		}
	);
	
	$("#review_date").addClass("required");
	
	$(':submit').each(
			   function()
			   {
			      var $this = $(this);
			      var f = this.form;
			      
			      var pn = this.parentNode;			      
			      $(pn).addClass("buttons");
			      
			      var link = $('<a href="#">' + $this.val() + '</a>')
			         .bind(
			            'click',
			            function()
			            {
			               $(f).trigger('submit');
			               return false;
			            }
			         )
			      $this.after(link).css({position:'absolute', top:'-2000px'});
			   }
			);
	
	/*
    // Form validation
    $forms = $('.validated-form');
    if ($forms.length) {
     
        $forms.validate(
        {
            errorPlacement: function(error, element)
                {
                    element.after(error);
                    error.hide().slideDown();
                }
        }
    );   
    }
	*/
	
	// Set up text areas as jHtmlArea
	$("textarea").each(function(e)
		{
			var $this = $(this);
			$this.attr({cols: "60"});
			$this.htmlarea();	
		});
	
	
	
	
	/*
	$('.tt').each(
			   function()
			   {
			      var $tt = $(this);
			      $tt.simpletip(
			         $tt.attr('title'),
			         {
			            hook: {
			               tooltip: 'topLeft',
			               target: 'bottomLeft'
			            },
			            offset: [20, -5],
			            stem: {
			               corner: 'topLeft',
			               color: '#000',
			               size: 15
			            }
			         }
			      ).attr('title', '');
			   }
			);
	*/
});
