var timout_is_on = 0;
jQuery(document).ready(function($){
	//oeffnet die grossansicht
	$('a.mgwFotoSlideLink').click(function(e){
		timout_is_on = 0;
		clearTimeout(t);
		e.preventDefault();
		var viewportHeight = $(window).height();
		var viewportWidth = $(document).width();
		var documentHeight = $(document).height();
		$('body').prepend('<div id="mgwFotoSlideLayer" style="height:'+documentHeight+'px; width:'+viewportWidth+'px">');
		openPic($('a.mgwFotoSlideLink').index(this));
		$('#mgwFotoSlideLayer').click(function(){$(this).remove();timout_is_on = 1;t=setTimeout("slide()",3000);});
	});
	
	
	for(var i=0;i<3;i++){
		$('a.mgwFotoSlideLink img').eq(i).addClass('vis');
	 }
	 timout_is_on = 1;
	var t=setTimeout("slide()",3000);
	
	
	//GalerieSeite
	$('.mgwFotoSlideGaleryLink').click(function(e){
		e.preventDefault();
		switchGaleryPic.call(this);
	});
	
	
	$('.moreGalleries h4').click(function(){
		$('.moreGalleries ul').toggle('slow');
	});
	
	
});

function slide(){
	if(timout_is_on){
	//min-width muss gesetzt werden, da chrome sonst die bilder rechts (bzw. links) an den rand setzt
		$('a.mgwFotoSlideLink img').eq(3).show(2000,function(w){
			var first = $('a.mgwFotoSlideLink').eq(2);
			first.css('min-width','');
			});
		$('a.mgwFotoSlideLink img').eq(0).hide(2000,function(w){
			var first = $('a.mgwFotoSlideLink').eq(0);
			first.css('min-width','60px');
			$('#mgwFotoSlide').append(first);
			 });
		$('a.mgwFotoSlideLink img').eq(1).animate({'max-height':'60px','max-width':'60px', 'opacity':0.7 },2000);
		$('a.mgwFotoSlideLink img').eq(2).animate({'max-height':'100px','max-width':'100px','opacity':1 },2000);
		t=setTimeout('slide()',5000);
	}

 }

function openPic(i){

	var viewportHeight = $(window).height();
	var j = i + 1;
	var k = i - 1;
	if(j == $('a.mgwFotoSlideLink').length)j = 0;
	if(Math.abs(k) > $('a.mgwFotoSlideLink').length)k = $('a.mgwFotoSlideLink').length - 1;
	$('body').css('cursor','wait');
		var source = new Array();
		source[0]= $('a.mgwFotoSlideLink').eq(i).attr('href');
		source[1]= $('a.mgwFotoSlideLink').eq(j).attr('href');
		source[2]= $('a.mgwFotoSlideLink').eq(k).attr('href');
		
		var imgMargins = 20;
		var img = $('<img class="mgwFotoSlideImg" src="'+source[0]+'" />');
		var next = $('<a id="mgwFotoSlideNext" href="'+source[1]+'">&gt;</a>');
		var prev = $('<a id="mgwFotoSlidePrev" href="'+source[2]+'">&lt;</a>');
		var inner = $('<div id="mgwFotoSlideImgwrap"  style="height:100%; line-height:'+(viewportHeight * 0.8)+'px">').append(prev).append(img).append(next);
		
		var caption = $('<div class="mgwFotoSlideImgCaption" style="background:red">'+$('.mgwFotoSlideCaption').eq(i).text()+'</div>');
		var block = $('<div id="mgwFotoSlideBlock" style="height:'+(viewportHeight * 0.8)+'px">').click(function(e){e.stopPropagation();}).append(inner);

		img.load(function(){
			$('body').css('cursor','default'); 
			$('#mgwFotoSlideLayer').html(block);//.append(caption);
		});

		
		next.click(function(e){
			e.preventDefault();
			openPic(k);
		});
		prev.click(function(e){
			e.preventDefault();
			openPic(j);
		});
		
		$(window).resize(function() {
			var viewportHeight = $(window).height();
			block.height((viewportHeight * 0.8)+'px');
			$('#mgwFotoSlideImgwrap').css('line-height',(viewportHeight * 0.8)+'px');
		});
		
 }
 
 
 function switchGaleryPic(){
 	var index = $('.mgwFotoSlideGaleryLink').index(this);
 	var src = $(this).attr('href');
 	var img = $('<img src="'+src+'" />');
 	var caption = $('.mgwFotoSlideGaleryMenuCaption').eq(index).html();
 	
 	img.load(function(){
 		$('.mgwFotoSlideGaleryImg').html(img);
 		$('.mgwFotoSlideGaleryCaption').html(caption);
 	});
 
 }


(function($) { /* some code that uses $ */ })(jQuery);

