$(document).ready(function(){
	$('#hard_questions_comment_submit_form form').validate({
		rules: {
			name: {required: true, minlength: 6}
			,email: {required: true, email: true}
			,comment: {required: true, maxlength: 150}
		}
		,messages: {
			name: {
				required: "Please specify your name"
				,minlength: jQuery.format("At least {0} characters required!")
			}
			,email: {
				required: "We need your email address to contact you"
				,email: "Your email address must be in the format of name@domain.com"
			}
			,comment: {
				required: "Please provide a comment to this question"
				,maxlength: jQuery.format("No more than {0} characters allowed!")
			}
		}
		,errorClass: "invalid"
		,errorContainer: "#messagebox"
		,errorLabelContainer: "#messagebox ul"
		,wrapper: "li"
		,submitHandler: function(form) {
			$(form).ajaxSubmit({
				success: function(data){
					$('<div />').css('text-align','center').css('vertical-align','middle').html('<h2>Thanks for submitting your comments!</h2>').dialog({
						height: 150
						,width: 600
						,modal: true
						,closeOnEscape: true
					});
					$(form).find('input[name="name"]').val('');
					$(form).find('input[name="email"]').val('');
					$(form).find('textarea[name="comment"]').val('');
				}
			});
		}
	});
	$('#hard_questions_top_box_middle span.ask_your_hard_question a').bind('click.hard', function(e){
		$('<div id="ask-hard-question-dialog"/>').load('/ajax/post-hard-question/', function(){
			$(this).dialog({
				height: 300
				,width: 650
				,modal: true
				,closeOnEscape: true
				,title: 'Ask a hard question'
				,zIndex: 3999
				,bgiframe: true
				,open: function(e, ui){
					$('#ask-hard-question-dialog form').ready(function(){
						$('#ask-hard-question-dialog form').validate({
							rules: {
								title: {required: true, minlength: 20}
								,field_id_58: {required: true, email: true}
							}
							,messages: {
								title: {
									required: "Please specify your question"
									,minlength: jQuery.format("At least {0} characters required!")
								}
								,field_id_58: {
									required: "Please enter a valid email address"
									,email: "Your email address must be in the format of name@domain.com"
								}
							}
							,errorClass: "invalid"
							,errorContainer: "#messagebox"
							,errorLabelContainer: "#messagebox ul"
							,wrapper: "li"
						});					
					});
				}
				,buttons: {
					"Submit Question": function(){
						if ($('#ask-hard-question-dialog form').valid()){
							$('#ask-hard-question-dialog form').ajaxSubmit({
								success:function(data){
									$('#ask-hard-question-dialog').html('<div>Thanks for submitting your Question!</div>');
									$('#ask-hard-question-dialog').dialog('option','buttons',{"Finish":function(){
										$('#ask-hard-question-dialog').dialog('close').dialog('destory');
									}});
								}
							});
						}
					}
				}
			});
		});
		return false;
	});
});