/**
 * AXE Imagetagger
 */

YAHOO.namespace("AXE");
axe = YAHOO.AXE;
mouseX = 0;
mouseY = 0;
UA = YAHOO.env.ua;

// Setup constants

            // QUIRKS FLAG, FOR BOX MODEL
            var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");

            // UNDERLAY/IFRAME SYNC REQUIRED
            var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));

            // PADDING USED FOR BODY ELEMENT (Hardcoded for example)
            var PANEL_BODY_PADDING = (10*2) // 10px top/bottom padding applied to Panel body element. The top/bottom border width is 0



function loadFile(filename){
    
   var req = new GetXmlHttpObject();
    
    req.open('GET', './openText.php?fn='+filename, false);
    req.send(null);
    newText =  req.responseText;

    return newText;
}
function trueValue(value, scale) {
	//reduce given value down to the base scale
	value = parseInt(value);
	
	if(scale >0) {
		return (value / scale);
	} else if (scale < 0) {
		return (value * Math.abs(scale));
	} else {
		return value;
	}
}

//----------Line-------------------
axe.line = function(image,x0,y0,x1,y1){
	
	this.x0 = x0;
	this.x1 = x1;
	this.y0 = y0;
	this.y1 = y1;
	this.dotArray = [];
	this.image=image;
	
}
axe.line.prototype.remove=function(){
	if (this.dotArray) {
		for (i in this.dotArray) {
		
			this.dotArray[i].HTML.parentNode.removeChild(this.dotArray[i].HTML);
			
		}

	} 
}
axe.line.prototype.hide=function() {
	for(i in this.dotArray) {
		YAHOO.util.Dom.setStyle(this.dotArray[i].HTML.id, "display", "none");
	}
}
axe.line.prototype.drawLine = function(limit){
limit=2;
//This function is modified from code at http://ajaxphp.packtpub.com/ajax/whiteboard/
  
  var dy = this.y1 - this.y0;
  var dx = this.x1 - this.x0;

  var stepx, stepy, limit;
  if (dy < 0) 
  {
    dy = -dy;
    stepy = -1;
  }
  else
  {
    stepy = 1; 
  }
  if (dx < 0) 
  {
    dx = -dx;  
    stepx = -1; 
  }
  else 
  {
    stepx = 1; 
  }
  if (limit < 0 || limit == null) {
  	limit = (Math.ceil((dy / 100)))*2;
  }
 
  dy <<= 1; 
  dx <<= 1; 
  dot = new axe.dot(this.image,[this.x0, this.y0, 10, 10]);
 this.dotArray.push(dot);
 // eval(visitFunction);
  if (dx > dy) 
  {
    fraction = dy - (dx >> 1); 
	dcount=0;
    while (this.x0 != this.x1)
    {
      if (fraction >= 0) 
      {
        this.y0 += stepy;
        fraction -= dx;
      }
      this.x0 += stepx;
      fraction += dy;
  if (dcount == limit) {
  
  	dot = new axe.dot(this.image, [this.x0, this.y0, 10, 10]);
	this.dotArray.push(dot);
	dcount = 0;
  }
  else {
  	dcount++;
  }
  
  //eval(visitFunction);
        
    }
  }
  else
  {
    fraction = dx - (dy >> 1);
	dcount = 0;
    while (this.y0 != this.y1) 
    {
      if (fraction >= 0) 
      {
        this.x0 += stepx;
        fraction -= dy;
      }      
      this.y0 += stepy;
      fraction += dx;
      if (dcount == limit) {
  	dot = new axe.dot(this.image, [this.x0, this.y0, 10, 10]);
	this.dotArray.push(dot);
	dcount = 0;
  }
  else {
  	dcount++;
  }
  //eval(visitFunction);
    }
  }
}
function GetXmlHttpObject()
{
  	var xmlHttp=null;
  	try
    {
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
    }
  	catch (e)
    {
    	// Internet Explorer
    	try
      	{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
    	catch (e)
      	{
      		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      	}
    }
  	return xmlHttp;
}
//------Image Tagger Function
function getCoords(image, mode, shape, arr) {
	if (shape == "box") {
		if (mode == "new") {
			
			cLeft = arr[0];
			cTop = arr[1];
			cWidth = arr[2];
			cHeight = arr[3];
			scale = image.scale;
			if (scale != 0) {
				//scale elements down to original height and width
				while (scale != 0) {
					//scale = (scale >0) ? scale - 2 : scale + 2;
					if (scale > 0) {
						scale -= 2;
						//alert(scale);
						cLeft /= 2;
						cTop /= 2;
						cWidth /= 2;
						cHeight /= 2;
					}
					else 
						if (scale < 0) {
							scale += 2;
							//alert(scale);
							cLeft *= 2;
							cTop *= 2;
							cWidth *= 2;
							cHeight *= 2;
						}
				}
			}
			coords = parseInt(cLeft) + "," + parseInt(cTop) + "," + parseInt(cWidth) + "," + parseInt(cHeight);
			
			/*pparams = "?coord=" + coords + "&mode=new&type=" + shape + "&width=" + (document.getElementById(imageid).width) +
			"&height=" +
			(document.getElementById(imageid).height);
			phpCall('./coordDB.php', pparams, 'GET');*/
		}
		else 
			if (mode == "overwrite") {
				cLeft = arr[0];
				cTop = arr[1];
				cWidth = arr[2];
				cHeight = arr[3];
					
				coords = parseInt(cLeft) + "," + parseInt(cTop) + "," + parseInt(cWidth) + "," + parseInt(cHeight);
				
				/*pparams = "?coord=" + coords + "&mode=overwrite&width=" + (document.getElementById(imageid).width) +
				"&height=" +
				(document.getElementById(imageid).height) +
				"&id=" +
				obj.isetID;
				phpCall('./coordDB.php', pparams, 'GET');*/
			}
	} else {
		if (mode == "new") {
			
			var coords = "";
			for (i in arr) {
				node = arr[i];
				coords += trueValue(node.dot.HTML.style.left, image.scale) + "," + trueValue(node.dot.HTML.style.top, image.scale) + "-";
				
			}
			//generate a random number between 1 and 1000
			dbID = Math.floor(Math.random() * 1000 + 1);
			coords += coords.substring(0, coords.length-1);
			
			var width = trueValue(image.HTML.width, image.scale);
			var height = trueValue(image.HTML.height, image.scale);
			
			params = '?coords='+coords+'&id='+dbID+'&width='
			+width+'&height='+height+'&type=poly';
			response = phpCall('./database/coords.php', params, 'GET');
				
			
			return coords;
			
			/*
			pparams = "?coord=" + coords.substring(0, (coords.substring-1)) 
			+ "&mode=new&type=" + shape + "&width=" + (document.getElementById(imageid).width) 
			+ "&height=" + (document.getElementById(imageid).height);
			phpCall('./db_API/coordDB.php', pparams, 'GET');*/
		}
		else 
			if (mode == "overwrite") {
				coords = "";
				
				var coords = "";
				for (i in arr) {
					node = arr[i];
					coords += trueValue(node.dot.HTML.style.left, image.scale) + "," + trueValue(node.dot.HTML.style.top, image.scale) + "-";
				
				}
				
				return coords;
			/*	pparams = "?coord=" + (coords.substring(0, (coords.length-1))) + "&mode=overwrite&width=" + (document.getElementById(imageid).width) +
				"&height=" +
				(document.getElementById(imageid).height) +
				"&id=" +
				obj.isetID;
				phpCall('./db_API/coordDB.php', pparams, 'GET');*/
			}
	}
}

axe.workspace.prototype.startUpTagset = function(e, obj) {
	

	//Set up tagset
	
	
	createImage(obj);
	
	
	
}



axe.shapeDragged = new YAHOO.util.CustomEvent("shapeDragged");
axe.shapeMoved = new YAHOO.util.CustomEvent("shapeMoved");

//------tagArea
axe.tagArea = function(panel) {
	//create area to display TEI and HTML imagemap tags
	this.panel = panel;
	this.id = YAHOO.util.Dom.generateId(this.HTML, "textarea");
	
	panel.desktop.objects[this.id] = this;
	
	this.HTML = document.createElement("div");
	this.HTML.className = "tagArea";
	
	
	this.textarea = document.createElement("textarea");
	
	this.textarea.rows = "5";
	this.textarea.columns = "30";
	this.textarea.name = "tagArea";
	
	
	this.format = document.createElement("select");
	this.format.name = "formatChoose";
	this.format.label = "_Output Format_";
	this.format.innerHTML = "<option onclick=\"setTag(panel, 'TEI')\" label=\"_Output Format_\">TEI</option>" + 
	"<option onclick=\"setTag(panel, 'HTML')\" label=\"_Output Format_\">HTML</option>";
	
	this.HTML.appendChild(this.textarea);
	this.HTML.appendChild(this.format);
	
	
}

function setTag(panel, type) {
	if (panel.shapeType == 'poly') {
		alert("Polygon shapes are not TEI compliant. Sorry, you can only use this with boxes.");
	}
	else {
		panel.tagArea.displayTag(panel, type);
	}
}
axe.tagArea.prototype.displayTag = function(obj, type) {
	//type indicates TEI or HTML
	switch(type) {
		case 'TEI':
			tag = "<facsimile>";
			for(i in obj.image.imageRegions) { //output all boxes
				tag += "<graphic mimeType=\"image/jpg\" height=\""+obj.image.HTML.height+"\" width=\""+obj.image.HTML.width+"\" url=\"\"/>";
				tag += "<zone ulx=\""+obj.image.imageRegions[i].HTML.style.left+"\" uly=\""+obj.image.imageRegions[i].HTML.style.top+"\""+
				"lrx=\""+obj.image.imageRegions[i].HTML.style.width+"\" lry=\""+obj.image.imageRegions[i].HTML.style.height+"\">";
				tag += "<desc>"+i+"</desc>";
				
			}
			tag += "</facsimile>";
			obj.tagArea.textarea.value = tag;
			
			//display in a separate window
			this.createWin(tag, obj);
			
			break;
		case 'HTML':
			tag = "<img usemap=\"#"+obj.image.id+"\"/>";
			
			tag += "<map id=\""+obj.image.id+"\" name=\""+obj.image.id+"\">";
			for(i in obj.image.imageRegions) { //output all boxes
				temp = obj.image.imageRegions[i];
				tag += "<area shape=\"rect\" coords=\""+temp.HTML.style.left+","+temp.HTML.style.top+","+temp.HTML.style.width+","+temp.HTML.style.height+"\" href=\"#\" target=\"_blank\" alt=\""+i+"\" />"
			}
			tag += "</map>";
			obj.tagArea.textarea.value = tag;
			break;
	}
	
}
axe.tagArea.prototype.createWin=function(tag, obj){
	fobject = document.createElement("div");
	fobject.id = YAHOO.util.Dom.generateId("fobject");
	fobject.className = "tagWin";
	handle = document.createElement("div");
	handle.style.right = "0px";
	handle.style.width="10px";
	handle.style.height="10px";
	handle.id = YAHOO.util.Dom.generateId("fobjecthandle");
	fobject.appendChild(handle);
	zobject = obj.tagArea.HTML;
	fobject.appendChild(zobject);
	
	dragTag = new YAHOO.util.DD(fobject.id);
	dragTag.setHandleElId(handle.id);
	closeButton = document.createElement("button");
	closeButton.style.width="100px";
	closeButton.style.height="10px";
	closeButton.type="submit";
	closeButton.value="Close";
	
	fobject.appendChild(closeButton);
	
	obj.HTML.parentNode.appendChild(fobject);
	YAHOO.util.Event.addListener(closeButton, "click", this.closeWin, fobject);
	
}
axe.tagArea.prototype.closeWin = function(e, obj){
	
	fobject = document.getElementById(obj.id);
	fobject.parentNode.removeChild(fobject);
}
//------LoadBar
axe.loadBar = function(panel){
	
	this.HTML = document.createElement("input");
	this.HTML.className = "loadBar";
	this.HTML.value = "";
	//change button
	button = document.createElement("img");
	
	button.name = "changeImage";
	button.alt = "changeImage";
	panel.header.HTML.appendChild(button);
	
	panel.header.HTML.appendChild(this.HTML);
	YAHOO.util.Event.addListener(this.HTML, "click", this.handleClick, this);
	YAHOO.util.Event.addListener(button, "click", this.change, panel, true);
}
axe.loadBar.prototype.change = function(obj) {
	this.image.change(this.loadBar.HTML.value);
}
axe.loadBar.prototype.handleClick = function(obj) {
	this.focus();
	
}

//--------Dot----------------
axe.dot = function(image,points){

		this.image = image;

	this.id = YAHOO.util.Dom.generateId(this,"dot");
	this.HTML = document.createElement("div");
	this.HTML.id = this.id;

	
	this.HTML.className = "dot";
	
	this.top = parseInt(points[1]) - YAHOO.util.Dom.getY(this.image.HTML);
	this.left = parseInt(points[0]) - YAHOO.util.Dom.getX(this.image.HTML);
	this.realTop = parseInt(points[1]) - YAHOO.util.Dom.getY(this.image.HTML.parentNode);
	this.realLeft = parseInt(points[0]) - YAHOO.util.Dom.getX(this.image.HTML.parentNode);

	this.setX(points[0]);
	this.setY(points[1]);

	this.HTML.style.top = this.top+"px";
	this.HTML.style.left = this.left+"px";

	this.HTML.style.display = "block";
	

	this.image.HTML.parentNode.appendChild(this.HTML);
	
}
axe.dot.prototype.setX=function(x){
	this.x = parseInt(x);
	//YAHOO.util.Dom.setStyle(this.HTML,"left",this.x-YAHOO.util.Dom.getX(this.image.HTML.parentNode));
}
axe.dot.prototype.setY=function(y){
	this.y = parseInt(y);
	}
//-----------Polygon--------------
axe.node = function(image,points,start){
	
	this.dot = new axe.dot(image,points);
	this.dot.HTML.className = "node";
	this.image = image;
	this.area = null;
	
	this.start=start;
	
	YAHOO.util.Event.addListener(this.dot.id,"mouseover",this.mouseOverHandler,this);
	YAHOO.util.Event.addListener(this.dot.id,"mouseout",this.mouseOutHandler,this);
	
	
		YAHOO.util.Event.onAvailable(this.dot.id, this.makeDraggable, this);

	


}

axe.node.prototype.makeDraggable = function(obj){
	draggable = new YAHOO.util.DD(this.id);
	
	draggable.onMouseDown = function(){
		obj.area.clearLines(obj.area);
		
	}
	
	draggable.onDrag = function(){
	
		obj.dot.setX(YAHOO.util.Dom.getX(obj.dot.HTML));
		obj.dot.setY(YAHOO.util.Dom.getY(obj.dot.HTML));
	}
	
	draggable.onMouseUp = function(){
	
		
		if (obj.start) {
		
			obj.area.completeShape(obj.area);
	
			
		}

	}
	

	
	
	if (desktop.objects[this.id]){
		
		desktop.objects[this.id].draggable = draggable;
		
	}
	
}
axe.node.prototype.dbCall=function(obj) {
	
				//lines active
				//update database
				var coords = "";
				for (i in obj.area.nodes) {
					node = obj.area.nodes[i];
					coords += trueValue(node.dot.HTML.style.left) + "," + trueValue(node.dot.HTML.style.top) + "-";
					
				}
				//generate a random number between 1 and 1000
				obj.area.dbID = Math.floor(Math.random() * 1000 + 1);
				coords += coords.substring(0, coords.length-1);
				
				var width = obj.image.trueValue(obj.image.HTML.width);
				var height = obj.image.trueValue(obj.image.HTML.height);
				params = '?coords='+coords+'&id='+obj.area.dbID+'&width='
				+width+'&height='+height+'&type=poly';
				response = phpCall('./database/coords.php', params, 'GET');
				
				coords = "";
			
}
axe.node.prototype.mouseOverHandler = function(e,obj){
	if (obj.dot.HTML.className == "firstNode") {
		
	 obj.dot.HTML.className = "selectedFirstNode";
	}
	else {
		obj.dot.HTML.className = "selectedNode";
	}
}
axe.node.prototype.mouseOutHandler = function(e,obj){
	if (obj.dot.HTML.className == "selectedFirstNode") {
		
	 obj.dot.HTML.className = "firstNode";
	}
	else {
		obj.dot.HTML.className = "node";
	}
}
axe.node.prototype.nodeClicked = function(e,obj){
	image = obj.image;
	lastDot = null;
	coords = [];
	area = obj.image.curArea;
	(image.dotArray) ? passArray=image.dotArray : passArray=false;
	
	if (obj.dot.HTML.className == "selectedFirstNode") {
	
		mainDrag = null;
		
		if (area.lines.length > 0) {
			//node lines already activated
			
			
		}
		else {
			area.nodes = image.dotArray;
			for (i in image.dotArray) {
			
				aNode = image.dotArray[i];
		
			
			if (lastDot) {
				line = new axe.line(image, lastDot.dot.x, lastDot.dot.y, aNode.dot.x, aNode.dot.y);
				
				line.drawLine(1);
				area.lines.push(line);
				obj.image.lines.push(line);
				//lineBresenham(image, lastDot.dot.x, lastDot.dot.y, aNode.dot.x, aNode.dot.y);	
			
			}
			
			coords.push(aNode.dot.x - YAHOO.util.Dom.getX(aNode.dot.HTML.parentNode.parentNode));
			coords.push(aNode.dot.y - YAHOO.util.Dom.getY(aNode.dot.HTML.parentNode.parentNode));
			lastDot = aNode;
			
			
		}
			
		line = new axe.line(image, lastDot.dot.x, lastDot.dot.y, image.dotArray[0].dot.x, image.dotArray[0].dot.y);
		line.drawLine(1);
			
		area.lines.push(line);
		obj.image.lines.push(line);
		
		
		coordString = "";
		
		for (c in coords) {
			coordString = coordString + coords[c] + ",";
		}
		
		area.HTML.coords = coordString.substring(0, coordString.length - 1);
		
		for(i in image.dotArray) {
			aNode = image.dotArray[i];
				
			//aNode.area.nodes.push(aNode);
			aNode.area = area;
			aNode.image = image;
			aNode.isetID = obj.isetID;
			
			YAHOO.util.Event.addListener(aNode.dot.HTML.id, "mouseup", this.dotDropped, aNode, true);
		}
		getCoords(image, 'new', 'poly', obj.area.nodes);
		
		//add to nodes array for image
		obj.image.nodes.push(area.nodes[0]);
		var arr = {top: area.nodes[0].dot.HTML.style.top, left: area.nodes[0].dot.HTML.style.left};
		
		obj.image.panel.sidePanel.refreshView(arr);
		//obj.image.panel.desktop.sideBar.add(arr, obj.image);
		
		image.dotArray = new Array();
		//database functions
		
		
		}
	} 
		
}

axe.area = function(image,type){
	this.id = YAHOO.util.Dom.generateId(this,"area");
	this.HTML = document.createElement("area");	
	this.HTML.id = this.id;
	this.image = image;
	//this.HTML.setAttribute("onclick","alert('hi')");
	this.HTML.shape = type;
	this.nodes = [];
	this.lines = [];
	this.xml = null;
	this.dbID = null;
	 
}
axe.area.prototype.clearLines=function(obj){
	for (i in obj.lines){
		obj.lines[i].remove();
	}
	obj.lines = [];
	obj.image.curArea = obj;
}
axe.area.prototype.completeShape=function(area){
	image = area.image;
	lastDot=null;
	
	var y0, y1, x0, x1;
	
	for (i in area.nodes) {
			
			aNode = area.nodes[i];
			
			
			if (lastDot) {
				y0 = parseInt(YAHOO.util.Dom.getY(lastDot.dot.HTML));
				y1 = parseInt(YAHOO.util.Dom.getY(aNode.dot.HTML));
				x0 = parseInt(YAHOO.util.Dom.getX(lastDot.dot.HTML));
				x1 = parseInt(YAHOO.util.Dom.getX(aNode.dot.HTML));
			
				line = new axe.line(image, x0, y0, x1, y1);
				
				line.drawLine(5);
				area.lines.push(line);
				
			}
			
			lastDot = aNode;
			
			
		}
		y0 = parseInt(YAHOO.util.Dom.getY(lastDot.dot.HTML));
		y1 = parseInt(YAHOO.util.Dom.getY(area.nodes[0].dot.HTML));
		x0 = parseInt(YAHOO.util.Dom.getX(lastDot.dot.HTML));
		x1 = parseInt(YAHOO.util.Dom.getX(area.nodes[0].dot.HTML));	
		line = new axe.line(image, x0, y0, x1, y1);
		line.drawLine(5);		
		area.lines.push(line);
		area.image.curArea=null;
		
		//var randn = Math.floor(Math.random()*1000+1);
		//update database
		this.coords = getCoords(image, 'new', 'poly', area.nodes);
		var arr = (!area.dbID) ? {id: area.id, type: "poly", area: area, coords: this.coords} : {id: area.dbID, type: "poly", overwrite: true, area: area, coords: this.coords};
		if(!area.dbID) {
			area.xml = this.image.panel.addZone(area, this.coords);
			axe.tagCreated.fire({obj: area.xml});
		}
		area.image.panel.sidePanel.refreshView(arr);
		
		area.dbID = (area.dbID == null) ? area.id : area.dbID;
		
		
		
	
}
axe.imageRegion = function(image,points, id){

	this.image = image;
	this.dbID = null;
	this.id = (!id) ? YAHOO.util.Dom.generateId(this,"imageRegion") : id;
	this.HTML = document.createElement("div");
	this.HTML.id = this.id;
	this.area = new axe.area(this.image, "box");
	
	
	this.HTML.className = "box";
	top = parseInt(points[1])-parseInt(YAHOO.util.Dom.getY(this.image.HTML));
	left = parseInt(points[0])-parseInt(YAHOO.util.Dom.getX(this.image.HTML));
	
	realTop = top - this.image.HTML.parentNode.parentNode.firstChild.offsetHeight;
	
	this.HTML.style.top = top+"px";
	this.HTML.style.left = left+"px";
	this.HTML.style.height = points[2] + "px";
	this.HTML.style.width = points[3] + "px";
	this.HTML.style.display = "block";
	this.image.HTML.parentNode.appendChild(this.HTML);
	//Call Database

	coords = "";
	this.area.xml = this.image.panel.addZone(this.area, coords);
	
	obj = {id: this.id, type: "box", area: this.area, ulx: trueValue(this.HTML.style.left, this.image.scale), uly: trueValue(this.HTML.style.top, this.image.scale), lrx: trueValue(this.HTML.style.width, this.image.scale), lry: trueValue(this.HTML.style.height, this.image.scale)};
	this.image.panel.sidePanel.refreshView(obj);
	axe.tagCreated.fire({obj: this.area.xml});
	/*pparams = "?coord="+coords+"&mode=new&height=" +
		(document.getElementById(this.image.id).height) +
		"&width=" +
		(document.getElementById(this.image.id).width)+"&type=box&table=add";
	
	this.isetID = phpCall('../db_API/coordDB.php', pparams, 'GET');*/
	
	YAHOO.util.Event.onAvailable(this.HTML.id, this.handleOnAvailable, this, true);	
	YAHOO.util.Event.addListener(this.HTML.id, "mouseup", this.changed, this, true);
	image.imageRegions.push(this);
	
	
	//this.dbCall(this);
}
axe.imageRegion.prototype.changed = function(e, obj) {
	//var coords = new Array(this.HTML.style.left,this.HTML.style.top,this.HTML.style.width,this.HTML.style.height);
	//getCoords(obj.image, 'overwrite', 'box', coords);
	//obj.dbCall(obj);
	arr = {id: this.id, type: "box", overwrite: true, ulx: trueValue(this.HTML.style.left, this.image.scale), uly: trueValue(this.HTML.style.top, this.image.scale), lrx: trueValue(this.HTML.style.width, this.image.scale), lry: trueValue(this.HTML.style.height, this.image.scale)};
	this.image.panel.sidePanel.refreshView(arr);
	//this.image.panel.desktop.sideBar.add(obj, this.image);
	 	/*pparams = "?coord="+coords+"&mode=overwrite&height="+(document.getElementById(this.image.id).height) +
		"&width=" +
		(document.getElementById(this.image.id).width)+"&id="+this.isetID;
		phpCall('../db_API/coordDB.php', pparams, 'GET');*/
}
axe.imageRegion.prototype.dbCall=function(obj) {
	//update database
	var coords = "" + trueValue(obj.HTML.style.left, obj.image.scale) + "," + trueValue(obj.HTML.style.top, obj.image.scale) + "," + trueValue(obj.HTML.style.width, obj.image.scale) + "," + trueValue(obj.HTML.style.height, obj.image.scale);
	var height = trueValue(obj.image.HTML.height, obj.image.scale);
	var width = trueValue(obj.image.HTML.width, obj.image.scale);
	//generate a random number between 1 and 1000
	obj.dbID = Math.floor(Math.random() * 1000 + 1);
	params = '?coords='+coords+'&id='+obj.dbID+'&width='
				+width+'&height='+height+'&type=box';
	response = phpCall('./database/coords.php', params, 'GET');
			
}
axe.imageRegion.prototype.handleOnAvailable=function(){
	makeDragResize(this.HTML);
}
axe.imageRegion.prototype.getPoints = function(){
	
	return ([this.HTML.style.left,this.HTML.style.top,this.HTML.style.width,this.HTML.style.height]);
}
axe.imageRegion.prototype.handleOnAvailable = function(me){

	draggable = new YAHOO.util.DD(this.id);
	//draggable.setHandleElId(this.id+"_handle");
	
	// Yahoo code for making a resizable panel
	
	this.resize = new YAHOO.util.Resize(this.id, {
		handles: ['br'],
		
		autoRatio: false,
		minWidth: 10,
		minHeight: 10
	
	});
	
	
}
//--------------Image--------------------
axe.image = function(src,loc){
		
	this.id = YAHOO.util.Dom.generateId(this,"image");
	desktop = loc.desktop;
	
	desktop.objects[this.id] = this;
	
	//this.dotArray = new Array();
	this.curArea = null;
	this.leftDot=null;
	this.topDot=null;
	this.bottomDot=null;
	this.rightDot=null;
	this.src = src;
	this.HTML = document.createElement("img");
	this.HTML.src=src;
	this.HTML.id = this.id;
	this.areas=[];
	this.origWidth = this.HTML.width;
	this.origHeight = this.HTML.height;
	this.panel = loc;
	this.imageRegions = new Array(); //storing boxes
	this.nodes = new Array(); //storing polygons
	this.lines = new Array(); //storing line dots
	this.scale = 0;	
	YAHOO.util.Event.addListener(this.HTML, "click", this.imageClick, this);
	}

axe.image.prototype.change=function(src){
	
	this.HTML.src = src;
	this.src=src;

		
}
axe.image.prototype.trueValue=function(value) {
	//find the scaled up or scaled down value
	if(this.scale >0) {
		return (value / this.scale);
	} else if (this.scale < 0) {
		return (value / Math.abs(this.scale));
	} else {
		return value;
	}
}
axe.image.prototype.imageClick = function(e,args){
	
	if (args.panel.shapeType == "poly") {
		
		if (args.curArea) {
		aNode = new axe.node(args, [mouseX, mouseY, 10, 10],false);
			args.curArea.nodes.push(aNode);
		}
		else{
		aNode = new axe.node(args, [mouseX, mouseY, 10, 10],true);
			aNode.dot.HTML.className = "firstNode";
			args.curArea = new axe.area(args,"poly");
			args.curArea.nodes.push(aNode);
			args.areas.push(args.curArea);
			//YAHOO.util.Event.addListener(aNode,"mousedown",args.curArea.clearLines,args.curArea);
			
		}	
		//YAHOO.util.Event.addListener(aNode.dot.id,"mousedown",args.curArea.clearLines,args.curArea);
	
		aNode.area = args.curArea;
		
			
		
		
	}
	else if(args.panel.shapeType == "box") {
		box = new axe.imageRegion(args,[mouseX, mouseY, 10, 10]);
	
	}
}

axe.image.prototype.getCoordOnOrig = function(img,x,y){
	xCor = parseInt(x);
	yCor = parseInt(y);
	
	imgObj = desktop.objects[img.id];
	xPercent = xCor/parseInt(img.offsetWidth);
	yPercent = yCor/parseInt(img.offsetHeight);
	realX = Math.round(xPercent * parseInt(imgObj.origWidth));
	realY = Math.round(yPercent * parseInt(imgObj.origHeight));
	result = [realX,realY];
	
	return  result;
	
}
axe.image.prototype.getCoordOnCur = function (img,x,y){
		xCor = parseInt(x);
		yCor = parseInt(y);
		imgObj = desktop.objects[img.id];
	
		xPercent = xCor/imgObj.origWidth;
	yPercent = yCor/imgObj.origHeight;
	
	realX = Math.round(xPercent * parseInt(img.offsetWidth));
	realY = Math.round(yPercent * parseInt(img.offsetHeight));
	result = [realX,realY];
	
	return  result;
}

axe.xml = function(xmlSrc){
	this.src = xmlSrc;
	this.xml = loadXMLDoc(xmlSrc);
	
	
	
}

//----------------------
axe.mouseMoveEvent = function(e){
	mouseX = YAHOO.util.Event.getPageX(e);
	mouseY = YAHOO.util.Event.getPageY(e)
} 

YAHOO.util.Event.onDOMReady(function() {
		YAHOO.util.Event.addListener(document, "mousemove", axe.mouseMoveEvent);
		//desktop = new axe.workspace("workspace");
		
		//createWindow(desktop); 
});

makeDragResize = function(me){
	draggable = new YAHOO.util.DD(this.id);
	
	
	
	this.resize = new YAHOO.util.Resize(this.id, {
		handles: ['br','tl','tr','bl'],
		
		autoRatio: false,
		minWidth: 10,
		minHeight: 10
	
	});
	
	if (desktop.objects[this.id]){
		
		desktop.objects[this.id].draggable = draggable;
		
	}
}
