function isLoggedIn(){
	loggedIn = loggedIn = phpCall("isLoggedIn.php","","GET");
	if (loggedIn=="true"){
		return true;
	}
	else{
		return false;
	}
}
function XMLtoString(elem){
	// From : http://extjs.com/forum/showthread.php?t=34553
	
	var serialized;
	
	try {
		// XMLSerializer exists in current Mozilla browsers
		serializer = new XMLSerializer();
		serialized = serializer.serializeToString(elem);
	} 
	catch (e) {
		// Internet Explorer has a different approach to serializing XML
		serialized = elem.xml;
	}
	
	return serialized;
}

function showXML(node,fn){
	var name = node.nodeName;
			var attString = ""
		if (node.attributes) {
			for (var j = 0; j < node.attributes.length; j++) {
				if ((node.attributes[j].name.substring(0, 4)) == "axe_") {
				  
				}
				else {
					attString = attString + " " + (node.attributes[j].name + "='" + node.attributes[j].value + "'");
				}
			}
		}
	var string = "<" + name + attString + ">"
	var kids = node.childNodes;
	
	for (var i=0;i<kids.length;i++){
		

	
		if (kids[i].nodeName == "#text") {
			txt = kids[i].nodeValue.split(",");
	

			offset=txt[0];
			length=txt[1];
			string = string+loadText(fn,offset,length);	
			
		}
		else {
				var newstring =showXML(kids[i],fn);
			
			 string = string + newstring;
		}
	}
	string = string +"</"+name+">";
	return (string);
	
}
function loadFile(filename){
    
   var req = new GetXmlHttpObject();
    
    req.open('GET', './openText.php?fn='+filename, false);
    req.send(null);
    newText =  req.responseText;

    return newText;
}

function createLinks(desktop){

	inits = {workspace: "workspace", desktop: desktop};
		
	myPanel = new axe.LinkPanel(inits);	
	
	
} 

function newFile(type){
	oldWrapper = document.getElementById("window_new_wrapper");
	newWrap = document.createElement("div");
	newWrap.className = "window_body";
	newWrap.id = "window_new_wrapper";


	switch (type){
		case ("text"):
			schemaDiv = document.createElement("div");
			sLabel = document.createElement("span");
			sLabelTxt = document.createTextNode("Schema: ");
			sLabel.appendChild(sLabelTxt);
			schemaDiv.appendChild(sLabel);
			schemaInput = document.createElement("input");
			schemaInput.type = "text";
			schemaInput.id = "schema";
			schemaDiv.appendChild(schemaInput);
			newWrap.appendChild(schemaDiv);
			
			openButton = document.createElement("a");
			openButton.className="window_button";
			openButton.appendChild(document.createTextNode("Open"));
			newWrap.appendChild(openButton);
			YAHOO.util.Event.addListener(openButton,"click",createText);
			enterListener = new YAHOO.util.KeyListener(document,{keys:13}, createText);	
			enterListener.enable();

		break;
		case ("audio"):
			openButton = document.createElement("a");
			openButton.className="window_button";
			openButton.appendChild(document.createTextNode("Open"));
			newWrap.appendChild(openButton);
			YAHOO.util.Event.addListener(openButton,"click",createAudio);	
			enterListener = new YAHOO.util.KeyListener(document,{keys:13}, createAudio);
			enterListener.enable();

		break;
		case ("video"):
			openButton = document.createElement("a");
			openButton.className="window_button";
			openButton.appendChild(document.createTextNode("Open"));
			newWrap.appendChild(openButton);
			YAHOO.util.Event.addListener(openButton,"click",createVideo);
			enterListener = new YAHOO.util.KeyListener(document,{keys:13}, createVideo);	
			enterListener.enable();

		break;
		case ("image"):
			openButton = document.createElement("a");
			openButton.className="window_button";
			openButton.appendChild(document.createTextNode("Open"));
			newWrap.appendChild(openButton);
			YAHOO.util.Event.addListener(openButton,"click",createImage);
			fileURL = document.getElementById("fileURL");
			enterListener = new YAHOO.util.KeyListener(document,{keys:13}, createImage);	
			enterListener.enable();

		break;
		
		
		
	}
		if(isLoggedIn()){
		projectNameDiv = document.createElement("div");
		
			pnLabel = document.createElement("span");
			pnLabelTxt = document.createTextNode("Project Name: ");
			pnLabel.appendChild(pnLabelTxt);
			projectNameDiv.appendChild(pnLabel);
			pnInput = document.createElement("input");
			pnInput.type = "text";
			pnInput.id = "schema";
			projectNameDiv.appendChild(pnInput);
			newWrap.appendChild(projectNameDiv);
	}
		cancelButton = document.createElement("a");
	cancelButton.className="window_button";
	cancelButton.appendChild(document.createTextNode("Cancel"));
	YAHOO.util.Event.addListener(cancelButton,"click",closewindow,"new");
	oldWrapper.parentNode.replaceChild(newWrap,oldWrapper);
	
	callwindow("new");
}

function createText(){
		filename = document.getElementById("filename").value;
		schema = document.getElementById("schema").value;
	inits = {workspace: "workspace", desktop: desktop, root: "TEI",schema: schema,url: filename, tagSetName: "test", mode: "tree"};
		
	myPanel = new axe.TextPanel(inits);	
	closewindow("new");
	
} 
function createImage(){
	filename = document.getElementById("filename").value;
	DOM = (new DOMParser()).parseFromString("<graphic mimeType=\"image/jpg\"></graphic>", "text/xml");
	var inits = {
		

		desktop: desktop, filename: filename, DOM: DOM
	};
	myPanel = new axe.ImagePanel(inits);
closewindow("new");
	
} 

function createAudio(){
		filename = document.getElementById("filename").value;
	DOM = (new DOMParser()).parseFromString("<timeline unit='ns'></timeline>", "text/xml");
	audInits = {desktop: desktop, url: filename,DOM: DOM};
		
	audPanel = new axe.AudioPanel(audInits);
	closewindow("new");
	
}
function createVideo(){
		filename = document.getElementById("filename").value;
	DOM = (new DOMParser()).parseFromString("<timeline unit='ns'></timeline>", "text/xml");
	
	vidInits = {desktop: desktop, url: filename, xml: "BlackCrook1867.xml",DOM: DOM};
	
	vidPanel = new axe.VideoPanel(vidInits);
	closewindow("new");
}
function GetXmlHttpObject(loc)
{	

  	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;
}
function createDom(){
	try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
  return xmlDoc;
}
function phpCall(file,params,method){
    var req = GetXmlHttpObject();
   
   if (method == "POST"){
      req.open(method,file,false);
      req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      req.send(params);
   }
   else{
       req.open(method,file+params,false);
   }   req.send(null);

   return (req.responseText);
}

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;
		
	}
}

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