if (typeof Horton == 'undefined') {
	var Horton = {};
}

if (typeof Horton.widget == 'undefined') {
	Horton.widget = {};
}

(function () {

	var Dom   = YAHOO.util.Dom;
	var Event = YAHOO.util.Event;
	var Anim  = YAHOO.util.Anim;

	Horton.widget.PhotoPager = function(params)
	{
		this.id           = params.id;
		this.width        = params.width;
		this.height       = params.height;
		this.image        = params.image;
		this.frames       = params.frames;
		this.link         = document.getElementById(this.id + '_link');
		this.title        = document.getElementById(this.id + '_frame_title');
		this.currentFrame = 0;
		this.container    = document.getElementById(this.id);

		_init.call(this);
	};

	var proto = Horton.widget.PhotoPager.prototype;

	var _originalImage = null;
	var _currentImage = null;
	var _interval = null;

	Horton.widget.PhotoPager.crossFadePeriod = 1.0; // in seconds
	Horton.widget.PhotoPager.nextText = 'Next Photo';
	Horton.widget.PhotoPager.prevText = 'Previous Photo';
	Horton.widget.PhotoPager.photographerText = 'Photographer: %s';
	Horton.widget.PhotoPager.interval = 4000; // in milliseconds

	var _init = function()
	{
		_originalImage = document.getElementById(this.id + '_image');

		// draw prev link
		var prevLink = document.createElement('a');
		prevLink.appendChild(document.createTextNode(
			Horton.widget.PhotoPager.prevText));

		prevLink.title = Horton.widget.PhotoPager.prevText;
		prevLink.className = 'photo-pager-prev';
		prevLink.href = '#';

		Event.on(prevLink, 'click', function (e) {
			Event.preventDefault(e);
			this.clearInterval();
			this.setInterval();
			this.previousFrame();
		}, this, true);

		this.container.appendChild(prevLink);

		// draw next link
		var nextLink = document.createElement('a');
		nextLink.appendChild(document.createTextNode(
			Horton.widget.PhotoPager.nextText));

		nextLink.title = Horton.widget.PhotoPager.nextText;
		nextLink.className = 'photo-pager-next';
		nextLink.href = '#';

		Event.on(nextLink, 'click', function (e) {
			Event.preventDefault(e);
			this.clearInterval();
			this.setInterval();
			this.nextFrame();
		}, this, true);

		this.container.appendChild(nextLink);
		this.setInterval();
	};

	var _crossFadeFrame = function(frame)
	{
		var lastImage = _currentImage;

		var framePosition = this.width * this.currentFrame;

		var imageSpan = document.createElement('span');

		imageSpan.className = 'photo-pager-image';

		imageSpan.style.position           = 'absolute';
		imageSpan.style.width              = this.width + 'px';
		imageSpan.style.height             = this.height + 'px';
		imageSpan.style.backgroundImage    = 'url(' + this.image + ')';
		imageSpan.style.backgroundRepeat   = 'no-repeat';
		imageSpan.style.backgroundPosition = '-' + framePosition + 'px 0';

		Dom.setStyle(imageSpan, 'opacity', 0);

		_currentImage = imageSpan;

		this.link.appendChild(imageSpan);
		var xy = Dom.getXY(_originalImage);
		Dom.setXY(imageSpan, xy);

		var animation = new Anim(
			imageSpan,
			{ opacity: { to: 1 } },
			Horton.widget.PhotoPager.crossFadePeriod
		);

		_setFrameTitle.call(this, frame.title);
		this.link.href  = frame.uri;
		this.link.title = frame.title;

		animation.onComplete.subscribe(
			function()
			{
				if (lastImage) {
					lastImage.style.display = 'none';
					lastImage.parentNode.removeChild(lastImage);
				}
			},
			this,
			true
		);

		animation.animate();
	};

	var _setFrameTitle = function(title)
	{
		while (this.title.firstChild) {
			this.title.removeChild(this.title.firstChild);
		}

		this.title.appendChild(document.createTextNode(title));
	};

	var _getNextFrame = function()
	{
		this.currentFrame++;

		if (this.currentFrame > this.frames.length - 1) {
			this.currentFrame = 0;
		}

		return this.frames[this.currentFrame];
	};

	var _getPreviousFrame = function()
	{
		this.currentFrame--;

		if (this.currentFrame < 0) {
			this.currentFrame = this.frames.length - 1;
		}

		return this.frames[this.currentFrame];
	};

	proto.previousFrame = function()
	{
		var frame = _getPreviousFrame.call(this);
		_crossFadeFrame.call(this, frame);
	};

	proto.nextFrame = function()
	{
		var frame = _getNextFrame.call(this);
		_crossFadeFrame.call(this, frame);
	};

	proto.setInterval = function()
	{
		// set up next-photo interval
		var that = this;
		_interval = setInterval(function()
			{
				that.nextFrame();
			}, Horton.widget.PhotoPager.interval);
	};

	proto.clearInterval = function()
	{
		if (_interval) {
			clearInterval(_interval);
		}
	};

})();
