
var wwwroot = '';
var galleries = new Array();

$(document).ready(function() {
	initGalleries();
	initBlog4posts();
	initAltContent();
	initBorderBoxes();
});


/*************
 * Galleries *
 *************/

function initGalleries() {
	var galleryN = $('div.csc-textpic-imagewrap').length;
	for(var j = 0; j < galleryN; j++) {
		if($($('div.csc-textpic-imagewrap')[j]).css('width') != '635px') continue;
		
		var gId = j;
		var imageWrap = $($('div.csc-textpic-imagewrap')[j]);
		var imgN = imageWrap.children().length;
		
		galleries.push(new Gallery(gId, imgN));
		
		if(imageWrap.css('display') == 'none') {
			
			var width = parseInt(imageWrap.parent('div.csc-textpic').css('width'));
			var height = Math.round(0.3748 * width);
			
			imageWrap.css('width', width + 'px');
			if(imgN > 0) {
				var img;
				var showImg = 0;
				if(imageWrap.prev().hasClass('current-image')) {
					showImg = imageWrap.prev().text();
				}
				imageWrap.children().css('width', width + 'px');
				if(imageWrap.hasClass('csc-textpic-single-image')) {
					img = imageWrap.find('img');
				}
				else {
					var dl;
					if(imgN == 1) dl = imageWrap.children('dl');
					else dl = $(imageWrap.children()[showImg]).children('dl');
					dl.css('width', width + 'px');
					dl.children('dd').css('display', 'none');
					img = dl.children('dt').find('img');
				}
				img.attr('width', width);
				img.attr('height', height);
				img.attr('src', wwwroot + '/typo3/thumbs.php?file=%2E%2E%2F' + img.attr('src') + '&size=' + width + 'x' + height);
				for(var i = 0; i < imgN; i++) {
					if(i != showImg) $(imageWrap.children()[i]).css('display', 'none');
				}
				imageWrap.css('display', 'block');
			}
			continue;
		}
		
		if($($('div.csc-textpic-imagewrap')[j]).hasClass('csc-textpic-single-image')) {
			galleries[gId].imgs.push({'src':imageWrap.html(), 'cap':'&copy;', 'desc':'Beschreibung'});
			imageWrap.find('img').css('display','none');
		}
		else {
			
			if(imgN == 1) {
				var cap;
				var desc;
				try { cap = imageWrap.children('dl').children('dd').html(); }
				catch(e) { cap = '&copy;'; }
				try { desc = imageWrap.children('dl').children('dt').find('img').attr('longdesc'); }
				catch(e) { desc = 'Beschreibung'; }
				galleries[gId].imgs.push({'src':imageWrap.children('dl').children('dt').html(), 'cap':cap, 'desc':desc});
				imageWrap.children('dl').css('display','none');
			}
			else {
				imageWrap.children().each(function(index) {
					galleries[gId].imgs.push({'src':$(this).children('dl').children('dt').html(), 'cap':$(this).children('dl').children('dd').html(), 'desc':$(this).children('dl').children('dt').find('img').attr('longdesc')});
					$(this).css('display','none');
				});
			}
		}
		
		var controls = '';
		for(var i = 0; i < galleries[gId].imgN; i++) {
			controls += "<div id=\"flex_gallery_imgbutton" + gId + "_" + i + "\" class=\"flex_gallery_imgbutton";
			if(i == galleries[gId].selN) controls += " active";
			controls += "\"></div>";
		}
		
		var thickboxGallery = '';
		gallery_search:
		{
			var parents = imageWrap.parents();
			for(var i = 0; i < parents.length; i++) {
				if($(parents[i]).hasClass('itemBody') && $(parents[i]).children('div.thickbox_gallery').find('a').attr('href') !== undefined) {
					thickboxGallery = $(parents[i]).children('div.thickbox_gallery').find('a').attr('href');
					break gallery_search;
				}
			}
		}
		if(thickboxGallery.length) thickboxGallery = "<div class=\"flex_gallery_thickbox\" id=\"flex_gallery_tb" + gId + "\"><a href=\"" + wwwroot + "/" + thickboxGallery + "?z=1&height=480&width=480\" class=\"thickbox\" title=\"Gallery\"><img src=\"" + wwwroot + "fileadmin/template/style/img/transparent.gif\" width=\"20\" height=\"20\" /></a></div>";
		
		imageWrap.append("<div class=\"flex_gallery\" id=\"flex_gallery" + gId + "\"><div class=\"flex_gallery_image\" id=\"flex_gallery_image" + gId + "\">" + galleries[gId].imgs[galleries[gId].selN].src + "</div><div class=\"flex_gallery_controls\">" + thickboxGallery + "<div class=\"flex_gallery_infobutton\" id=\"flex_gallery_ib" + gId + "\"></div>" + controls + "</div><div class=\"flex_gallery_info\" id=\"flex_gallery_info" + gId + "\"></div></div>");
		
		$('#flex_gallery_ib' + gId).click(infoButton_click);
		
		for(var i = 0; i < galleries[gId].imgN; i++) {
			$('#flex_gallery_imgbutton' + gId + '_' + i).click(imgButton_click);
		}
		
		if(gId == 0 && $('div.bottomImg').length > 0) {
			var list = "<div><p>Bilder:</p><ul>";
			for(var i = 0; i < galleries[gId].imgN; i++) {
				var desc = galleries[gId].imgs[i].desc;
				list += "<li>" + desc.substring(0, desc.indexOf('||')) + "<br>" + galleries[gId].imgs[i].cap + "</li>";
			}
			list += "</ul></div>";
			$('div.bottomImg').append(list);
			
			if((/MSIE 7/).test(navigator.userAgent)) {
				$('div.bottomImg').css('padding-top', (parseInt($('div.bottomNav').css('height')) - parseInt($('div.bottomImg').children('div').css('height')) - 10).toString() + 'px');
			}
		}
		
		tb_init($('#flex_gallery_tb'+gId).children('a'));
	}
}


