/*  ImageAnnotation - The JavaScript image annotation backing bean API
 *  (c) 2007 Guillaume Texier
 *
 *  ImageAnnotation is a class which talks with the backing bean.
/*--------------------------------------------------------------------------*/

// =======================================

function ImageAnnotation(containerId,notesArray)
{
//this.ref, this.schemaName, this.fieldName

	//DEBUG('ImageAnnotation() {');

	// Constants
	this.NOTE_DEFAULT_TEXT		= '';
	this.NOTE_DEFAULT_LEFT		= 10;
	this.NOTE_DEFAULT_TOP		= 10;
	this.NOTE_DEFAULT_WIDTH		= 50;
	this.NOTE_DEFAULT_HEIGHT	= 50;
	this.NOTE_MAXTOP   = document.getElementById('mainIMG').height;
	this.NOTE_MAXLEFT  =  document.getElementById('mainIMG').width;

// Properties
	this.notes			= null;

	// Initialization

	// Creates the Photo Note Container
	this.notes = new PhotoNoteContainer(document.getElementById(containerId));

	// Action
	//DEBUG('typeof(notesArray)='+typeof(notesArray))
	//DEBUG('instanceof Array='+(notesArray instanceof Array))

	// Creates and add notes objects to notes container if a notesArray is passed
	if (notesArray instanceof Array) {
		for (i=0;i<notesArray.length;i++) {
			//DEBUG('note:'+i);
			//DEBUG(notesArray[i].toString());
	                // create a note object, and add them to the notes container
	                var newNote		= new PhotoNote(notesArray[i]);
/*
	                with (notesArray[i]) {
	                	//var newNote		= new PhotoNote(text,id,new PhotoNoteRect(left,top,width,height));
	                }
*/
	                // Add events handlers
	                newNote.onsave		= this.save;
	                newNote.ondelete	= this.remove;
	                // Add note to notes container
	                if (!this.notes.AddNote(newNote)) break;
		}
	}

	//DEBUG('ImageAnnotation }');
};


// =======================================

ImageAnnotation.prototype.addNote = function(maxleft,maxtop)
{
	//DEBUG('addNote() {');
Spry.Utils.removeEventListener("mainIMG", "mouseover", hoverhandle, false);
	// Add note if container is not in readonly mode
	if (!this.notes.isReadonly()) {
		// Creates the note object
			
		//var newNote		= new PhotoNote(this.NOTE_DEFAULT_TEXT,-1,new PhotoNoteRect(this.NOTE_DEFAULT_LEFT,this.NOTE_DEFAULT_TOP,this.NOTE_DEFAULT_WIDTH,this.NOTE_DEFAULT_HEIGHT,this.NOTE_MAXTOP,this.NOTE_MAXLEFT));
var notesArray	= [
			{'id':'-1','rect':{'left':10,'top':10,'width':50,'height':50},'title':'New note','text':'new note','maxleft': this.NOTE_MAXLEFT, 'maxtop': this.NOTE_MAXTOP }
			
			 ];
		var newNote		= new PhotoNote(notesArray[0]);
				   
                // Add events handlers
                newNote.onsave		= this.save;
                newNote.ondelete	= this.remove;
		if (this.notes.AddNote(newNote)) {
			newNote.Select();
		} else {
			newNote	= null;
		}
	}

	//DEBUG('addNote }');
}

// =======================================

ImageAnnotation.prototype.setReadonly = function(readonly)
{
	//DEBUG('setReadonly() {');

	this.notes.setReadonly(readonly);

	//DEBUG('setReadonly }');
}

// =======================================

ImageAnnotation.prototype.toggleReadonly = function()
{
	//DEBUG('toggleReadonly() {');

	this.notes.toggleReadonly();

	//DEBUG('toggleReadonly }');
}

// =======================================

ImageAnnotation.prototype.save = function(note)
{
	//DEBUG('SAVE');
	//DEBUG(note.toXML());
	//Seam.Component.getInstance("ImageAnnotationActions").save(this.ref, this.schemaName, this.fieldName, note.id,
	//  note.rect.left, note.rect.top, note.rect.width, note.rect.height, note.text);
//	//DEBUG("SAVE : ['+id+'] ('+x0+','+y0+','+width+','+height+')')
//	//DEBUG("TEXT : '+text)

	if (this.id != -1)
	{
	  UpdateNote(this.rect.left,this.rect.top,this.rect.height,this.rect.width,this.text,this.id,querystr.imgID);
	}
	else
	{
	InsertNote(this.rect.left,this.rect.top,this.rect.height,this.rect.width,this.text,querystr.imgID);
	}
//alert(this.id);
	return 1;
}

// =======================================

ImageAnnotation.prototype.remove = function(note)
{
	//DEBUG('REMOVE');
	//DEBUG(note.toString());
//	Seam.Component.getInstance("ImageAnnotationActions").remove(this.ref, this.schemaName, this.fieldName, note.id);

//	//DEBUG("REMOVE : ['+id+']')
   DeleteNote(this.id,querystr.imgID);
	return true;

}

// =======================================

ImageAnnotation.prototype.showAllNotes = function()
{
	//DEBUG('showAllNotes');
	this.notes.ShowAllNotes();
}

// =======================================

ImageAnnotation.prototype.hideAllNotes = function()
{
	//DEBUG('hideAllNotes');
	this.notes.HideAllNotes();
}

// =======================================

ImageAnnotation.prototype.isReadonly = function()
{
	//DEBUG('isReadOnly');
	return this.notes.isReadonly();
}

// =======================================
