// JavaScript Document
$(function() {
	$("form.hide-submit .hide-me").before('<span class="styled-button">Go</span>').addClass("hidden");
	$(".styled-button").bind("click", function() {
		$(this).closest('form').trigger("submit");
	});
	$("form .submit-button").before('<span class="btn-submit">Submit</span>').addClass("hidden");
	$(".btn-submit").bind("click", function() {
		$(this).closest('form').trigger("submit");
	}); 

	$("#post-to-blog").click(function(e) {
		e.preventDefault();
		$('#dialog-form').dialog('open');
	});
	$("#view-comments").click(function(e) {
		e.preventDefault();
		$('#dialog-comments').dialog('open');
	});
	
		var name = $("#blg-name"),
			email = $("#blg-email"),
			title = $("#blg-title"),
			content = $("#blg-content"),
			targetPost = $("#targetKey"),
			allFields = $([]).add(name).add(email).add(title).add(content).add(targetPost),
			tips = $(".validateTips");

		function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be less than "+max+" characters.");
				return false;
			} else if ( o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be more than "+min+" characters.");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog-form").dialog({
			autoOpen: false,
			height: 380,
			width: 600,
			modal: true,
			buttons: {
				'Post Comment': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"name",3,16);
					bValid = bValid && checkLength(email,"email",6,80);
					bValid = bValid && checkLength(title,"title",3,140);
					bValid = bValid && checkLength(content,"message",0,800);

					//bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"please provide valid a email address");
					
					if (bValid) {
						updateTips("Sending message please wait...");
						allFields.attr("disabled", "disabled");
						$.ajax({
							type: 'POST',
							url: "/post-message.php",
							data: allFields,
							global: false,
							cache: false,
							success: function(html) {
								$("#dialog-form").dialog('close');
								$("#post-result").show().html(html);
								$("#post-to-blog").remove();
								setTimeout(function() {
									$("#post-result").fadeOut("slow");
								}, 5000);
								/*
								$(targetItem).animate({backgroundColor: "#fefefe"}, 750).removeClass("unread");
								var messages = parseInt($('span#messageCount').text());
								if (messages == 1) {
									$('span#messageNumber').remove();
								} else {
									$('span#messageCount').text(messages - 1);
								}
								*/
							},
							error: function() {
								updateTips("There was an error please try again");
								allFields.removeAttr("disabled");
							}
						});

					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		$("#dialog-comments").dialog({
			autoOpen: false,
			height: 380,
			width: 600,
			modal: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
		$("#search-field").focus(function() {
		   if ($(this).val() == 'search...') {
			  $(this).val("");
		   }
		}).blur(function() {
		   if ($(this).val() == '') {
			  $(this).val("search...");
		}
});


});

/*



*/
