// -*- mode: javascript -*-

var portadita = {

	FLIP_GROW_FACTOR: 400, // This in hundredths (123 is 1.23)
	FLIP_SHRINK_FACTOR: 25, // same...
	SCALE_GROW_FACTOR: 110,
	SCALE_SHRINK_FACTOR: 90,
	SCALE_SPEED: 50,
	SCALE_MIN: 150,
	SCALE_MAX: 250,
	BASE: "",

	PLANILLA_DATA: [
		{pdf: "portada", title: "portada", label: "Portada", thumb: "portadita" },
		{pdf: "contraportada", title: "contraportada", label: "Contraportada", thumb: "contraportadita" },
		{pdf: "enmedio", title: "Jornada de enmedio", label: "La Jornada<br /><i>de enmedio</i>", thumb: "enmedito" }
	],

	flip: function (img, width, dir) {
		width *= (dir / 100);
		if (width < 2) {
			img.cur = img.next % this.PLANILLA_DATA.length;
			if (img.cur < 0)
				img.cur += this.PLANILLA_DATA.length;
			img.next = img.cur;

			data = this.PLANILLA_DATA[img.cur];
			img.src = "/v7.0/imagenes/pixel.gif";
			dodo.delayRun (function () {
				if (!window.CIMPRESADAY)
					addday = '';
				else
					addday = window.CIMPRESADAY;

				img.src = portadita.BASE + addday + "planitas/" + data.thumb + ".jpg";
			}, this.SCALE_SPEED * 2);
			img.alt = img.title = "Ver la " + data.title + " en PDF";
			img.parentNode.href = portadita.BASE + data.pdf + ".pdf";
			this.planillaWidget.labelNode.innerHTML = data.label;
				
			dir = this.FLIP_GROW_FACTOR;
		} else if (width >= this.SCALE_MIN) {
			width = this.SCALE_MIN;
			dir = 0;
		}

		var delta = (this.SCALE_MIN - width) / 2;

		img.width = width;
		img.style.width = width + "px";
		img.style.left = (delta + img.left) + "px";
		
		if (dir) {
			dodo.delayRun (function () {
				portadita.flip (img, width, dir);
			}, this.SCALE_SPEED);
		}
	},

	scale: function (img, width, dir) {
		if (img.state && img.state != dir)
			return;

		width *= dir / 100;
		if (width <= this.SCALE_MIN) {
			width = this.SCALE_MIN;
			img.state = dir = 0;
		} else if (width >= this.SCALE_MAX) {
			width = this.SCALE_MAX;
			img.state = dir = 0;
		}

		var delta = (this.SCALE_MIN - width) / 2;

		img.width = width;
		img.style.width = width + "px";
		img.style.height = (width * img.ratio) + "px";
		img.style.left = (delta + img.left) + "px";
		img.style.top = (img.ratio * delta * 2) + "px";

		if (dir) {
			img.scaleId = dodo.delayRun (function () {
				portadita.scale (img, width, dir);
			}, this.SCALE_SPEED);
		}
	},

	onMouseOver: function () {
		this.state = portadita.SCALE_GROW_FACTOR;
		this.parentNode.parentNode.style.zIndex = 400;
		portadita.scale (this, this.width, portadita.SCALE_GROW_FACTOR);
	},

	onMouseOut: function () {
		this.state = portadita.SCALE_SHRINK_FACTOR;
		this.parentNode.parentNode.style.zIndex = 200;
		if (this.scaleId)
			dodo.clearTimeout (this.scaleId);
		portadita.scale (this, this.width, portadita.SCALE_SHRINK_FACTOR);
	},

	onMouseMove: function () {
		if (this.state != portadita.SCALE_SHRINK_FACTOR) {
			this.state = portadita.SCALE_GROW_FACTOR;
			portadita.scale (this, this.width, portadita.SCALE_GROW_FACTOR);
		}
	},

	init: function () {
		var img = document.getElementById ("portadita-img");
		img.ratio = img.height / img.width;
		img.left = img.offsetLeft;
		img.state = 0;
		img.onmouseover = this.onMouseOver;
		img.onmouseout = this.onMouseOut;
		
		img.cur = img.next = 0;

		img.parentNode.target = "_blank";

		var widget = this.planillaWidget = document.getElementById ("portadita-widget");
		widget.arrowLeft = widget.childNodes[0];
		widget.labelNode = widget.childNodes[2];
		widget.arrowRight = widget.childNodes[1];
		widget.arrowLeft.onclick = function () { img.next --; portadita.flip (img, portadita.SCALE_MIN, portadita.FLIP_SHRINK_FACTOR); };
		widget.arrowRight.onclick = function () { img.next ++; portadita.flip (img, portadita.SCALE_MIN, portadita.FLIP_SHRINK_FACTOR); };
		
		if (window.PORTADITA_BASE)
			portadita.BASE = window.PORTADITA_BASE;
	}
};

dodo.onLoadPush (function () { portadita.init (); });

