/**
 * @author grantd
 * link.js
 * 
 */

axe.link=function(inits) {
	this.loc = inits.loc;
	this.obj = inits.obj;
	this.panel = inits.panel;
	this.sidePanelObject = inits.sidePanelObject;
	this.sidepanel = inits.sidepanel;
	this.type = inits.type;
	this.saved = false;
	this.origColor = this.loc.style.background;
	//this.loc.style.background = "#00FF00";
	this.XML = null;
	this.HTML = document.createElement("div");
	this.HTML.id = YAHOO.util.Dom.generateId(this.HTML, "link");
	this.HTML.className = "imgPanelSaveBar";
	
	this.deleteButton = document.createElement("button"); 
	this.deleteButton.id = YAHOO.util.Dom.generateId(this.deleteButton, "linkButton");
	
	this.saveButton=document.createElement("button");
	this.saveButton.id = YAHOO.util.Dom.generateId(this.saveButton, "linkButton");
	/*
closeButton=document.createElement("button"); 
	closeButton.id = YAHOO.util.Dom.generateId(closeButton, "linkButton");
	closeButton.type="button"; closeButton.innerHTML="Cancel";
*/
	this.deleteButton.type="button"; this.deleteButton.innerHTML="Discard";
	this.saveButton.type="button"; this.saveButton.innerHTML="Save";
	this.HTML.appendChild(this.deleteButton); this.HTML.appendChild(this.saveButton); 
	this.loc.appendChild(this.HTML);
	
	//this.linkRemove = new YAHOO.util.CustomEvent("linkRemove");
	//YAHOO.util.Event.addListener(closeButton.id, "click", this.closeLink, this);
	YAHOO.util.Event.addListener(this.saveButton.id, "click", this.saveLink, this);
	YAHOO.util.Event.addListener(this.deleteButton.id, "click", this.deleteLink, this);
//	YAHOO.util.Event.addListener(this.HTML.id, "click", this.itemClicked, this);
	
	//axe.sidePanelClick.subscribe(this.showLink);
}

/*
axe.link.prototype.closeLink=function(e, args) {
	args.dropBox();
}
*/
axe.link.prototype.saveLink=function(e, args) {
	args.sidepanel.saveItem({obj: args.obj, type: args.type});
	args.HTML.removeChild(args.saveButton);
	args.deleteButton.innerHTML = "Delete";
	//args.saveButton.parentNode.removeChild(args.saveButton);
	//change color to normal
	
	
	//fire off to LinkPanel
	//axe.tagClicked.fire({obj: args.obj.area.xml});
	
	//args.dropBox();
}
axe.link.prototype.deleteLink=function(e, args) {
	args.deleteObj();
	//args.dropBox();
}
axe.link.prototype.deleteObj=function() {
	node = document.getElementById(this.obj.id);
	
	this.sidepanel.removeItem({obj: this.obj, main: this.sidePanelObject, type: this.type});
	if (this.type == "box") {
		node.parentNode.removeChild(node);
	}
	//this.removeLink.fire({obj: this.obj});
}
axe.link.prototype.dropBox=function() {
	this.HTML.parentNode.removeChild(this.HTML);
	this.loc.style.background = this.origColor;
	this.obj.link = null;
}
axe.link.prototype.showLink=function(type, args) {
	tag = args[0].obj;
	desktop = args[0].desktop;
	link = args[0].link;
	if(!desktop.linkMode || desktop.linkMod == false) {
		
		desktop.linkMode=true;
		
		filling = "<ul>";
		for(i in tag) {
			filling += "<li>"+i+": "+tag[i]+"</li>";
		}
		filling += "</ul>";
		link.HTML.innerHTML = filling;
		
		fobject = document.getElementById(tag.id+"tag");
		//fobject.style.background = "#00FF00";
		
		follow = document.createElement("div");
		follow.id = "follow"; follow.style.color="#000"; follow.style.position = "relative"; follow.style.background = "#FFF";
		follow.innerHTML = "<p>Link this tag with another tag</p>";
		//YAHOO.util.Event.addListener(desktop.id, "mousemove", this.mouseEvent, follow);
		
		fobject.appendChild(follow);
		
		var linkNode = {tag: tag, desktop: desktop, link: link};
		desktop.linkArray.push(linkNode);
		
	} else if(desktop.linkMode == true) {
		
		filling = "<ul>";
		for(i in tag) {
			filling += "<li>"+i+": "+tag[i]+"</li>";
		}
		filling += "</ul>";
		link.HTML.innerHTML = filling;
		
		link2 = desktop.linkArray[0].link;
		link2.HTML.style.top = "113px";
		link2.HTML.style.left = "114px";
		YAHOO.util.Event.addListener(desktop.id, "mousemove", this.mouseLinkEvent, link2.HTML);
		
		
		fobject = document.getElementById(desktop.id);
		
		fobject.appendChild(link.HTML);
		fobject.appendChild(link2.HTML);
		desktop.linkMode = false;
		
		follow = document.getElementById("follow");
		follow.parentNode.removeChild(follow);
		
	}
	
	//document.firstChild.appendChild(this.HTML);
}
axe.link.prototype.mouseLinkEvent=function(e, obj) {
	alert(obj.style.top);
	obj.style.top = YAHOO.util.Dom.getPageY(e)+"px";
	obj.style.left = YAHOO.util.Dom.getPageX(e)+"px";
}
