var options = {	
	beforeSubmit:  showRequest,  // pre-submit callback 
	success:       showResponse,  // post-submit callback
	clearForm: true,       // clear all form fields after successful submit 
	resetForm: true        // reset the form after successful submit 
}; 

$(document).ready(function() {

	// Show suggestion form
	$(".suggest-new-link").bind("click", function(e) { 
		$(".suggest-site").show();
		return false;	
	});

	// Hide suggestion form
	$(".hide-suggest-site").live("click", function(e) { 
		$(".suggest-site").hide();
		return false;	
	});
	
	// Send site suggestion
	$(".suggest-new-link-send").live("click", function(e) { 
		var url = $("input[name=url]").val();
		if(url.length > 0)
		{
			$.get("wp-content/themes/tyonhakuopas/suggest_site.php", { url: url },  function(data) {
				if(data == 'success')
				{
					$.jGrowl("Sivustoehdotus lähetettiin onnistuneesti!");
				}
				else
				{
					$.jGrowl("Sivustoehdotuksen lähetys epäonnistui!");
				}			
				$(".suggest-site").hide();
			});	
		}
		else
		{
			$.jGrowl("Sivuston osoite on tyhjä, lisää osoite ja yritä uudelleen!");
		}
		return false;	
	});
	
	// Submit comment
	$('form').submit(function() { 
		var author = $("#author", this).val();
		var email = $("#email", this).val();
		var comment = $("#comment", this).val();
		if(author.length < 3)
		{
			showQTip($("#author", this), "Nimimerkin pitää olla vähintään 3 merkkiä pitkä!");
			return false;
		}
		else
		{
			try
			{
				$("#author", this).qtip("destroy");
			}
			catch(e)
			{
			}
		}
		
		if(email.length > 0)
		{
			if(!isValidEmailAddress(email))
			{
				showQTip($("#email", this), "Sähköpostiosoite on virheellinen!");
			return false;
			}
		}
		
		if(comment.length < 5)
		{
			showQTip($("#comment", this), "Kommentin pitää olla vähintään 5 merkkiä pitkä!");
			return false;
		}
		else
		{
			try
			{
				$("#comment", this).qtip("destroy");
			}
			catch(e)
			{
			}
		}
        $(this).ajaxSubmit(options);
		$.jGrowl("Kommentti lähetettiin onnistuneesti!");
        return false; 
    }); 
	
});

function showRequest(formData, jqForm, options) {
    return true; 
} 

function showResponse(responseText, statusText, xhr, $form)  {
	window.location.reload();
} 

function showQTip(element, message)
{
	element.qtip({
		content: message,
		position: { corner: { tooltip: 'leftTop' } },
		show: {
			when: false, // Don't specify a show event
			ready: true // Show the tooltip when ready
		},
		hide: false, // Don't specify a hide event
		style: {
			border: {
			width: 5,
			radius: 10
			},
			padding: 10, 
			textAlign: 'center',
			tip: true, // Give it a speech bubble tip with automatic corner detection
			name: 'light' // Style it according to the preset 'cream' style
		}
	});
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
