function _bdb(t,b){
	if(t){global_themen_id=t;}else{global_themen_id=0;}
	if(b){global_bild_id=b;}else{global_bild_id=0;}
}

var path = global_rdir+'/lokale_seiten/lightroom2/';
var IElt7 = 0;

if (window.XMLHttpRequest){}else{IElt7 = 1;}
	  
(function($) {
	$.fn.myBox = function() {
		var link = $(this);

		//sizes
		var modalWidth = 700,imageMaxHeight = 350,showText = 1, showT1 = 'Text ausblenden',showT2 = 'Text einblenden',overlay = $("<div id='galShadow'></div>"), modspace = $("<div id='galModalspace'></div>");
		var maskHeight = $(document).height(),maskWidth = $(window).width(),modalLeft = (maskWidth - modalWidth) / 2;
		var iframe = '',fullLength = 0,galThumbnailsWidth = 0;

		if (IElt7) 
		{
			iframe = $("<iframe id='overlayifr' src='about:blank'></iframe>");
			overlay.append(iframe);
			iframe.css({'display':'block','width':maskWidth,'height':maskHeight,'opacity':0});
		}
		
		var modalWindowDefault = $("<div id='galModal'><div class='corneruttop'><img class='utl' src='"+path+"cornerut_topl.gif' alt='' /><img class='utr' src='"+path+"cornerut_topr.gif' alt='' /></div><div id='galInfo'></div></div>");
		var modalWindow = modalWindowDefault;
		var galThumbnails = $("<div id='galThumbnails'></div>");
		var galRCbottom   = $("<div class='cornerutbot'><img class='ubl' src='"+path+"cornerut_botl.gif' alt='' /><img class='ubr' src='"+path+"cornerut_botr.gif' alt='' /></div>");
		var thumbnails = [],obj = {},current = 0;
		
		$('body').append(modalWindow.append("<div id='galClose'>Fenster schlie&szlig;en</div><div id='galImage'><img /><div id='galImgText'><div class='galImgTextClose'>"+showT1+"</div><div id='galImgTextInner'></div></div></div>")).append(modspace);

		link.click(function(){
			if (global_bild_id != current) {current = 0;}		
		
			//set top-position of modalWindow
			modalWindow.css('top',$(window).scrollTop()+40);
			//add grey overlay
			$("body").append(overlay.click(function(){ 
				  modalHide(); 
			}));
			
			if (IElt7)
			{
				$("body").append(iframe.click(function(){ 
					  modalHide(); 
				}));
			}
			
			$(document).keydown(handleEscape);

			//catch data
			$.getJSON(path+'imageList.php?t_id='+global_themen_id+'&bild_id='+global_bild_id+'&'+Math.random(),{},function(data){
				obj = eval(data);
				for(var i in obj.images){
					if(obj.images[i]){
						thumbnails[i] = $('<div><span>&nbsp;</span></div>');
						galThumbnails.append(thumbnails[i]);
						thumbnails[i].css('background','#eee url('+obj.thumbPath+obj.images[i][4]+') no-repeat center center');
					}
				}
				overlay.css({'width':'100%','height':maskHeight,'opacity':0.8}).fadeIn();
				modspace.css('display','block');
				$('body').append(modalWindow.append(galThumbnails));
				$('body').append(modalWindow.append(galRCbottom));
				modalWindow.css({'left':modalLeft}).fadeIn(function(){
					changeImage(current);
				});
				modalWindow.find('#galClose').click(function(){modalHide();});
	
				handleArrows();
				handleBigArrows();
				listen();
				listenTextShow();
			});

			return false;
		});
		
		function modalHide() {
			$(document).unbind("keydown", handleEscape);
			overlay.fadeOut();
			
			modalWindow.fadeOut(function() {
				modalWindow = modalWindowDefault;
				modalWindow.children('#galImage').children('img').replaceWith('<img />');	
				if (IElt7) {iframe.css('display','none');}
			});
			modalWindow.css('display','none');
			modspace.css('display','none');
			galThumbnails = $("");
			obj = {};
			fullLength = 0;
			galThumbnailsWidth = 0;
		}

		function handleEscape(e){
		  if(e.keyCode == 27){
			  modalHide();
		  }
		}

		function listen() {
			jQuery.each(thumbnails,function(i,n){
				$(this).click(function(){
					changeImage(i);
					current = i;
				});
			});
		}

		function handleArrows(){
			galThumbnails.animate({scrollLeft: 0},1);
			galThumbnailsWidth = galThumbnails.width();
			fullLength = (thumbnails.length * 87 ); //76 is wide and margin of thumbnails
			if(!galThumbnailsWidth || fullLength <= galThumbnailsWidth) {return;}

			// erstmal zuruecksetzen
			modalWindow.children('#galLeftArrow').remove();
			modalWindow.children('#galRightArrow').remove();

			//add arrows;
			var leftArrow = $('<div id="galLeftArrow"></div>');
			var rightArrow = $('<div id="galRightArrow"></div>');
			modalWindow.append(leftArrow).append(rightArrow);

			leftArrow.click(function(){
				var sLeft = galThumbnails.scrollLeft();
				if(sLeft === 0){sLeft = fullLength + 40;}
				if(sLeft < galThumbnailsWidth) {
					w = 0;
				}else{
					w = sLeft - galThumbnailsWidth;
				}
				
				galThumbnails.animate({scrollLeft: w},500,'swing');
				
			});

			rightArrow.click(function(){
				var sRight = galThumbnails.scrollLeft() + galThumbnailsWidth;
				if(sRight >= fullLength){sRight = 0;}
				if(sRight + galThumbnailsWidth > fullLength) {
					w = fullLength - galThumbnailsWidth + 40;
				}else{
					w = sRight;
				} 
			
				galThumbnails.animate({scrollLeft: w},500,'swing');
			});
		}

		function handleBigArrows(){
			var leftBArrow = $('<div id="galLeftBigArrow"></div>');
			var rightBArrow = $('<div id="galRightBigArrow"></div>');

			modalWindow.children('#galImage').append(leftBArrow).append(rightBArrow);
			modalWindow.children('#galImage').mouseover(function() {
				leftBArrow.fadeIn();
				rightBArrow.fadeIn();
			}).mouseleave(function(){
				leftBArrow.fadeOut();
				rightBArrow.fadeOut();

			});
			
			leftBArrow.click(function(){
				if(current===0){current = thumbnails.length;}
				current--;
				changeImage(current);
			});

			rightBArrow.click(function(){
				if(current >= thumbnails.length - 1){current=-1;}
				current++;
				changeImage(current);
			});
		}

		function changeImage(i) {
			var img = new Image();
			global_bild_id = i;
			modalWindow.children('#galImage').children('img').fadeTo('1000',0.5);
			if (showText) {
				modalWindow.children('#galImage').children('#galImgText').fadeTo('1000',0.8);
			}else{
				$('.galImgTextClose').text(showT2).addClass('galImgTextCloseD');
			}
			$(img).load(function(){
				setTimeout(function() {
					var wd = img.width, hg = img.height;
					if (wd > modalWidth) { var fak = wd / modalWidth; img.width = modalWidth; img.height = hg / fak; }
											
					modalWindow.find('#galInfo').html((i+1)+'/'+thumbnails.length);
					modalWindow.children('#galImage').animate({height: img.height},500);
					modalWindow.children('#galImage').children('#galImgText').fadeOut(100);
					modalWindow.children('#galImage').children('img').fadeOut(200, function() {
						modalWindow.children('#galImage').children('img').replaceWith(img);
						modalWindow.children('#galImage').children('img').hide().fadeIn(500);
						if(obj.images[i][2].length > 0 || obj.images[i][3].length > 0) {
							modalWindow.find('#galImgTextInner').html('<h3>'+obj.images[i][2]+'</h3><p>'+obj.images[i][3]+'</p>');
							modalWindow.find('#galImgText').css('opacity',0.8).find('*').css('opacity',1);
							modalWindow.find('#galImgText').fadeIn('500');
						} 
						else 
						{
							modalWindow.find('#galImgTextInner').html('');
						}
						var leftA = modalWindow.find('#galLeftBigArrow');
						var rightA = modalWindow.find('#galRightBigArrow');
						leftA.css({top: (img.height - leftA.height() ) / 2 });
						rightA.css({top: (img.height - rightA.height() ) / 2 });
					});
				}, 200);
			}).attr({src: obj.path+obj.images[i][0]+obj.images[i][1]});
		}

		function listenTextShow() {
			$('.galImgTextClose').click(function() {
				if(showText==1) {
					modalWindow.find('#galImgTextInner').slideUp(200,function() {
						$('.galImgTextClose').text(showT2).addClass('galImgTextCloseD');
						showText=0;
					modalWindow.find('#galImgText').css('background-color', 'transparent');
					modalWindow.find('#galImgText').css('opacity',1.0).find('*').css('opacity',1);
					});
				} else {
					modalWindow.find('#galImgTextInner').slideDown(200,function() {
						$('.galImgTextClose').text(showT1).removeClass('galImgTextCloseD');
						showText=1;
					modalWindow.find('#galImgText').css('background-color', '#111');
					modalWindow.find('#galImgText').css('opacity',0.8).find('*').css('opacity',1);
				});
				}
			});
		}
	};
})(jQuery);

