// Copyright (C) 2008 Stephane Lavergne <http://www.imars.com/>
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>.

// Animation facilitation
imars.anim = {

	// 50ms is 20fps
	tick: 50,

	// Scrolling

	scrollTick: function (id, stepX, stepY, i, finalX, finalY) {
		var e = document.getElementById(id);
		if (e) {
			if (stepX != 0) e.style.left = (parseInt(e.style.left) + stepX) + 'px';
			if (stepY != 0) e.style.top = (parseInt(e.style.top) + stepY) + 'px';
			if (i > 1) setTimeout('imars.anim.scrollTick("'+id+'", '+stepX+', '+stepY+', '+(i-1)+', '+finalX+', '+finalY+')', imars.anim.tick);
			else if ((finalX != 0) || (finalY != 0))
				setTimeout('imars.anim.scrollTick("'+id+'", '+finalX+', '+finalY+', 0, 0, 0)', imars.anim.tick);
		};
	},

	scroll: function (id, dispX, dispY, secs) {
		// WARNING: Due to the nature of .style, left/top must be specified inline.
		// A work-around could be to specify it in JavaScript at startup.
		var e = document.getElementById ? document.getElementById(id) : null;
		if (e && e.style && (secs > 0)) {
			var i = Math.ceil(secs * 1000 / imars.anim.tick);
			var stepX;
			if (dispX > 0) stepX = Math.floor(dispX / i);
			else stepX = Math.ceil(dispX / i);
			var finalX = dispX - (stepX * i);
			var stepY;
			if (dispY > 0) stepY = Math.floor(dispY / i);
			else stepY = Math.ceil(dispY / i);
			var finalY = dispY - (stepY * i);
			imars.anim.scrollTick(id, stepX, stepY, i, finalX, finalY);
		};
		return false;
	},


	// Fading

	setOpacity: function (style, percent) {
		var f = percent / 100;
		style.opacity = f;
		style.MozOpacity = f;
		style.KhtmlOpacity = f;
		style.filter = "alpha(opacity=" + percent + ")";
		return false;
	},

	setOpacityById: function (id, percent) {
		var e = document.getElementById ? document.getElementById(id) : null;
		if (e && e.style) {
			imars.anim.setOpacity(e.style, percent);
		};
		return false;
	},

	fadeTick: function (id, stepO, i, finalO) {
		var e = document.getElementById(id);
		if (e) {
			var nowO = e.style.opacity;
			if (!nowO) nowO = 100;
			else nowO *= 100;
			nowO += stepO;
			imars.anim.setOpacity(e.style, nowO);

			// FIXME: What's this doing here? Isn't it a bugfix for MSIE for
			//        imars.anim.thumbs?
			var x = document.getElementById('zoom2');
			imars.anim.setOpacity(x.style, 100);
			x.zindex = 3;

			if (i > 1) setTimeout('imars.anim.fadeTick("'+id+'", '+stepO+', '+(i-1)+', '+finalO+')', imars.anim.tick);
			else if (finalO != 0) setTimeout('imars.anim.fadeTick("'+id+'", '+finalO+', 0, 0)', imars.anim.tick);
		};
	},

	fade: function (id, opacity, secs) {
		var e = document.getElementById ? document.getElementById(id) : null;
		if (e && e.style && (secs > 0)) {
			var i = Math.ceil(secs * 1000 / imars.anim.tick);
			var nowO = e.style.opacity;
			if (isNaN(nowO)) nowO = 100;
			else nowO *= 100;
			var dispO = opacity - nowO;
			var stepO;
			if (dispO > 0) stepO = Math.floor(dispO / i);
			else stepO = Math.ceil(dispO / i);
			var finalO = dispO - (stepO * i);
			imars.anim.fadeTick(id, stepO, i, finalO);
		};
		return false;
	}

};
