
function Opening(id, dimensions, position, shape){
	try{
		this.id = id;
		this.dimensions = dimensions;
		this.position = position;
		this.shape = shape;
		this.margin = 0;
		this.templateId = '';
		this.openingTypes = { 'image' : 0, 'text': 1 };
		this.openingType = this.openingTypes.image;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening]</B> ' + e + '</FONT>' ); return false; } 
}


Opening.prototype.toString = function (){
	try{
		var string = "(o)";
		string += this.templateId + ",";
		string += this.dimensions.width + "," + this.dimensions.height;
		string += "," + this.position.x + "," + this.position.y;
		string += "," + this.shape;
		string += "," + this.borderType;
		string += "," + ( this.rotate ? this.rotate : "");
		string += "," + ( this.opacity ? this.opacity : "");
		string += ( this.isImage() ? this.image.toString() : "");
		string += ( this.isCaption() ? this.text.toString() : "");
		return string;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.toString]</B> ' + e + '</FONT>' ); return false; } 
}


Opening.prototype.readOpeningParam = function(string){
	try{
		var openingData = string.replace(/\(o\)/,"");
		if ( openingData.indexOf("[i]") != -1 ){
			var imageData = openingData.substring(openingData.indexOf("[i]")+3);
			openingData = openingData.substring(0,openingData.indexOf("[i]"));
		}
		
		if ( openingData.indexOf("[t]") != -1 ){
			var textData = openingData.substring(openingData.indexOf("[t]")+3);
			openingData = openingData.substring(0,openingData.indexOf("[t]"));
		}
		
		openingData = openingData.split(",");
		if ( openingData[0] )
			this.templateId = openingData[0];
		this.dimensions = {width: Number(openingData[1]), height: Number(openingData[2])};
		this.position = {x: Number(openingData[3]), y: Number(openingData[4])};
		this.shape = openingData[5];
		this.borderType = openingData[6];
		if ( openingData[7] )
			this.rotate = openingData[7];
		if ( openingData[8] )
			this.opacity = openingData[8];
		else
			this.opacity = 100;
		//alert("Opening string: "+string+"\nthis.templateId: "+this.templateId+"\nthis.dimensions: "+this.dimensions.width+","+this.dimensions.height+"\nthis.position: "+this.position.x+","+this.position.y+"\nthis.shape: "+this.shape+"\nthis.borderType: "+this.borderType+"\nthis.rotate: "+this.rotate+"\nthis.opacity: "+this.opacity);
		//dbg.add("<FONT color='green'><B>Reading Opening string</B>: "+string,"<I>this.templateId</I>: "+this.templateId,"<I>this.dimensions</I>: "+this.dimensions.width+","+this.dimensions.height,"<I>this.position</I>: "+this.position.x+","+this.position.y,"<I>this.shape</I>: "+this.shape,"<I>this.borderType</I>: "+this.borderType,"<I>this.rotate</I>: "+this.rotate,"<I>this.opacity</I>: "+this.opacity+"</FONT><BR>");
		if ( imageData ){
			this.image = new ImageData();
			this.image.readImageParam(imageData);
			this.openingType = this.openingTypes.image;
		}
		if ( textData ){
			this.text = new TextData();
			this.text.readTextParam(textData);
			this.openingType = this.openingTypes.text;
		}
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.readOpeningParam]</B> ' + e + '</FONT>' ); return false; } 
}


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


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


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


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

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


Opening.prototype.removeImage = function(){
	try{
		var RemovedScannum = ( this.image ? this.image.getScannum() : null );
		this.image = null;
		return RemovedScannum;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.removeImage]</B> ' + e + '</FONT>' ); return false; } 
}


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


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


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


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


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

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

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


Opening.prototype.hasRotation = function(){
	try{
		return ( this.rotate && this.rotate != "0");
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.hasRotation]</B> ' + e + '</FONT>' ); return false; } 
}


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


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


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

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