function Gallery(id, imgN) {
	this.id = id;
	this.imgN = imgN;
	if(imgN > 0) this.selN = 0;
	else this.selN = -1;
	
	this.imgs = new Array();
}


function infoButton_click() {
	var id = this.id;
	var glrN = id.substring(id.lastIndexOf('b') + 1);
	var selN = galleries[glrN].selN;
	
	if($(this).hasClass('active')) {
		$('#flex_gallery_info' + glrN).animate({
			height: '0px',
			'margin-top': '-20px'
		}, 600);
		$(this).removeClass('active');
	}
	else {
		$('#flex_gallery_info' + glrN).html('<div><p>' + galleries[glrN].imgs[selN].desc.replace(/\|\|/,'') + '</p><p>' + galleries[glrN].imgs[selN].cap +'</p></div>');
		$('#flex_gallery_info' + glrN).css('height', '0px');
		$('#flex_gallery_info' + glrN).animate({
			height: '105px',
			'margin-top': '-125px'
		}, 600);
		$(this).addClass('active');
	}
}


function imgButton_click() {
	var id = this.id;
	var selN = id.substring(id.lastIndexOf('_') + 1);
	var glrN = id.substring(id.lastIndexOf('n') + 1, id.lastIndexOf('_'));

	if($('#flex_gallery_ib' + glrN).hasClass('active')) {
		$('#flex_gallery_info' + glrN).animate({
			height: '0px',
			'margin-top': '-20px'
		}, 200);
		$('#flex_gallery_ib' + glrN).removeClass('active');
	}

	$('#flex_gallery_imgbutton' + glrN + '_' + galleries[glrN].selN).removeClass('active');
	$('#flex_gallery_imgbutton' + glrN + '_' + selN).addClass('active');

	galleries[glrN].selN = selN;
	var oldSrc = $($('#flex_gallery_image' + glrN).find('img').get()).attr('src');

	// ToDo: replace only img here, don't replace div.
	$('#flex_gallery_image' + glrN).replaceWith("<div class=\"flex_gallery_image\" id=\"flex_gallery_image" + glrN + "\" style=\"background:url('" + oldSrc + "') bottom no-repeat\">" + galleries[glrN].imgs[selN].src + "</div>");
	$($('#flex_gallery_image' + glrN).find('img').get()).hide();

	$($('#flex_gallery_image' + glrN).find('img').get()).fadeIn('slow');
}


