var active = 0;

$.fn.extend({
	equalHeight: function() {
		var maxHeight = 0;
		this.each(function() { maxHeight = Math.max(maxHeight, $(this).outerHeight()); });
		return this.each(function() { $(this).css({height: maxHeight - $(this).outerHeight() + $(this).height(), minHeight: 'auto'}); });
	}
});

function transformLink(url) {
	// return url.replace(/\.html/, '/ajax');
	return url + (url.match(/\?/)?'&':'?') + 'type=99';
}

function crossfade() {
	$("#left img:eq(" + active + ")").fadeOut('slow');
	active = (active + 1) % $("#left img").length;
	$("#left img:eq(" + active + ")").fadeIn('slow');
}

$(document).ready(function(){
	$('#left,#right').equalHeight();
	$('.more-link a').fancybox({
		'transformLink': transformLink,
		titleShow: false,
		padding: 25,
		overlayColor: '#000',
		overlayOpacity: 0.1,
		ajax: {cache: false},
		outerStyles: {
			background: 'transparent url(/fileadmin/img/papier.jpg) no-repeat 0 0',
			'-moz-box-shadow': '#000 1px 1px 5px 1px',
			'-webkit-box-shadow': '#000 1px 1px 5px 1px'
		},
		innerStyles: {
			border: '3px dashed #be2d18',
			padding: '15px',
			top: '7px',
			left: '7px'
		},
		onComplete: function() {
			/*$('#fancybox-inner').css('overflow-x', 'hidden');*/
			$('.images img:first').addClass('active');
			if($('.images img').length > 1) {
				$('.images').everyTime(3000, 'cycle', function() {
					var active = $('.active');
					var next = active.next().length ? active.next() : $('.images img:first');
					var prev = $('.prev');
					prev.removeClass('prev');
					active.removeClass('active').addClass('prev');
					next.addClass('active').hide().fadeIn(200);
				});
			}
		},
		onCleanup: function() {
			if($('.images img').length > 1) {
				$('.images').stopTime('cycle');
			}
		}
	});

	$('.webcam .polaroid a').fancybox({titlePosition: 'over', outerStyles: {background: '#fff', '-moz-box-shadow': '', '-webkit-box-shadow': ''}, innerStyles: {border: 'none', padding: '10px', top: '0', left: '0'}, onComplete: function() {}, onCleanup: function() {}});
	$('body').append('<div id="temp"></div>');
	$('#temp').hide();
	/*
	$('#contentright').everyTime(10000, 'load_webcam_image', function() {
		$.ajax({
			url: '/?type=99&webcam=1',
			success: function(data) {
					$('#temp').empty().append(data);
					$('#temp').oneTime(200, 'replace_webcam_image', function() {
						var polaroid = $('.webcam .polaroid');
						polaroid.empty();
						$('#temp').children().remove().appendTo(polaroid);
						$('.webcam img').eq($('.webcam').data('activeImage') ? 0 : 1).hide();
						polaroid.find('a').fancybox({titlePosition: 'over', outerStyles: {background: '#fff', '-moz-box-shadow': '', '-webkit-box-shadow': ''}, innerStyles: {border: 'none', padding: '10px', top: '0', left: '0'}, onComplete: function() {}, onCleanup: function() {}});
					});
				}
		});
	});
	$('.news-latest').hide();

	if($('.webcam img').length > 1) {
		$('.webcam').data('activeImage', 0);
		$('.webcam img').eq($('.webcam').data('activeImage') ? 0 : 1).hide();
		$('.webcam').oneTime(3000, 'switch_webcam_image', function() {
			var activeImage = $(this).data('activeImage');
			activeImage = activeImage ? 0 : 1;
			$('img', this).hide().eq(activeImage).show();
			$(this).data('activeImage', activeImage);
		});
	}

	$('#contentright').everyTime(6000, 'switch_news_webcam', function() {
		var switcher = $('.webcam,.news-latest');
		visible = switcher.filter(':visible');
		hidden = switcher.not(visible);
		visible.hide();
		hidden.show();
		if($('.webcam img').length > 1) {
			$('.webcam').oneTime(3000, 'switch_webcam_image', function() {
				var activeImage = $(this).data('activeImage');
				activeImage = activeImage ? 0 : 1;
				$('img', this).hide().eq(activeImage).show();
				$(this).data('activeImage', activeImage);
			});
		}
	});*/

	// cycle images in content area
	$('#left img:gt(0)').hide();
	//setInterval('crossfade()', 9000);
});