Opening.prototype.isCaption = function(){
	try{
		if (this.openingType == this.openingTypes.text && this.text != null) { return true; }
		return false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.isCaption]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.isImage = function(){
	try{
		if (this.openingType == this.openingTypes.image && this.image != null) { return true; }
		return false;
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.isImage]</B> ' + e + '</FONT>' ); return false; } 
}


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

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


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

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


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

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

Opening.prototype.getPositionX0 = function (){
	try{
		return this.position.x - ( this.dimensions.width/2 );
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getPositionX0]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getPositionY0 = function (){
	try{
		return this.position.y - ( this.dimensions.height/2 );
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getPositionY0]</B> ' + e + '</FONT>' ); return false; } 
}

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

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

Opening.prototype.getTotalPositionX0 = function (){
	try{
		return this.getPositionX() - ( this.totalWidth()/2 + this.margin );
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getTotalPositionX0]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getTotalPositionY0 = function (){
	try{
		return this.getPositionY() - ( this.totalHeight()/2 + this.margin );
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getTotalPositionY0]</B> ' + e + '</FONT>' ); return false; } 
}


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

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

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

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

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

Opening.prototype.shapeIsRectangular = function (){
	try{
		return this.shape == "rect";
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getShape]</B> ' + e + '</FONT>' ); return false; } 
}


Opening.prototype.getBoundingBoxX0 = function (){
	try{
		return this.getPositionX() - (this.getBoundingBoxWidth()/2);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxX0]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getBoundingBoxY0 = function (){
	try{
		return this.getPositionY() - (this.getBoundingBoxHeight()/2);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxY0]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getBoundingBoxXRight = function (){
	try{
		return this.getPositionX() + (this.getBoundingBoxWidth()/2);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxXRigth]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getBoundingBoxYBottom = function (){
	try{
		return this.getPositionY() + (this.getBoundingBoxHeight()/2);
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxYBottom]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getBoundingBoxWidth = function (){
	try{
		if ( this.hasRotation() ){
			if ( this.shape == "rect" )
				return this.rectBoundingWidth(this.rotate);
			if ( this.shape == "diamond" )
				return this.diamBoundingWidth(this.rotate);
			if ( this.shape == "oval" || this.shape == "circle" )
				return this.ovalBoundingWidth(this.rotate);
		}
	else
		return this.getWidth();
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxWidth]</B> ' + e + '</FONT>' ); return false; } 
}

Opening.prototype.getBoundingBoxHeight = function (){
	try{
		if ( this.hasRotation() ){
			if ( this.shape == "rect" )
				return this.rectBoundingHeight(this.rotate);
			if ( this.shape == "diamond" )
				return this.diamBoundingHeight(this.rotate);
			if ( this.shape == "oval" || this.shape == "circle" )
				return this.ovalBoundingHeight(this.rotate);
		}
	else
		return this.getHeight();
	}
	catch(e){ dbg.add ( '<FONT COLOR="red"><B>[Opening.getBoundingBoxHeight]</B> ' + e + '</FONT>' ); return false; } 
}


Opening.prototype.totalWidth = function (){
	if ( this.rotate != null ){
		var x1 = rotateX((this.getPositionX()-(this.getWidth()/2)),(this.getPositionY()-(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var x2 = rotateX((this.getPositionX()+(this.getWidth()/2)),(this.getPositionY()-(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var x3 = rotateX((this.getPositionX()-(this.getWidth()/2)),(this.getPositionY()+(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var x4 = rotateX((this.getPositionX()+(this.getWidth()/2)),(this.getPositionY()+(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var x0 = Math.min(x1,x2,x3,x4);
		var xRigth = Math.max(x1,x2,x3,x4);
		return (xRigth - x0);
	}
	else
		return this.getWidth();
}

Opening.prototype.totalHeight = function (){
	if ( this.rotate != null ){
		var y1 = rotateY(this.getPositionX()-(this.getWidth()/2),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var y2 = rotateY(this.getPositionX()+(this.getWidth()/2),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var y3 = rotateY(this.getPositionX()-(this.getWidth()/2),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var y4 = rotateY(this.getPositionX()+(this.getWidth()/2),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),this.rotate,this.getWidth(),this.getHeight());
		var y0 = Math.min(y1,y2,y3,y4);
		var yBottom = Math.max(y1,y2,y3,y4);
		return (yBottom - y0);
	}
	else
		return this.getHeight();
}


Opening.prototype.rectBoundingWidth = function (rot){
	var x1 = rotateX((this.getPositionX()-(this.getWidth()/2)),(this.getPositionY()-(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x2 = rotateX((this.getPositionX()+(this.getWidth()/2)),(this.getPositionY()-(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x3 = rotateX((this.getPositionX()-(this.getWidth()/2)),(this.getPositionY()+(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x4 = rotateX((this.getPositionX()+(this.getWidth()/2)),(this.getPositionY()+(this.getHeight()/2)),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x0 = Math.min(x1,x2,x3,x4);
	var xRigth = Math.max(x1,x2,x3,x4);
	return (xRigth - x0);
}

Opening.prototype.rectBoundingHeight = function (rot){
	var y1 = rotateY(this.getPositionX()-(this.getWidth()/2),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y2 = rotateY(this.getPositionX()+(this.getWidth()/2),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y3 = rotateY(this.getPositionX()-(this.getWidth()/2),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y4 = rotateY(this.getPositionX()+(this.getWidth()/2),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y0 = Math.min(y1,y2,y3,y4);
	var yBottom = Math.max(y1,y2,y3,y4);
	return (yBottom - y0);
}

Opening.prototype.diamBoundingWidth = function (rot){
	var x1 = rotateX(this.getPositionX(),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x2 = rotateX(this.getPositionX(),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x3 = rotateX(this.getPositionX()+(this.getWidth()/2),this.getPositionY(),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x4 = rotateX(this.getPositionX()-(this.getWidth()/2),this.getPositionY(),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var x0 = Math.min(x1,x2,x3,x4);
	var xRigth = Math.max(x1,x2,x3,x4);
	return (xRigth - x0);
}

Opening.prototype.diamBoundingHeight = function (rot){
	var y1 = rotateY(this.getPositionX(),this.getPositionY()-(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y2 = rotateY(this.getPositionX(),this.getPositionY()+(this.getHeight()/2),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y3 = rotateY(this.getPositionX()+(this.getWidth()/2),this.getPositionY(),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y4 = rotateY(this.getPositionX()-(this.getWidth()/2),this.getPositionY(),this.getPositionX0(),this.getPositionY0(),rot,this.getWidth(),this.getHeight());
	var y0 = Math.min(y1,y2,y3,y4);
	var yBottom = Math.max(y1,y2,y3,y4);
	return (yBottom - y0);
}

Opening.prototype.ovalBoundingWidth = function (rot){
	return ((this.rectBoundingWidth(rot) + 2*this.diamBoundingWidth(rot) ) / 3);
}

Opening.prototype.ovalBoundingHeight = function (rot){
	return ((this.rectBoundingHeight(rot) + 2*this.diamBoundingHeight(rot) ) / 3);
}

