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

	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();
		
		
		// INFO BOX
	
		$('.info_box .info_link_show > a, .info_box .info_link_hide > a').live('click', function()
		{
			var box = $(this).closest('.info_box');
			$('.info_link_show', box).toggle();
			$('.info_link_hide', box).toggle();
			$('.info_text', box).slideToggle(500);
			return false;
		});
	
	
		// TABS
	
		$('.tabs .tab-links a').live('click', function()
		{
			var tabs = $(this).closest('.tabs');
			
			$('.tab-links a.active', tabs).removeClass('active');
			$(this).addClass('active');
			
			$('.tab-visible', tabs).removeClass('tab-visible').addClass('tab-hidden');
			$('#' + $(this).attr('rel'), tabs).removeClass('tab-hidden').addClass('tab-visible').select();
			
			return false;
		});
		$('.tabs .tab-links a').live('hover',
		function()
		{
			if (!$(this).hasClass('active'))
				$(this).addClass('hover');
		},
		function()
		{
			$(this).removeClass('hover');
		});
		
		
		// LIST NAVIGATION
		
		$('.item_list_navigation .prev_button, .item_list_navigation .next_button').live(
			'click', $.ajaxutils.ajax_link_click);
		
		
		// AJAX
		
		$('.link_button:not(.no_ajax)').live('click', $.ajaxutils.ajax_link_click);
		
		
		// FORM DIALOGS
		
		if ($.dialogutils)
		{
			$.dialogutils.texts.confirm = 'Weet je zeker dat je';
			$.dialogutils.texts.confirm_twice = 'Weet je heel zeker dat je';
			$.dialogutils.texts.yes_button = 'Ja';
			$.dialogutils.texts.cancel_button = 'Annuleer';
			$.dialogutils.texts.confirm_button = 'Opslaan';
		
			$('.delete_button').live('click', function()
			{
				var name = $('.item_name input:not(input[type="hidden"])', $(this).closest('tr')).val();
				$.dialogutils.confirm_twice_dialog('Verwijderen', "'"+name+"' wilt verwijderen",
					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('Toevoegen', "'"+name+"' wilt toevoegen",
					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('Wijzigen', "de wijzigingen op wilt slaan",
					function (result, link) {
						if (result)
						{
							if ($(link).hasClass('no_ajax'))
								$(link).closest('form').submit();
							else
								$(link).ajax_form_submit();
						}
					}, this);
				return false;
			});
		}
		
		
		// PICTURE LIST
		
		$('.picture_list .item_list_navigation a').live('click', function()
		{
			if ($(this).hasClass('busy'))
				return false;
			
			$('.picture_list .item_list_navigation a', nav).addClass('busy');
			
			var list = $(this).closest('.picture_list');
			var nav = $(this).closest('.item_list_navigation');
	
			var currPage = parseInt($('.nav_info .nav_current_page', nav).text());
			var maxPage = parseInt($('.nav_info .nav_max_page', nav).text());
			var nextPage = $(this).hasClass('gallery_prev_button') ? currPage - 1 : currPage + 1;
			
			if (currPage == 1 || nextPage == 1)
				$('.nav_link_prev', nav).toggleClass('hide');
			if (currPage == maxPage || nextPage == maxPage)
				$('.nav_link_next', nav).toggleClass('hide');
			
			$(list).css('height', $(list).height() + 'px');
			$('.album_table_' + currPage, list).fadeOut(400,
			function()
			{
				$('.album_table_' + nextPage, list).fadeIn(400,
				function()
				{
					$('a', nav).removeClass('busy');
					$(list).css('height', '');
				});
			});
			
			$('.nav_info .nav_current_page', nav).text(nextPage);
			return false;
		});
		
		
		// INPUT VALIDATION
		
		if ($.validation)
		{
			$.validation.texts.error_dialog_title = 'Foutieve gegevens';
			$.validation.texts.error_dialog_msg = 'Niet all gegevens zijn goed ingevoerd. Verbeter alsjeblieft de fouten.';
		}
		
		
		// SMILEYS
		
		$('.smiley_link').live('click', function()
		{
			var message = $('textarea', $(this).closest('.smileys_list').parent());
			
			if ($.browser.msie)
			{
				message.focus();
				selection = document.selection.createRange();
				selection.text = $(this).attr("title");
			}
			else if (message.attr("selectionStart") != undefined)
			{
				var startPos = message.attr("selectionStart");
				var endPos = message.attr("selectionEnd");
				var currValue = message.val();
				var addValue = $(this).attr("title");
				message.val(
					currValue.substring(0, startPos) +
					addValue +
					currValue.substring(endPos));
	
				message.attr("selectionStart", startPos + addValue.length);
				message.attr("selectionEnd", startPos + addValue.length);
			}
			else {
				message.val(message.val() + $(this).attr("title"));
			}
	
			message.change();
			message.keyup();
			return false;
		});
	});
	
	$.fn.initialize = function()
	{
		return this.each(function()
		{
			$('a.external[href], a.external_img[href]', 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() {
				$(this).fancybox({
					transitionIn: 'elastic',
					transitionOut: 'elastic',
					overlayShow: true,
					autoDimensions: $(this).hasClass('fancybox_autodimensions'),
					autoScale: !$(this).hasClass('fancybox_noscale')
				});
			});

			$('.picture_gallery', this).each(function()
			{
				var gallery = this;
				var max_page = parseInt($('.nav_info .nav_max_page', gallery).text());

				$('a.picture_gallery_link', this).fancybox({
					transitionIn: 'elastic',
					transitionOut: 'elastic',
					overlayShow: true,
					onStart: function(array, index) {
						var table = $(array[index]).closest('.album_table');
						var new_table = $('.album_table', gallery).index(table) + 1;
						var current_table = parseInt($('.nav_current_page', gallery).text());
						
						if (current_table != new_table)
						{
							$('.album_table_'+current_table, gallery).hide();
							$('.album_table_'+new_table, gallery).show();
							$('.nav_current_page', gallery).text(new_table);
							
							$('.nav_link_prev', gallery).toggleClass('hide', new_table == 1);
							$('.nav_link_next', gallery).toggleClass('hide', new_table == max_page);
						}
					}
				});
			});
			
			$('.info_box', this).each(function()
			{
				if ($(this).hasClass('show_directly'))
				{
					$('.info_text', this).css('border-top', 'none');
					$('.info_link_hide', this).show();
				}
				else
				{
					$('.info_text', this).hide().css('border-top', 'none');
					$('.info_link_show', this).show();
				}
			});
			
			$('.picture_list').each(function()
			{
				$('table', this).not(':first').hide();
				$('.item_list_navigation').show();
			});
			
			if ($.fn.sortable)
			{
				$('.drag_lists .drag_list_entries', this).sortable({
					cursor: 'move',
					cancel: 'img, input',
					placeholder: 'placeholder drag_list_entry',
					opacity: 0.8,
					appendTo: 'body'
				});
				
				$('.drag_lists .left .drag_list_entries', this).sortable(
				'option', 'connectWith', '.drag_lists .right .drag_list_entries');
				$('.drag_lists .right .drag_list_entries', this).sortable(
					'option', 'connectWith', '.drag_lists .left .drag_list_entries');
			}
			
			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()
					{
						/*var fromVal = Math.ceil(100*Math.random()) + '% ' +
							Math.ceil(100*Math.random()) + '% ' +
							'1.' + Math.floor(6*Math.random()) + 'x';
						
						var toVal = Math.ceil(100*Math.random()) + '% ' +
							Math.ceil(100*Math.random()) + '% ' +
							'1.' + Math.floor(6*Math.random()) + 'x';
					
						images.push({
							src: $(this).attr('src'),
							from: fromVal,
							to: toVal,
							time: time
						});*/
						
						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/',
						lang: 'nl',
						logoHtml: '',
						classesHtml: '',
						containersHtml: '',
						statusHtml: '',
						toolsItems: [
							{'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);
				});
			}
			
			$('.smileys_list', this).each(function()
			{
				$('.smileys_show_link, .smileys_hide_link', this).click(function()
				{
					var parent = $(this).closest('.smileys_list');
					$('.smileys_show_link', parent).toggle();
					$('.smileys_hide_link', parent).toggle();
					$('.smileys', parent).slideToggle('normal');
					return false;
				});
				$(this).show();
			});
			
			for (var i in initialize_functions)
				initialize_functions[i].apply(this);
		});
	};
	
})(jQuery);