var Carousel = 
{
	Current: 0,
	Previous: 0,
	FadeStep: 0,
	FadeDelay: 10,
	FadeSteps: 10,
	RotateDelay: 5000,
	initialized: false,
	itemCount: 0,
	
	Initialize: function()
	{
		this.initialized = true
		
		if (!document.getElementById("NewsCarouselDetail")) return false
		
		// Set id's of the elements
		var divNewsCarouselDetail = document.getElementById("NewsCarouselDetail")
		var divs = divNewsCarouselDetail.getElementsByTagName('div')
		var number = 0
		for(var i=0; i<divs.length; i++)
		{
			if (divs[i].className == 'newshighlightInner')
			{
				if (number != this.Current)
				{
					divs[i].style.display = 'none'
				}
				divs[i].id = 'HighLight' + number
				number ++
			}
		}
		this.itemCount = number
		
		var divNewsCarouselLinkList = document.getElementById("NewsCarouselLinkList")
		var links = divNewsCarouselLinkList.getElementsByTagName('a')
		number = 0
		for(var i=0; i<links.length; i++)
		{
			if (links[i].className == 'carousel-link true' || links[i].className == 'carousel-link' || links[i].className == 'hi reverse' || links[i].className == 'hi reverse true')
			{
				if (number == this.Current && links[i].className != 'carousel-link true')
				{
					this.AddClass(links[i], 'true')
				}
				links[i].id = 'link' + number
				number ++
			}
		}

		// Set Height
		if (document.getElementById('NewsCarouselWrapper'))
			document.getElementById('NewsCarouselWrapper').style.height = document.getElementById('NewsCarouselWrapper').clientHeight + 'px'
			
		if (document.getElementById('HighLight' + 0))
			document.getElementById('HighLight' + 0).style.position = 'absolute'
	},
	
	SetRotateTimeout: function()
	{
		// No rotation timeout required.
		// this.RotateTimeout = setTimeout('Carousel.Rotate()', this.RotateDelay)
	},
	
	Rotate: function()
	{
		if (this.initialized == false)
		{
			// Initialize, if neccessary
			if(!this.Initialize())
			{
				return
			}
		}
		var next = this.Current+1
		if (next >= this.itemCount) next = 0;
		this.Hover(next)
		this.SetRotateTimeout()
	},
	
	MouseOut: function(n)
	{
		this.SetRotateTimeout()
	},
	
	Hover: function(n)
	{
		if (this.RotateTimeout) clearTimeout(this.RotateTimeout)
		if (this.initialized == false)
		{
			// Initialize, if neccessary
			this.Initialize()
		}
		
		// Hover on the current item: do nothing
		if (this.Current == n) return
		
		if (this.Previous != -1)
		{
			// Hide previous
			document.getElementById('HighLight' + this.Previous).style.display = 'none'
		}
		
		// Set current
		
		this.Previous = this.Current
		this.Current = n

		var previousDetail = document.getElementById('HighLight' + this.Previous)
		var currentDetail = document.getElementById('HighLight' + this.Current)
		
		// Set opacity and other styles
		if (this.FadeStep != 0)
		{
			clearTimeout(this.FadeTimeout)
			previousDetail.style.filter = 'none';
			previousDetail.style.MozOpacity = 1;
			previousDetail.style.opacity = 1;
		}
		
		this.AddClass(document.getElementById('link' + this.Current), 'true')
		this.RemoveClass(document.getElementById('link' + this.Previous), 'true')
		if (currentDetail)
		{
			currentDetail.style.opacity = 0
			currentDetail.style.MozOpacity = 0
			currentDetail.style.filter = 'alpha(opacity=0)';
			currentDetail.style.position = 'absolute'
			currentDetail.style.display = 'block'
		}
		previousDetail.style.zIndex = 2;
		
		// Height might have changed
		this.SetWrapperHeight()
		
		currentDetail.style.zIndex = 3;

		// Start fading
		this.FadeStep = 0
		this.Fade()
	},
	
	// Fade in:
	Fade: function()
	{
		this.FadeStep ++
		var previousDetail = document.getElementById('HighLight' + this.Previous)
		var currentDetail = document.getElementById('HighLight' + this.Current)
		
		if (this.FadeStep == this.FadeSteps)
		{
			// Done. No new timeout
			currentDetail.style.filter = 'none';
			previousDetail.style.display = 'none'
			this.FadeStep = 0
		}
		else
		{
			// Fade styles
			var fadeIn = this.FadeStep / this.FadeSteps
			var fadeOut = 1 - fadeIn
			
			fadeIn = 1 - Math.sin((1-fadeIn) * Math.PI/2)
			fadeOut = 1 - Math.sin((1-fadeOut) * Math.PI/2)
			
			currentDetail.style.filter = 'alpha(opacity=' + Math.floor(fadeIn*100) + ')';
			previousDetail.style.filter = 'alpha(opacity=' + Math.floor(fadeOut*100) + ')';
			
			currentDetail.style.MozOpacity = fadeIn
			previousDetail.style.MozOpacity = fadeOut
			
			currentDetail.style.opacity = fadeIn
			previousDetail.style.opacity = fadeOut
			
			// Loop again
			this.FadeTimeout = setTimeout('Carousel.Fade()', this.FadeDelay)
		}
	},
	
	// Add class to an element
	AddClass: function(element, className)
	{
		if (element)
			element.className += ' ' + className
	},
	
	// Remove class from an element
	RemoveClass: function(element, className)
	{
		var classes = element.className.split(' ')
		for(var i=0; i<classes.length; i++)
		{
			if (classes[i] == className)
			{
				classes.splice(i,1);
			}
		}
		element.className = classes.join(' ')
	},
	
	// Set height for this newsitem
	SetWrapperHeight: function()
	{
		var wrapper = document.getElementById('NewsCarouselWrapper')
		var current = document.getElementById('HighLight' + this.Current)
		var currentHeight = current.clientHeight + 10 // px, padding
		var maxHeight = wrapper.clientHeight
		
		if (currentHeight > maxHeight)
		{
			maxHeight = currentHeight
		}
		
		wrapper.style.height = maxHeight + 'px'
	}
}

Carousel.SetRotateTimeout()