$(function(e) {
	loginManager.init();
	$('#content .slideshow').each(function(i){
		var c = $(this);
		if(c.hasClass('full')) {
			new slideshow(c, 0);
		} else if(c.hasClass('strip')) {
			new slideshow(c, 1);
		} else if(c.hasClass('carousel')) {
			new slideshow(c, 2);
		}
	});
	$('#service .slideshow').each(function(i){
		var s = $(this);
		if(s.hasClass('full')) {
			new slideshow(s, 0);
		} else if(s.hasClass('strip')) {
			new slideshow(s, 1);
		} else if(s.hasClass('carousel')) {
			new slideshow(s, 2);
		}
	});
	// rotator - start
	initSwapAds();
	// rotator - end
});

function initSwapAds() {
	$(".nextpic").bind('click',function() {addPI();});
	$(".prevpic").bind('click',function() {addPI();});
	$("span.topic_backward a").bind('click',function() {addPI();});
	$("span.topic_forward a").bind('click',function() {addPI();});
	$('#content div.slideshow.full ul.thumbs').bind('click',function() {addPI();});
}

var adframe_loading = false;

function swapAds() {
	if (!adframe_loading) {
		adframe_loading = true;
		document.write = function(test) {
			$.noop();
		}
		document.writeln = function(test) {
			$.noop();
		}
		$("#adframe").empty();
		$("#adframe").remove();
		$("#footer").append($('<iframe id="adframe" style="width:0px !important;height:0px !important;border:none !important;" class="adframe" name="adframe" src="/ads/adframe.jsp">'));
	}	
}

function addPI() {
	oCount();
	swapAds();
	gCount();
}

function oCount() {
	$('#footer img').remove();
	$('#footer').append($('<img width="1" height="1" alt="oewapixel" src="'+OEWA+'?d='+new Date().getTime()+'">'));
}

function gCount() {
	try {
	var pageTracker = _gat._getTracker("UA-12710069-1");
	pageTracker._trackPageview();
	} catch(err) {}
}

/* CLOCK
***********************************************************************/
var clock = function() {
	var xgif = '/images/gradient/x.gif';
	var started = false;
	var checker = 2;
	var iFront, iBack;
	function check(e, front) {
		if(front) iFront = $(e);
		else iBack = $(e);
		if(--checker == 0) start();
	}
	function start() {
		if(document.all && !window.XMLHttpRequest) {
			iFront.attr({'width': iFront.width(), 'height': iFront.height()}).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+iFront.attr('src')+'\',sizingMethod=\'scale\')').attr('src', xgif);
		}
		var t = new Date();
		window.setTimeout("clock.tick(); window.setInterval(clock.tick, 60000);", 60000-(t.getSeconds()*1000+t.getMilliseconds()));
		tick();
	}
	function tick() {
		var h, m;
		h = new Date();
		m = h.getMinutes();
		h = h.getHours()%12;
		var lB = m*-68;
		var lF = (h*5+Math.round(m/12));
		lF *= (lF == 60)?0:-68;
		iFront.css('left', lF+'px');
		iBack.css('left', lB+'px');
	}
	return {
		tick: function() {
			return tick();
		},
		start: function(e, front) {
			return check(e, front);
		}
	}
}();

var lTimer = null;

/* LOGINMANAGER
***********************************************************************/
function showLogin() {
	$('#lgForm').addClass('hovered');
	$('#lgForm').show();
}

function hideLogin() {
	$('#lgForm').removeClass('hovered');
	$('#lgForm').hide();
}

var loginManager = function() {
	function initialize() {
		$('.not_logged ').children('a').hover(function(e){
			if(lTimer != null) {
				window.clearTimeout(lTimer);
				lTimer = null; 
			}
			showLogin();
		}, function(e){
			lTimer = window.setTimeout(function(){hideLogin();}, 400);
			//$(this).children('a').removeClass('jsHover');
		});
		$('#lgForm').children().hover(function(e){
			if(lTimer != null) {
				window.clearTimeout(lTimer);
				lTimer = null; 
			}
			showLogin();
		}, function(e){
			if ($(e.relatedTarget).parents("body").length == 1)
				lTimer = window.setTimeout(function(){hideLogin();}, 400);
			//$(this).children('a').removeClass('jsHover');
		});
	}
	return {
		init: function() {
			return initialize();
		}
	}
}();

