var MaltellaCom = {
    init : function (){
        MaltellaCom.container = new Array();
        $("[cref],[pref],[fref]").live("click",function(){
            if ($(this).attr("cref")!=undefined){
                MaltellaCom.get($(this).attr("cref"));
            }
            if ($(this).attr("pref")!=undefined){
                var id = $(this).attr("pref");
                $(id).ajaxForm({success: function(comxml){
                    MaltellaCom.run(comxml);
                },dataType:'xml'});
                $(id).submit();
            }
        });
    },
    get : function(url,params){
        var t = new Date();
        if (params==undefined) params = {_t:t.getTime()};
        if (typeof(params)!="object") params = {_t:t.getTime()}; else params._t = t.getTime();
        $("body").css("cursor","wait");
        $.get(url,params,function(comxml){
            MaltellaCom.run(comxml);
            $("body").css("cursor","default");
        },"xml");
    },
    run : function(comxml){
        $(comxml).find("INSERT").each(function(){
            MaltellaCom.insert(this);
        });
        $(comxml).find("REMOVE").each(function(){
            MaltellaCom.remove(this);
        });
        $(comxml).find("EXECUTE").each(function(){
            MaltellaCom.execute(this);
        });
    },
    insert : function(elem){
        var data = $(elem).children("DATA").text();
        var target = $(elem).children("TARGET").text();
        var id;
        if ($(elem).children("ID").text()!=""){
            id = $(elem).children("ID").text();
        } else {
            if (typeof Sha1 != 'undefined'){
                id = Sha1.hash(data);
            } else {
                throw("Sha1 algorithm not present!");
            }
        }
        if (MaltellaCom.container[id]!=undefined) return id;
        switch($(elem).children("TYPE").text()){
            case "jscript":
                MaltellaCom.container[id] = document.createElement("script");
                MaltellaCom.container[id].setAttribute("type", "text/javascript");
                MaltellaCom.container[id].text = data;
                document.getElementsByTagName("head")[0].appendChild(MaltellaCom.container[id]);
                return id;
                break;
            case "css":
                MaltellaCom.container[id] = document.createElement("style");
                MaltellaCom.container[id].setAttribute("type", "text/css");
                $(MaltellaCom.container[id]).append(data);
                document.getElementsByTagName("head")[0].appendChild(MaltellaCom.container[id]);
                return id;
                break;
            case "jscriptfile":
                MaltellaCom.container[id] = document.createElement("script");
                MaltellaCom.container[id].setAttribute("type", "text/javascript");
                MaltellaCom.container[id].setAttribute("src", data);
                document.getElementsByTagName("head")[0].appendChild(MaltellaCom.container[id]);
                return id;
                break;
            case "cssfile":
                MaltellaCom.container[id] = document.createElement("link");
                MaltellaCom.container[id].setAttribute("type", "text/css");
                MaltellaCom.container[id].setAttribute("rel", "stylesheet");
                MaltellaCom.container[id].setAttribute("href", data);
                document.getElementsByTagName("head")[0].appendChild(MaltellaCom.container[id]);
                return id;
                break;
            case "html":
                if (target==undefined) target="body";
                if (target=="") target="body";
                MaltellaCom.container[id] = "class";
                var html = $(data);
                html.addClass("MaltellaCom-"+id);
                $(target).append(html);
                $(".MaltellaCom-"+id).unload(function(){
                    alert(id);
                    if (MaltellaCom.containter[id]!=undefined) delete MaltellaCom.containter[id];
                });
                return id;
                break;
            default:
                return null;
                break;
        }
    },
    execute : function(elem){
        MaltellaCom.removeId(MaltellaCom.insert(elem));
    },
    removeId : function(id){
        if (MaltellaCom.container[id]==undefined) return false;
        switch(MaltellaCom.container[id]){
           case "class":
               $(".MaltellaCom-"+id).remove();
               break;
           default:
               MaltellaCom.container[id].parentNode.removeChild(MaltellaCom.container[id]);
               break;
       }
       delete MaltellaCom.container[id];
       return true;
    },
    remove : function(elem){
        $(elem).children("ID").each(function(){
           id = $(this).text();
           return MaltellaCom.removeId(id);
        });
    },
    container : new Array()
}




