var Canvas = window.Canvas || {};

(function () {

	// Container box class
	Canvas.Container = function(ImgElement, ID) {
		this._ID = ID;					// Set up ID
		this._Img = ImgElement;			// Set up pointers
		ImgElement._Box = this;
		this._aImages = [];
		this.addImage_callback = function () {};
		this.delImage_callback = function () {};
	};

	Canvas.Container.prototype._Img = null;
	Canvas.Container.prototype._aImages = null;
	Canvas.Container.prototype._dragImg = null;
	
	Canvas.Container.prototype.scaled = false;
	Canvas.Container.prototype._ID = null;
	
	Canvas.Container.prototype.addImage_callback = null;
	Canvas.Container.prototype.delImage_callback = null;
	
	Canvas.Container.prototype.setID = function(myID) {
		this._ID = myID;
		this._Img._oElement.id = "box" + myID;
	};
	
	Canvas.Container.prototype.addImage = function(oImg, ajaxcall) {
		oImg._inBox = this;					// Make the Img element be aware of the box it belongs to
		this._aImages.push(oImg);

		if(ajaxcall && this._ID > 0)		// Negative IDs are for special boxes not in the DB. 
			CanvasAjax.addToCollection(oImg, this);
		
		this.fitIntoBox(oImg);

		this.addImage_callback();
	};

	Canvas.Container.prototype.delImage = function(oImg, ajaxcall) {
		for(var i = 0; i < this._aImages.length; i++) {
			if(this._aImages[i] == oImg) 
				break;
		}

		this._aImages.splice(i, 1);
		oImg._inBox = null;			// Remove the reference to the box in the Img element
		
		if(ajaxcall && this._ID > 0) 
			CanvasAjax.removeFromCollection(oImg, this);
		
		this.delImage_callback();
		
		//if ("console" in window) console.log("Deleted %o from %o", oImg, this);
	};
	
	Canvas.Container.prototype.delAllImages = function(ajaxcall) {
		for(var i = 0; i < this._aImages.length; i++) {
			this.delImage(this._aImages[i], false);
		}

		if(ajaxcall) 
			CanvasAjax.removeAllItemsFromCollection(this);
	};
	
	Canvas.Container.prototype.dragOver = function(oImg) {
		this._dragImg = oImg;
		//console.log("Dragged %o over %o", oImg, this._Img);
	};
	
	Canvas.Container.prototype.dragOff = function() {
		//console.log("Dragged %o off %o", this._dragImg, this._Img);
		this._dragImg = null;
	};	
	
	/*Canvas.Container.prototype.downloadMedia = function () {
		if(downloadFrame = document.getElementById('downloadIframe')) {
			downloadFrame.src="BMAjax/downloadMediaCollection/"+this._ID;
		} else {
			downloadFrame=document.createElement("iframe");
			downloadFrame.name="downloadIframe";
			downloadFrame.id="downloadIframe";
			downloadFrame.setAttribute('width', '0px', null);
			downloadFrame.setAttribute('height', '0px', null);
			downloadFrame.src="BMAjax/downloadMediaCollection/"+this._ID;
			document.body.appendChild(downloadFrame);
		}
	};*/
	
	Canvas.Container.prototype.fitIntoBox = function(oImg) {
		boxleft = this._Img.left - this._Img.currentWidth/2 + this._Img.currentWidth*0.035;
		boxright = this._Img.left + this._Img.currentWidth/2 - this._Img.currentWidth*0.078;
		boxtop =  this._Img.top - this._Img.currentHeight/2 + this._Img.currentWidth*0.075;
		boxbottom =  this._Img.top + this._Img.currentHeight/2 - this._Img.currentWidth*0.018;		
		
		while(true) {
			imgleft = Math.min(Math.min(oImg.oCoords.bl.x, oImg.oCoords.br.x), Math.min(oImg.oCoords.tl.x, oImg.oCoords.tr.x));
			imgright = Math.max(Math.max(oImg.oCoords.bl.x, oImg.oCoords.br.x), Math.max(oImg.oCoords.tl.x, oImg.oCoords.tr.x));
			imgtop = Math.min(Math.min(oImg.oCoords.bl.y, oImg.oCoords.br.y), Math.min(oImg.oCoords.tl.y, oImg.oCoords.tr.y));
			imgbottom = Math.max(Math.max(oImg.oCoords.bl.y, oImg.oCoords.br.y), Math.max(oImg.oCoords.tl.y, oImg.oCoords.tr.y));
		
			if((imgbottom - imgtop) > (boxbottom - boxtop)) {
				oImg.scalex = oImg.scalex -  0.001;
				oImg.scaley = oImg.scalex;
				oImg.setImageCoords();
				continue;
			}

			if((imgright - imgleft) > (boxright - boxleft)) {
				oImg.scalex = oImg.scalex -  0.001;
				oImg.scaley = oImg.scalex;
				oImg.setImageCoords();
				continue;
			}
		
			if(imgleft < boxleft)
				oImg.left = oImg.left + (boxleft - imgleft);
			
			if(imgright > boxright)
				oImg.left = oImg.left - (imgright - boxright);
			
			if(imgtop < boxtop)
				oImg.top = oImg.top + (boxtop - imgtop);
				
			if(imgbottom > boxbottom)
				oImg.top = oImg.top - (imgbottom - boxbottom);
				
			break;
		}
	};
	
}());