var slideshow;

function cSlideshow(path, images)
{
	//alert('hit');
	this.imgArr = images;
	this.path = path;
	this.container = document.getElementById('slideshow');
	this.container.onmousemove = function(){ slideshow.mouseMove(); };
	this.container.onclick = function(){ slideshow.clickMenu(); };

	this.bg = document.createElement('div');
	this.bg.className = 'slideshowBg';
	this.container.appendChild(this.bg);

	var div = document.createElement('div');
	this.container.appendChild(div);
	
	this.menuUl = document.createElement('ul');
	this.menuUl.className = 'slideshowMenu';
	this.menuUl.onmouseover = function(){ slideshow.mouseOverMenu(); };
	this.menuUl.onmouseout = function(){ slideshow.mouseOutMenu(); };

	div.appendChild(this.menuUl);
	
	var li = document.createElement('li');
	this.menuUl.appendChild(li);
	var a = document.createElement('a');
	a.innerHTML = 'Start';
	a.href = '#';
	a.onclick = function() { slideshow.start(); return false; }
	li.appendChild(a);
	
	li = document.createElement('li');
	this.menuUl.appendChild(li);
	a = document.createElement('a');
	a.innerHTML = 'Stop';
	a.href = '#';
	a.onclick = function() { slideshow.stop(); return false; }
	li.appendChild(a);


	li = document.createElement('li');
	this.menuUl.appendChild(li);
	var form = document.createElement('form');
	form.action = '';
	form.method = 'POST';
	form.onsubmit = function() { slideshow.setInterval(this); return false; }
	li.appendChild(form);
	var inpt = document.createElement('input');
	inpt.value = this.interval / 1000;
	inpt.size = 1;
	inpt.maxLength = 1;
	inpt.type = 'text';
	inpt.name = 'interval';
	inpt.style.fontFamily = 'monospace';
	form.appendChild(inpt);
	var span = document.createElement('span');
	span.innerHTML = 'Sekunder';
	form.appendChild(span);

	this.info = document.createElement('li');
	this.info.innerHTML = 'Bild 1 av ' + this.imgArr.length + ' ('+ this.timeLeft() + ')';
	this.menuUl.appendChild(this.info);

	
	li = document.createElement('li');
	li.style.cssFloat = 'right';
	this.menuUl.appendChild(li);
	a = document.createElement('a');
	a.innerHTML = 'Stäng';
	a.href = '#';
	a.onclick = function() { slideshow.close(); return false; }
	li.appendChild(a);
	
	li = document.createElement('li');
	li.className = 'clearer';
	this.menuUl.appendChild(li);
	
	var table = document.getElementById('AlbumTable');
	if (table)
		table.style.display = 'none';
	
	var ul = document.createElement('ul');
	ul.className = 'slideshow';
	div.appendChild(ul);

	this.li[0] = document.createElement('li');
	ul.appendChild(this.li[0]);
	var img = document.createElement('img');
	img.onload = function() { slideshow.onImageLoaded(this); }
	this.li[0].appendChild(img);

	this.li[1] = document.createElement('li');
	ul.appendChild(this.li[1]);
	img = document.createElement('img');
	img.onload = function() { slideshow.onImageLoaded(this); }
	this.li[1].appendChild(img);

	this.li[1].style.display = 'none';	
	
	this.loadImage(this.imgArr[0]);
	this.switchLi();
	this.loadImage(this.imgArr[1]);
	//this.resize(this.li[this.currLi].childNodes[0]);


	
	this.switchLi();
	this.currImg = 1;
	this.timer = setTimeout("slideshow.start();",this.interval+1000);
	this.timerMenu = setTimeout("slideshow.toggleMenu();",2000);
}

cSlideshow.prototype.container;
cSlideshow.prototype.bg;
cSlideshow.prototype.li = new Array();
cSlideshow.prototype.imgArr = new Array();
cSlideshow.prototype.currLi = 0;
cSlideshow.prototype.running = false;
cSlideshow.prototype.currImg = 1;
cSlideshow.prototype.timer;
cSlideshow.prototype.timerMenu;
cSlideshow.prototype.interval = 3000;
cSlideshow.prototype.menuUl;
cSlideshow.prototype.menuVisible = false;
cSlideshow.prototype.path;
cSlideshow.prototype.clicked = false;

cSlideshow.prototype.start = function()
{
	this.running = true;
	slideshow.showImage();
}

cSlideshow.prototype.next = function()
{

}

cSlideshow.prototype.previous = function()
{

}

cSlideshow.prototype.stop = function()
{
	clearTimeout(this.timer);
}

cSlideshow.prototype.close = function()
{
	this.stop();
	this.container.innerHTML = '';
	var table = document.getElementById('AlbumTable');
	if (table)
		table.style.display = '';
	//this = 'undefined';
}

