var _host = "http://www.flowdrops.com", _themeURI = "/blog/wp-content/themes/cordobo-green-park-2";
var Flowdrops =
{   
	init: function()
	{
		Flowdrops.setup();
	},
	
	setup: function()
	{
		Flowdrops.smoothScroll();
		if (jQuery('#ppDonate').length){Flowdrops.checkPP()};
		if (jQuery('#contact').length){Flowdrops.parseContact()};
	},
					
	smoothScroll: function()
	{
		jQuery('a[href*=#]').click(function()
		{
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname)
			{
				var $target = jQuery(this.hash);
				$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
				if ($target.length)
				{
					var targetOffset = $target.offset().top - 0;
					jQuery('html,body').animate({scrollTop: targetOffset}, 1000);
					return false;
				}
			}
		});
	},
		
	checkPP: function()
	{
		jQuery('#ppDonate').submit(function() {
			if (jQuery('#ppAmount').val() > null)
			{
				if (jQuery('#ppAmount').val() < 1.35)
				{
					jQuery('#noAmount').hide();
					jQuery('#moreAmount').fadeIn();
					return false;
				}
				else
				{
					jQuery('#moreAmount').hide();
					jQuery('#noAmount').hide();
					jQuery('#ppGo').fadeIn();
					return true;
				}
			}
			else
			{
				jQuery('#moreAmount').hide();
				jQuery('#noAmount').fadeIn();
				return false;
			}
		});
	},
		
	parseContact: function()
	{
		jQuery('#contact').submit(function()
		{
			jQuery(".error").hide();
			var xError = false;
			
			if(this.vName && this.vName.value === '')
			{
				jQuery('#errorMessage').html('&nbsp;Please enter your name');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			if(this.vEmail && this.vEmail.value === '')
			{
				jQuery('#errorMessage').html('&nbsp;Please enter your email address');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			if(this.vEmail && !(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(this.vEmail.value)))
			{
				jQuery('#errorMessage').html('&nbsp;Please enter a valid email address');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			if(this.vSubject && this.vSubject.value === '')
			{
				jQuery('#errorMessage').html('&nbsp;Please enter a subject');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			if(this.vMessage && this.vMessage.value === '')
			{
				jQuery('#errorMessage').html('&nbsp;Please enter your message');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			// Check Captcha Code
			// DEVELOPER HOST:
			// var _host = 'http://localhost/flowdrops'
			var captchaURI = _host+"/ajax/check_captcha.php";
			var codeValue = jQuery('#captcha').val().toLowerCase();
			var code_valid = false;
			jQuery.ajax(
			{
				type: "POST",
				url: captchaURI,
				data: "captcha="+codeValue,
				async: false,
				success: function(rsp)
				{
					if(rsp == '1')
					{
						code_valid = true;
					}
				}
			});
		
			if( code_valid == false )
			{
				jQuery('#imgCaptcha').attr( 'src', _host+_themeURI+'/CaptchaSecurityImages.php?width=100&height=40&characters=5&key'+Math.random() );
				jQuery('#errorMessage').html('&nbsp;Incorrect Code');
				jQuery('#error').fadeIn();
				xError = true;
				return false;
			}
			
			if(xError)
			{
				// there is still something wrong
				jQuery('#errorMessage').html('&nbsp;Unknown error occured');
				jQuery('#error').fadeIn();
				return false;
			}
			
			else
			
			{
				// let's go
				jQuery('#error').hide();
				jQuery('#success').hide();
				jQuery('#submit').attr("disabled", true);
				jQuery('#activity').show();
				
				var inputs = [];
				jQuery(':input', this).each(function()
				{
					inputs.push(this.name + '=' + encodeURIComponent(this.value));
				});
				
				jQuery.ajax(
				{
					type: "POST",
					data: inputs.join('&'),
					url: this.action,
					timeout: 10000,
					error: function(err) {console.log(err);},
					success: function(data) {jQuery('#cResponse').html(data);}
				});
			}
		return false;
		});
	}
};
jQuery(document).ready(Flowdrops.init);