$(document).ready(function() {
	
	
	var lbQuit = 'FALSE';

	// LIGHTBOX
	var $j = jQuery;
	$j.fn.extend({
		lbClose : function(speed) {
			$j("#lb_loader").remove();
			$j("#lightbox").animate({ opacity: 0 }, speed, function() {
				$(this).remove();
			});
			$j("#lb_close").remove();
			$j("#lb_image").remove();
			$j("#the_prev").remove();
			$j("#the_next").remove();
			$j("#prev_page").remove();
			$j("#next_page").remove();
			lbQuit = 'FALSE';
			$('.ngg-gallery-thumbnail .active').focus();
		}
	});
	$j.fn.extend({
		lightbox : function (image) {
			var thiz = this;
			lbQuit = 'TRUE';
			if($("a[href='"+image+"']").parent('div').attr('class') == 'ngg-gallery-thumbnail') {
				$('.ngg-gallery-thumbnail .active').removeClass('active').blur();
				$("a[href='"+image+"']").addClass('active').focus();
			}
			if(image != 'bald') {
				var title = $("a[href='"+image+"']").attr('title');
				
				if(!title || title == ' ' || title == '') {
					var arr = $("a[href='"+image+"']").attr('href').split('/');
					var title = arr[arr.length-1];
				} 
			}
			var prev = $("a[href='"+image+"']").parent('td').prev("td").children('a').attr('href');
			if(!prev){
				var prev = $("a[href='"+image+"']").parent('td').parent('tr').prev("tr").children('td').last().children('a').attr('href');
			}
			if(!prev){
				var prev = $("a[href='"+image+"']").closest('.ngg-gallery-thumbnail-box').prevAll('.ngg-gallery-thumbnail-box').first().find('a').attr('href');
			}
			
			var next = $("a[href='"+image+"']").parent('td').next("td").children('a').attr('href');
			if(!next){
				var next = $("a[href='"+image+"']").parent('td').parent('tr').next("tr").children('td').children('a').attr('href');
			}
			if(!next){
				var next = $("a[href='"+image+"']").closest('.ngg-gallery-thumbnail-box').nextAll('.ngg-gallery-thumbnail-box').first().find('a').attr('href');
			}
			if(!next && $('.ngg-navigation .next').attr('class') == 'next'){
				var nextpage = 'TRUE';
				var next = $('.ngg-navigation .next').attr('href');
			}
			
			$j("#header").before("<div id=\"lightbox\"></div><div id=\"lb_image\"></div>");	
			var time = new Date();
			var theTime = 500+(time.getTime());
			var img = new Image();
			$j("#header").before('<img id="lb_loader" src="http://www.uptoyou.de/wp-content/themes/uptoyou/images/loadinfo.gif" alt="One Moment, loading the Image" />');	
			
			if(image == 'bald') {
				var baldflash = '/wp-content/uploads/1a-autoservice/Bald_Pageflip.swf';
				var object = '<div id="bald_wrap">';
				object += '<object classid="CLSID:D27CDB6E-AE6D-11cf-96B8-444553540000" width="840" height="600" data="'+baldflash+'" type="application/x-shockwave-flash">';
				object += '<param name="movie" value="'+baldflash+'" />';
				object += '<param name="bg_color" value="#ffffff" />';
				object += '<param name="scale" value="exactfit">';
				object += '<param name="quality" value="high">';
				object += '<param name="menu" value="false">';
				object += '<embed src="'+baldflash+'" quality="high" scale="exactfit" menu="false"';
           		object += 'bgcolor="#ffffff" width="840" height="600" swLiveConnect="false"';
           		object += 'type="application/x-shockwave-flash"';
           		object += 'pluginspage="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">';
    			object += '</embed>';
				object += '</object></div>';
				
				$j('#lb_image').html(object);
				$j("#lb_image").prepend('<a id="lb_close" href="SCHLIESSEN" title="Schlie&szlig;en" style="margin-left: 424px; margin-top: -328px;"></a>');
					
				$j("#lightbox").click(function(){
					$j(this).lbClose(500);
				});
				$j("#lb_close").click(function(event){
						event.preventDefault();
						$j(this).lbClose(500);
					});
			} else {
				$j(img).load(function () {
					$j("#lb_image").append(this);
					$j(this).hide();
					var winheight = $j(window).height();
					var winwidth = $j(window).width();				
					var iheight = $j(this).height();
					var iwidth = $j(this).width();	
					if(iheight > winheight-80){
						var factor = (winheight-108)/iheight;
						var iheight = winheight-108;
						var iwidth = iwidth*factor;
					}
					if(iwidth > winwidth-80){
						var factor = (winwidth-80)/iwidth;
						var iwidth = winwidth-80;
						var iheight = iheight*factor;
					}
					var yoff = (iheight/2);
					var xoff = (iwidth/2);
					var bighref = image;
					var bigtitle = 'Bild in voller gr&ouml;&szlig;e anzeigen';
					if($j(thiz).closest('a').find('.lbbig').hasClass('lbbig')) {
						var l = new String($j(thiz).closest('a').find('.lbbig').html()).split('|');
						bighref = l[0];
						if(l[1]) {
							bigtitle = l[1];
						}
					}
					if(prev) {
						$j("#lb_image").children("img").before('<a id="the_prev" title="vorheriges Bild" href="'+prev+'" style="margin-left: -'+(xoff)+'px; margin-top: '+(yoff-4)+'px;"></a>');
					}
					if(next) {
						$j("#lb_image").children("img").before('<a id="the_next" title="n&auml;chstes Bild" href="'+next+'" style="margin-left: '+(xoff-24)+'px; margin-top: '+(yoff-4)+'px;"></a>');
						$j("#lb_image").children("img").before('<a id="the_big" title="'+bigtitle+'" href="'+bighref+'" target="_blank" style="margin-left: '+(xoff-53)+'px; margin-top: '+(yoff-4)+'px;"></a>');
					} else {
						$j("#lb_image").children("img").before('<a id="the_big" title="'+bigtitle+'" href="'+bighref+'" target="_blank" style="margin-left: '+(xoff-24)+'px; margin-top: '+(yoff-4)+'px;"></a>');
					}
					if(title) {
						$j("#lb_image").children("img").before('<span id="the_title" style="width: '+iwidth+'px; margin-left: -'+(xoff-35)+'px; margin-top: '+(yoff+1)+'px;">'+title+'</span>');
					}
					$j("#lb_image").children("img").before('<a id="lb_close" href="SCHLIESSEN" title="Schlie&szlig;en" style="margin-left: '+(xoff+4)+'px; margin-top: -'+(yoff+42)+'px;"></a>');
						$j(this).attr({ 
						height: iheight,
						width: iwidth
					});
					var cssObj = {
						'margin-top' : -((iheight/2)+34)
						//'margin-left' : -((iwidth/2)+10)
					}
					$j(this).css(cssObj);
					var time = new Date();
					var newTime = (time.getTime());
					if(newTime > theTime){
						$j("#lb_loader").remove();
						$j(this).fadeIn(1000);
					} else {
						$j("#lb_loader").remove();
						$j(this).fadeIn(100);
					}
					$j("#prev_page").click(function(event){
						$j(this).lbClose(0);
					});
					$j("#next_page").click(function(event){
						$j(this).lbClose(0);
					});
					$j("#the_next").click(function(event){
						if(nextpage == 'TRUE') {
							$j(this).lbClose(0);
						} else {
							event.preventDefault();	
							$j(this).lbClose(0);
							var image = $(this).attr('href');
							$j(this).lightbox(image);
						}
					});
					$j("#the_prev").click(function(event){
						event.preventDefault();
						$j(this).lbClose(0);
						var image = $(this).attr('href');
						$j(this).lightbox(image);
					});
	
					$(window).keyup(function(event) {
						if($('#lb_image').attr('id') == 'lb_image') {
							if (event.keyCode == '37') {
								if($('#the_prev').attr('id') == 'the_prev') {
									var image = $('#the_prev').attr('href');
									$j(this).lbClose(0);
									$j(this).lightbox(image);
								}
							} else if(event.keyCode == '39') {
								if($('#the_next').attr('id') == 'the_next') {
									if(nextpage == 'TRUE') {
										$j(this).lbClose(0);
									} else {
										var image = $('#the_next').attr('href');
										$j(this).lbClose(0);
										$j(this).lightbox(image);
									}
								}
							} else if(event.keyCode == '27') {
								$j(this).lbClose(500);
							}
						}
					});
					$j("#lb_close").click(function(event){
						event.preventDefault();
						$j(this).lbClose(500);
					});
				}).attr('src', image);
				$j("#lightbox").click(function(){
					$j(this).lbClose(500);
				});
			}
		}
	});

	$('.ngg-gallery-thumbnail a').focus(function() {
		$('.active').removeClass('active').blur();
		$(this).addClass('active');
	});
	$('.ngg-gallery-thumbnail a').focusout(function() {
		if(lbQuit == 'FALSE') {
			$(this).removeClass('active').blur();
		}
	});
	
	if ($.browser.opera) {
		$(window).keypress(function(event) {
			escapeAndArrows(event);
		});
	} else {
		$(window).keydown(function(event) {
			escapeAndArrows(event);
		});
	}


	
	function escapeAndArrows(event) {
		if (event.keyCode == '13') {
			if($('#lb_image').attr('id') != 'lb_image' && $('.ngg-gallery-thumbnail .active').hasClass('active')) {
				event.preventDefault();
				$j(this).lightbox($('.ngg-gallery-thumbnail .active').attr('href'));
				return false;
			} else if ($('#lb_image').attr('id') == 'lb_image') {
				event.preventDefault();
				return false;
			}
		} else if(event.keyCode == '38') {
			if(thumbHasFocus()) {
				event.preventDefault();
				var nrident = $('.active .nrident').html();
				var next = $('.active').closest('.'+nrident).prevAll('.'+nrident).first().find('a');
				if(next) {
					$('.active').removeClass('active').blur();
					next.addClass('active').focus();
				}
				return false;
			}
		} else if(event.keyCode == '39') {
			if(thumbHasFocus()) {
				event.preventDefault();
				var next = $('.active').closest('.ngg-gallery-thumbnail-box').nextAll('.ngg-gallery-thumbnail-box').first().find('a');
				if(next) {
					$('.active').removeClass('active').blur();
					next.addClass('active').focus();
				}
				return false;
			}
		} else if(event.keyCode == '40') {
			if(thumbHasFocus()) {
				event.preventDefault();
				var nrident = $('.active .nrident').html();
				var next = $('.active').closest('.'+nrident).nextAll('.'+nrident).first().find('a');
				if(next) {
					$('.active').removeClass('active').blur();
					next.addClass('active').focus();
				}
				return false;
			}			
		} else if(event.keyCode == '37') {
			if(thumbHasFocus()) {
				event.preventDefault();
				var next = $('.active').closest('.ngg-gallery-thumbnail-box').prevAll('.ngg-gallery-thumbnail-box').first().find('a');
				if(next) {
					$('.active').removeClass('active').blur();
					next.addClass('active').focus();
				}				
			}
			return false;
		}	
	}
	
	function thumbHasFocus() {
		if($('#lb_image').attr('id') != 'lb_image') {
			if($('.ngg-gallery-thumbnail .active').hasClass('active')) {
				return true;
			} else {
				$('.ngg-gallery-thumbnail a').first().addClass('active').focus();
				return false;
			}
		} else {
			return false
		}
	}
	
	
	$('.thumb_overlay').click(function(event) {
		if($(this).parent('span').parent('a').attr('href'))  {
			var link = $(this).parent('span').parent('a').attr('href');
			var length = link.length;
			var end = link.substring((length-3), length);
			if(end == 'png' || end == 'jpg' || end == 'gif' || end == 'JPG' || end == 'GIF' || end == 'PNG') {
				event.preventDefault();
				$j(this).lightbox(link);
			}	
		}	
	});
	$('img').each(function(){
		if($(this).parent('a').attr('href') && $('body').attr('id') != 'post817')  {
			var link = $(this).parent('a').attr('href');
			var length = link.length;
			var end = link.substring((length-3), length);
			if(end == 'png' || end == 'jpg' || end == 'gif' || end == 'JPG' || end == 'GIF' || end == 'PNG') {
				lbhint($(this));
				$(this).click(function(event) {
					event.preventDefault();
					$j(this).lightbox(link);
				});
			} else if(!$(this).parent('a').hasClass('nohint')) {
				$(this).parent('a').attr('target', '_blank');
				nshint($(this), $(this).parent('a').attr('href'));
			}
		}
	});
	$('.bald_lightbox').click(function(event) {
		event.preventDefault();
		$j(this).lightbox('bald');
	});

	
	if($('.wrap_2').height() < $(window).height()-165 && $('html').height() <= $(window).height()) {
		$('.wrap_2').height($(window).height()-165);
		$('.wrap_2 .footer').css({'bottom':'0px', 'position':'absolute', 'left':'50%', 'margin-left':'-510px'});
	}

	// CLICKSHOW
	var clickshow_i = new Array();
	$('.clickshow').each(function(){
		clickshow_i[$(this).attr('id')] = 1;
		//$(this).after('<p><small style="color: #aaa;">Klicken sie auf das Bild f&uuml;r weitere Ansichten</small></p>');
		$(this).attr('title', 'Für weitere Ansichten bitte klicken.');
		$(this).click(function(event) {
			var x = clickshow_i[$(this).attr('id')]*$(this).html();
			if($(this).hasClass('animate')) {
				$(this).stop().animate({
				    backgroundPosition: -x
				}, 500);
			} else {
				$(this).css({'background-position' : '-'+x+'px'});
			}
			clickshow_i[$(this).attr('id')]++;
		});
	});
	
	// Hints
	function lbhint(item) {
		item.attr('title', 'Für größere Ansicht bitte klicken.');
	}
	function nshint(item, link) {
		item.attr('title', 'Klicken um die Seite "'+link.substring(0, 6)+link.substring(7).split('/', 1)+'/" zu öffnen.');
	}
	
});
