(function($) {
	$(document).ready(function() {
		$.fn.extend({
			'eviewLoad': function(targetElement, xhrData) {
				// only work with anchors and forms.
				if (!($(this).is('a') || $(this).is('form'))) {
					return;
				}
				var sourceElement = $(this);
				var xhr = {
					success: function(data) {
						if (typeof data == 'string') {
							if (data.substring(0, 1) == '{') {
								// try parsing JSON data
								data = eval('(' + data + ')');
							}
						}

						if (typeof data == 'object') {
							if (data['redirect']) {
								window.location.href = data['redirect'];
							}
						}

						if ($.scrollTo) {
							if (!$(sourceElement).attr('class').match('no-scrolling')) {
								$.scrollTo( $(targetElement), 1000 , {easing:'swing'} );
							}
						}

						targetElement.replaceWith(data);
					}
				}

				if (!targetElement) {
					targetElement = $(this).parents('div.eview-link-target').eq(0);
				}
				else if (typeof targetElement != 'object') {
					targetElement = $(targetElement);
				}

				// set some defaults if a form is encountered.
				if ($(this).is('form')) {
					xhr.type = $(this).attr('method');
					xhr.url = $(this).attr('action');
					xhr.data = $(this).serialize();
					$(this).find('input[type="submit"]').attr({'disabled' : 'disabled'});	// Disable the submit button(s)
				}

				// set some defaults if an anchor is encountered.
				if ($(this).is('a')) {
					xhr.url = $(this).attr('href');
				}

				// extend defaults with xhrData
				xhr = $.extend(xhr, xhrData);
				$.ajax(xhr);

				return false;
			}
		});
	})
})(jQuery);