function createGalleryLayout(fn) {
	var galleryFrame = $('#galleryFrame');
	var editMode = ($('body.editMode').length > 0);
	
	$('.galleryItem').appendTo(galleryFrame);
	$('.galleryCol').remove();
	
	var colCount = Math.ceil(galleryFrame.children().length/3);
	var pageCount = Math.ceil(colCount/4);
	if(!editMode) galleryFrame.css('width', colCount*230);

	galleryFrame.data('arrowLeft', $('#arrowLeft'));
	galleryFrame.data('arrowRight',$('#arrowRight'));
	
	var images = galleryFrame.children();
	for (var i = 0; i < colCount; i++) {
		images.slice(i*3, (i+1)*3).wrapAll('<div class="galleryCol"></div>');
	}
	
	if(galleryFrame.data('currentPage') >= pageCount-1) galleryFrame.data('arrowRight').hide();
	else if(!editMode) galleryFrame.data('arrowRight').show();
		
	galleryFrame.data('pageCount', pageCount);
	galleryFrame.data('colCount', colCount);
	
	if(fn) fn();
	
}

$('document').ready(function() {
	$("a.fancyboxImage").fancybox({
		onStart: function() {
		if($('body').hasClass('preventFancybox') || $('body').hasClass('editMode')) {
			$('body').removeClass('preventFancybox');
			return false;
			}
		},
		'margin': 80
	});

	$("a.fancyboxInline").fancybox({
        'autoScale': false,
        onStart: function() {
    		if($('body').hasClass('preventFancybox') || $('body').hasClass('editMode')) {
    			$('body').removeClass('preventFancybox');
    			return false;
    			}
    		},
    	'margin': 80
        });
	
	$('.documentText .mainImage:not(.editBar)').click(function() {
		var images = [];

		$('.documentText .documentTextImages a').each(function() {		
			images.push({
				'href':$(this).attr('href')
			});
		});
		
		
		$.fancybox(images, {
			'margin': 80
		});
		return false;
	});
		
	var galleryFrame = $('#galleryFrame');
	if(galleryFrame.length > 0) {		
		galleryFrame.data('currentPage', 0);
		
		createGalleryLayout();
		
		galleryFrame.data('arrowLeft').hide();
		
		galleryFrame.data('arrowRight').click(function() {
			currentPage = galleryFrame.data('currentPage'); 
			if(currentPage < galleryFrame.data('pageCount')) {
				currentPage++;
				if(currentPage >= galleryFrame.data('pageCount')-1) galleryFrame.data('arrowRight').hide();
				if(currentPage > 0) galleryFrame.data('arrowLeft').show();
				galleryFrame.animate({'left' : -1*currentPage*920}, 400, 'swing');
			}
			galleryFrame.data('currentPage', currentPage);
		});
		galleryFrame.data('arrowLeft').click(function() {
			currentPage = galleryFrame.data('currentPage');
			if(currentPage > 0) {
				currentPage--;
				if(currentPage < galleryFrame.data('pageCount')) galleryFrame.data('arrowRight').show();
				if(currentPage < 1) galleryFrame.data('arrowLeft').hide();
				galleryFrame.animate({'left' : -1*currentPage*920}, 400, 'swing');
			}
			galleryFrame.data('currentPage', currentPage);
		});
		
	}
	
	$('.galleryItem, #homeImages li, #galleries li, .documentText .right img').live('mouseover', function() {
		$('.galleryItem:not(.deleted), #homeImages li:not(.deleted), #galleries li, .documentText .right img').not(this).stop().animate({'opacity': 0.3}, 200);			
	})
		.live('mouseout', function() {
		$('.galleryItem:not(.deleted), #homeImages li:not(.deleted), #galleries li, .documentText .right img').stop().animate({'opacity': 1}, 200);
	});
	
	var galleries = $('#galleries');
	if(galleries.length > 0) {
		$('.gallery').hover(function() {
			var bg = $(this).find('span.bg_hover');
			bg.css('width', '0px').stop().show().animate({'width':bg.parent().width()}, 80);
		},
		function() {
			$(this).find('span.bg_hover').stop().animate({'width':'0px'}, 80, function() {
				$(this).hide();
			});
		});
	}
	
	$("a").click(function() {
		  $(this).blur();
		});
	
	var documentSpecials = $('#documentSpecials');
	if(documentSpecials.length > 0) {
		documentSpecials.find('#arrowLeft').click(function() {
			var current = $(documentSpecials).find('.active');
			var next = current.prev();
			if(next.length < 1) next = documentSpecials.find('li:last');
			
			current.css('left', '0').removeClass('active').animate({'left':'690px'}, 400);
			next.css('left', '-690px').addClass('active').animate({'left':'0px'}, 400);
		});
		documentSpecials.find('#arrowRight').click(function() {
			var current = $(documentSpecials).find('.active');
			var next = current.next();
			if(next.length < 1) next = documentSpecials.find('li:first');
			

			current.css('left', '0').removeClass('active').animate({'left':'-690px'}, 400);
			next.css('left', '690px').addClass('active').animate({'left':'0px'}, 400);
		});
	}
	
	$('#editorIcons .iconPublish').click(function(){
		if(confirm("Wollen Sie die Änderungen veröffentlichen?")) {
			$.ajax({
	            type: "GET",
	            url: BaseURL+'/previews/publish/'
			});
		}
	});
	
});
