// Version 09.11.18

/*
Usage (all settings are optional):

new SlideSet(
{
	theme: 'theme_name',				// Default 'default'; stored in /themes/SlideSet/[theme_name]/theme.css
	vertical: true,						// Default false
	buttons: false,						// Default true
	speed: 15,							// Default 5
	autoscroll: true,					// Default false
	autoscroll_pause: 2000,				// Default 1000
	autoscroll_pause_after_click: 9000,	// Default 30000
	slides:
	[
		{
			image: '/images/img1.png',
			onclick: function()
			{
				alert('Hi.');
			}
		},
		{
			url: '/page.html'
		},
		{
			html: '<p>This is a paragraph.</p>'
		}
	]
}).write('slider_div');
*/

function SlideSet(options)
{
	var i, div, img;
	
	if(!options || typeof(options.slides)!='object' || isNaN(options.slides.length))
		return false;
	
	this.slides=options.slides;
	this._theme='default';
	if(!!options.theme)
		this._theme=options.theme;
	this.buttons=(options.buttons!==false);
	this.autoscroll=(options.autoscroll===true);
	this.speed=20;
	if(!isNaN(options.speed))
		this.speed=1000/options.speed;
	this.autoscroll_pause=1000;
	if(!isNaN(options.autoscroll_pause))
		this.autoscroll_pause=options.autoscroll_pause;
	this.autoscroll_pause_after_click=30000;
	if(!isNaN(options.autoscroll_pause_after_click))
		this.autoscroll_pause_after_click=options.autoscroll_pause_after_click;
	this._interval=false;
	this._autoscroll_timeout=false;
	
	this.left_button=document.createElement('div');
	this.left_button.className='SlideSet_left_button iepngfix';
	this.left_button.onclick=function()
	{
		this.slide_by(-1, true);
	}.bind(this);
	
	this.slider=document.createElement('div');
	this.slider.className='SlideSet_slider';
	
	this.slide_container=document.createElement('div');
	this.slide_container.className='SlideSet_slide_container';
	
	for(i=0; i<this.slides.length; i++)
	{
		div=document.createElement('div');
		div.className='SlideSet_slide';
		if(!!this.slides[i].image)
		{
			img=document.createElement('img');
			img.src=this.slides[i].image;
			div.appendChild(img);
		}
		else if(!!this.slides[i].url)
		{
			// TODO: AJAX the content in.
		}
		else if(!!this.slides[i].html)
			div.innerHTML=this.slides[i].html;
		
		if(!!this.slides[i].onclick)
		{
			div.onclick=this.slides[i].onclick.bind(this);
			div.style.cursor='pointer';
		}
		
		this.slides[i].div=this.slide_container.appendChild(div);
	}
	
	this.right_button=document.createElement('div');
	this.right_button.className='SlideSet_right_button iepngfix';
	this.right_button.onclick=function()
	{
		this.slide_by(1, true);
	}.bind(this);
	
	this.slider.appendChild(this.slide_container);
	
	return this;
}

SlideSet.prototype.write=function(container_id)
{
	var fn;
	
	fn=function()
	{
		var container;
		
		this.setTheme(this._theme);
		
		container=document.getElementById(container_id);
		if(!container)
		{
			alert('The container div with the id "'+ container_id +'" was not found.');
			return false;
		}
		
		if(this.buttons)
			container.appendChild(this.left_button);
		container.appendChild(this.slider);
		if(this.buttons)
			container.appendChild(this.right_button);
		this.slider.scrollLeft=0;
		
		if(this.autoscroll)
		{
			this._autoscroll_timeout=setTimeout(function()
			{
				this.slide_by(1);
			}.bind(this), this.autoscroll_pause);
		}
	}.bind(this).defer();
	
	return this;
}

