
function ImageData (path, historyString, scannum, zoom_factor, crop_x, crop_y){
	try{
		this.rotationsData = new RotationsData();
		if (path){
			this.path = path;
			this.readHistory(historyString);
			this.scannum = scannum;
			if ( zoom_factor ){
				this.zoomCommand = { zoomFactor: zoom_factor, cropX: crop_x, cropY: crop_y };
			}
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.readImageParam = function(string){
	try{
		string = string.replace(/\[i\]/,"");
		var imageData = string.split(",");
		this.path = imageData[0];
		this.scannum = imageData[1];
		if ( imageData[3] ){
			this.zoomCommand = {zoomFactor: imageData[3], cropX: imageData[4], cropY: imageData[5]};
		}
		//alert("Image string: "+string+"\nthis.path: "+this.path+"\nthis.scannum: "+this.scannum+(this.zoomCommand?("\nthis.zoomCommand: "+this.zoomCommand.zoomFactor+","+this.zoomCommand.cropX+","+this.zoomCommand.cropY):""));
		//dbg.add("<FONT color='green'><B>Reading Image string</B>: "+string,"<I>this.path</I>: "+this.path,"<I>this.scannum</I>: "+this.scannum,( this.zoomCommand? ("<I>this.zoomCommand</I>: "+this.zoomCommand.zoomFactor+","+this.zoomCommand.cropX+","+this.zoomCommand.cropY+"<BR>" ) : "" )+"</FONT>" );
		if (imageData[2])
			this.readHistory(imageData[2]);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.readImageParam]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.readHistory = function(historyString){
	try{
		historyCommands = historyString.split("|");
		for ( var i = 0; i < historyCommands.length; i++ ){
			if ( historyCommands[i].indexOf("sharpen") != -1 )
				this.setSharpen();
			if ( historyCommands[i].indexOf("contrast") != -1 )
				this.setContrast();
			if ( historyCommands[i].indexOf("brighten") != -1 )
				this.brighten();
			if ( historyCommands[i].indexOf("darken") != -1 )
				this.darken();
			if ( historyCommands[i].indexOf("flip") != -1 )
				this.flipImg(true);
			if ( historyCommands[i].indexOf("flop") != -1 )
				this.flopImg(true);
			if ( historyCommands[i].indexOf("desaturate") != -1 )
				this.desaturate();
			if ( historyCommands[i].indexOf("sepia") != -1 )
				this.sepia();
			if ( historyCommands[i].indexOf("rotat") != -1 )
				this.rotate(Number(historyCommands[i].replace(/\D*(\d+).*/, "$1")));
		}
		//alert("History string: "+historyString+"\nRotations: "+this.rotationsData.getRotationsPISCommand()+"\nSharpen: "+this.getSharpen()+"\nContrast: "+this.getContrast()+"\nBrighten: "+this.getBrighten()+"\nDarken: "+this.getDarken()+ "\nToning: "+this.getToning()+"\n toString: "+this.getHistoryString());
		//dbg.add("<FONT color='green'><B>Reading Image History string</B>: "+historyString,"<I>Rotations</I>: "+this.rotationsData.getRotationsPISCommand(),"<I>Sharpen</I>: "+this.getSharpen(),"<I>Contrast</I>: "+this.getContrast(),"<I>Brighten</I>: "+this.getBrighten(),"<I>Darken</I>: "+this.getDarken(),"<I>Toning</I>: "+this.getToning(),"<I>toString</I>: "+this.getHistoryString()+"<BR></FONT>");
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.readHistory]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getHistoryString = function(){
	try{
		var imagePISHistoryString = new PIS();
		imagePISHistoryString.addPISCommand( this.rotationsData.getRotationsPISCommand() );
		if ( this.getSharpen() )
			imagePISHistoryString.sharpen();
		if ( this.getContrast() )
			imagePISHistoryString.contrast();
		if ( this.getBrighten() )
			imagePISHistoryString.brighten(this.getBrighten());
		if ( this.getDarken() )
			imagePISHistoryString.darken(this.getDarken());
		if ( this.getToning() )
			imagePISHistoryString.tone(this.getToning());
		return imagePISHistoryString.getPISCommand(true);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getHistoryString]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.toString = function(){
	try{
		var string = "[i]";
		string += this.path;
		string += "," + this.scannum;
		string += "," + this.getHistoryString();
		string += "," + ( this.zoomCommand ? this.zoomCommand.zoomFactor : "" );
		string += "," + ( this.zoomCommand ? this.zoomCommand.cropX : "" );
		string += "," + ( this.zoomCommand ? this.zoomCommand.cropY : "" );
		return string;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.toString]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getCropX = function(){
	try{
		if ( this.zoomCommand )
			return this.zoomCommand.cropX;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getCropX]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getCropY = function(){
	try{
		if ( this.zoomCommand )
			return this.zoomCommand.cropY;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getCropY]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getZoomFactor = function(){
	try{
		if ( this.zoomCommand )
			return this.zoomCommand.zoomFactor;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getZoomFactor]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.isCropped = function(){
	try{
		return Boolean(this.zoomCommand);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.isCropped]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getSharpen = function(){
	try{
		return Boolean(this.sharpen);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getSharpen]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getContrast = function(){
	try{
		return Boolean(this.contrast);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getContrast]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getBrighten = function(){
	try{
		if ( this.brightOrDarken > 0 )
			return this.brightOrDarken;
		return false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getBrighten]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getDarken = function(){
	try{
		if ( this.brightOrDarken < 0 )
			return Math.abs(this.brightOrDarken);
		return false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getDarken]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getToning = function(){
	try{
		return this.toning;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getToning]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getPath = function(){
	try{
		return this.path;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getPath]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.getScannum = function(){
	try{
		return this.scannum;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.getScannum]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.setSharpen = function(){
	try{
		return this.sharpen = true;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.setSharpen]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.removeSharpen = function(){
	try{
		return this.sharpen = false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.removeSharpen]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.setContrast = function(){
	try{
		return this.contrast = true;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.setContrast]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.removeContrast = function(){
	try{
		return this.contrast = false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.removeContrast]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.sepia = function(){
	try{
		this.toning = "sepia";
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.sepia]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.desaturate = function(){
	try{
		this.toning = "desaturate";
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.desaturate]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.rotate = function(rot){
	try{
		this.rotationsData.rotate(rot);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.rotate]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.brighten = function(){
	try{
		if ( this.brightOrDarken )
			this.brightOrDarken++;
		else
			this.brightOrDarken = 1;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.brighten]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.darken = function(){
	try{
		if ( this.brightOrDarken )
			this.brightOrDarken--;
		else
			this.brightOrDarken = -1;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.darken]</B> ' + e + '</FONT>' ); return false; } 
}

ImageData.prototype.resetToning = function(){	// ** TODO ** CHECK THIS
	try{
		this.toning = null;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.resetToning]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.flipImg = function(initializing){
	try{
		this.rotationsData.flip();
		if ( this.isCropped() && !initializing )
			this.zoomCommand.cropY = 1 - this.zoomCommand.cropY;		
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.flipImg]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.flopImg = function(initializing){
	try{
		this.rotationsData.flop();
		if ( this.isCropped() & !initializing )
			this.zoomCommand.cropX = 1 - this.zoomCommand.cropX;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.flopImg]</B> ' + e + '</FONT>' ); return false; } 
}


ImageData.prototype.cropImg = function(zoom_factor, crop_x, crop_y){
	try{
		this.zoomCommand = { zoomFactor: zoom_factor, cropX: crop_x, cropY: crop_y };
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[ImageData.cropImg]</B> ' + e + '</FONT>' ); return false; } 
}




function RotationsData(){
	try{
		this.data = new Array();
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[RotationsData]</B> ' + e + '</FONT>' ); return false; } 
}


RotationsData.prototype.flop = function(){
	try{
		var lastPosition = this.data.length - 1;
		if ( this.data[lastPosition] == 'flop' )
			this.data.splice(lastPosition, 1);
		else if ( this.data[lastPosition] == 'flip' ){
			if ( this.data[lastPosition - 1] == 'flop' )
				this.data.splice(lastPosition - 1, 1);
			else{
				this.data.splice(lastPosition, 1);
				this.data.push('180');
			}
		}
		else
			this.data.push('flop');
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[RotationsData.flop]</B> ' + e + '</FONT>' ); return false; } 
}


RotationsData.prototype.flip = function(){
	try{
		var lastPosition = this.data.length - 1;
		if ( this.data[lastPosition] == 'flip' )
			this.data.splice(lastPosition, 1);
		else if ( this.data[lastPosition] == 'flop' ){
			if ( this.data[lastPosition - 1] == 'flip' )
				this.data.splice(lastPosition - 1, 1);
			else{
				this.data.splice(lastPosition, 1);
				this.data.push('180');
			}
		}
		else
			this.data.push('flip');
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[RotationsData.flip]</B> ' + e + '</FONT>' ); return false; } 
}


RotationsData.prototype.rotate = function(rotation){
	try{
		var lastPosition = this.data.length - 1;
		if ( this.data[lastPosition] != 'flip' && this.data[lastPosition] != 'flop' ){
			var lastRot = this.data.splice(lastPosition, 1)[0];
			var newRot = ( ( lastRot ? lastRot : 0 )+ rotation) % 360;
			if ( newRot )
				this.data.push( newRot );
		}
		else{
			var newRot = rotation % 360;
			if ( newRot )
				this.data.push( newRot );
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[RotationsData.rotate]</B> ' + e + '</FONT>' ); return false; } 
}


RotationsData.prototype.getRotationsPISCommand = function(){
	try{
		var pis = new PIS();
		for ( var i = 0; i < this.data.length; i++ ){
			if ( this.data[i] == 'flop' )
				pis.flop();
			else if ( this.data[i] == 'flip' )
				pis.flip();
			else
				pis.rotate(this.data[i]);
		}
		return pis.getPISCommand(true);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[RotationsData.getRotationsPISCommand]</B> ' + e + '</FONT>' ); return false; } 
}


