var istInfo = 0;
var welcheGalerie = '';
var welchesBild = '';
var welchesBildIndex = 0;
var wievielBild = 0;
var welchesDiv = '';
var innereHoeheStart = 600;
var innereBreiteStart = 940;

$(document).ready(function($){$(function(){


	$(".vorschaubild2 a").click(function(){
		welcheGalerie = $(this).parent('div').attr('id');
		welchesBild = $(this).find('img').attr('src');
		welchesBildIndex = $('div#'+welcheGalerie+' a').index(this);
		wievielBild = $('div#'+welcheGalerie+' a').length;
	});
	
  $(".vorschaubild2 a").fancybox({
 				'centerOnScroll'	: true,
  				'content'	: '<div id="komplettRahmen"><div class="vorschau"><div><a href="javascript:minus()" class="minus"><img src="/jss/fancybox/fancy_nav_left.png" /></a><div id="galerieRahmen1"></div><a href="javascript:plus()" class="plus"><img src="/jss/fancybox/fancy_nav_right.png" /></a></div></div><div class="vollbild"><a href="javascript:minus()" class="minus"><img src="/jss/fancybox/fancy_nav_left.png" /></a><div id="infoRahmen"><p><span class="derText"></span><a class="derLink"> ...&nbsp;zum&nbsp;Artikel</a></p></div><div id="vollbildRahmen1"><img src="/gifs/leer.gif" /></div><a href="javascript:plus()" class="plus"><img src="/jss/fancybox/fancy_nav_right.png" /></a></div></div>',  				
  				'onComplete'	: function(){
  											galerieKopieren();
  											$('div.vollbild').mouseenter(function(){
												if (istInfo == 1){
  													$('div#infoRahmen').slideDown();
  												}
  												});
  											$('div.vollbild').mouseleave(function(){
												if (istInfo == 1){
  													$('div#infoRahmen').slideUp();
  												}
  												});
  											$('div#infoRahmen').hide();
  										},
  				'padding'	: 10,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});		
			
});});


function minus() {
	welchesBildIndex -= 1;
	void umschalten();
}

function plus() {
	welchesBildIndex += 1;
	void umschalten();
}

function bild(welches) {
	welchesBildIndex = welches;
	void umschalten()
}




function galerieKopieren(){
	welchesDiv = 'div#'+welcheGalerie;
	var galerie = $(welchesDiv).html();
	galerie = galerie.replace(/>\s*</g,'><');
	$(galerie).prependTo('#galerieRahmen1');
	var breite = 0;
		for (i=0;i<=wievielBild-1;i++){
		$('div#galerieRahmen1 a').eq(i).attr('href','javascript:bild('+i+')');
		
		breite += $('div#galerieRahmen1 a').eq(i).width();
	}
	breite += (wievielBild * 10);
	var maxBreite = $('#komplettRahmen').innerWidth();
	if (breite < 510) {
		breite = 510;
	}
	if (breite > maxBreite){
		breite = maxBreite;
	}
	$('div#komplettRahmen').find('div.vorschau>div').css('width',''+breite+'px');	
	umschalten();
}


function infoEin(){
	if (istInfo == 1){
		$('div#infoRahmen').slideDown();
	}
}

function infoAus(){
	if (istInfo == 1){
		$('div#infoRahmen').slideUp();
	}
}


function umschalten(){
	var galerieHoehe = $('div#komplettRahmen div.vorschau').height();
	var innereBreite = $('#vollbildRahmen1').width();
	var innereHoehe = innereHoeheStart - galerieHoehe + 72;	
	var speicher = new Image();
	speicher.src = $('div#galerieRahmen1 a').eq(welchesBildIndex).find('img').attr('src');
	$('#vollbildRahmen1').find('img').attr('src',speicher.src);
	var bildAlt = new Array;
	bildAlt = $('div#galerieRahmen1 a').eq(welchesBildIndex).find('img').attr('alt').split('##');
	var bildHoehe = speicher.height;
	var bildBreite = speicher.width;
	var bildRatio = bildBreite/bildHoehe;
		
	if (bildBreite >= innereBreite) {
		bildBreite = innereBreite;
		bildHoehe = Math.round(bildBreite / bildRatio);	
	}
	
	if (bildHoehe >= innereHoehe){
		bildHoehe = innereHoehe;
		bildBreite = Math.round(bildHoehe * bildRatio);
	}
	var polster = ((innereHoehe - bildHoehe)/2)-5;
	if (polster < 5){
		polster = 0;
	}
	var polsterInfo = (polster + 10)+'px';
	polster += 'px';	
	bildHoehe += 'px';	
	$('div#vollbildRahmen1').find('img').css('margin-top',polster);
	$('div#vollbildRahmen1').css('height',bildHoehe);
	$('div#vollbildRahmen1').find('img').css('height',bildHoehe);
	var polsterLinks = (Math.round((innereBreiteStart - bildBreite)/2))+'px';
	bildBreite += 'px';
	$('div#infoRahmen').css('width',bildBreite);
	$('div#infoRahmen').css('margin-left',polsterLinks);
	$('div#infoRahmen').css('top',polsterInfo);
	
	if ( (bildAlt[0] != undefined) && (bildAlt[0] != '') ){
		istInfo = 1;
		$('div#infoRahmen p span.derText').html(bildAlt[0]);
		$('div#infoRahmen p span.derText').show();
	}
	else {
		istInfo = 0;
		$('div#infoRahmen p span.derText').hide();
	}
	
	if (bildAlt[1] != undefined){
		var artikelUrl = '/id/' + bildAlt[1] + '/';
		$('div#infoRahmen p a.derLink').attr('href',artikelUrl);
		$('div#infoRahmen p a.derLink').show();
	}
	else {
		$('div#infoRahmen p a.derLink').hide();
	}
	$('div#galerieRahmen1 a').find('img').removeClass('aktiv');
	$('div#galerieRahmen1 a').eq(welchesBildIndex).find('img').addClass('aktiv');
	if (welchesBildIndex == 0) {
		$('a.minus').css('display','none');
		$('a.plus').css('display','inline');
	}
	else {
		$('a.minus').css('display','inline');		
	}
	if (welchesBildIndex + 1 == wievielBild) {
		$('a.plus').css('display','none');
		$('a.minus').css('display','inline');
	}
	else {
		$('a.plus').css('display','inline');		
	}
	if (wievielBild == 1){
		$('a.plus').css('display','none');
		$('a.minus').css('display','none');	
	}
	
	
	/*
	alert('fertig1');*//*
	fancybox.resize();*/
	return;
	
}
