$(document).ready(
	function(){
		$('form .error:not(:empty)').parents('li').addClass('error');
		
		$('form.dsform ol').addClass('clearfix');		
		
			
		//watermarking
		$('form.dsform .watermark:input')
		    .each(
		    	function(){
		    		var t = $(this);
		    		t.data('watermark', t.val());
		    	}
		    )
		    .focus(
		    	function(){
		    		var t = $(this);
		    		if( $.trim(t.val()) == t.data('watermark') ){
		    			t.val("");
		    		}
		    	}
		    )
		    .blur(
		    	function(){
		    		var t = $(this);
		    		if( $.trim(t.val()) == '' || $.trim(t.val()) == t.data('watermark')){
		    			t.addClass('watermark').val(t.data('watermark'));
		    		}else{
		    			t.removeClass('watermark');
		    		}
		    	}
		    );
		
		//submit spinner
		$('form.dsform')
		    .submit(
		    	function( e ){
		    		
		    		if( !$(this).is('.submitting') && $(this).find(':submit[rel]').length > 0 ){
		    			
		    			//remove this in real production
		    			e.preventDefault();
		    			//end remove
		    			
		    			//get a handline to the submit button, while adding a submitting 
		    			//class to the form
		    			var submitButton = $(this)
		    				.addClass('submitting')
		    				.find(':submit');
		    			
		    			//default the messing, but replace it with the rel attribute if given						
		    			var submitMessage = submitButton.attr('rel') || "Submitting form";
		    			
		    			//add a submitting class to the button, disable it, unfocus it,
		    			//and place the processing message after the button. The submitting
		    			//class should hide the button or change it as desired
		    			submitButton
		    				.addClass('submitting')
		    				.hide()
		    				.attr('disabled','true')
		    				.blur()
		    				.after( $('<div class="submit_processing">' + submitMessage + '</div>').fadeIn('slow') );
		    				
		    			//remove this in real production
		    			var f = $(this);
		    			setTimeout( function(){f.submit();}, 2000 );
		    			//end remove
		    		}
		    		
		    	}
		    );
				
		//enable datepickers
		if( $.datepicker ){
		    $('form.dsform input.date')
		    	.datepicker({ 
		    		speed: 0,
		    	    beforeShow: function( input ){
		    	    	if( $(input).is('.end_date') ){
		    	    		return {maxDate:null,minDate: ( $(input).parent().siblings().find('input.start_date').datepicker("getDate") || null) };
		    	    	
		    	    	}else if( $(input).is('.start_date') ){
		    	    		return {minDate: null, maxDate: ( $(input).parent().siblings().find('input.end_date').datepicker("getDate") || null)};
		    	    	}
		    	    	
		    	    	return true;
		    	    
		    	    }
		    	});
		}
		
		
	}
);