SlideSet.prototype.setTheme = function(theme)
{
	if(!!this._themeLinkScreen)
		document.getElementsByTagName('head')[0].removeChild(this._themeLinkScreen);
	if(!!this._themeLinkPrint)
		document.getElementsByTagName('head')[0].removeChild(this._themeLinkPrint);
	
	fileref=document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('href', '/themes/SlideSet/'+theme+'/theme.css');
	fileref.setAttribute('media', 'screen');
	this._themeLinkScreen = document.getElementsByTagName('head')[0].appendChild(fileref);
	
	fileref=document.createElement('link');
	fileref.setAttribute('rel', 'stylesheet');
	fileref.setAttribute('type', 'text/css');
	fileref.setAttribute('href', '/themes/SlideSet/'+theme+'/print.css');
	fileref.setAttribute('media', 'print');
	this._themeLinkPrint = document.getElementsByTagName('head')[0].appendChild(fileref);
}

SlideSet.prototype.slide_by=function(steps, click)
{
	var index, position, target, next_target, scroll_amount;
	
	if(this._autoscroll_timeout!==false)
	{
		clearTimeout(this._autoscroll_timeout);
		this._autoscroll_timeout=false;
	}
	
	if(click!==true)
		click=false;
	
	//this.position=(this.slides.length - (Math.abs((this.position+steps)-this.slides.length)%this.slides.length)) % this.slides.length;
	next_target=this.slides[1].div;
	position=0;
	if(steps < 0)
	{
		next_target=this.slides[0].div;
		this._rearrange_slides(-1);
		this.slider.scrollLeft=next_target.offsetLeft;
		next_target=this.slides[0].div;
		position=1;
	}
	
	index=(this.slides.length - (Math.abs((position+steps)-this.slides.length)%this.slides.length)) % this.slides.length;
	target=this.slides[index].div;
	
	scroll_amount=parseInt((target.offsetLeft-this.slider.scrollLeft)/this.speed);
	if(index > this.slides.length / 2)
		scroll_amount=-parseInt((this.slides[this.slides.length-index].div.offsetLeft-this.slider.scrollLeft)/this.speed);
	
	if(scroll_amount != 0)
	{
		if(this._interval!==false)
			clearInterval(this._interval);
		this._interval=setInterval(function()
		{
			var pic, pause;
			
			this.slider.scrollLeft += scroll_amount;
			
			if((this.slider.scrollLeft >= next_target.offsetLeft && scroll_amount > 0) || (this.slider.scrollLeft <= next_target.offsetLeft && scroll_amount < 0))
			{
				if(steps < 0)
				{
					this._rearrange_slides(-1);
					this.slider.scrollLeft=next_target.offsetLeft;
					next_target=this.slides[0].div;
				}
				else
				{
					this._rearrange_slides(1);
					this.slider.scrollLeft=next_target.offsetLeft;
					next_target=this.slides[1].div;
				}
			}
			
			if((this.slider.scrollLeft >= target.offsetLeft && scroll_amount > 0) || (this.slider.scrollLeft <= target.offsetLeft && scroll_amount < 0))
			{
				if(steps < 0)
					this._rearrange_slides(1);
				this.slider.scrollLeft = target.offsetLeft;
				clearInterval(this._interval);
				this._interval=false;
				
				if(this.autoscroll)
				{
					if(click)
						pause=this.autoscroll_pause_after_click;
					else
						pause=this.autoscroll_pause;
					
					this._autoscroll_timeout=setTimeout(function()
					{
						this.slide_by(1);
					}.bind(this), pause);
				}
			}
		}.bind(this), 10);
	}
}

SlideSet.prototype._rearrange_slides=function(steps)
{
	while(steps != 0)
	{
		if(steps < 0)
		{
			pic=this.slides.splice(this.slides.length-1, 1)[0];
			this.slides.splice(0, 0, pic);
			this.slide_container.removeChild(pic.div);
			this.slide_container.insertBefore(pic.div, this.slide_container.childNodes[0]);
		}
		else
		{
			pic=this.slides.splice(0, 1)[0];
			this.slides.splice(this.slides.length, 0, pic);
			this.slide_container.removeChild(pic.div);
			this.slide_container.appendChild(pic.div);
		}
		
		steps=steps-(Math.abs(steps)/steps);
	}
}
