var current_image = 1;
var prev_image;
var interval;

function goToImage(nr){
	nr_images = $$('.content_image').length;
	if(nr_images > 1){
		if(prev_image != null){
			new Fx.Style(prev_image, 'opacity', {duration: 2000} ).start(1,0);
		}
		images = $$('.content_image');
		image = images[nr-1];
		new Fx.Style(image, 'opacity', {duration: 2000} ).start(0,1);
		prev_image = image;
		current_image = nr;
	}
}

function goToNextImage(){
	clearInterval(interval);
	nr_images = $$('.content_image').length;
	if(current_image == nr_images){
		goToImage(1);
	} else {
		goToImage(current_image+1);
	}
	interval = setInterval(goToNextImage, 5000);
}

function goToPrevImage(){
	clearInterval(interval);
	nr_images = $$('.content_image').length;
	if(current_image <= 1){
		goToImage(nr_images);
	} else {
		goToImage(current_image-1);
	}
	interval = setInterval(goToNextImage, 5000);
}

function pngfix(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5 && version < 7) && (document.body.filters)){
		var png_images = $$('.pngfix').each(function(element) {
			var img = $(element);
			if(!img.hasClass('pngfixed')){
				img.addClass('pngfixed');
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML	
			}
		});
	} else {
		$$('.pngfix').each(function(el, i){
			el.addClass('pngfixed');
		});
	}
}

var content_images = new Array();
var slideshowcounter = 0;
var slideshowtimer;
var fading = 0;

function preinit(){
	content_images = $$('.content_image');
	content_images.each(function(el, i){
		el.setStyle('opacity', 0);
		el.setStyle('display', 'block');
	});
	
	pngfix();
	
	$$('.top').each(function(item){
		item.setStyle('opacity', 0.7);
	});
	$$('.bottom').each(function(item){
		item.setStyle('opacity', 0.7);
	});
	
	if($defined($('image_nav')) && content_images.length > 1){ 
		$('image_nav').setStyle('opacity', 0.4); 
	} else {
		if($defined($('image_nav'))){
			$('image_nav').setStyle('display', 'none'); 	
		}
	}
	if($defined($('nextimage'))){ $('nextimage').addEvent('click', reload); }
	if($defined($('previmage'))){ $('previmage').addEvent('click', preload); }
	
	init();
	
}

function init(){
	if(content_images.length){ 
		fading = 1;
		new Fx.Tween(content_images[0], {duration: 1000, onComplete: function(){ fading = 0; }}).start('opacity', 0, 1);
		if(content_images.length > 1){ 
			slideshowtimer = setTimeout('reload()', 5000);	
		}
	}
}

function reload(){
	clearInterval(slideshowtimer);
	if(content_images.length > 1){
		if(fading == 0){
			fading = 1;
			new Fx.Tween(content_images[slideshowcounter], {duration: 1000}).start('opacity', content_images[slideshowcounter].getStyle('opacity'),0);
			slideshowcounter++;
			if((slideshowcounter+1) > content_images.length){ slideshowcounter = 0; }
			new Fx.Tween(content_images[slideshowcounter], {duration: 1000, onComplete: function(){ fading = 0; }}).start('opacity', content_images[slideshowcounter].getStyle('opacity'),1);
			slideshowtimer = setTimeout('reload()', 5000);	
		}
	}
}

function preload(){
	clearInterval(slideshowtimer);
	if(content_images.length > 1){
		if(fading == 0){
			fading = 1;
			new Fx.Tween(content_images[slideshowcounter], {duration: 1000}).start('opacity', content_images[slideshowcounter].getStyle('opacity'),0);
			slideshowcounter--;
			if(slideshowcounter < 0){ slideshowcounter = content_images.length - 1; }
			new Fx.Tween(content_images[slideshowcounter], {duration: 1000, onComplete: function(){ fading = 0; }}).start('opacity', content_images[slideshowcounter].getStyle('opacity'),1);
			slideshowtimer = setTimeout('reload()', 5000);
		}
	}
}

window.addEvent('load', preinit);
