    
    
    function initFootnotes( admin ) {
        
        connectInfoLayer( );
        connectFootnotes( );
        getFootnotes( admin );
        
    }
    
    
    function connectInfoLayer( ) {
        dojo.forEach( dojo.query(".info a"), function( entry ) {
            dojo.connect( entry, 'onclick', function( e ){ 
                dojo.stopEvent( e ); 
                var icon_id = dojo.query("img", entry);
                if (icon_id.length > 0) {
                    icon_id = icon_id[0].id;
                } else {
                    icon_id = dojo.query("span.infoLink", entry)[0].id;
                }
                
                icon_id = icon_id.substring(0, icon_id.length-5);
                dijit.byId( icon_id ).show();
                dojo.connect( dojo.query( "#"+icon_id+" a.fnclose" )[0], 'onclick', function( e ){ 
                    dojo.stopEvent( e ); 
                    dijit.byId( icon_id ).onCancel();
                } );
                if (dojo.query( "#"+icon_id+" a.fnprint" ).length>0) {
                    dojo.connect( dojo.query( "#"+icon_id+" a.fnprint" )[0], 'onclick', function( e ){ 
                        dojo.stopEvent( e ); 
                        printLayer( icon_id );
                    } );
                }
                dojo.connect( dojo.query( ".dijitDialogUnderlayWrapper" )[0], 'onclick', function( e ){ 
                    dojo.stopEvent( e ); 
                    dijit.byId( icon_id ).onCancel();
                } );
            });
        });
    }
    
    
    function connectFootnotes( ) {
        dojo.forEach( dojo.query(".fn"), function( entry ) {
            var innerFN = (dojo.query("span span", entry).length > 0) ? dojo.query("span span", entry)[0].innerHTML : null;
            if (parseInt(innerFN)>0) {
                dojo.query("span span", entry)[0].innerHTML = "&nbsp;";
            } else {
                if (dojo.isIE > 0) { 
                    dojo.forEach( dojo.query("span span", entry), function( exschnub ) {
                        exschnub.innerHTML = "&nbsp;";
                    });
                }
            }
            dojo.connect( entry, 'onclick', function( e ){ 
                dojo.stopEvent( e ); 
                var fn_id = dojo.query(":nth-child(1)",entry)[0].className;
                dijit.byId( fn_id ).show();
            });
        });
    }
    
    
    function connectFootnotesNode( node ) {
        console.log( dojo.query(".fn",node) );
        dojo.forEach( dojo.query(".fn",node), function( entry ) {
            var innerFN = (dojo.query("span span", entry).length > 0) ? dojo.query("span span", entry)[0].innerHTML : null;
            if (parseInt(innerFN)>0) {
                dojo.query("span span", entry)[0].innerHTML = "&nbsp;";
            }
            dojo.connect( entry, 'onclick', function( e ){ 
                dojo.stopEvent( e ); 
                var fn_id = dojo.query(":nth-child(1)",entry)[0].className;
                dijit.byId( fn_id ).show();
            });
        });
    }
    
    
    function showLayer( id ) {
       dijit.byId( id ).show();
       dojo.connect( dojo.query( "#"+id+" a.fnclose" )[0], 'onclick', function( e ){ 
            dojo.stopEvent( e ); 
            dijit.byId( id ).onCancel();
        } );
        dojo.connect( dojo.query( ".dijitDialogUnderlayWrapper" )[0], 'onclick', function( e ){ 
            dojo.stopEvent( e ); 
            dijit.byId( id ).onCancel();
        } );
    }
    
    
    function showFootnote( id ) {
        id = "fn_"+id;
        dijit.byId( id ).show();
    }
    
    
    function getFootnotes( admin ) {
        dojo.xhrGet ({
            url: '/footnotes/data/fn.json',
            handleAs: 'json',
            load: function( data ) {
                getFootnotesMap( data, admin );
            },
            error: function( error ) { }
        });
    }
    
    
    function getFootnotesMap( fn, admin ) {
        
        var footnote = new Array();
        var textPost = new Array();
        var fn_ids = new Array();
        
        dojo.forEach( dojo.query(".fn"), function( entry ) {
            var fn_id = dojo.query(":nth-child(1)",entry)[0].className.substring(3);
            fn_ids.push(fn_id);
        });
        dojo.xhrGet ({
            url: '/footnotes/data/fn_text.json',
            handleAs: 'json',
            load: function( fn_text ) {
                dojo.forEach( fn, function( entry_fn ) {
                    if (dojo.indexOf(fn_ids,entry_fn.fn_id)>=0) {
                        var text_ids = new Array();
                        dojo.forEach( fn_text, function( entry_fn_text ) {
                            if( entry_fn.fn_id == entry_fn_text.fn_id ) { 
                                text_ids.push( { id:entry_fn_text.prio, t_id:entry_fn_text.t_id, text:entry_fn_text.text } );
                                if (dojo.indexOf(textPost, entry_fn_text.t_id)<0) {
                                    textPost.push( entry_fn_text.t_id );
                                }
                            }
                        });
                        text_ids = sortPrio( text_ids );
                        footnote.push( { id: entry_fn.fn_id, headline: entry_fn.headline, text_id:text_ids } );    
                    }
                });
                textPost = sortArray( textPost );
                if ( textPost.length > 0 ) {
                    getFootnotesText( footnote, textPost, admin );
                }
            },
            error: function ( error ) { }
        });
        
    }
    
    
    function getFootnotesText( footnote, textPost, admin ) {
        dojo.xhrGet ({
            url: '/footnotes/data/get_fn.php?ids='+textPost,
            handleAs: 'json',
            load: function( data ) {
                var layerContent = dojo.create("div", { className:"layerContent" }, dojo.query("body")[0], "last");
                dojo.forEach( footnote, function( entry_fn ) {

                    if (!dojo.isObject( dijit.byId( "fn_"+entry_fn.id ))) {
                
                        var node = dojo.create("span", { id:"fn_"+entry_fn.id+"_content", className:"fnlayer" }, layerContent, "first");
                        dojo.create("span", { className:"layer_header" }, node, "first" );
                        var close = dojo.create("a", { className:"fnclose", href:"#", title:"Schließen" }, node, "last" );
                        dojo.connect( close, 'onclick', function( e ){ dojo.stopEvent( e ); dijit.byId( "fn_"+entry_fn.id ).onCancel(); });
                        
                        var print = dojo.create("a", { className:"fnprint", href:"#", title:"Drucken" }, node, "last" );
                        dojo.connect( print, 'onclick', function( e ){ dojo.stopEvent( e ); printLayer( entry_fn.id ); });
                        var clear = dojo.create("span", {  clear:"both", display:"block" }, node, "last" );
                        var content = dojo.create("span", { id:"c-"+entry_fn.id, className:"content" }, node, "last" );
                        dojo.create("h2", { innerHTML:entry_fn.headline }, content, "first" );
                        
                        var ul = dojo.create("ul", { id:"fn-"+entry_fn.id+"-ul" }, content, "last" );
                        var text_ids = new Array();
                        dojo.forEach( entry_fn.text_id, function( entry_fn_text ) {
                            dojo.forEach( data, function( p ) {
                                if( p.id == entry_fn_text.t_id ) {
                                    if ( admin==true ) {
                                        var cname = entry_fn_text.text.substring(0,entry_fn_text.text.length-4);
                                        dojo.create("li", { innerHTML:p.text, className:cname }, ul, "last" );
                                    } else {
                                        dojo.create("li", { innerHTML:p.text }, ul, "last" );
                                    }
                                }
                            });
                        });
                        var clear2 = dojo.create("span", { clear:"both", display:"block" }, content, "last" );
                        
                        var dialog = new dojox.widget.Dialog({
                            id: "fn_"+entry_fn.id,
                            dimensions: [540,40],
                            draggable: false,
                            easing: dojo.fx.easing.circIn,
                            sizeMethod: "combine",
                            sizeDuration: 300,
                            style:{ backgroundColor:"transparent" }
                        }, "fn_"+entry_fn.id );
                        dijit.byId( "fn_"+entry_fn.id ).domNode.appendChild( dojo.byId( "fn_"+entry_fn.id+"_content" ) );
                        dialog.startup();
                        
                        dojo.query( ".layerContent" )[0].appendChild( dojo.byId( "fn_"+entry_fn.id ) );
                        
                        var t = 0;
                        var elm = dojo.byId( "fn_"+entry_fn.id+"_content" );
                        if (typeof(elm.style && elm.style.height) == 'number') {
                            t = elm.style.height;
                        } else if (typeof(elm.style && elm.style.height) == 'string' && !isNaN(parseInt(elm.style.height))) {
                            t = parseInt(elm.style.height);
                        } else if (elm.offsetHeight) {
                            t = elm.offsetHeight;
                        } else if (typeof(elm.style && elm.style.pixelHeight) == 'number') {
                            t = elm.style.pixelHeight;
                        } else if (elm.clientHeight) {
                            t = elm.clientHeight;
                        }
                        t = (t<40) ? 410 : t;
                        t = 210;
                        var h = -(Math.round(t / 2));
                        dojo.byId( "fn_"+entry_fn.id+"_content" ).style.margin = h+"px 0 0 0";
                        
                    }
                });
                
            },
            error: function ( error ) { }
        });
    }
    
    
    function printLayer( id ) {
        if (dojo.isIE > 0) { 
            var w = window.open("about:blank", "Entertain", "width=580,height=500,location=no,menubar=no,scrollbars=no,status=no,toolbar=no");
            w.document.open();
            w.document.write("<html><head>");
            w.document.writeln("<style rel='stylesheet' type='text/css'>");
            w.document.writeln(".fnlayer .content { float:left; padding:12pt 0 12pt; border:1pt solid #666 !important; }");
            w.document.writeln(".fnlayer h2 { padding:0 12pt 6pt 12pt; margin:0; font-size:12pt !important; font-family:arial,helvetica,verdana,geneva,sans-serif !important; line-height:18pt !important; font-weight:bold; color:#E20074; }");
            w.document.writeln(".fnlayer ul { padding:0 0 0 23pt; margin:0; list-style-type:square; vertical-align:top; }");
            w.document.writeln(".fnlayer li { padding:0 16pt 6pt 0; margin:0; font-size:10pt; font-family:arial,helvetica,verdana,geneva,sans-serif; line-height:13pt; color:#000; }");
            w.document.writeln("</style>");
            w.document.writeln("</head><body><div class='layerContent'><div class='fnlayer'><div class='content'>" + dojo.query("#fn_"+id+" .content")[0].innerHTML);
            w.document.writeln("</div></div></div></body></html>"); 
            w.document.close(); 
            w.focus();
            w.print();
            w.close();
        } else {
            window.print();
        }
    }
    
    

    function sortArray( list ) { 
        var x, y, holder;
        for(x = 0; x < list.length; x++) {
            for(y = 0; y < (list.length-1); y++) {
                if(list[y] > list[y+1]) {
                    holder = list[y+1];
                    list[y+1] = list[y];
                    list[y] = holder;
                }
            }
        }
        return list;
    }

    
    function sortPrio( list ) { 
        var x, y, holder;
        for(x = 0; x < list.length; x++) {
            for(y = 0; y < (list.length-1); y++) {
                if(list[y].id > list[y+1].id) {
                    holder = list[y+1];
                    list[y+1] = list[y];
                    list[y] = holder;
                }
            }
        }
        return list;
    }
    