//////////////////////////////////////////////////////////////////////////////
// custom drag and drop implementation
//////////////////////////////////////////////////////////////////////////////
var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;

YAHOO.example.DDList = function(id, sGroup, config) {

    YAHOO.example.DDList.superclass.constructor.call(this, id, sGroup, config);

    this.logger = this.logger || YAHOO;
    var el = this.getDragEl();
    Dom.setStyle(el, "opacity", 0.67); // The proxy is slightly transparent

    this.goingUp = false;
    this.lastY = 0;
};

YAHOO.extend(YAHOO.example.DDList, YAHOO.util.DDProxy, {

    startDrag: function(x, y) {
        this.logger.log(this.id + " startDrag");

        // make the proxy look like the source element
        var dragEl = this.getDragEl();
        var clickEl = this.getEl();
        Dom.setStyle(clickEl, "visibility", "hidden");

        dragEl.innerHTML = clickEl.innerHTML;

        Dom.setStyle(dragEl, "color", Dom.getStyle(clickEl, "color"));
        Dom.setStyle(dragEl, "backgroundColor", Dom.getStyle(clickEl, "backgroundColor"));
        Dom.setStyle(dragEl, "border", "2px solid gray");
    },

    endDrag: function(e) {

        var srcEl = this.getEl();
        var proxy = this.getDragEl();
		

        // Show the proxy element and animate it to the src element's location
        Dom.setStyle(proxy, "visibility", "");
        var a = new YAHOO.util.Motion( 
            proxy, { 
                points: { 
                    to: Dom.getXY(srcEl)
                }
            }, 
            0.2, 
            YAHOO.util.Easing.easeOut 
        )
        var proxyid = proxy.id;
        var thisid = this.id;

        // Hide the proxy and show the source element when finished with the animation
        a.onComplete.subscribe(function() {
                Dom.setStyle(proxyid, "visibility", "hidden");
                Dom.setStyle(thisid, "visibility", "");
            });
        a.animate();
    },

    onDragDrop: function(e, id) {
		
        // If there is one drop interaction, the li was dropped either on the list,
        // or it was dropped on the current location of the source element.
        if (DDM.interactionInfo.drop.length === 1) {

            // The position of the cursor at the time of the drop (YAHOO.util.Point)
            var pt = DDM.interactionInfo.point; 

            // The region occupied by the source element at the time of the drop
            var region = DDM.interactionInfo.sourceRegion; 

            // Check to see if we are over the source element's location.  We will
            // append to the bottom of the list once we are sure it was a drop in
            // the negative space (the area of the list without any list items)
            if (!region.intersect(pt)) {
				
                var destEl = Dom.get(id);
				
                var destDD = DDM.getDDById(id);
                destEl.appendChild(this.getEl());
			
                destDD.isEmpty = false;
                DDM.refreshCache();
            }

        }
	
    },

    onDrag: function(e) {

        // Keep track of the direction of the drag for use during onDragOver
        var y = Event.getPageY(e);

        if (y < this.lastY) {
            this.goingUp = true;
        } else if (y > this.lastY) {
            this.goingUp = false;
        }

        this.lastY = y;
    },

    onDragOver: function(e, id) {
    
        var srcEl = this.getEl();
        var destEl = Dom.get(id);

        // We are only concerned with list items, we ignore the dragover
        // notifications for the list.
        if ((destEl.nodeName.toLowerCase() == "li") && (destEl.className=="listItem")){
            var orig_p = srcEl.parentNode;
            var p = destEl.parentNode;

            if (this.goingUp) {
                p.insertBefore(srcEl, destEl); // insert above
            } else {
                p.insertBefore(srcEl, destEl.nextSibling); // insert below
            }

            DDM.refreshCache();
        }
		else{
			pId = srcEl.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
			
		}
		
	
    }
});





