$(function() {
	// Lightbox
	$('a.lightbox').lightBox();
	
	// Rounded corners
	//$('.add>div').corner("round 8px").parent().css('padding', '3px').corner("round 10px");
	
	// Click-toggling
	$('.add .topborder + div[class!="noHover"]').live('click', function(e) {
		$(this).closest('.add').find('.addContent').toggle(200).parent().toggleClass('noHover');
	});

	$('.add .minimizer').click(function(e) {
		e.preventDefault();
		$(this).parent().toggle(200).parent().toggleClass('noHover');
		return false;
	});
	
	/*** Slideshow-functionality ***/
	// Set up containers
	var hiddenAdds = $('.addContent:hidden');
	hiddenAdds.show();
	$('.addImageContainer').each(function() {
		if($(this).children('img').size() > 0) {
			// Check if we need the Next-button
			if($(this).children('img').size() > 1) {
				$(this).find('div:last').show();
			}

			var h = $(this).children('img:visible').outerHeight();
			if(h < 3) return;
			$(this).height(h + 'px');
		}
	});
	hiddenAdds.hide();
	
	// Prev-button
	$('.addImageContainer>div:first-child').click(function() {
		
		// Make sure we don't screw up during fade-times
		if($(this).siblings('img:animated').size() > 0) return;
		
		// If there's no more previous images after this one, hide this button
		if($(this).siblings('img:visible').prevAll().size() == 2)
			$(this).hide();
		
		// Fadeout the visible, fadeIn previous
		$(this).siblings('img:visible').fadeOut()
		.prev().css({position: 'absolute', top: '0px', left: '0px'}).fadeIn(function() {
			$(this).css('position', 'relative');
			$(this).parent().animate({height: $(this).outerHeight() + 'px'});
		});
		
		// If there are next images and the Next-button is hidden, show the Next-button
		$(this).siblings('div:last:hidden').show();
	});
	
	// Next-button
	$('.addImageContainer>div:last-child').click(function() {
		
		// Make sure we don't screw up during fade-times
		if($(this).siblings('img:animated').size() > 0) return;
		
		// If there's no more next images after this one, hide this button
		if($(this).siblings('img:visible').nextAll().size() == 2)
			$(this).hide();
		
		// Fadeout the visible, fadeIn next
		$(this).siblings('img:visible').fadeOut()
		.next().css({position: 'absolute', top: '0px', left: '0px'}).fadeIn(function() {
			$(this).css('position', 'relative');
			$(this).parent().animate({height: $(this).outerHeight() + 'px'});
		});
		
		// If there are previous images and the Prev-button is hidden, show the Prev-button
		$(this).siblings('div:first:hidden').show();
	});
	
	// Fix row-lengths
	hiddenAdds.show();
	$('.add .infos>div').each(function() {
		var prev = $(this).prev().position();
		var curr = $(this).position();
		var next = $(this).next().position();
		
		if((prev && prev.top != curr.top) && (next && next.top != curr.top))
			$(this).width(($(this).parent().innerWidth()-14) + 'px');
	});
	hiddenAdds.hide();
	
	// Rented-calendar
	$('.jCalTarget').each(function() {
		var cal = $(this);
		$.get('js/dates.php?q=' + cal.attr('rel'), function(data) {
			var dates = eval('('+ data +')');
			cal.jCal({
				dCheck:		function(day) {
					var t = Date.parse(day) / 1000;
					if($.inArray(t, dates.rented) != -1) {
						return 'invday rentedDay';
					} else if(t >= dates.start && t < dates.stop) {
						return 'day';
					} else {
						return 'invday';
					}
				}
			});
		});
	});
});