/*********
 * Share *
 *********/

function share(btn) {
	btn.disabled = true;
	var script = "fileadmin/share.php";
	$.ajax({
		type: "POST",
		url: script,
		data: {
			'send': 'true',
			'senderName' : $("#senderName").attr('value'),
			'senderEmail' : $("#senderEmail").attr('value'),
			'senderMessage' : $("#senderMessage").attr('value'),
			'receiverName' : $("#receiverName").attr('value'),
			'receiverEmail' : $("#receiverEmail").attr('value')
		},
		success: function(data){
			$("#shareForm").replaceWith(data);
		}
	});
}


/****************
 * Border Boxes *
 ****************/

function initBorderBoxes() {
	var boxes = $('div.contentBorder').children('div.csc-default');
	
	for(var i = 0; i < boxes.length; i++) {
		
		if(i < boxes.length - 1 && $(boxes[i]).css('width') == '310px' && $(boxes[i+1]).css('width') == '310px') {
			var height = Math.max(parseInt($(boxes[i]).css('height')), parseInt($(boxes[i+1]).css('height'))) + 'px';
			$(boxes[i]).css('height', height);
			$(boxes[i+1]).css('height', height);
		}
		
		if($(boxes[i]).css('width') == '625px') {
			var imgs = $(boxes[i]).children('div.csc-textpic').children('div.csc-textpic-imagewrap').children('dl.csc-textpic-image');
			var height = 0;
			var max = 0;
			for(var j = 0; j < imgs.length; j++) {
				height = $($(imgs[j]).children('dd.csc-textpic-caption')[0]).css('height');
				max = Math.max(max, parseInt(height));
			}
			height = Math.min(max, 44) + 'px';
			for(var j = 0; j < imgs.length; j++) {
				$($(imgs[j]).children('dd.csc-textpic-caption')[0]).css('height', height);
			}
		}
	}
}


/***************
 * Alt Content *
 ***************/
 
function initAltContent() {
	var boxes = $('div.contentAlt').children('div.csc-default');
	
	var height = 0;
	for(var i = 0; i < boxes.length; i++) {
		height = Math.max(height, parseInt($(boxes[i]).css('height')));
	}
	
	for(var i = 0; i < boxes.length; i++) {
		var margin = height - parseInt($(boxes[i]).css('height'));
		$(boxes[i]).css('height', height + 'px');
		$(boxes[i]).find('div.more').css('margin-top', margin + 'px');
	}
}


/****************
 * Main Content *
 ****************/
 
function initBlog4posts() {
	var boxes = $('div.blog4posts').children('div.item');
	
	var height = 0;
	var margin = 0;
	for(var i = 0; i < boxes.length; i+=2) {
		height = Math.max(parseInt($(boxes[i]).css('height')), parseInt($(boxes[i+1]).css('height')));
		margin = height - parseInt($(boxes[i]).css('height'));
		$(boxes[i]).css('height', height + 'px');
		$(boxes[i]).find('div.more').css('margin-top', margin + 'px');
		margin = height - parseInt($(boxes[i+1]).css('height'));
		$(boxes[i+1]).css('height', height + 'px');
		$(boxes[i+1]).find('div.more').css('margin-top', margin + 'px');
	}
}