function trim(str) {
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

/* SLIDESHOW
***********************************************************************/
var sId = 1;

function shownextpic(button) {
	$("#next_slide_"+button).click();
}

function initSlideshow(sInt,slideShowid) {
	if (sInt != null) {
		window.clearTimeout(sInt);
		$("#slides_button_"+slideShowid).addClass("paused");
		$("#slides_button_"+slideShowid).attr("src","/images/controls/slideshow.png").attr("alt","play").attr("title","play");
		return null;
	} else {
		nInt = window.setInterval('shownextpic(\''+slideShowid+'\')', 3000);
		$("#slides_button_"+slideShowid).removeClass("paused");
		$("#slides_button_"+slideShowid).attr("src","/images/controls/pause.png").attr("alt","pause").attr("title","pause");
	}
	return nInt;
}

var slideshow = function(o, c) {
	sId = sId +1;
	var slideShowid = sId;
	var self = this;
	var sInt = null;
	this.container = $(o).css('overflow', 'hidden');
	this.container.children('.thumbs').removeClass('noscript').css('position', 'absolute');
	this.style = c;
	this.kids = [];
	this.kidSize = [];
	this.actImg = 0;
	this.container.children('.thumbs').children('li').each(function(i){
		self.kids[i] = $(this);
		self.kids[i].css('position', 'absolute');
	});
	var max = c?25:1;
	if(c==1) {
		if(self.kids.length < 5) {
			if(self.kids.length == 2)
				max = 6;
			else if(self.kids.length != 1)
				max = self.kids.length*2;
		}
	}
	var i = 0;
	while(self.kids.length < max)
		self.kids[self.kids.length] = self.kids[i++].clone().appendTo(self.kids[0].parent());
	this.w = this.container.width();
	this.h = this.container.height();
	this.container.children('ul').css('top', 0).css('left', 0).width(this.w);
	this.container.css('position', 'relative');
	this.nButt = $('<img class="nextpic" id="next_slide_'+sId+'" src="/images/controls/next.png" alt="n&auml;chstes" title="n&auml;chstes" style="position: absolute; z-index:5; cursor: pointer;" />').click(function(){self.next[self.style](self);});
	this.pButt = $('<img class="prevpic" src="/images/controls/prev.png" alt="n&auml;chstes" title="n&auml;chstes" style="position: absolute; z-index:5; cursor: pointer;" />').click(function(){self.prev[self.style](self);});
	this.sButt = $('<img class="slide" id="slides_button_'+sId+'" src="/images/controls/slideshow.png" alt="play" title="play" style="position: absolute; z-index:5; cursor: pointer;" />').click(function(){sInt = initSlideshow(sInt,slideShowid);});
	if(document.all && !window.XMLHttpRequest) {
		this.nButt.css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/next.png', sizingMethod='scale')").css('background', 'none').attr({'src': '/images/gradient/x.gif', 'width': (this.style==2)?20:31, 'height': (this.style==2)?20:31});
		this.pButt.css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/prev.png', sizingMethod='scale')").css('background', 'none').attr({'src': '/images/gradient/x.gif', 'width': (this.style==2)?20:31, 'height': (this.style==2)?20:31});
		this.sButt.css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/controls/slideshow.png', sizingMethod='scale')").css('background', 'none').attr({'src': '/images/gradient/x.gif', 'width': (this.style==2)?20:31, 'height': (this.style==2)?20:31});
	}
	this.container.append(this.nButt).append(this.pButt).append(this.sButt);
	this.arrange();
	this.container.find('.photo_').show();
	this.container.find('.caption').show();
};

slideshow.prototype.arrange = function() {
	switch(this.style) {
	// full
		case 0:
			this.itemsPerSide = 1;
			this.tw = this.w-120;
			this.th = this.tw*0.67;
			this.h = this.th+60;
			this.nButt.css('bottom', this.h/2-15+'px').css('right', '15px');
			this.pButt.css('bottom', this.h/2-15+'px').css('left', '15px');
			this.sButt.css('bottom','30px').css('right', '15px');
			this.sButt.show();
			for(var i=0; i<this.kids.length; i++) {
				var self = this;
				
				var a = this.kids[i].children('a');
				var img = a.children('img');
				
				if(img.width() > img.height())
					img.width(this.tw);
				else
					img.height(this.th);
				img.css({'margin-left': (this.w-img.width())/2, 'margin-top': '30px'});

				var nimg = $('<img style="position: absolute; left: 1000px;" alt="'+i+'" />').load(function(e){
					var me = $(this);
					if(me.width() > me.height())
						me.width(self.tw);
					else
						me.height(self.th);
					me.css({'margin-left': (self.w-me.width())/2+'px', 'margin-top': '30px'});
					me.click(function(e){self.next[self.style](self);});
					
					self.kids[this.alt].children('a').prepend(me).click(function(e){e.preventDefault();});
					me.css('position', 'static');
					me.next().css('display', 'none');
				});
				this.container.append(nimg);
				nimg.attr('src', a.attr('href'));
				
				this.kids[i].css({'opacity': 1, 'left': '0px', 'top': '0'}).height(this.h);
				if(i!=0) this.kids[i].css('display', 'none');
				
				this.kids[i].children('div').css({'position': 'absolute', 'left': '60px', 'top': '7px', 'line-height': '16px', 'text-align': 'center', 'color': 'white'}).width(this.w-120).append('  (Bild '+(i+1)+'/'+this.kids.length+")");
				this.kids[i].append($('<div>'+img.attr('title')+'</div>').css({'position': 'absolute', 'left': '60px', 'bottom': '14px', 'line-height': '16px', 'font-size': '10px', 'color': 'white'}).width(this.w-120));//);
			}
			this.container.height(this.h).css('background', '#9c9e9f');
			break;
	// strip
		case 1:
			this.container.append(this.container.children('h1').css({'color': 'white', 'background': '#9c9e9f', 'display': 'inline', 'position': 'absolute', 'line-height': '20px', 'font-size': '14px', 'padding': '0 4px'}));
			this.itemsPerSide = 4;
			this.th = 85;
			this.tw = this.th/0.67+(this.w-120 - this.th*this.itemsPerSide/0.67)/(this.itemsPerSide-1);
			this.h = this.th+40;
			this.nButt.css('bottom', this.h/2-15+'px').css('right', '15px');
			this.pButt.css('bottom', this.h/2-15+'px').css('left', '15px');
			this.sButt.hide();
			this.container.children('ul').css({'overflow': 'hidden', 'top': '20px', 'left': '60px'}).height(Math.round(this.h-8)).width(this.w-120);
			for(var i=0; i<this.kids.length; i++) {
				var self = this;
				
				var a = this.kids[i].children('a');
				var img = a.children('img');
				
				img.height(this.th).css('margin-left', (this.th/0.67-img.width())/2+'px');
				
				this.kids[i].css({'opacity': 1, 'left': this.tw*i+'px', 'top': '0', 'background': 'transparent'}).height(this.th+16).width(this.th/0.67);
				
				this.kids[i].children('a').prepend(this.kids[i].children('div').addClass('info'));
			}
			this.container.height(this.h).css('background', '#9c9e9f');
			break;
	// carousel
		case 2:
			var self = this;
			this.nButt.css({'top': '53px', 'right': '10px', 'width': '20px'});
			this.pButt.css({'top': '53px', 'left': '10px', 'width': '20px'});
			this.sButt.hide();
			this.itemsPerSide = 5;
			this.th = 110;
			this.tw = (this.w-80-this.th*0.67)/4;
			this.h = this.th+40;
			this.container.children('ul').css({'position': 'absolute', 'top': '8px', 'left': '40px'}).height(this.h-16).width(this.w-80);
			for(var i=0; i<this.kids.length; i++) {
				var self = this;
				
				var a = this.kids[i].children('a');
				
				var img = a.children('img');
				
				img.height(this.th);
				
				this.kids[i].css({'opacity': 1, 'left': '0', 'top': '0', 'background': 'transparent'}).height(this.th+24).width(this.w-80);
				img.attr('alt', img.width()).css('position', 'absolute');
				img.css(this.getPos(i, 0));
				a.append(this.kids[i].children('div'));
				a.children('div').css({'position': 'absolute', 'bottom': '0', 'width': this.w, 'text-align': 'center'});
				if(i==0) a.children('div').css('left', '-40px');
				else a.children('div').css('left', ((i>this.kids.length/2)?(i-this.kids.length):i)*this.w-40+'px');
				if(i!=0) {
					if(i<this.kids.length/2)
						this.kids[i].insertBefore(this.kids[i-1]);
					else
						this.kids[i].insertBefore(this.kids[Math.abs(i-this.kids.length)]);
				}
			}
			this.container.height(this.h).css('background', '#9c9e9f');
			this.curImg = this.actImg;
			this.timer = window.setInterval(function(e){self.update(e);}, 50);
			break;
	}
};
slideshow.prototype.next = {
	0: function(self) {
		if(self.kids.length == 1) return;
		var old = self.actImg++;
		if(self.actImg >= self.kids.length) self.actImg -= self.kids.length;
		self.kids[self.actImg].fadeIn(500);
		self.kids[old].fadeOut(500);
	},
	1: function(self) {
		self.actImg++;
		if(self.actImg >= self.kids.length) self.actImg -= self.kids.length;
		for(var i=0; i<self.kids.length; i++) {
			var l = i-self.actImg;
			if(l < -1)
				l += self.kids.length;
			if(l >= self.kids.length-1)
				l -= self.kids.length;
				
			self.kids[i].stop().css('left', (l+1)*self.tw+'px');
			self.kids[i].animate({left: l*self.tw+'px'}, 500);
		}
	},
	2: function(self) {
		self.lastImg = -1;
		self.actImg++;
		if(self.actImg >= self.kids.length) self.actImg-=self.kids.length;
	}
};
slideshow.prototype.prev = {
	0: function(self) {
		if(self.kids.length == 1) return;
		var old = self.actImg--;
		if(self.actImg < 0) self.actImg += self.kids.length;
		self.kids[self.actImg].fadeIn(500);
		self.kids[old].fadeOut(500);
	},
	1: function(self) {
		self.actImg--;
		if(self.actImg < 0) self.actImg += self.kids.length;
		for(var i=0; i<self.kids.length; i++) {
			var l = i-self.actImg;
			if(l < 0)
				l += self.kids.length;
			if(l >= self.kids.length)
				l -= self.kids.length;
				
			self.kids[i].stop().css('left', (l-1)*self.tw+'px');
			self.kids[i].animate({left: l*self.tw+'px'}, 500);
		}
	},
	2: function(self) {
		self.lastImg = 1;
		self.actImg--;
		if(self.actImg < 0) self.actImg+=self.kids.length;
	}
};
slideshow.prototype.getPos = function(i, act) {
	var diff = i-act;
	if(diff < -this.kids.length/2) diff+=this.kids.length;
	if(diff > this.kids.length/2) diff-=this.kids.length;
	if(diff>2)diff=2; if(diff<-2)diff=-2;
	var w = this.kids[i].children('a').children('img').attr('alt')*((2-Math.abs(diff))*.25+.5);
	var h = this.th*((2-Math.abs(diff))*.25+.5);
	var l = (this.w-80-w)/2+diff*this.tw;
	var t = (this.th-h)/2;
	return {left: l+'px', top: t+'px', height: h+'px'};
};
slideshow.prototype.update = function(e) {
	if(this.curImg == this.actImg) return true;
	if(this.lastImg > 0)
		this.curImg -= .1;
	else
		this.curImg += .1;
	if(this.curImg < 0) this.curImg+=this.kids.length;
	if(this.curImg >= this.kids.length) this.curImg-=this.kids.length;
	if(Math.abs(this.actImg-this.curImg) < .1) this.curImg = this.actImg;
	
	for(var i=0; i<this.kids.length; i++) {
		var diff = this.curImg-i;
		if(diff < -this.kids.length/2) diff+=this.kids.length;
		if(diff > this.kids.length/2) diff-=this.kids.length;
		
		this.kids[i].find('img').css(this.getPos(i, this.curImg));
		var l = -diff*this.w-40+'px';
		this.kids[i].find('div').css('left', l);
		if(Math.abs(diff) > 4) diff=4;
		this.kids[i].css({'z-index': 4-Math.round(Math.abs(diff))});
	}
};

