var thumbs=new Array();
var SLIDESHOWDELAY=5000;
var slideShow;
var width;
var linkWidth;
var autoScroll=false;
var dragging=false;
var stopAutoScrollTimeout;
var sliderOffsetX;
var sliderPositionX;
var infoTextClosing=false;
var maxSlide;
var currentSlide;

$(document).ready(function() {
	resize();resize();
	$(window).resize(function() {
		resize();
	});
	linkWidth=$('#navi a').width();
	$(window).scroll(function(e) {
		scrolled();
	});
	$('#navi a').click(function(e) {
		e.preventDefault();
		if($('#popup').length!=0) {
//			closePopup();
//			setTimeout("scrollTo("+$(this).attr('id')+");",1000);
			$('#popup').remove();
		} 
			scrollTo($(this).attr('id'));
	});
	$('#slider').mousedown(function(e) {
		e.preventDefault();
//		debug();debug($('html,body').scrollLeft());
		sliderOffsetX=e.pageX-(window.pageXOffset?window.pageXOffset:$('html,body').scrollLeft());
		sliderPositionX=$('#slider').position().left;
		autoScroll=true;
		dragging=true;
	});
	$(document).mouseup(function(e) {
		if(dragging) {
			dragging=false;
			autoScroll=false;
			var scrollLeft=$(window).scrollLeft();
			scrollTo(Math.round(scrollLeft/width),true);
		}
	});
	$(document).mousemove(function(e) {
//		debug();
//		for(var i in e) {
//			debug(i+':'+e[i]);
//		}
		if(dragging) {
			var x=e.pageX;
			var scrollLeft=window.pageXOffset?window.pageXOffset:$('html,body').scrollLeft();
			x=sliderPositionX+x-sliderOffsetX-scrollLeft;
//			debug();
//			debug('sliderOffsetX:'+sliderOffsetX);
//			debug('e.layerX:'+e.pageX);
//			debug('scrollLeft:'+scrollLeft);
			x=x<0?0:x>linkWidth*3?linkWidth*3:x;
//			debug(x);
//			sliderPositionX=x;
			$('#slider').css('left',x+'px');
//			debug(sliderPositionX-sliderOffsetX+x);
//			$('html,body').css('scrollLeft',(x>0?Math.round(x/linkWidth*width):0)+'px');
			$('html,body').scrollLeft(x>0?Math.round(x/linkWidth*width):0);
//			debug(Math.round((x-sliderOffsetX-$('html,body').scrollLeft())/linkWidth*width));
		}
//		$('#slider').css('top',e.pageY-sliderOffsetY+'px');
	});
	$('.thumb').mouseenter(function() {
		$(this).attr('src','img/referenzen/'+$(this).attr('id')+'/tn.jpg');
		var text=$(this).attr('alt');
		var n=$('#infotext').queue('fx').length;
		if(n>0) {
			$('#infotext').stop(true);
			$('#infotext').animate({
				top:-20
			},200,'linear',function(){		
				$('#infotext').html(text);
				$('#infotext').animate({
					top:0
				},200,'swing');
			});
		} else {
			$('#infotext').html($(this).attr('alt'));
			$('#infotext').animate({
				top:0
			},200,'swing');
		}
	});
	$('.thumb').mouseleave(function() {
		$(this).attr('src','img/referenzen/'+$(this).attr('id')+'/tn_sw.jpg');
		$('#infotext').stop();
		$('#infotext').animate({
			top:-20
		},500,'linear',function(){			
			$('#infotext').html('');
		});
	});
	$('.thumb').click(function() {
		var pos=$(this).position();
		popup($(this),$(this).parent(),pos.left,pos.top,$(this).width(),$(this).height());
	});
	
	$('#footer a.footer_right').click(function(e) {
		e.preventDefault();
		scrollTo(3);
	});
	$('#logo').click(function() {
		scrollTo(0);
	});
	$('.thumb').each(function() {
		thumbs.push($(this));
	});
});

function scrollTo(id,fast) {
	clearTimeout(stopAutoScrollTimeout);
	var scrollTo=width*parseInt(id);
	autoScroll=true;
	var speed=fast?200:500;
	stopAutoScrollTimeout=setTimeout("autoScroll=false;",1000);
	$('html,body').animate({'scrollLeft':scrollTo},speed,'swing');
	$('#slider').stop();
	$('#slider').animate({left:(scrollTo/width*linkWidth)+'px'},speed,'swing');
}

function resize() {
	width=$(window).width();
	var height=$(window).height()/2-265;
	height=height<15?15:height;
	$('#header').css('width',width+'px');
	$('#footer').css('width',width+'px');
	$('#grad_top').css('width',width+'px');
	$('#grad_bottom').css('width',width+'px');
	$('#grad_top').css('top',(height-130)+'px');
	$('#grad_bottom').css('top',(height+550)+'px');
	$('#header').css('top',height+'px');
	$('#footer').css('top',(height+520)+'px');
	$('#container').css('margin-top',(height+100)+'px');
	$('#container').css('width',(width*4)+'px');
	$('.page').css('width',width+'px');
}

