/* $Id: module-img-viewer-class-imageversion.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 PivImageVersion.

	Cette classe représente les informations sur une version d'une image.
*/

// Création de la classe
var PivImageVersion = Class.create();		// Utilitaire de la librairie Prototype
PivImageVersion.prototype = {

	initialize: function(baseUrl, config) {
		this.baseUrl = baseUrl;
		if ( config ) {
			this.role = config.role;
			this.info = config;
		}
		this.rotation = 0;
	},

	getRotation: function() {
		return this.rotation;
	},

	reInitRotation: function() {
		this.rotation = 0;
	},

	setRotation: function(angle, add) {
		if ( angle && angle > 0 && angle <= 360 ) {
			this.rotation += angle;
			if( this.rotation > 360 ) {
				this.rotation -= 360;
			// if(add === false) this.rotation += angle;
			// if( this.rotation > 360 ) {
			// 	this.rotation -= 360;
			}
		}
	},

	/**
	* Renvoie 'true' si la rotation est de 90° ou 270° ; 'false' sinon
	*/
	isFlipped: function(){
		return (this.rotation == 90 || this.rotation == 270)?true:false;
	},

	getUrl: function() {
		var prefix = this.baseUrl + "/";
		if ( this.info.src.startsWith('http://')
				|| this.info.src.startsWith('file:/') ) {
			prefix = "";
		}
		var _url = prefix + this.info.src;
		if ( this.rotation > 0 && this.rotation < 360 ) {
			_url +=  ( (_url.indexOf('\?')!=-1 ) ? '&':'?' ) + "r=" + this.rotation
		}
		return _url;
	},

	getNameWithoutQueryString: function() {
		var name = this.getName();
		if ( name.indexOf("?") < 0 ) return name;
		else return name.substring(0, name.indexOf("?"));
	},

	getRole: function() {
		return this.role;
	},

	getWidth: function() {
		if (this.info) return this.info.width;
		else return 0;
	},

	getHeight: function() {
		if (this.info) return this.info.height;
		else return 0;
	},

	getName: function() {
		/*if (this.info) return this.info.src;*/
		if (this.info){
			return (this.info.name) ? this.info.name : this.info.src;
		}
		else return "";
	},

	getLabel: function() {
		if (this.info) return this.info.label;
		else return "";
	},

	/**
	*	Retourne un résumé de la version (étiquette + taille).
	*/
	getSummary: function() {
		var ret = this.getLabel() + " (" + this.info.width + "x" + this.info.height + "px";
		if ( this.fileSize ) {
			var size = "" + (this.fileSize / (1024*1024)).toFixed(2) + "Mo"	// TODO i18n
			ret += ", " + size.gsub(/\./, ',');
		}
		ret += ")";
		return ret;
	},

	getMimeType: function() {
		return this.info.mimetype;
	},

	getTile: function() {
		// Retourne la tuile, ou undefined s'il n'y en a pas
		return this.info.tile;
	},

	/**
	*	Indique la taille (poids) de l'image (sous IE seulement, sinon reçoit undefined)
	*/
	setFileSize: function(s) {
		this.fileSize = s;
	},

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