var slideshow = false;
var slideshowIndex = null;
var slideshowTimer = null;
var imageOpacity = 100;
var imageOpacityMode = 1;
var opacityInterval = null;

var original_filename = null;
var original_width = null;
var original_height = null;

window.onload = preloadImages;

function preloadImages()
{
	for(var i = 0; i < 20; i++)
	{
		preloadNextImage(i);
	}
}

function showImage(index, slideshowMode, filename, width, height)
{
	if(slideshowMode == null || slideshowMode != true && slideshow == true)
	{
		return false;
	}
	
	slideshowIndex = index;
	
	if(filename != null && width != null && height != null)
	{
		original_filename = filename;
		original_width = width;
		original_height= height;
		
		document.getElementById('offer-image-link').href = original_filename;
		//document.getElementById('offer-big-image').onclick = popImage;
	}
	
	window.clearInterval(opacityInterval);
	opacityInterval = null;
	
	if(document.getElementById('offer_thumb_'+activeImage) != null)
	{
		opacityInterval = window.setInterval(function()
		{
			var img = document.getElementById('offer_image');
			
			if(imageOpacityMode == 1)
			{
				imageOpacity -= 10;
				
				if(imageOpacity == 0)
				{
					document.getElementById('offer_image').src = imagesAdr+'/'+index+'.png';
					
					imageOpacityMode = 2;
				}
			}
			else
			{
				imageOpacity += 10;
				
				if(imageOpacity == 100)
				{
					window.clearInterval(opacityInterval);
					opacityInterval = null;
					
					imageOpacityMode = 1;
				}
			}
			
			img.style.opacity = imageOpacity/100;
			img.style.filter = 'alpha(opacity='+imageOpacity+')';
		}, 10);
		
		
		if(activeImage != null)
		{
			document.getElementById('offer_thumb_'+activeImage).className = 'offer-thumb';
		}
		
		document.getElementById('offer_thumb_'+index).className = 'active-offer-thumb';
		
		activeImage = index;
		
		return true;
	}
	
	return false;
}

function showSlideshowImage(index, slideshowMode, filename, width, height)
{
	if(slideshowMode == null || slideshowMode != true && slideshow == true)
	{
		return false;
	}
	
	slideshowIndex = index;
	
	/*
	if(index > 5)
	{
		if(index < 16)
		{
			scrollbar.contentScroll((index - 5) * 80 - 40, 0, false);
		}
		else
		{
			scrollbar.contentScroll(800, 0, false);
		}
	}
	else
	{
		scrollbar.contentScroll(0, 0, false);
	}
	*/
	
	window.clearInterval(opacityInterval);
	opacityInterval = null;
	
	if(document.getElementById('offer_thumb_'+activeImage) != null)
	{
		opacityInterval = window.setInterval(function()
		{
			var img = document.getElementById('slideshow-image');
			
			if(imageOpacityMode == 1)
			{
				imageOpacity -= 10;
				
				if(imageOpacity < 0) imageOpacity = 0;
				
				if(imageOpacity == 0)
				{
					document.getElementById('slideshow-image').style.background = 'url('+imagesAdr+'/'+index+'.png) center center no-repeat';
					
					imageOpacityMode = 2;
				}
			}
			else
			{
				imageOpacity += 10;
				
				if(imageOpacity > 100) imageOpacity = 100;
				
				if(imageOpacity == 100)
				{
					window.clearInterval(opacityInterval);
					opacityInterval = null;
					
					imageOpacityMode = 1;
				}
			}
			
			img.style.opacity = imageOpacity/100;
			img.style.filter = 'alpha(opacity='+imageOpacity+')';
		}, 10);
		
		
		if(activeImage != null)
		{
			document.getElementById('offer_thumb_'+activeImage).className = 'offer-thumb';
		}
		
		document.getElementById('offer_thumb_'+index).className = 'active-offer-thumb';
		
		activeImage = index;
		
		return true;
	}
	
	return false;
}

function preloadImage(index)
{
	if(document.getElementById('offer_thumb_'+index) != null)
	{	
		img = new Image();
		img.src = imagesAdr+'/'+index+'.png';
	}
	
	return false;
}

function preloadNextImage(currentIndex)
{
	var nextIndex = parseInt(currentIndex)+1;
	
	if(nextIndex > 20)
	{
		nextIndex = 1;
	}
	
	var searches = 0;
			
	while(document.getElementById('offer_thumb_'+nextIndex) == null)
	{
		nextIndex++;
		searches++;
		
		if(nextIndex > 20)
		{
			nextIndex = 1;
		}
		
		if(searches == 20)
		{
			return false;
		}
	}
	
	if(document.getElementById('offer_thumb_'+nextIndex) != null)
	{
		preloadImage(nextIndex);
	}
}

function toggleSlideshow(obj, start_text, stop_text)
{
	if(slideshowIndex == null)
	{
		slideshowIndex = parseInt(activeImage);
	}
	
	if(slideshow == false)
	{
		if(start_text != null && stop_text != null)
		{
			obj.value = stop_text;
		}
		else
		{
			obj.className = 'active-slideshow-button';
		}
		
		preloadNextImage(slideshowIndex);
		
		slideshowIndex++;
		
		slideshowTimer = window.setInterval(function()
		{			
			if(slideshowIndex > 20)
			{
				slideshowIndex = 1;
			}
			
			var searches = 0;
			
			while(document.getElementById('offer_thumb_'+slideshowIndex) == null)
			{
				slideshowIndex++;
				searches++;
				
				if(slideshowIndex > 20)
				{
					slideshowIndex = 1;
				}
				
				if(searches == 10)
				{
					return false;
				}
			}
			
			showSlideshowImage(slideshowIndex, true);
			
			preloadNextImage(slideshowIndex);
			
			slideshowIndex++;
		}, 5000);
			
		slideshow = true;
	}
	else
	{
		window.clearTimeout(slideshowTimer);
		slideshowTimer = null;
		slideshow = false;
		
		if(start_text != null && stop_text != null)
		{
			obj.value = start_text;
		}
		else
		{
			obj.className = 'slideshow-button';
		}
	}
}

var original_image_win = null;

function popImage(filename, width, height)
{
	if(filename == null || width == null || height == null)
	{
		filename = original_filename;
		width = original_width;
		height = original_height;
	}
	
	var x = screen.width / 2 - width / 2;
	var y = (screen.height - 100) / 2 - height / 2;
	
	if(original_image_win != null)
	{
		original_image_win.close();
	}
	
	original_image_win = window.open(filename, 'maillist', 'width='+(width+20)+', height='+(height+20)+', left='+x+', top='+y+', toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resizable=1');
}

function popSlideShow(filename)
{
	var width = 804;
	var height = 686;

	var x = screen.width / 2 - (width+8) / 2;
	var y = 0;
	
	if(screen.height > 768)
	{
		var y = (screen.height) / 2 - (height+60) / 2 - 10;
	}
	
	if(y < 0) y = 0;
	
	if(original_image_win != null)
	{
		original_image_win.close();
	}
	
	original_image_win = window.open(filename, 'maillist', 'width='+width+', height='+height+', left='+x+', top='+y+', toolbar=0, scrollbars=0, location=0, statusbar=0, menubar=0, resizable=0');
}

var scrollbar = null;

function initScroller()
{
	scrollbar = startScroller('scrollarea');
}
