$(function() {
	var ajaxRequests = [];

	function ajaxAbort()
	{
		/*for (i in ajaxRequests) {
			ajaxRequests[i].abort();
		}
		ajaxRequests = [];*/
	}

	$('#cal .cal-prev,#cal .cal-next').livequery(function() {
		$(this).bind('click', function() {
			if ($(this).is('.cal-prev-inactive')) {
				return false;
			}

			var data = $(this).metadata();

			$('#cal .cal-content').insertLoading({top: '27px'}).find('table').css('opacity', .33);
			$('#cal-container').load($(this).parent().metadata().url + 'kalender/kalender/?display='+data.display + '&date='+data.date, function() {
				if ($('td.past', this).length) {
					$('#cal .cal-prev').addClass('cal-prev-inactive');
				} else {
					$('#cal .cal-prev').removeClass('cal-prev-inactive');
				}
			});
			return false;
		});
	});
	
	$('#tooltip.notification .save').livequery(function() {
		$(this).bind('click', function() {
			var data = $(this).metadata();
			$.post(
				data.url,
				$(this).parents('form').serialize()
			);

			$($.tooltip.current()).addClass('notification-active');
			$.tooltip.hide();
		});
	});
	
	$('#tooltip.notification .delete').livequery(function() {
		$(this).bind('click', function() {
			var data = $(this).metadata();
			$.post(
				data.url
			);

			$($.tooltip.current()).removeClass('notification-active')/*.removeData('tooltip-content')*/;
			$.tooltip.hide();
		});
	});
	
	$('#manage .event').livequery(function() {
		var self = this;
		$('.location', self).bind('click', function(e) {
       		e.preventDefault();
       		var href = $(this).attr('href');
       		if (href)
       			eventsDialogMapOpen(href, $(this).text());
       		return false;
    	});
	});

	$('#list .event, #result .event').livequery(function() {
		var data = $(this).metadata();
		var self = this;

		$('.notification', self).tooltip({
    		bodyHandler: function() {
    			var data = $(this).metadata();
    			var $c = $(this).data('tooltip-content');
    			if (!$c)
    				$c = $('<div class="tooltip-content"/>').html(loadHtml).load(data.url, function() {
    					$.tooltip.update();
    				});
    			
    			$(this).data('tooltip-content', $c);

    			return $c;
    		},
    		delay: 0,
			showURL: false,
			extraClass: "notification",
			top: 10,
			left: 10,
			id: "tooltip",
			viewport: '#rgt',
			css: {
				width: '200px',
				minHeight: '120px'
			}
    	}, 'click');

		$('.arrow', self).bind('click', function() {
			if ($(this).is('.arrow-open')) {
				$(this).removeClass('arrow-open');
				$('.extended', self).slideUp();
			} else {
				$(this).addClass('arrow-open');
				$('.extended', self).slideDown();
			}
			return false;
		});

		$('.name', self).bind('click', function() {
			$('.arrow', self).trigger('click');
			return false;
		}).css('cursor', 'pointer');

		$('.fav', self).bind('click', function(e) {
       		var onFavList = $(this).is('.on-fav-list');

       		if (onFavList) {
       			if (!confirm(decode('Willst Du den Eintrag wirklich aus der Liste l&ouml;schen?'))) {
					e.stopPropagation();
					e.preventDefault();
					return false;
				}
       		}
       		
       		var data = $(this).metadata();

        	if (this.checked) {
        		$(this).next().removeClass('option-inactive').html('Gemerkt');
        	} else {
        		$(this).next().addClass('option-inactive').html('Merken');
        	}

        	$.post(
        		data.url,
            	{active: this.checked ? 1 : 0}
        	);
        	
        	if (!this.checked && onFavList) {
            	$(this).parents('.result-row').fadeOut();
            }
    	});

    	$('.recommend', self).bind('click', function(e) {
       		e.preventDefault();
       		var href = $(this).attr('href');
       		if (href)
       			eventsDialogRecommendOpen(href, $(this).attr('title'));
       		return false;
    	});
    	
    	$('.communevent', self).bind('click', function(e) {
       		e.preventDefault();
       		var href = $(this).attr('href');
       		if (href)
       			eventsDialogCommuneventOpen(href, $(this).attr('title'));
       		return false;
    	});
    	
    	$('.map,.location', self).bind('click', function(e) {
       		e.preventDefault();
       		var href = $(this).attr('href');
       		if (href)
       			eventsDialogMapOpen(href, $(this).attr('title'));
       		return false;
    	});

    	$('.print,.url,.tickets,.description a', self).bind('click', function(e) {
       		var href = $(this).attr('href');
       		if (href) {
       			e.preventDefault();
       			var w = window.open(href);
       			return false;
       		}
    	});
    	
    	$('.thickbox', self).thickbox();
	});

	$('#close_all').bind('click', function() {
		ajaxAbort();
		
		$('#list th.active').removeClass('active').removeClass('clicked');

		$('#list td.active').removeClass('active').removeClass('clicked');
		$('#list th.date span').removeClass('arrow-open');

		$('#list tr.result-row').hide();
		return false;
	});

	$('#open_all').bind('click', function() {
		$('#list th.date:not(.clicked) span').trigger('click', [true]);
		$('#list th.category').removeClass('active');
		return false;
	});

	$('#list th.category a').bind('click', function(e, fromAll) {
		var data = $(this).metadata();

		if (!fromAll) {
			ajaxAbort();
		}

		var $parent = $(this).parent();
		
		if ($parent.is('.clicked')) {
			$parent.removeClass('active').removeClass('clicked');
			$('#list td.category-'+data.category_id).removeClass('active').removeClass('clicked');
			$('#list .result-row.category-'+data.category_id).hide();
		} else {
			if (!fromAll && !$('#list th.category.clicked').length) {
				$('#list th.date').removeClass('active').removeClass('clicked');
				$('#list th.date span').removeClass('arrow-open');
				$('#list td.active').removeClass('active').removeClass('clicked');
				$('#list tr.result-row').hide();
			}

			$parent.addClass('active').addClass('clicked');
			$('#list td.category-'+data.category_id).addClass('active').addClass('clicked').find('.category-count').trigger('click', [true]);
		}

		return false;
	});
	
	$('#list th.category').hover(function() {
		var data = $('a', this).metadata();
		$(this).addClass('active');
		$('#list td.category-'+data.category_id).addClass('active');
	}, function() {
		var data = $('a', this).metadata();
		if ($(this).is('.clicked')) {
			return;
		}
		$(this).removeClass('active');
		$('#list td.category-'+data.category_id+':not(.clicked)').removeClass('active');
	});

	$('#list th.date span').bind('click', function(e, fromAll) {
		var data = $(this).metadata();

		if (!fromAll) {
			ajaxAbort();
		}
		
		var $parent = $(this).parent();

		if ($parent.is('.clicked')) {
			$(this).removeClass('arrow-open');
			$parent.removeClass('active').removeClass('clicked');
			$('#list td.date-'+data.date).removeClass('active').removeClass('clicked');
			$('#list .result-row.date-'+data.date).hide();
		} else {
			if (!fromAll && !$('#list th.date.clicked').length) {
				$('#list th.category').removeClass('active').removeClass('clicked');
				$('#list td.active').removeClass('active').removeClass('clicked');
				$('#list tr.result-row').hide();
			}

			$(this).addClass('arrow-open');
			$parent.addClass('active').addClass('clicked');

			$('#list td.date-'+data.date).addClass('active').addClass('clicked').find('.category-count').trigger('click', [true]);
		}
		return false;
	});
	
	$('#list th.date').hover(function() {
		var data = $('span', this).metadata();
		$(this).addClass('active');
		$('#list td.date-'+data.date).addClass('active');
	}, function() {
		var data = $('span', this).metadata();
		if ($(this).is('.clicked')) {
			return;
		}
		$(this).removeClass('active');
		$('#list td.date-'+data.date+':not(.clicked)').removeClass('active');
	});
	
	$('#list th.date').bind('click', function() {
		$('span', this).trigger('click');
	})
	.css('cursor', 'pointer');

	$('#list td .category-count:not(.category-count-spacer)').bind('click', function(e, fromGroup) {
		var data = $(this).metadata();
		var $parent = $(this).parent();

		var $existing = $('#list tr.result-row.category-'+data.category_id+'.date-'+data.date);

		if ($existing.size() > 0) {
			if (!fromGroup && $parent.is('.clicked')) {
				$parent.removeClass('active').removeClass('clicked');
				$existing.hide();

				if (!$('#list td.date-'+data.date+'.active:not(.first,.last)>.category-count').length) {
					$('#list td.date-'+data.date+'.active').removeClass('active').removeClass('clicked');
					$('#list th.date-'+data.date+'.active').removeClass('active').removeClass('clicked');
					$('#list th.date-'+data.date+' span.arrow-open').removeClass('arrow-open');
				}

				if (!$('#list td.category-'+data.category_id+'.active:not(.first,.last)>.category-count').length) {
					$('#list td.category-'+data.category_id+'.active').removeClass('active').removeClass('clicked');
					$('#list th.category-'+data.category_id+'.active').removeClass('active').removeClass('clicked');
				}
			} else {
				$parent.addClass('active').addClass('clicked');
				$existing.show();
			}
			return false;
		}
		
		$parent.addClass('active').addClass('clicked');
		
		var num = toInt($(this).text());
		if (!num) {
			return false;
		}

		var $row = $('#list tr.placeholder-row.category-'+data.category_id+'.date-'+data.date);

		for (var i = 0; i < num; i++) {
			var $rr = $('<tr class="result-row category-'+data.category_id+' date-'+data.date+'"><th></th><td>&nbsp;</td><td colspan="11">'+loadHtml+'</td></tr>');
			$row = $row.after($rr).next();
		}

		var url = $('#list').metadata().url + 'event/ajax/?' + $.param(data);
		
		ajaxRequests.push(
			$.get(url, function(html) {
				var $tds = $('#list tr.result-row.category-'+data.category_id+'.date-'+data.date).reverse();
				$('<div/>').html(html).find('.event').each(function(i) {
					$($tds.get(i)).find('td:eq(1)').html(this);
				});
			})
		);
		
		return false;
	})
	.parent()
	.hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});
	
	$('.event-preview').bind('click', function(e) {
		e.preventDefault();
		eventPreviewDialogOpen($(this).attr('href'), $(this).text());
		return false;
	});

	$('.event-absagen').bind('click', function(e) {
		e.preventDefault();
		eventAbsagenDialogOpen($(this).attr('href'), $(this).text());
		return false;
	});

	
});

