// this file contains functions for the slideshow
// author:    Dominik Scholz, schlotzz@go4u.de
// changed:   2010-07-21


var IMAGESLIDE = {
	
	// private vars
	timeout: 30,
	step: 5,
	interval: 7500,
	slides: [],
	
	
	// run imageslide
	go: function(id, slides)
	{
		if (slides.length <= 0)
			return;
			
		slides = this.decodeURIs(slides);

		var parent = document.getElementById('imageslide'+id);
		
		var insertWrapper = document.createElement('div');
		insertWrapper.className = 'wrapper';
		insertWrapper.style.left = '0px';
		parent.appendChild(insertWrapper);
		
		var insertBullets = document.createElement('div');
		insertBullets.className = 'bullets';
		parent.appendChild(insertBullets);
		
		var wrapperWidth = 0;
		var bulletsWidth = 0;
		var insertSlide;
		var insertImage;
		var insertText;
		var insertBullet;
		for (i=0; i<slides.length; i++)
		{
			insertSlide = document.createElement('div');
			insertSlide.className = 'slide';
			insertWrapper.appendChild(insertSlide);
			
			insertImage = document.createElement('img');
			insertImage.className = 'image';
			insertImage.src = slides[i][0];
			insertSlide.appendChild(insertImage);
			
			if (slides[i][1]+slides[i][2] != '')
			{
				insertText = document.createElement('p');
				insertText.className = 'message';
				insertText.innerHTML = this.formatMessage(slides[i][1], slides[i][2]);
				insertSlide.appendChild(insertText);
			}
			
			insertWrapper.appendChild(insertSlide);
			wrapperWidth += insertSlide.offsetWidth;
			
			insertBullet = document.createElement('div');
			insertBullet.className = (i == 0)? 'bulletActive':'bullet';
			insertBullet.id = 'bullet' + id + '-' + i;
			insertBullet.onclick = function(evt) {
				if (!evt)
					evt = window.event;
				var target;
				if (evt.target) target = evt.target;
				else if (evt.srcElement) target = evt.srcElement;
				if (target.nodeType == 3) // defeat Safari bug
					target = target.parentNode;
				var htmlId = target.id;
				var id = htmlId.split('bullet').pop().split('-').shift();
				var num = htmlId.split('-').pop();
				IMAGESLIDE.click(id, num);
			};
			insertBullets.appendChild(insertBullet);
			
			bulletsWidth += insertBullet.offsetWidth;
		}
		
		var bulletsLeft = parseInt((insertSlide.offsetWidth - bulletsWidth) / 2);
		
		insertWrapper.style.width = wrapperWidth + 'px';
		insertBullets.style.width = bulletsWidth + 'px';
		insertBullets.style.left = bulletsLeft + 'px';
		
		this.slides[id] = {
			slides: slides,
			length: slides.length,
			count: 0,
			animation: 0,
			timer: null,
			wrapper: insertWrapper,
			bullets: insertBullets
		}
		
		this.slides[id].timer = window.setTimeout("IMAGESLIDE.next("+id+");", this.interval);
	},
	
	
	// handle click on bullet
	click: function(id, num)
	{
		if (this.slides[id].timer != null)
			window.clearTimeout(this.slides[id].timer);
			
		this.slides[id].animation = 0;
		this.slides[id].timer = window.setTimeout("IMAGESLIDE.next("+id+","+num+");", this.timeout);
	},
	
	
	// format message text with html
	formatMessage: function(head, text)
	{
		if (head != '' && text != '')
		{
			return '<b>' + head + '</b>' + text;
		}
		else if (head != '')
		{
			return '<b>' + head + '</b>';
		}
		else if (text != '')
		{
			return text;
		}
		return '';
	},
	
	
	// goto next position
	next: function(id, num)
	{
		var lastBullet = this.slides[id].bullets.getElementsByTagName('div')[this.slides[id].count];
		lastBullet.className = 'bullet';
	
		if (num != null)
		{
			this.slides[id].count = num;
		}
		else
		{
			this.slides[id].count++;
			if (this.slides[id].count >= this.slides[id].length)
				this.slides[id].count = 0;
		}

		var currentBullet = this.slides[id].bullets.getElementsByTagName('div')[this.slides[id].count];
		currentBullet.className = 'bulletActive';
			
		var width = this.slides[id].wrapper.firstChild.offsetWidth;
		var from  = parseInt(this.slides[id].wrapper.style.left);
		var to    = -this.slides[id].count * width;

		this.slides[id].timer = window.setTimeout("IMAGESLIDE.scroll("+id+", "+from+", "+to+");", this.timeout);
	},
	
	
	// scroll to next position
	scroll: function(id, from, to)
	{
		this.slides[id].animation += this.step;
		
		var delta = to - from;
		var pos   = from + delta * this.magic(this.slides[id].animation / 100);
		
		this.slides[id].wrapper.style.left = pos + 'px';
		
		if (this.slides[id].animation == 100)
		{
			this.slides[id].animation = 0;
			this.slides[id].timer = window.setTimeout("IMAGESLIDE.next("+id+");", this.interval);
		}
		else
		{
			this.slides[id].timer = window.setTimeout("IMAGESLIDE.scroll("+id+", "+from+", "+to+");", this.timeout);
		}
	},
	
	
	// decode urlencoded strings
	decodeURIs: function(slides)
	{
		for (var i=0; i<slides.length; i++)
		{
			for (var j=0; j<=2; j++)
			{
				slides[i][j] = decodeURIComponent(slides[i][j]);
			}
		}
		
		return slides;
	},
	
	
	// set opacity of any element, use (element.style, 0-100)
	opacity: function(o, p)
	{
		var pInt       = Math.ceil(p);
		o.filter       = 'Alpha(opacity='+pInt+')';
		o.MozOpacity   = '' + pInt/100;
		o.KTHMLOpacity = '' + pInt/100;
		o.opacity      = '' + pInt/100;
	},


	// convert with sinus-function for smoother animation
	magic: function(pos)
	{
		return ((-Math.cos(pos*Math.PI)/2) + 0.5);
	}
	
}
