dojo.provide("utils.originalimage");

dojo.declare("utils.originalimage", null, {
	arrHandler: [],
	domNode: null,
	domDialog: null,
	strHref: '',
	strTitle: '',
	intWidth: 0,
	intHeight: 0,
	nodeID: 0,

	constructor: function(params, srcNodeRef) {	    
		dojo.require("dijit.Dialog");
		this.arrHandler = [];
		this.domNode = srcNodeRef;
		this.strHref = this.domNode.href;
		this.strTitle = params.strTitle;
		this.intWidth = params.intWidth;
		this.intHeight = params.intHeight;
		this.nodeID = params.nodeID;

		var handler = dojo.connect(this.domNode, "click", this, this.onClick);
		this.arrHandler.push(handler);
	},

	destroy: function() {
		var h;
		while( ( h = this.arrHandler.pop() ) != undefined )
			dojo.disconnect(h);
	},

	calculateSize: function() {
		// On autorise l'image à prendre 80% du viewport
		var maxWidth = dijit.getViewport().w * 0.8;
		var maxHeight = dijit.getViewport().h * 0.8;

		// Redimensionnement si nécessaire avec conservation du ratio largeur/hauteur
		if (this.intWidth > maxWidth || this.intHeight > maxHeight) {
			if (this.intWidth / maxWidth > this.intHeight / maxHeight) {
				this.intHeight = this.intHeight * maxWidth / this.intWidth;
				this.intWidth = maxWidth;
			} else {
				this.intWidth = this.intWidth * maxHeight / this.intHeight;
				this.intHeight = maxHeight;
			}
		}

		// Prise en compte des marges
		this.intWidth += 16;
		this.intHeight += 16;
	},

	onClick: function(e) {
		if (this.domDialog == null) {
			this.calculateSize();
			this.domDialog = new dijit.Dialog({draggable: false, title: this.strTitle, content: '<img id="jsImg' + this.nodeID + '" frameborder="0" style="width: '+this.intWidth+'px; height: '+this.intHeight+'px" src="'+this.strHref+'"/>', 'class': 'soria'});
		}
		
		var img = dojo.byId( 'jsImg' + this.nodeID );
		
		dojo.style( img.parentNode, { backgroundColor: 'black', textAlign: 'center' } );

		this.domDialog.show();
		dojo.stopEvent(e);
	}
});