// ---

function eventsFormCommuneventSuccessCallback() 
{
	eventsDialogCommuneventClose();
};

function eventsDialogCommuneventCreate(url, title)
{
	createIframeDialog('events_dialog_communevent', url, title);
};

function eventsDialogCommuneventRemove()
{
	$('#events_dialog_communevent').remove();
};

function eventsDialogCommuneventOpen(url, title)
{
	eventsDialogCommuneventCreate(url, title);
	$('#events_dialog_communevent').dialog('open');

	//$('.ui-dialog-overlay').bind('click', eventsDialogCommuneventClose);
};

function eventsDialogCommuneventClose()
{
	$('#events_dialog_communevent').dialog('close');
	$('.ui-dialog-overlay').unbind('click', eventsDialogCommuneventClose);
	eventsDialogCommuneventRemove();
};

// ---

function eventsDialogMapCreate(url, title)
{
	createIframeDialog('events_dialog_map', url, title);
};

function eventsDialogMapRemove()
{
	$('#events_dialog_map').remove();
};

function eventsDialogMapOpen(url, title)
{
	eventsDialogMapCreate(url, title);
	$('#events_dialog_map').dialog('open');

	//$('.ui-dialog-overlay').bind('click', eventsDialogMapClose);
};

function eventsDialogMapClose()
{
	$('#events_dialog_map').dialog('close');
	$('.ui-dialog-overlay').unbind('click', eventsDialogMapClose);
	eventsDialogMapRemove();
};

//---

function eventPreviewDialogCreate(url, title)
{
	createIframeDialog('event_preview_dialog', url, title);
};

function eventPreviewDialogRemove()
{
	$('#event_preview_dialog').remove();
};

function eventPreviewDialogOpen(url, title)
{
	eventPreviewDialogCreate(url, title);
	$('#event_preview_dialog').dialog('open');
};

function eventPreviewDialogClose()
{
	$('#event_preview_dialog').dialog('close');
};

//---

function eventAbsagenFormSuccessCallback() {
	eventAbsagenDialogClose();
	location.reload(true);
};


function eventAbsagenDialogCreate(url, title)
{
	createIframeDialog('event_absagen_dialog', url, title);
};

function eventAbsagenDialogRemove()
{
	$('#event_absagen_dialog').remove();
};

function eventAbsagenDialogOpen(url, title)
{
	eventAbsagenDialogCreate(url, title);
	$('#event_absagen_dialog').dialog('open');
};

function eventAbsagenDialogClose()
{
	$('#event_absagen_dialog').dialog('close');
};

