(function($)
{
	var validate_add_functions = new Array();
	var validate_update_functions = new Array();
	var initialize_functions = new Array();

	jQuery.add_validation_new = function(func)
	{
		validate_add_functions.push(func);
	};
	
	jQuery.add_validation_update = function(func)
	{
		validate_update_functions.push(func);
	};
	
	jQuery.add_initialize = function(func)
	{
		initialize_functions.push(func);
		func.apply(document);
	};

	$(document).ready(function()
	{
		$(this).initialize();
		
		$('.link_button:not(.no_ajax)').live('click', $.ajaxutils.ajax_link_click);
		
		$('.toggle_button').live('click', function()
		{
			if ($(this).hasClass('busy')) {
				return;
			}
			
			$(this).addClass('busy');
			
			var link = this;
			var cleanup = function() {
				$(link).removeClass('busy');
			}
			
			$(this).toggleClass('open close');
			$('.toggle_child', $(this).closest('.toggle_parent')).slideToggle('normal', cleanup);
			return false;
		});
		
		if ($.dialogutils)
		{
			$('.delete_button').live('click', function()
			{
				var name = $('.item_name input:not(input[type="hidden"])', $(this).closest('tr')).val();
				$.dialogutils.confirm_twice_dialog('Delete', "delete '"+name+"'",
					function (result, link) {
						if (result) $(link).ajax_link_click();
					}, this);
				return false;
			});
			
			$('.add_button').live('click', function()
			{
				for (var i in validate_add_functions)
				{
					if (!validate_add_functions[i].apply(this))
						return false;
				}
			
				var name = $('.item_name input:not(input[type="hidden"])', $(this).closest('tr')).val();
				$.dialogutils.confirm_dialog('Add', "add '"+name+"'",
					function (result, link) {
						if (result)
						{
							if ($(link).hasClass('no_ajax'))
								$(link).closest('form').submit();
							else
								$(link).ajax_form_submit();
						}
					}, this);
				return false;
			});
			
			$('.update_button').live('click', function()
			{
				if (!$(this).closest('form').hasClass('no_validate'))
				{
					for (var i in validate_update_functions)
					{
						if (!validate_update_functions[i].apply(this))
							return false;
					}
				}
				
				$.dialogutils.confirm_dialog('Save changes', "save the changes you made",
					function (result, link) {
						if (result)
						{
							if ($(link).hasClass('no_ajax'))
								$(link).closest('form').submit();
							else
								$(link).ajax_form_submit();
						}
					}, this);
				return false;
			});
		}
	});
	
	$.fn.initialize = function()
	{
		return this.each(function()
		{
			$('a[rel="external"][href], a.external[href], a.external_noimage', this).attr('target', '_blank');
			
			$('a.fancybox_ajax_link', this).each(function() {
				$(this).attr('href', $(this).attr('href') + '/ajax');
			});
			
			$('a.fancybox_link, a.fancybox_ajax_link', this).each(function() {
				var frameWidth = $.fn.fancybox.defaults.frameWidth;
				var frameHeight = $.fn.fancybox.defaults.frameHeight;
				if ($(this).hasClass('iframe'))
				{
					frameWidth = 900;
					frameHeight = 600;
				}
				
				$(this).fancybox({
					transitionIn: 'elastic',
					transitionOut: 'elastic',
					overlayShow: true,
					autoDimensions: $(this).hasClass('fancybox_autodimensions'),
					autoScale: !$(this).hasClass('fancybox_noscale'),
					frameWidth: frameWidth,
					frameWidth: frameHeight
				});
			});
			
			$('.toggle_button', this).each(function()
			{
				$(this).show();
				if ($(this).hasClass('open'))
				{
					var parent = $(this).closest('.toggle_parent');
					parent.css('width', parent.width()+'px');
					$('.toggle_child', parent).hide();
				}
			});
			
			
			if ($.fn.crossSlide)
			{
				$('.picture_slide_gallery', this).each(function()
				{
					if ($('img', this).length <= 1) {
						return;
					}
				
					var time = 5;
					var images = new Array();
					
					$('img', this).each(function()
					{
						images.push({ src: $(this).attr('src') });
					});
					
					$('.frame', this).crossSlide({
						fade: 1.5,
						sleep: 3
					}, images);
				});
			}
			
			if ($.fn.mask)
			{
				$('.input_date', this).mask('?99/99/9999');
				$('.input_time', this).mask('?99:99');
				$('.input_postcode', this).mask('?9999 aa');
				$('.input_phone', this).mask('?9999999999');
			}
			
			if ($.fn.wymeditor)
			{
				$('.input_textarea:not(.no_html), textarea.wysiwyg', this).each(function()
				{
					$(this).wymeditor({
						basePath: '/js/wymeditor/',
						skinPath: '/css/wymeditor/',
						logoHtml: '',
						classesHtml: '',
						containersHtml: '',
						statusHtml: '',
						toolsItems: tools = [
							{'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'},
							{'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
							{'name': 'Superscript', 'title': 'Superscript', 'css': 'wym_tools_superscript'},
							{'name': 'Subscript', 'title': 'Subscript', 'css': 'wym_tools_subscript'},
							{'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
							{'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
							{'name': 'Indent', 'title': 'Indent', 'css': 'wym_tools_indent'},
							{'name': 'Outdent', 'title': 'Outdent', 'css': 'wym_tools_outdent'},
							{'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
							{'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
							{'name': 'CreateLink', 'title': 'Link', 'css': 'wym_tools_link'},
							{'name': 'Unlink', 'title': 'Unlink', 'css': 'wym_tools_unlink'},
							{'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'},
							{'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'},
							{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'},
							{'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'}]
					});
				});
			}
			
			if ($.maxlength)
			{
				$('input[type="text"].maxlength, textarea.maxlength').each(function()
				{
					var name = $(this).attr('name');
					var options = {};
					var maxlength =  $('input[name="maxlength_'+name+'"]', $(this).closest('form'));
					
					if ($(maxlength).length == 1) {
						options.maxlength = $(maxlength).val();
					}
					
					$(this).maxlength(options);
				});
			}
			
			for (var i in initialize_functions)
				initialize_functions[i].apply(this);
		});
	};
	
})(jQuery);