function popup(element,eParent,eLeft,eTop,eWidth,eHeight) {
	if($('#popup').length!=0) {
		$('#popup').remove();
	}
	eParent.append('<div id="popup" style="position:absolute;width:'+(eWidth)+'px;height:'+(eHeight)+'px;top:'+eTop+'px;left:'+eLeft+'px;"></div>');
	$('#popup').html('<input type="hidden" name="id" value="'+element.attr('id')+'" /><div id="popup_content"></div>');
	$('#popup_content').html('<img src="img/pfeil_links.png" id="pfeil_links" /><img src="img/pfeil_rechts.png" id="pfeil_rechts" />'+
			'<div id="text" class="column float_left"></div><div id="pics" class="float_right"></div>');
	$('#pfeil_rechts').click(function() {
		var i=currentThumb();
		if(++i>=thumbs.length) i=0;
		$('#popup_content').animate({
			opacity:0
		},150,'swing',function() {
			$('input[name=id]').val(thumbs[i].attr('id'));
			loadReference(thumbs[i]);				
		});
	});
	$('#pfeil_links').click(function() {
		var i=currentThumb();
		if(--i<0) i=thumbs.length-1;
		$('#popup_content').animate({
			opacity:0
		},150,'swing',function() {
			$('input[name=id]').val(thumbs[i].attr('id'));
			loadReference(thumbs[i]);				
		});
	});
	$('#popup').css('opacity',0);
	$('#popup_content').css('opacity',0);
	$('#popup').css('background', '#ffffff');
	$('#popup').animate({
		opacity:1
	},250,'swing',function() {
		$('#popup').animate({
			top:-23,
			left:0,
			width:800,
			height:450
		},500,'swing',function() {
			loadReference(element);
		});
	});
}

function currentThumb() {
	for(var i=0;i<thumbs.length;i++) {
		if(thumbs[i].attr('id')==$('input[name=id]').val()) return i;
	}
}

function loadReference(element) {
	$.ajax({
		url:'pages/referenzen/'+element.attr('id')+'.html',
		success:function(data) {
			$.ajax({
				url:'index.php?action=count_images&id='+element.attr('id'),
				success:function(max) {
					setupGallery(parseInt(max));
				}
			});
			$('#popup_content #text').html('<a href="javascript:void(0)" id="close">&lt;&lt; zur&uuml;ck zur &Uuml;bersicht</a>'+data);
			$('#close').click(function() {
				closePopup();
			});
			$('#pfeil_links,#pfeil_rechts').fadeIn(500);
			$('#popup_content').animate({
				opacity:1
			},500,'swing');
		}
	});
}

function setupGallery(max) {
	$('#pics').empty();clearInterval(slideShow);
	maxSlide=max;
	currentSlide=1;
	$('#pics').html('<img id="rahmen" src="img/ref_rahmen.png" /><img id="btn_l" src="img/ref_btn_l.png" /><img id="btn_r" src="img/ref_btn_r.png" />');
	for(var i=1;i<=max;i++) {
		$('#pics').append('<img id="slide'+i+'" style="left:'+((i-1)*395)+'px;" class="slide" src="img/referenzen/'+$('input[name=id]').val()+'/'+i+'.jpg'+'" />');
	}
	$('#btn_l').click(function() {
		clearInterval(slideShow);
		moveLeft();
		slideShow=setInterval('moveLeft()',SLIDESHOWDELAY);
	});
	$('#btn_r').click(function() {
		clearInterval(slideShow);
		moveRight();
		slideShow=setInterval('moveRight()',SLIDESHOWDELAY);
	});
	slideShow=setInterval('moveRight()',SLIDESHOWDELAY);
}

function moveLeft() {
	var tmpCurrentSlide=currentSlide;
	var targetSlide=currentSlide-1;
	targetSlide=targetSlide<1?maxSlide:targetSlide;
	currentSlide=targetSlide;		
	$('#slide'+targetSlide).css('left','-395px');
	$('#slide'+targetSlide).animate({
		left:0
	},200,'swing');
	$('#slide'+tmpCurrentSlide).animate({
		left:395
	},200,'swing');
}

function moveRight() {
	var tmpCurrentSlide=currentSlide;
	var targetSlide=currentSlide+1;
	targetSlide=targetSlide>maxSlide?1:targetSlide;
	currentSlide=targetSlide;		
	$('#slide'+targetSlide).css('left','395px');
	$('#slide'+targetSlide).animate({
		left:0
	},200,'swing');
	$('#slide'+tmpCurrentSlide).animate({
		left:-395
	},200,'swing');
}

function closePopup() {
	if($('#popup input[name="id"]').length!=0) {
		var pos=$('#'+$('#popup input[name="id"]').val()).position();
		var eWidth=$('#'+$('#popup input[name="id"]').val()).width();
		var eHeight=$('#'+$('#popup input[name="id"]').val()).height();
		$('#popup_content').animate({
			opacity:0
		},250,'swing',function() {
			$('#popup_content').remove();
			$('#popup').animate({
				top:pos.top,
				left:pos.left,
				width:eWidth,
				height:eHeight
			},250,'swing',function() {
				$('#popup').animate({
					opacity:0
				},500,'swing',function() {
					$('#popup').remove();
				});
			});
		});
	}
}

function debug(text) {
	if($('#debug').length==0) {
		$('body').append('<div id="debug" style="font-size:16px;background:#000;color:#ff0000;font-weight:bold;position:fixed;top:0px;left:0;z-index:100000;"></div>');
	}
	if(text==undefined) {
		$('#debug').html('');		
	} else {
		$('#debug').html($('#debug').html()+text+'<br />');
	}
}

function scrolled() {
	if(autoScroll) {
//		autoScroll=false;
		return;
	}
//	debug('!');
	var scrollLeft=$(window).scrollLeft();
//	$('#slider').css('left',(scrollLeft/width*linkWidth)+'px');
	$('#slider').stop(true);
	$('#slider').animate({left:(scrollLeft/width*linkWidth)+'px'},500,'swing');
	
}
