function initializeEvcatButtons() {
	$('.modalEventCats').click(function (e) {
		e.preventDefault();
		// load the comment form using ajax
		var rnd=new Date().getTime();
		$.get("ax_get_evcat.php?rnd="+rnd, function(data){
			// create a modal dialog with the data
			$(data).modal({
				closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
				position: ["15%",],
				overlayId: 'eventcat-overlay',
				containerId: 'eventcat-container',
				onOpen: evcat.open,
				onShow: evcat.show,
				onClose: evcat.close
			});
		});
	});

	// preload images
	var img = ['cancel.png', 'form_bottom.gif', 'form_top.gif', 'loading.gif', 'send.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = '/images/modalpopup/' + this;
	});
}

var evcat = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#eventcat-container .eventcat-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#eventcat-container .eventcat-input').css({
				'font-size': '.9em'
			});
		}

		// dynamically determine height
		var h = 320;
		if ($('#eventcat-subject').length) {
			h += 26;
		}
		if ($('#eventcat-cc').length) {
			h += 22;
		}

		var title = $('#eventcat-container .eventcat-title').html();
		$('#eventcat-container .eventcat-title').html('Loading...');
		dialog.overlay.fadeIn(100, function () {
			dialog.container.fadeIn(100, function () {
				dialog.data.fadeIn(100, function () {
					$('#eventcat-container .eventcat-content').animate({
						height: h
					}, function () {
						$('#eventcat-container .eventcat-title').html(title);
						$('#eventcat-container form').fadeIn(100, function () {
							$('#eventcat-container #eventcat-name').focus();

							$('#eventcat-container .eventcat-cc').click(function () {
								var cc = $('#eventcat-container #eventcat-cc');
								cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
							});

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#eventcat-container .eventcat-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#eventcat-container .eventcat-send').click(function (e) {
			e.preventDefault();
			// validate form
			if (evcat.validate()) {
				var msg = $('#eventcat-container .eventcat-message');
				msg.fadeOut(function () {
					msg.removeClass('eventcat-error').empty();
				});
				$('#eventcat-container .eventcat-title').html('Sending...');
				$('#eventcat-container form').fadeOut(200);
				$('#eventcat-container .eventcat-content').animate({
					height: '100px'
				}, function () {
					$('#eventcat-container .eventcat-loading').fadeIn(200, function () {
						$.ajax({
							url: 'ax_sv_evcat.php',
							data: $('#eventcat-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							success: function (data) {
								$('#eventcat-container .eventcat-loading').fadeOut(200, function () {
									$('#eventcat-container .eventcat-title').html('Thank you!');
									msg.html(data).fadeIn(200);
								});
							},
							error: evcat.error
						});
					});
				});				
			}
			else {
				if ($('#eventcat-container .eventcat-message:visible').length > 0) {
					var msg = $('#eventcat-container .eventcat-message div');
					msg.fadeOut(100, function () {
						msg.empty();
						evcat.showError();
						msg.fadeIn(100);
					});
				}
				else {
					$('#eventcat-container .eventcat-message').animate({
						height: '30px'
					}, evcat.showError);
				}
				
			}
		});
	},
	close: function (dialog) {
		$('#eventcat-container .eventcat-message').fadeOut();
		$('#eventcat-container .eventcat-title').html('Closing...');
		$('#eventcat-container form').fadeOut(100);
		$('#eventcat-container .eventcat-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(100, function () {
				dialog.container.fadeOut(100, function () {
					dialog.overlay.fadeOut(100, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	validate: function () {
		evcat.message = '';

		if (evcat.message.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	showError: function () {
		$('#eventcat-container .eventcat-message')
			.html($('<div class="eventcat-error"></div>').append(evcat.message))
			.fadeIn(100);
	}
};

runWhenReady[runWhenReady.length] = 'initializeEvcatButtons';

