// Animator Class

PanelAnimator = function(ele, curX, curY, dstX, dstY, opts) {
	this.element = ele;
	this.curX = curX;
	this.curY = curY;
	this.dstX = dstX;
	this.dstY = dstY;
	this.fps = 60;
	this.duration = 400;
	this.transition = PanelAnimator.defaultTransition;
	this.startTime = 0;
	this.timerID = 0;
	this.finish = null;
	var self = this;

	PanelAnimator.setOptions(this, opts, true);
	this.intervalFunc = function() { self.step(); };
	this.interval = 1000/this.fps;
};

PanelAnimator.setOptions = function(obj, optionsObj, ignoreUndefinedProps) {
	if (!optionsObj)
		return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};

PanelAnimator.defaultTransition = function(time, begin, finish, duration) { 
	time /= duration; 
	return begin + ((2 - time) * time * finish); 
};

PanelAnimator.prototype.start = function() {
	this.stop();
	if (this.setup) {
		this.setup();
	}
	this.startTime = (new Date()).getTime();
	this.timerID = setTimeout(this.intervalFunc, this.interval);
};

PanelAnimator.prototype.stop = function() {
	if (this.timerID)
		clearTimeout(this.timerID);
	this.timerID = 0;
};

PanelAnimator.prototype.step = function() {
	var elapsedTime = (new Date()).getTime() - this.startTime;
	var done = elapsedTime >= this.duration;
	var x, y;

	if (done) {
		x = this.curX = this.dstX;
		y = this.curY = this.dstY;
	} else {
		x = parseInt(this.transition(elapsedTime, this.curX, this.dstX - this.curX, this.duration));
		y = parseInt(this.transition(elapsedTime, this.curY, this.dstY - this.curY, this.duration));
	}

	this.element.style.left = x + "px";
	this.element.style.top = y + "px";

	if (!done) {
		this.timerID = setTimeout(this.intervalFunc, this.interval);
	} else {
		if (this.finish) {
			this.finish();
		}
	}
};

// end Animator Class

function SetBigImage(el, imgFile, imgTitle, width, height, imgFileBig) {
	var image = document.getElementById("bigImage");
	if ( !image ) return false;
	if ( el ) {
		unsetSelected('photos', 'A');
		setSelected(el);
		size_inc_w = 0;
		size_inc_h = 0;
	} else {
		image.style.width = width + 'px';
		image.style.height = height + 'px';
		size_inc_w = 0;
		size_inc_h = 0;
	}
	source_dims = Spry.Effect.getDimensions(image);

	if ( imgFileBig ) {
		var image_html = '<a href="javascript:PopupImage(\'' + imgFileBig + '\', \'' + imgTitle + '\');"><img src="' + imgFile + '" width="' + width + '" height="' + height + '" alt="' + imgTitle + '" title="' + imgTitle + '" /></a>';
	} else {
		var image_html = '<img src="' + imgFile + '" width="' + width + '" height="' + height + '" alt="' + imgTitle + '" title="' + imgTitle + '" />';
	}

	se = new Spry.Effect.Size(image, source_dims, { width: (width + size_inc_w) , height: (height + size_inc_h), units:"px"}, 
	{
		duration: 400,
		setup: function() {
			image.innerHTML = image_html;
		}
	});
	se.start();
	return false;	
}

function photoSlideCheck() {
	if ( photoSliderPos >= 0 ) {
		document.getElementById('smallControlLeft').style.visibility = 'hidden';
	} else {
		document.getElementById('smallControlLeft').style.visibility = 'visible';
	}

	if ( Math.abs(photoSliderPos) >= photoSliderWidth2 ) {
		document.getElementById('smallControlRight').style.visibility = 'hidden';
	} else {
		document.getElementById('smallControlRight').style.visibility = 'visible';
	}
}

function photoSlideLeft() {
	var slider = document.getElementById("smallImagesSlider");
	if ( !slider || photoSlider ) return false;
	if ( photoSliderPos < 0 ) {
		photoSliderPos_old = photoSliderPos;
		_from = photoSliderPos;
		_to = (photoSliderPos + (photoSliderWidth1 * 3));
		photoSliderPos = photoSliderPos + 10;
		if ( _to > 0 ) {
			_to = 0;
		}
		photoSlider = new PanelAnimator(slider, _from, 0, _to, 0, { 
			finish: function() {
				photoSliderPos = this.dstX;
				photoSlideCheck();
				photoSlider = false;
			}
		} );
		photoSlider.start();
	}
	photoSlideCheck();
}

function photoSlideRight() {
	var slider = document.getElementById("smallImagesSlider");
	if ( !slider || photoSlider ) return false;
	if ( Math.abs(photoSliderPos) < photoSliderWidth2 ) {
		photoSliderPos_old = photoSliderPos;
		_from = photoSliderPos;
		_to = (photoSliderPos - (photoSliderWidth1 * 3));
		if ( _to > photoSliderWidth2 ) {
			_to = photoSliderWidth2;
		}
		photoSliderPos = photoSliderPos - 10;
		photoSlider = new PanelAnimator(slider, _from, 0, _to, 0, { 
			finish: function() {
				photoSliderPos = this.dstX;
				photoSlideCheck();
				photoSlider = false;
			} 
		} );
		photoSlider.start();
	}
	photoSlideCheck();
}
