/* $Id: module-img-viewer-class-mosaic.js 16503 2009-10-01 08:48:43Z jcwiklinski $ */
/**
Copyright (C) 2003-2009 AJLSM, Anaphore
Voir le fichier LICENCE
**/
/* Ce fichier de configuration fait partie de la distribution standard
de Pleade. Vous pouvez le modifier à votre guise. */
/**
	Classe PivMosaic

	Cette classe représente une mosaïque, soit le découpage d'une image en
	différentes tuiles.
*/

// Création de la classe
var PivMosaic = Class.create();		// Utilitaire de la librairie Prototype
PivMosaic.prototype = {
	initialize: function(config, set) {
		// On conserve le set
		this.set = set;
		// On conserve la version qui sert d'aperçu
		this.overviewVersion = set.getDefaultVersion();
		// On conserve la version qui correspond à l'original
		this.originalVersion = set.getOriginalVersion();
		// On conserve les dimensions des tuiles
		this.tileWidth = config.width;
		this.tileHeight = config.height;
		// On indique que l'initialisation n'a pas été faite
		this.initialized = false;
	},

	/**
	*	Lors d'un premier appel à cette mosaïque, on calcul les informations nécessaires.
	*/
	init: function() {
		// Si déjà fait, rien à faire
		if (this.initialized) return;
		// Nombre de tuiles en horizontal et en vertical
		var origW = this.originalVersion.getWidth();
		this.nbHTiles = parseInt(origW / this.tileWidth);
		if ( origW % this.tileWidth != 0 ) this.nbHTiles++;
		var origH = this.originalVersion.getHeight();
		this.nbVTiles = parseInt(origH / this.tileHeight);
		if ( origH % this.tileHeight != 0 ) this.nbVTiles++;

		// On boucle sur les tuiles pour les créer et les conserver
		this.tiles = new Array();
		for (i=1; i<=this.nbHTiles; i++) {
			this.tiles[i] = new Array();
			for (j=1; j<=this.nbVTiles; j++) {
				this.tiles[i][j] = new PivMosaicTile(this.set, this.originalVersion.getMimeType(), i, j, (i-1) * this.tileWidth + 1, (j-1) * this.tileHeight + 1, Math.min(this.tileWidth, (origW - (i-1) * this.tileWidth + 1)), Math.min(this.tileHeight, (origH - (j-1) * this.tileHeight + 1)));
			}
		}
		// On indique que l'initialisation a été faite
		this.initialized = true;
	},

	/**
	*	Ajoute des div à un élément HTML pour représenter la grille
	*/
	drawGrid: function(el, ratio) {
		this.init();
		for (i=1; i<=this.nbHTiles; i++) {
			for (j=1; j<=this.nbVTiles; j++) {
				var t = this.tiles[i][j];
				var h = t.getHTML(ratio);
				el.appendChild(h);
			}
		}
	},

	/**
	*	Retourne la tuile au-dessus d'une tuile passée en paramètre.
	*/
	above: function(t) {
		if ( t.j > 1 ) return this.tiles[t.i][t.j-1];
		else return undefined;
	},

	/**
	*	Retourne la tuile à gauche d'une tuile passée en paramètre.
	*/
	left: function(t) {
		if ( t.i > 1 ) return this.tiles[t.i - 1][t.j];
		else return undefined;
	},

	/**
	*	Retourne la tuile en-dessous d'une tuile passée en paramètre.
	*/
	below: function(t) {
		if ( t.j < this.nbVTiles ) return this.tiles[t.i][t.j+1];
		else return undefined;
	},

	/**
	*	Retourne la tuile à droite d'une tuile passée en paramètre.
	*/
	right: function(t) {
		if ( t.i < this.nbHTiles ) return this.tiles[t.i + 1][t.j];
		else return undefined;
	},

	identify: function() {
		return "Classe PivMosaic";
	}
}

var PivMosaicTile = Class.create();
PivMosaicTile.prototype = {
	initialize: function(set, mimetype, i, j, x, y, w, h) {
		this.set = set;
		this.mimetype = mimetype;
		this.i = i;
		this.j = j;
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	},

	getSrc: function() {
		var ret = this.set.getOriginalVersion().info.src;
		if ( ret.indexOf("?") > -1 ) ret += "&";
		else ret += "?";
		return ret + "x=" + this.x + "&y=" + this.y + "&w=" + this.w + "&h=" + this.h;
	},
	getName: function() {
		var ret = this.set.getOriginalVersion().info.name;
		return ret;
	},

	getUrl: function() {
		return this.set.baseUrl + "/" + this.getSrc();
	},

	getHTML: function(ratio) {
		var div = window.document.createElement("div");
		div = $(div);
		div.addClassName("piv-mosaic-grid");
		div.setStyle({position: "absolute", left: this.x * ratio + "px", top: this.y * ratio + "px", width: this.w * ratio + "px", height: this.h * ratio + "px"});
		var handler = this.handleClick(this);
		div.observe("click", handler);
		return div;
	},

	getVersion: function() {
		if (this.version) return this.version;
		var config = {
			src: this.getSrc(),
			name: this.getName(),
			width: this.w,
			height: this.h,
			label: "Tuile " + i + "," + j,	// TODO: i18n
			role: "tile",
			tile: this,
			mimetype: this.mimetype
		};
		this.version = new PivImageVersion(this.set.baseUrl, config);
		return this.version;
	},

	handleClick: function(tile) {
		return function() {
			// On appelle le chargement de cette image
			_pivViewer.gotoVersion(tile.set, tile.getVersion());
		}
	},

	identify: function() {
		return "Classe PivMosaicTile";
	}
}
