var TeaserSlider =
{
	images : [],
	texts : [],
	teaserBlock : null,
	index : 0,
	loadInterval : null,
	
	init : function()
	{
		this.teaserBlock = $('#teaserBlock');

		if (typeof(this.teaserBlock) != 'undefined' && this.teaserBlock != null && this.teaserBlock.hasClass('start'))
		{
			var self = this;
			
			if (this.isIE('8.0') || this.isIE('7.0'))
			{
				window.setTimeout(function()
				{
					self.loadInterval = setInterval(function() { self.loadCheck(); }, 50);
				}, 300);
			}
			else
			{
				this.loadInterval = setInterval(function() { self.loadCheck(); }, 10);
			}
		}
	},
	
	isIE : function(version)
	{
		return (navigator.userAgent.indexOf('MSIE ' + version) >= 0);
	},
	
	/**
	 * Workaround: JQuery.load() geht bei FF4 nicht mehr für img-Tags
	 */
	loadCheck : function()
	{
		if (this.teaserBlock.find('img:visible').height() > 0)
		{
			clearInterval(this.loadInterval);
			this.initImages();
			this.initTexts();
			this.start();
		}
	},
	
	start : function()
	{
		if (this.images.length > 1 && this.images.length == this.texts.length)
		{
			$('#teaserArrow').css('display', 'block');
			
			if(!this.teaserBlock.hasClass('blockTeaserSlider'))
			{
				var self = this;
				setInterval(function() { self.update(); }, Config.teaserSliderStandTime);
			}
		}
	},
	
	initTexts : function()
	{
		var nodes = $('#teaserArrow').children('p');
		this.texts = this.sharedInit('p', nodes);		
	},
	
	initImages : function()
	{
		var nodes = this.teaserBlock.find('img');
		this.images = this.sharedInit('img', nodes);
	},
	
	sharedInit : function(tag, nodes)
	{
		var array = [];

		var length = nodes.length;

		if (tag == 'img')
		{
			var imageHeight = this.teaserBlock.find('img:visible').height();
			var imageWidth = this.teaserBlock.find('img:visible').width();
			
			if (this.isIE('7.0'))
			{
				var topPadding = this.teaserBlock.css('paddingTop').replace(/px/g, '');
				this.teaserBlock.css({ paddingTop: '0px', marginTop: topPadding + 'px' });
			}
			
			this.teaserBlock.css('height', (imageHeight + 7) + 'px');
			
			this.teaserBlock.find('.shadow').css({ width : imageWidth + 'px', height : imageHeight + 'px' });
			$('#teaserArrow').css({ marginTop : (imageHeight - 80) + 'px' });
		}

		if (length > 1)
		{
			for (var i = 0; i < length; i++)
			{
				array[i] = $(nodes[i]);
				array[i].css({ position : 'absolute' });
			}
		}
		
		return array;
	},
	
	update : function()
	{
		var lastIndex = this.index;
			
		this.index++;
			
		if (this.index >= this.images.length)
		{
			this.index = 0;
		}
		
		this.updateImages(lastIndex);
		this.updateTexts(lastIndex);
	},
	
	updateImages : function(lastIndex)
	{	
		this.images[lastIndex].css({ 'z-index' : 100 });
		this.images[this.index].css({ 'z-index' : 99 });

		this.images[this.index].show();
		this.images[lastIndex].fadeOut(Config.teaserSliderFadingTime);
	},
	
	updateTexts : function(lastIndex)
	{	
		this.texts[lastIndex].css({ 'z-index' : 120 });
		this.texts[this.index].css({ 'z-index' : 119 });
		this.texts[this.index].fadeIn(Config.teaserSliderFadingTime);
		this.texts[lastIndex].fadeOut(Config.teaserSliderFadingTime);
	}
};

$(document).ready(function() { TeaserSlider.init(); });
