$(document).ready(function() {
	
	slider = new $.krd_page_slide();
	new $('ul li.areas-of-practice').krd_dropmenu();
	
	my_footer = document.getElementsByTagName('footer')[0];
	my_footer_top = my_footer.offsetTop;
	my_footer_height = $(my_footer).outerHeight(true);
	viewport_height = $('body').outerHeight(true);
	browser_height = window.innerHeight;
	//alert(my_footer_height)
	
	
	if(viewport_height < browser_height) {
		footer_diff = ((browser_height - viewport_height) + my_footer_height);
		$(my_footer).css({
			'height': footer_diff
		})
		
	}
	
	

});

//krd_slider plugin
(function($) {
	
	$.krd_page_slide = function() {
		
		var totalWidth = 0;
		var num_pages = 0;
		var current_page = 1;
		var pageWidth = 0;
		var form_defaults = [];
		var form_status = false;
		
		
		init();
		
		function init() {
			
			//position slides
			$('.krd_slide').each(function(e) {
				var div = this;
				var w = $(div).outerWidth(true);
				pageWidth = w;
				totalWidth += w;
				num_pages++;
			});
			
			//get form defaults
			$('.input_field, .textarea_field').each(function(i) {

				form_defaults[i] = $(this).val();
				
			})
			
			
			$('.krd_slide').wrapAll('<div class="krd_page_slide_inner" />');
			$('.krd_page_slide_inner').css('width', totalWidth);

			$('.krd_slide').each(function(e) {
				$(this).show();
			});
			
			$('.next').click(function() {
				
				next_slide();
				return false;
			});
			
			$('#krd_page_slide_widget_form').submit(function() {
				form_status = validate(form_defaults);
				if($('p.error').length) {
					$('p.error').remove();
				}
				params = $(this).serialize();
				my_url = '/files/scripts/form_process.php';
				if(!form_status)  {
					$('<p class="error">Please fill out all fields</p>').appendTo(this)
					return false;
				}
				$.ajax({
					url:my_url,
					data: params,
					type: 'post',
					success: function(data) {
						if(data) {
							next_slide();
							$('.input_field, .textarea_field').each(function(index) {
								$(this).val(form_defaults[index])
							})
						}
					}
				})
				//next_slide();
				return false;

			});

			$('.input_field, .textarea_field').each(function() {
				
				$(this).focus(function() {
					
					current_val = $(this).val();
					in_array = form_defaults.indexOf(current_val);
					if(in_array > -1) {
						$(this).val('')
					}
					
					
				}).blur(function() {
					value = $.trim($(this).val())
					if(value == '') {
						
						$(this).val(current_val);
						
					}
					
				});
				
			});
			
		}
		
		function next_slide() {
			
			
			
			if(current_page == num_pages) {
				
				current_page = 0;
				pos = pageWidth * current_page;
				
			} else {
				
				pos = pageWidth * current_page;

			}
			
			current_page++;
			
			move = '-'+pos+'px';

			$('.krd_page_slide_inner').animate({
				'left': move
			});
			
		}
		
		function validate(form) {

			result = false;
			errors = [];
			$('.input_field, .textarea_field').each(function(index) {
				
				value = $.trim($(this).val());
				if(form[index] == value || value == '') {
					errors[0] = this;
				} else {
					//alert($(this).val())
				}
				
				if(this.name == 'email') {
					
					pattern = /([aA-zZ0-9.-])+@[aA-zZ0-9-]+(.)[a-z]{3}/;
					regex = new RegExp(pattern);
					text = $(element).val();

					if(regex.test(text)) {
						alert('email good')
						result = true;

					}
					
				}
				
			});
			
			if(errors.length == 0) {
				result = true;
			}
			return result;
			
		}
		
	}
	
})(jQuery);

//krd_form validation
(function($) {
	
	$.krd_form_validation = function(form) {
		
		init();
		
		function init() {
			
		}
		
	}
	
})(jQuery);


/*
	
	krd_make_exceprt
	---------------------------
	A simple jQuery plugin to turn a paragrapph into anceprt
	by trimming a portion off the end and appening an ending '...'
	
	Options:
	num_words [int] - Number of words to display
	ending_string [string] - The string to be appended to the newly trimmed text
	
	Example:
	$('.selector p).krd_make_excerpt(options = {num_words: 50, ending_string: '...'});

*/
(function($) {
	
	$.fn.krd_make_excerpt = function(options) {
		
		var num_words = options.num_words;
		var ending_string = options.ending_string;
		var article_wrap_tag = options.wrap_tag;
		var article = $(this).html();
		var new_article = make();
		$(this).html(new_article)
		function make() {
			
			var result = article.replace(/<p>/, '').replace(/<\/p>/, '');
			var result_array = result.split(" ");
			var result_array = result_array.slice(0, num_words);
			var new_article = result_array.join(" ")+ending_string;
			return new_article;
			
		}
		
		
	}
	
})(jQuery);



