/*
    davbrowser_demo.js - demo of how to use the BrowserRoot class
    Copyright (C) 2004 Guido Wesdorp
    email johnny@debris.demon.nl

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    $Id: minisax.js,v 1.5 2004/07/31 00:10:15 johnny Exp $

*/

function DavEditor(doc, fs, editarea) {
    this.doc = doc;
    this.fs = fs;
    this.editarea = editarea;
    this.current_path = null;
};

DavEditor.prototype.loadContent = function(path) {
    this.current_path = path;
    this.fs.read(path, this._loadContentHandler, this);
};

DavEditor.prototype._loadContentHandler = function(error, content) {
    if (error) {
    } else {
        this.editarea.value = content;
    };
};

function DavBrowser(doc, textarea) {
    var icons = {'browser': 'images/browser_icon.png',
                    'directory': 'images/collection_icon.png',
                    'image/*': 'images/image_icon.png',
                    'text/*': 'images/text_icon.png',
                    '*': 'images/file_icon.png'
    };

    this.doc = doc;
    this.textarea = textarea;
    
    this.initialize = function(placeholder, propertyholder, root, host, 
                                port, proto) {
        this.placeholder = placeholder;
        this.propertyholder = propertyholder;
        this.root = root;
        this.host = host;
        this.port = port ? port : 80;
        this.proto = proto ? proto : 'http';
        
        this.fs = new davlib.DavFS();
        this.fs.initialize(host, port, proto);
        this.editor = new DavEditor(this.doc, this.fs, this.textarea);
        this.tree = new davbrowser.BrowserRoot(this.doc, placeholder, root,
                                               this.fs, icons);
        var wrapper = func.fixContext(this.handleItemClick, this);
        // XXX brr bad override... :|
        davbrowser.BrowserRoot.prototype.handleItemClick = wrapper;
        var string = 'DAV Browser';
        this.tree.init(string);
        this.tree.render();
    };
};

DavBrowser.prototype.handleItemClick = function(event, item) {
    if (this.skip_next_click) {
        this.skip_next_click = false;
        return;
    };
    if (this.propertyholder) {
        // set the properties
        // check if there are properties already, not very elegant but I don't 
        // know a better way in JavaScript...
        var hasproperties = false;
        for (var prop in item.properties) {
            hasproperties = true;
            break;
        };
        function HandlerWrapper(browser, item) {
            this.execute = function(error, content) {
                if (error) {
                    alert(error);
                    return;
                };
                item.properties = content;
                browser._fillProperties(item);
            };
        };
        var hw = new HandlerWrapper(this, item);
        this.fs.getProps(item.getPath(), hw.execute, hw);
    };
    // if the item is not a collection, load it in the textarea
    if (item.id.substr(item.id.length - 1) != '/' && item.id != 'root') {
        this.editor.loadContent(item.getPath());
    };
};

DavBrowser.prototype._fillProperties = function(item) {
    var propholder = this.propertyholder;
    while (propholder.hasChildNodes()) {
        propholder.removeChild(propholder.firstChild);
    };
    var h = this.doc.createElement('h3');
    var text = this.doc.createTextNode(item.id);
    h.appendChild(text);
    propholder.appendChild(h);
    for (var namespace in item.properties) {
        var h = this.doc.createElement('h4');
        var text = this.doc.createTextNode(namespace);
        h.appendChild(text);
        propholder.appendChild(h);
        for (var name in item.properties[namespace]) {
            var docel = 
                    item.properties[namespace][name].documentElement;
            var div = this.doc.createElement('div');
            var nobr = this.doc.createElement('nobr');
            div.appendChild(nobr);
            var text = null;
            if (docel.childNodes.length == 1 && 
                    docel.childNodes[0].nodeType == 3) {
                var text = name + ': ' + docel.childNodes[0].toXML();
            } else {
                var text = name + ': ' + docel.toXML();
            };
            nobr.appendChild(this.doc.createTextNode(text));
            propholder.appendChild(div);
        };
    };
};



