YAHOO.widget.DataTable.MSG_LOADING = 'wird geladen…';
YAHOO.widget.DataTable.MSG_ERROR = 'Fehler.';

YAHOO.util.Event.onDOMReady(function() {
    if (document.getElementById('schlichtung')) {
        var schlichtungTabView = new YAHOO.widget.TabView('schlichtung');


        var tab0 = schlichtungTabView.getTab(0);
        var labelEl0 = tab0.get('labelEl');
        /*        tab0.set('label', '+ ' + tab0.get('label'));*/
        labelEl0.parentNode.parentNode.setAttribute('title', 'aktiv');
        labelEl0.firstChild.appendChild(document.createTextNode('aktiv: '));

        function handleTab0(e) {
            var thistab = tab0;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                // console.log(tab0.get('label'));
                //        thistab.set('label', '+ ' + thistab.get('label'));
                //		labelEl.appendChild(activeLabel);
                //replaceChild(Textknoten, document.getElementById("z2").firstChild);
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                //var lab = thistab.get('label');
                //thistab.set('label', lab.substring(lab.indexOf(' ') + 1, lab.length));
                // console.log(tab0.get('label'));
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }

        }


        tab0.addListener('activeChange', handleTab0);

        var tab1 = schlichtungTabView.getTab(1);
        var labelEl1 = tab1.get('labelEl');
        labelEl1.firstChild.appendChild(document.createTextNode('inaktiv: '));

        function handletab1(e) {
            var thistab = tab1;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }
        }


        tab1.addListener('activeChange', handletab1);


        var tab2 = schlichtungTabView.getTab(2);
        var labelEl2 = tab2.get('labelEl');
        labelEl2.firstChild.appendChild(document.createTextNode('inaktiv: '));

        function handletab2(e) {
            var thistab = tab2;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
				
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }
        }


        tab2.addListener('activeChange', handletab2);


        var tab3 = schlichtungTabView.getTab(3);
        var labelEl3 = tab3.get('labelEl');
        labelEl3.firstChild.appendChild(document.createTextNode('inaktiv: '));

        function handletab3(e) {
            var thistab = tab3;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
				
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }
        }


        tab3.addListener('activeChange', handletab3);


        var tab4 = schlichtungTabView.getTab(4);
        var labelEl4 = tab4.get('labelEl');
        labelEl4.firstChild.appendChild(document.createTextNode('inaktiv: '));

        function handletab4(e) {
            var thistab = tab4;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
				
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }
        }


        tab4.addListener('activeChange', handletab4);


        var tab5 = schlichtungTabView.getTab(5);
        var labelEl5 = tab5.get('labelEl');
        labelEl5.firstChild.appendChild(document.createTextNode('inaktiv: '));

        function handletab5(e) {
            var thistab = tab5;
            var labelEl = thistab.get('labelEl');
            if (e.newValue == e.prevValue) {
                return true;
            }
            if (e.newValue) {
                labelEl.firstChild.replaceChild(document.createTextNode('aktiv: '), labelEl.firstChild.firstChild);
				
        		labelEl.parentNode.parentNode.setAttribute('title', 'aktiv');
            } else {
                labelEl.firstChild.replaceChild(document.createTextNode('inaktiv: '), labelEl.firstChild.firstChild);
        		labelEl.parentNode.parentNode.setAttribute('title', '');
            }
        }


        tab5.addListener('activeChange', handletab5);


    }
});
YAHOO.util.Event.onDOMReady(function() {
    YAHOO.example.EnhanceFromMarkup = new
    function() {

        var mySortFunction = function(a, b, desc) {

            var parseLinkFromString = function(sString) {
                return sString.substring(sString.indexOf('>') + 1, sString.indexOf('<', 2));
            };

            // Deal with empty values
            if (!YAHOO.lang.isValue(a)) {
                return (!YAHOO.lang.isValue(b)) ? 0: 1;
            }
            else if (!YAHOO.lang.isValue(b)) {
                return - 1;
            }

            var comp = YAHOO.util.Sort.compare;
            return comp(parseLinkFromString(a.getData("titel")), parseLinkFromString(b.getData("titel")), desc);
        };

        var mySortGermanString = function(a, b, desc) {

            var parseLinkFromString = function(sString) {
                if (sString.indexOf('>') != -1) {
                    return sString.substring(sString.indexOf('>') + 1, sString.indexOf('<', 2));
                }
            };

            var rerenderAeOeUe = function(sString) {
                // console.log("Eingang in den rerenderAeOeUe mit " + sString);
                /*
						if (sString.indexOf('Ö')==0) {
							 // console.log("OZZ" + sString.substr(2, sString.length)); 
							sString = "OZZ" + sString.substr(2, sString.length);
						}                  
						if (sString.indexOf('&Ouml;')==0) {
						// console.log(sString);  
							sString = "OZZ" + sString.substr(6, sString.length);
						}                                          
						if (sString.indexOf('Ä')==0) {
						// console.log(sString);
							sString = "AZZ" + sString.substr(1, sString.length);
						} 
						if (sString.indexOf('Ü')==0) {
						// console.log(sString);
							sString = "UZZ" + sString.substr(1, sString.length);
						}                   
						*/
                sString = sString.replace(/\u00C4/g, 'A~');
                //A umlaut
                sString = sString.replace(/\u00e4/g, 'a~');
                //a umlaut
                sString = sString.replace(/\u00D6/g, 'O~');
                //O umlaut
                sString = sString.replace(/\u00f6/g, 'o~');
                //o umlaut
                sString = sString.replace(/\u00DC/g, 'U~');
                //U umlaut
                sString = sString.replace(/\u00fc/g, 'u~');
                //u umlaut
                // console.log("Neu: " + sString);
                return sString;
            };

            // Deal with empty values
            if (!YAHOO.lang.isValue(a)) {
                return (!YAHOO.lang.isValue(b)) ? 0: 1;
            }
            else if (!YAHOO.lang.isValue(b)) {
                return - 1;
            }

            var comp = YAHOO.util.Sort.compare;
            var first = rerenderAeOeUe(a.getData("partner"));
            var second = rerenderAeOeUe(b.getData("partner"));
            // console.log("vergleiche " + first + " und " + second);
            return comp(first, second, desc);
        };

        var myColumnDefs = [
        {
            key: "partner",
            label: "Partner",
            sortable: true,
            sortOptions: {
                sortFunction: mySortGermanString
            }
        },
        {
            key: "titel",
            label: "Titel",
            sortable: true,
            sortOptions: {
                sortFunction: mySortFunction
            }
        },
        {
            key: "jahr",
            label: "Jahr",
            sortable: true
        },
        {
            key: "einigung",
            label: "Einigung",
            sortable: true
        },
        {
            key: "klage",
            label: "Klage",
            sortable: true
        }
        ];





        this.myDataSource = new YAHOO.util.DataSource(YAHOO.util.Dom.get("schlichtungentabelle"));
        this.myDataSource.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
        this.myDataSource.responseSchema = {
            fields: [{
                key: "partner",
                parser: YAHOO.util.DataSource.parseTextData
            },
            {
                key: "titel"
            },
            {
                key: "jahr"
            },
            {
                key: "einigung",
                parser: YAHOO.util.DataSource.parseTextData
            },
            {
                key: "klage",
                parser: YAHOO.util.DataSource.parseTextData
            }
            ]
        };

        this.myDataTable = new YAHOO.widget.DataTable("tabsch", myColumnDefs, this.myDataSource,
        {
            sortedBy: {
                key: "jahr",
                dir: "desc"
            }
        });
    };
});