var Slider = Class.create();
Slider.prototype = {
	
	initialize: function(imgContainer, imgTagType, imagesClassName)
	{	
		this.imagesClassName = imagesClassName;
		this.imgContainer = imgContainer;
		this.imgTagType = imgTagType;
		this.imgs = new Array();
		this.zInterval = null;
		this.current = 0;
		this.pause = false;
		
		if($(this.imgContainer))
		{
			this.prepareImg();	
		}
		
	},
	
	prepareImg: function()
	{	var self = this;
		var imgClassName = document.getElementsByClassName(this.imagesClassName);
		
		this.imgs = $(this.imgContainer).getElementsByTagName(this.imgTagType);
		imgClassName.each(function(img)
		{
			img.xOpacity = 0;
			img.setAttribute('width', img.getStyle('width').replace('px', ''));
			img.setAttribute('height', img.getStyle('height').replace('px', ''));
		});
		
		this.imgs[0].style.display = "block";
		this.imgs[0].xOpacity = .99;
		
		var self = this;
		setTimeout(function()
		{
			self.eventStart();
		}, 2000);	
	},
	
	eventStart: function()
	{
		var self = this;
		
		cOpacity = this.imgs[this.current].xOpacity;
		nIndex = this.imgs[this.current+1]?this.current+1:0;
		nOpacity = this.imgs[nIndex].xOpacity;
		
		cOpacity-=.05; 
		nOpacity+=.05;
		
		this.imgs[nIndex].style.display = "block";
		this.imgs[this.current].xOpacity = cOpacity;
		this.imgs[nIndex].xOpacity = nOpacity;
		
		this.setOpacity(this.imgs[this.current]); 
		this.setOpacity(this.imgs[nIndex]);
		
		if(cOpacity<=0) 
		{
			this.imgs[this.current].style.display = "none";
			this.current = nIndex;
			setTimeout(function()
			{
				self.eventStart();
			}, 5000); //<---- Her endres hastighet i milisekund!
		} 
		else 
		{
			setTimeout(function()
			{
				self.eventStart();
			}, 50);
		}
		

	},
	
	setOpacity: function(obj)
	{
				
		if(obj.xOpacity>.99) 
		{
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
		
	}
	
};
Event.observe(window, 'load', function()
{
	new Slider('slide', 'div', 'produkt');
});