(function($) {
	
	/*

		find_and_bold
		--------------------
		jQuery plugin which replaces any text string with <strong>my text</strong>
		A single parameter which is a regEx pattern passed to the plugin

		Options
		find [string] - A regEx expression or string used to find text within a selected text.

		Example:
		$('.selector').find_and_bold(/my company/);
		$('.selector').find_and_bold('contact');


	*/
		
	$.fn.find_and_bold = function(find) {
		
		var pattern = find;
		
		$(this).each(function() {
			
			
			var text = $(this).html();
			
			var new_text = text.replace(pattern, "<strong>$1</strong>");
			$(this).html(new_text);
			
			
			
		});
		
		
	};
	
	
	/*

		make_link
		--------------------
		jQuery plugin which replaces any text string with <a href="/index">A link</a>
		A single parameter which is a regEx pattern passed to the plugin

		Options
		find [string] - A regEx expression or string used to find text within a selected text.
		goto [string] - The href parameter

		Example:
		$('.selector').make_link(/click*( here)?/, '/my_page);


	*/

	$.fn.make_link = function(find, goto) {
		

		$(this).each(function() {
			
			var pattern = find;
			var text = $(this).html();
			var found = text.match(pattern);

			var new_text = text.replace(pattern, '<a href="'+goto+'">'+"$1"+'</a>');
			$(this).html(new_text);
			
		});
	}
	
	
	
	$.fn.krd_validate = function(a) {
		
		var form = this;
		var error_message = '';
		var status = false;
		var defaults = {};
		var $inputs = $(form+':input');
		var url = a.url;
		$inputs.not('button').after('<span class="error"></span>');
		$($inputs).each(function() {
			defaults[this.name] = $(this).val();
			$(this).focus(function() {
				
				if($(this).val() == defaults[this.name]) {
					$(this).val('')
				}
				
			});
			
			$(this).blur(function() {
				if($(this).val() == '') {
					$(this).val(defaults[this.name])
				}
			})
			
		})
		
		form.submit(function() {
		errors = [];
			$inputs.each(function() {
				
				if($(this).hasClass('required')) {
					status = required(this);
					if(!status) {
						errors[0] = this;
						$(this).next('.error').text('This field needs to be filled out');
					} else {
						$(this).next('.error').text('');
					}
				}
				
				if($(this).hasClass('email')) {
					status = email(this);
					if(!status) {
						errors[0] = this;
						$(this).next('.error').text('Your email is invalid');
					} else {
						$(this).next('.error').text('');
					}
					
				}
				
			});

			if(errors.length == 0) {
				params = $(form).serialize();
				$.ajax({
					url: url,
					parameters: params,
					method: 'post'
				})
				return true;

			}
			return false;
		});
		
		
		
		required = function required(element) {

			if($(element).val() === '' || typeof $(element).val() == null || typeof $(element).val() === 'unidentified' || $(element).val() == defaults[element.name]) {
				error_message = 'Field '+element.name+' is required';
				return false;
			}
			
			return true;
		}
		
		
		email = function email(element) {
			
			pattern = /([aA-zZ0-9.-])+@[aA-zZ0-9-]+(.)[a-z]{3}/;
			regex = new RegExp(pattern);
			text = $(element).val();;
			
			if(!regex.test(text)) {
				
				error_message = 'Your email address is invalid';
				return false;
				
			}
			return true;
			
		}
		
		
	}
	
	$.fn.krd_dropmenu = function() {
	
		$(this).mouseenter(function() {
		
			$(this).children('ul').css({
				display:'block'
			})
			
		}).mouseleave(function() {
			$(this).children('ul').css({
				display:'none'
			})
		})
		
	}
	
})(jQuery)

var options = {
	'num_words': 35,
	'ending_string': '...',
};

if($('.krd_template_2 .krd_section p').length) {
	var exceprt = new $('.latest_cases p').krd_make_excerpt(options);
	var exceprt = new $('.blog_main p').krd_make_excerpt(options);
}

$('.krd_section p, .krd_center_content ul li').find_and_bold(/(Ronemus( )(&|&amp;)( )Vilensky)/gi);
$('.krd_center_content p').make_link(/(contact*( us)?)/gi, '/contact');

if($('#contact_form').length) {
	
	$('#contact_form').krd_validate(o = {'url':'/ronvil/files/scripts/form_process.php'});
	
}



