/**
 * PhotoArea.writeSlideshow - generate slideshow page
 * @method
 * @param {XMLNode} xml - XML data
 */
PhotoArea.prototype.writeSlideshow = function(xml)
{
	if(!this.ssSpeed) this.ssSpeed = 5000;

	this.prevPhoto = new Element('div');
	this.nextPhoto = new Element('div');
	this.forwards = true;

	var photoNode = xml.getElementsByTagName('photos')[0];
	results = parseInt(photoNode.getAttribute('results'));
	this.photoIndex = parseInt(getNodeValue(photoNode.getElementsByTagName('index')[0]));

	this.contents.removeChildren();
	var slideshow = this.contents.appendChild(new Element('div'));
	slideshow.className = 'ss_container';

	var thisPhoto;
	var photos = photoNode.getElementsByTagName('photo');
	for(var i = 0, il = photos.length; i < il; i++)
	{
		var hash = photos[i].getAttribute('hash');
		var filetype = photos[i].getAttribute('type');
		var rot = photos[i].getAttribute('orientation');
		if(photos[i].getAttribute('selected'))
		{
			var resultsBox = new Element('div');
			resultsBox.className = 'results';
			resultsBox.innerHTML = language('photo') + ' ' + this.photoIndex + ' ' + language('outof') + ' ' + results;
			slideshow.appendChild(resultsBox);

			closeBox = new TextButton(this.ssClose.bind(this), 'close');
			closeBox.className = 'close';
			slideshow.appendChild(closeBox);
			thisPhoto = new Photobox();
			thisPhoto.init(this.layout, photos[i]);
			this.layout.ssHash = thisPhoto.hash;
			slideshow.appendChild(thisPhoto.getButtons({noSlideshowButton: true, noViewButton: true}));

			var image = new ImageButton(
				this.ssNext.bind(this), thisPhoto.getViewSrc(), '');

			if(this.playing)
				Event.observe(image, 'load', (function() {
					this.ssTimeout = setTimeout((function() {
						this.ssPlay(this.forwards, false, false);
					}).bind(this), this.ssSpeed);
				}).bind(this));

			slideshow.appendChild(image);
		}
		else
		{
			if(i)
			{
				this.nextPhoto.photo = new ImageButton(this.layout.loadData.bind(this.layout, {hash: hash}),
					settings.thumbs_directory + '/' + filename(hash) + '.' + filetype + '?rot=' + rot, 'Next', 'Next');
				this.nextPhoto.photo._hash = hash;
				this.nextPhoto.appendChild(this.nextPhoto.photo);
			}
			else
			{
				this.prevPhoto.photo = new ImageButton(this.layout.loadData.bind(this.layout, {hash: hash}),
					settings.thumbs_directory + '/' + filename(hash) + '.' + filetype + '?rot=' + rot, 'Previous', 'Previous');
				this.prevPhoto.photo._hash = hash;
				this.prevPhoto.appendChild(this.prevPhoto.photo);
			}
		}
	}
	var playControls = new Element('div');
	playControls.className = 'controls';
	playControls.appendChild(new TextButton(this.ssPlay.bind(this, false, true), 'play backwards')); //lang
	playControls.appendChild($T(' | '));
	playControls.appendChild(new TextButton(this.ssStop.bind(this), 'stop')); //lang
	playControls.appendChild($T(' | '));
	playControls.appendChild(new TextButton(this.ssPlay.bind(this, true, true), 'play forwards')); //lang

	var speedControls = new Element('div');
	speedControls.className = 'controls';
	var speedInput = new Element('input');
	speedInput.type = 'text';
	speedInput.value = this.ssSpeed / 1000;
	speedInput.size = 2;
	var speedButton = new Element('input');
	speedButton.type = 'button';
	speedButton.value = 'set delay'; //lang
	speedButton.clickHandler = (function() {
		this.ssSetSpeed(parseInt(speedInput.value) * 1000);
		speedInput.value = this.ssSpeed / 1000;
	}).bind(this);
	speedControls.appendChild(speedInput);
	speedControls.appendChild($T(' seconds '));//lang
	speedControls.appendChild(speedButton);


	metaBar = thisPhoto.getMetaBar(xml);
	metaBar.className = 'metabar metadata';

	this.nextPhoto.className = 'next';
	this.prevPhoto.className = 'prev';
	slideshow.appendChild(this.prevPhoto);
	slideshow.appendChild(this.nextPhoto);
	slideshow.appendChild(thisPhoto.getButtons({noSlideshowButton: true, noViewButton: true}));
	slideshow.appendChild(playControls);
	slideshow.appendChild(speedControls);
	slideshow.appendChild(metaBar);
}

/**
 * PhotoArea.ssSetSpeed - set slideshow speed
 * @method
 * @param {int} speed - delay in milliseconds
 */
PhotoArea.prototype.ssSetSpeed = function(speed)
{
	this.ssSpeed = speed;
}
/**
 * PhotoArea.ssPlay - start playing the slideshow
 * @method
 * @param {boolean} forwards - flag to play forwards or backwards
 * @param {boolean} start - indicates a change picture call, rather than a setTimeout call
 */
PhotoArea.prototype.ssPlay = function(forwards, start)
{
	clearTimeout(this.ssTimeout);
	if(!start)
	{
		if(forwards && this.nextPhoto.photo)
		{
			this.layout.loadData({hash: this.nextPhoto.photo._hash})
			this.forwards = forwards;
			this.playing = true;
		}

		if(!forwards && this.prevPhoto.photo)
		{
			this.layout.loadData({hash: this.prevPhoto.photo._hash})
			this.forwards = forwards;
			this.playing = true;
		}
	}
	else
	{
		this.ssTimeout = setTimeout(this.ssPlay.bind(this, forwards, false), this.ssSpeed);
	}
}

PhotoArea.prototype.ssNext = function()
{
	if(this.forwards && this.nextPhoto.photo)
	{
		this.layout.loadData({hash: this.nextPhoto.photo._hash})
	}

	if(!this.forwards && this.prevPhoto.photo)
	{
		this.layout.loadData({hash: this.prevPhoto.photo._hash})
	}
}

/**
 * PhotoArea.ssStop - stop playing slideshow
 * @method
 */
PhotoArea.prototype.ssStop = function()
{
	clearTimeout(this.ssTimeout);
	this.playing = false;
}
/**
 * PhotoArea.ssMove - browse slideshow using keystrokes
 * @method
 * @param {int} event - event
 */
PhotoArea.prototype.ssMove = function(event)
{
	var kc = event.keyCode;
	if(this.layout.viewMode == 'slideshow')
	{
		if((kc == Event.KEY_LEFT || kc == Event.KEY_UP) && this.prevPhoto.photo)
			this.layout.loadData({hash: this.prevPhoto.photo._hash});
		if((kc == Event.KEY_RIGHT || kc == Event.KEY_DOWN) && this.nextPhoto.photo)
			this.layout.loadData({hash: this.nextPhoto.photo._hash});
	}
}

PhotoArea.prototype.ssClose = function()
{
	this.ssStop();
	this.layout.setViewMode(this.layout.lastViewMode ? this.layout.lastViewMode : 'list');

	if(this.layout.viewMode == 'list')
		this.layout.page = Math.floor((this.photoIndex - 1) / settings.photos_per_page) + 1;

	this.layout.datePage(this.layout.page, this.layout.lastYear, this.layout.lastMonth, this.layout.lastDay, true);
}