axe.LinkPanel = function(inits){
	axe.LinkPanel.superclass.constructor.call(this, inits);	
	this.desktop = inits["desktop"];
	this.wrapper = document.createElement("div");	
	this.wrapper.className="linkWrapper";	
	
	this.top = document.createElement("div");
	this.top.className="linkTop";
	this.bottom = document.createElement("div");
	this.bottom.className="linkBottom";

	this.leftList = new axe.LinkList();
	this.leftList.HTML.className = "linkList leftLinkList";
	
	this.rightList = new axe.LinkList();
		this.rightList.HTML.className = "linkList rightLinkList";
	this.bottom.appendChild(this.leftList.HTML);
	this.bottom.appendChild(this.rightList.HTML);
	this.wrapper.appendChild(this.top);
	this.wrapper.appendChild(this.bottom);
	this.trash = document.createElement("div");
	this.trash.className="linktrash";
	this.trash.id = "trash_"+this.id;
	this.wrapper.appendChild(this.trash);
	this.content.HTML.appendChild(this.wrapper);
	
	this.viewButton = new axe.panelButton("./theme/images/icon_xml.png","view",this);
this.header.HTML.insertBefore(this.viewButton.HTML,this.closeButton.HTML);
YAHOO.util.Event.addListener(this.viewButton.HTML,"click",this.showXML,this);
}
YAHOO.lang.extend(axe.LinkPanel,axe.panel);
axe.LinkPanel.prototype.showXML=function(e,obj){
	phpCall("clearLinks.php","?id="+obj.id,false);
 	xmlout = window.open('','xmlout',''); 
	left = obj.leftList.ul.childNodes;
	right = obj.rightList.ul.childNodes;
	txt = "<linkGrp>";
	for (i=0;i<left.length;i++){
		if (right[i]){
			lftxt = left[i].firstChild.nodeValue;
	
			rttxt = right[i].firstChild.nodeValue;
			phpCall("saveLink.php","?id="+obj.id+"&left="+left[i].firstChild.nodeValue+"&right="+right[i].firstChild.nodeValue,false);
			entry = "<link targets='"+lftxt+" "+rttxt+"' />";
			
			txt = txt+entry;
		}
		
	}
	txt = txt+"</linkGrp>";
	
	//obj.DOM.documentElement.setAttribute("origin",obj.DOM.documentElement.firstChild.getAttribute("xml:id"));
	//txt = (XMLtoString(obj.DOM.documentElement));
	
	//txt = obj.constructXML(obj);
	html = "<textarea rows=500 cols=200>"+txt+"</textarea>";
	xmlout.document.write(html);
	xmlout.document.close();



} 
axe.LinkList = function(){
	this.HTML = document.createElement("div");

	
	this.id = YAHOO.util.Dom.generateId(this,"linkList");
	this.HTML.id = this.id;

	this.linkControls = document.createElement("div");
	this.linkControls.className = "linkControls";
	
	this.recordNewTagButton = document.createElement("span");
	this.recordNewTagButton.className = "lc_record";
	this.recordNewTagButton.appendChild(document.createTextNode("Record"));
	
	this.touchNewTagButton = document.createElement("span");
	this.touchNewTagButton.className = "lc_touch";
	this.touchNewTagButton.appendChild(document.createTextNode("Touch"));
	
	this.stop = document.createElement("span");
	this.stop.className = "lc_stop";
	this.stop.appendChild(document.createTextNode("Stop"));
	
	
	this.ul = document.createElement("ul");
	this.ul.className = "linkListUL"
		this.ul.id = YAHOO.util.Dom.generateId(this,"linkUL");
	this.linkControls.appendChild(this.recordNewTagButton);
	this.linkControls.appendChild(this.touchNewTagButton);
	this.linkControls.appendChild(this.stop);
	
	this.HTML.appendChild(this.linkControls);
	this.HTML.appendChild(this.ul);
	 new YAHOO.util.DDTarget(this.ul.id);

	YAHOO.util.Event.addListener(this.recordNewTagButton,"click",this.startTagRecorder,this);
	YAHOO.util.Event.addListener(this.touchNewTagButton,"click",this.startTouchRecorder,this);
	YAHOO.util.Event.addListener(this.stop,"click",this.stopTagRecorder,this);
	
}
axe.LinkList.prototype.stopTagRecorder = function(e,obj){
	
	axe.tagClicked.unsubscribe(obj.recordNewTag,obj.ul);
	axe.tagCreated.unsubscribe(obj.recordNewTag,obj.ul);
	
}
axe.LinkList.prototype.startTagRecorder = function(e,obj){
	
	axe.tagClicked.unsubscribe(obj.recordNewTag,obj.ul);
	axe.tagCreated.unsubscribe(obj.recordNewTag,obj.ul);
	axe.tagCreated.subscribe(obj.recordNewTag,obj.ul);
}
axe.LinkList.prototype.recordNewTag=function(type,obj,me){

	linkName = document.createElement("li");
	linkName.className="listItem";
	id = YAHOO.util.Dom.generateId(linkName,"listItem");
	
	tagId = "";
	
	if (obj[0].obj.getAttribute("xml:id")){
		
		tagId = obj[0].obj.getAttribute("xml:id");
	
	}
	else {

		tagId = obj[0].obj.id;
	
	}

	linkName.appendChild(document.createTextNode(tagId));
	closeButton = document.createElement("img");
	closeButton.src = "./theme/images/icon_audio_delete.png";
	closeButton.id = "close_"+id;
	linkName.appendChild(closeButton);
	YAHOO.util.Event.addListener(closeButton.id,"click",removeItem,linkName);
	me.appendChild(linkName);
	new YAHOO.example.DDList(linkName);
}
removeItem = function(e,obj){

	obj.parentNode.removeChild(obj);
	
}
axe.LinkList.prototype.startTouchRecorder = function(e,obj){
	
	axe.tagClicked.unsubscribe(obj.recordNewTag,obj.ul);
	axe.tagCreated.unsubscribe(obj.recordNewTag,obj.ul);
	axe.tagClicked.subscribe(obj.recordNewTag,obj.ul);
	alert("Clicked elements will now be added to this list.");
}
axe.PanelList = function(desktop){
	this.HTML = document.createElement("div");
	this.HTML.className="linkPanelList";
	
	for (i in desktop.objects) {
		if (desktop.objects[i].url) {
		
			aDiv = document.createElement("div");
		
			aDiv.appendChild(document.createTextNode(desktop.objects[i].url));
			this.HTML.appendChild(aDiv);
		}
	}
	
	
	
}
