
$(document).ready(init);

function init(){
	$('#subNavDemoReel').hide();
	$('#subNavFilme').hide();
	
	$('#navDemoReel').click(function(){
		$('#subNavDemoReel').slideToggle();
	});
	
	$('#navFilme').click(function(){
		$('#subNavFilme').slideToggle();
	});
	
	defeatSpam3000();
	externalLinks();
	
	if($('body#gallery').length>0){
		initGallery();
	}
	
	$('.showImg').click(function(e){e.preventDefault(); parent.callTB(this)});
	
}


function defeatSpam3000(){
	var emailText    = $('.nospam').text();
	var emailAddress = emailText.replace(/ \[at\] /,'@');
	$('.nospam').replaceWith($.create('a',{'class':'nospam','href':'mailto:'+emailAddress},emailAddress));
}

function externalLinks(){
	$('a.extern').attr('target','_blank');
}

function resizeDemoreel(){
	$('#TB_iframeContent').animate({width: '550px', height: '400px'});
	$('#TB_window').animate({width: '550px', height: '430px',marginLeft: '-290px'});
}

function callTB(link){
	var path = link.href;
	
	var imgOverlay = $.create('div',{'id':'imgOverlay'}).appendTo($('body')).hide();
	var imgBox  = $.create('div',{'id':'imgBox'}).appendTo($('body')).hide();
	var imgDiv  = $.create('div',{'id':'imgView','title':'schliessen'}).appendTo(imgBox);
	var img     = $.create('img',{'src':path}).appendTo(imgDiv);
	
	imgBox.css({
			position: 'absolute',
			zIndex: '1338',
			left: '0',
			top: '0',
			width: $('body').width() + 'px',
			height: $('body').height() + 'px',
			textAlign: 'center',
			paddingTop: '66px'
	});
	
	imgOverlay.css({
			position: 'absolute',
			zIndex: '1337',
			backgroundColor: 'white',
			opacity: '0.7',
			left: '0',
			top: '0',
			width: $('body').width() + 'px',
			height: $('body').height() + 'px',
			textAlign: 'center',
			paddingTop: '66px'
	});
	
	img.css({
			border: 'solid 5px black'
	});
	
	imgOverlay.fadeIn();
	imgOverlay.click(function(){
			$(this).fadeOut(null,function(){
					$('#imgBox').remove();
					$(this).remove();
			});
	});
	imgBox.fadeIn();
	imgBox.click(function(){
			$(this).fadeOut(null,function(){
					$('#imgOverlay').remove();
					$(this).remove();
			});
	});
}

function moveGallery(){
	$('#TB_window').css({'top':'40px'});
}

function initGallery(){
	$('.gallerythumb:first').addClass('aktiv');
	var imgSrc = $('.gallerythumb:first').attr('src').replace(/thumb_/,'');
	$('#anzeige').attr('src',imgSrc);
	
	$('#bildtext').empty();
	var imgText = $('.gallerythumb:first').attr('alt');
	if(imgText){
		$('#bildtext').html(imgText);
	}
	
	$('.gallerythumb').click(function(){galleryShow(this)});
	$('#prev').click(function(){galleryPrev()});
	$('#next').click(function(){galleryNext()});

}

function galleryShow(img){
	$('.gallerythumb').removeClass('aktiv');
	$(img).addClass('aktiv');
	var imgSrc = $(img).attr('src').replace(/thumb_/,'');
	$('#anzeige').attr('src',imgSrc);
	
	$('#bildtext').empty();
	var imgText = $(img).attr('alt');
	if(imgText){
		$('#bildtext').html(imgText);
	}
	
}

function galleryNext(){
	if($('.aktiv').next().length < 1){
		var nextImg = $('.gallerythumb:first');
	}else{
		var nextImg = $('.aktiv').next();
	}
	galleryShow(nextImg);
}

function galleryPrev(){
	if($('.aktiv').prev().length < 1){
		var prevImg = $('.gallerythumb:last');
	}else{
		var prevImg = $('.aktiv').prev();
	}
	galleryShow(prevImg);
}