cSlideshow.prototype.toggleMenu = function(show)
{
	if (typeof(show) != 'undefined')
	{
		//alert('show: '+ show);
		if (show == true)
			this.menuUl.style.top = 0;
		else
			this.menuUl.style.top = '-'+this.menuUl.offsetHeight+'px';
	}
	else
	{
		//alert(this.menuUl.offsetTop);
		if (this.menuUl.offsetTop < 0)
			this.menuUl.style.top = 0;
		else
			this.menuUl.style.top = '-'+this.menuUl.offsetHeight+'px';
		//alert(this.menuUl.style.top);
	}
}

cSlideshow.prototype.mouseMove = function()
{
	clearTimeout(this.timerMenu);
	if (this.clicked == false)
	{
		this.toggleMenu(true);
		if (this.menuVisible == false)
			this.timerMenu = setTimeout("slideshow.toggleMenu();",2000);
	}
}

cSlideshow.prototype.mouseOverMenu = function()
{
	this.menuVisible = true;
	clearTimeout(this.timerMenu);
	//this.toggleMenu(true);
}

cSlideshow.prototype.mouseOutMenu = function()
{
	this.menuVisible = false;
	clearTimeout(this.timerMenu);
	//this.toggleMenu(true);
	if (this.clicked == false)
		this.timerMenu = setTimeout("slideshow.toggleMenu();",2000);
}

cSlideshow.prototype.clickMenu = function()
{
	//alert('toggle ' + this.menuVisible);
	clearTimeout(this.timerMenu);
	if (this.menuVisible == false)
	{
		if (this.clicked == false)
			slideshow.toggleMenu(true);
		else
			slideshow.toggleMenu(false);
		this.clicked = (this.clicked == false ? true : false);
	}
}

cSlideshow.prototype.setInterval = function(form)
{
	if (isInt(form.childNodes[0].value) && form.childNodes[0].value >= 1)
		this.interval = form.childNodes[0].value * 1000;
	else
		form.childNodes[0].value = this.interval / 1000;
	
}

cSlideshow.prototype.loadImage = function(url)
{
	this.li[this.currLi].childNodes[0].src = '';
	this.li[this.currLi].childNodes[0].style.height = '';
	this.li[this.currLi].childNodes[0].style.width = '';
	this.li[this.currLi].childNodes[0].src = this.path + url;
}

cSlideshow.prototype.onImageLoaded = function(img)
{
	
	if (this.running == true)
		this.timer = setTimeout("slideshow.showImage();",this.interval);
	else
		this.resize(this.li[this.currLi].childNodes[0]);
}

cSlideshow.prototype.showImage = function()
{

	this.li[this.currLi].style.display = 'none';
	
	this.switchLi();
	this.li[this.currLi].style.display = '';

	this.resize(this.li[this.currLi].childNodes[0]);

	
	this.switchLi();
	
	this.currImg++;
	this.info.innerHTML = 'Bild '+this.currImg+' av ' + this.imgArr.length + ' ('+ this.timeLeft() + ')';
	if (this.currImg > this.imgArr.length-1)
		setTimeout("slideshow.close()", this.interval);
		//this.currImg = 0;
	
	this.loadImage(this.imgArr[this.currImg]);
	this.switchLi();
}

cSlideshow.prototype.resize = function(img)
{
	var winHeight;
	var winWidth;
	if (window.innerHeight)
	{
		 winHeight = window.innerHeight;
		 winWidth = window.innerWidth;
	}
	else
	{
		winHeight = document.documentElement.clientHeight;
		winWidth = document.documentElement.clientWidth;
	}

	winHeight -= (/*this.menuUl.offsetHeight+*/8);
	winWidth -= 8;
	
	//alert(img.offsetHeight+' > '+winHeight);
	//img.style.border = '2px solid green';
	if (img.offsetHeight > winHeight)
		img.style.height = winHeight+'px';
	if (img.offsetWidth > winWidth)
	{
		img.style.height = '';
		img.style.width = winWidth+'px';
	}
} 

cSlideshow.prototype.switchLi = function()
{
	if (this.currLi == 0)
		this.currLi = 1;
	else
		this.currLi = 0;
}

cSlideshow.prototype.timeLeft = function()
{
	var sec = (this.interval / 1000) * (this.imgArr.length - (this.currImg-1));
	
	if (sec > 60)
	{
		var min = Math.floor(sec / 60);
		var s = sec % 60;
		return (min > 9 ? min : '0'+min) +':'+(s > 9 ? s : '0'+s)+' kvar';
	}
	else
		return '00:'+(sec > 9 ? sec : '0'+sec) + ' kvar';
}

function isInt(val)
{
	return val.toString() == parseInt(val).toString();
}


