var base_url = 'http://marshvorspan.com/';

jQuery(function($) {
	
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		enhancements : {
			comments : function(){
				$('#comment_form').submit(function(){
				
					$('.form_error').each(function(){
						$(this).hide();
					});
					
					if($('#comment_name').val() == ''){
						$('#name_error').show();
						$('#comment_name').focus();
						return false;
					}
					
					if($('#comment_email').val() == ''){
						$('#email_error').show();
						$('#comment_email').focus();
						return false;
					}
					
					var email_regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
					var comment_email = $('#comment_email').val();
					if( ! email_regex.test(comment_email)){
						$('#email_error').show();
						$('#comment_email').focus();
						return false;
					}

					if($('#comment_message').val() == ''){
						$('#message_error').show();
						$('#comment_message').focus();
						return false;
					}
					
					// Disable the submit button
					$('#comment_btn').attr('disabled','disabled');
					
					//show the progress loader
					$('#comment_loader').html('<img src="' + base_url + 'public/images/comment_loader.gif" />');
					
					var post_url = $('#comment_form').attr('action');
					$.post(post_url,{
						comment_name : $('#comment_name').val(),
						comment_email : $('#comment_email').val(),
						comment_message : $('#comment_message').val(),
						record_id : $('#record_id').val()
					},
					function(data){
						//clear all the values from the comment form
						$('#comment_name, #comment_email, #comment_message').val('');
						
						//re-enable the submit btn
						$('#comment_btn').removeAttr("disabled");
						
						//clear the progress loader and display the success message
						$('#comment_loader').html('');
						$('#success').html('<p>' + data + '</p>');
						$('#success').slideDown();
						setTimeout(function(){
							$('#success').slideUp();
						}, 5000);
					});
					return false;
				});
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.enhancements.comments();
});