(function($){
	$.fn.myAccordion = function(){
		var accordion = $(this);

		var current = 0;
		var resizeFactor = 1.93;
		var texts = accordion.find('div.acc');
		var parts = [];
		var type = 0;
		var acounter = 0;
	
		//split texts and print only first part
		jQuery.each(texts,function(i,n) {
			//$(this).parent('.acc_container').prepend('<div class="slideDown"></div>');
			parts[i] = {texts: $(this).html().split("<!-- more -->")};
			
			if(parts[i].texts[1] === undefined) {parts[i].texts[1] = '';}
						
			$(this).html(parts[i].texts[0]+'<div class="moreText">'+parts[i].texts[1]+'</div>').show();
			$(this).hide().show();			
			$(this).find('a[rel="box"]').each(function(){$(this).myBox();});
			$(this).find('area[rel="box"]').each(function(){$(this).myBox();});
			$(this).children('.moreText').hide();
			
			acounter++;

		});
		
		accordion.find('.accdown').click(function(){$(this).parent('.acc_container').find('.slideDown').trigger('click');});
		
		accordion.find('.slideDown').click(function(){
			accordion.find('*').stop(false,true);
			
			if($(this).hasClass('slideUp')){
				//change Text
				$(this).removeClass('slideUp').parent().find('.moreText').slideUp(200,function(){$(this).prev('.hellip').show();});
			}else{
				var open=accordion.find('.slideUp');
				jQuery.each(open,function(){
					$(this).removeClass('slideUp').parent().find('.moreText').slideUp(200,function(){$(this).prev('.hellip').show();});
				});
				//change Text
				$(this).addClass('slideUp').parent().find('.moreText').slideDown(300).prev('.hellip').hide();
			}
		});
		
		$('.firstacc').find('.slideDown').addClass('slideUp').parent().find('.moreText').slideDown(300).prev('.hellip').hide();
	};
})(jQuery);

jQuery(document).ready(function(){
jQuery('head').append('<link rel="stylesheet" href="'+path+'gallery.css" type="text/css" /><!--[if IE]><link rel="stylesheet" href="'+path+'iestyle.css" type="text/css" media="screen" /><![endif]-->');
jQuery('a[rel="box"]').each(function(){$(this).myBox();});
jQuery('area[rel="box"]').each(function(){$(this).myBox();});
jQuery('div.accordion').each(function(){$(this).myAccordion();});
});

