var fileTreeViewClass = "/page/tools/management/FileTreeView.class";
var imageResizerClass = "/page/image/ImageResizer.class";

function performDivUpdate(strURL) {

    var xmlHttpReq = false;

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
    }
    xmlHttpReq.open('GET', strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            if (xmlHttpReq.status == 200) { 
                var res = xmlHttpReq.responseText;
                var vidx = res.indexOf("\r\n");
				var label = "\r\njavascript:";
                var jidx = res.lastIndexOf(label);                
                // first line indicates id to be updated            
                var key = res.substring(0, vidx);
                // data upto CRLFjavascript: displayed
                var value = res.substring(vidx+2);
                // data after CRLFjavascript: executed as javascript
                if (jidx > vidx) {
                    value = res.substring(vidx+2, jidx);
                	var script = res.substring(jidx+label.length);
                }                

                var newdiv = document.createElement("div");
                newdiv.innerHTML = value;
                
				// Replace existing content
                var element = document.getElementById(key);
				childElement = element.firstChild;
				if (childElement != null) element.removeChild(childElement);			
                element.appendChild(newdiv);     
                
                
                // Execute any java script
                if (script) eval(script);
            } 
        }
    }
    xmlHttpReq.send(null);
}



function showImage(id, path, options, linkoptions) {
    linkElement = document.getElementById(id + "_link");
    href = "javascript:showImage(\'" + id + "\', \'" + escape(path) + "\', \'" + linkoptions + "\',\'" + options + "\')";
    linkElement.href = href;
	imageElement = document.getElementById(id);
	imageElement.src = imageResizerClass + "?path=" + path + "&" + options;
}

function showPath(id, path, options, selected) {
  	resourcePath = '/templates/default/tree/';
  	divElement = document.getElementById(id);
  	iconElement = document.getElementById(id + "_icon"); 
	if (iconElement) {
  		iconElement.src = resourcePath + "busy.gif";
	}  	
  	childElement = divElement.firstChild;
  	if (childElement == null) {  	
    	openDirectory(path, options, selected);	
  	}  
  	else if (options) {
    	divElement.removeChild(childElement);
    	openDirectory(path, options, selected);
  	}  	
  	else {
    	divElement.removeChild(childElement);
    	if (iconElement) {
      		iconElement.src = resourcePath + "folder.png";
    	}
  	}
}

function openDirectory(path, options, selected) {
    var strURL = fileTreeViewClass + "?path=" + path + "&" + options + "&selected=" + selected;
    performDivUpdate(strURL);
}
    

