var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        
        var utftext = '';
        /* string = string.replace(/\r\n/g, '\n'); */
        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = '';
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

};



function toggleComment(id) {
    var displayvalue;
    if (browser.isFirefox || browser.isOpera) {
        displayvalue = 'table-row';
    }
    else if (browser.isIE) {
        displayvalue = 'inline';
    }
    else
    {
        displayvalue = 'table-row';
    }


    if (document.getElementById) {
        var rowComment = document.getElementById('comment' + id)
        if (rowComment.style.display == 'none') {
            document.getElementById('imgstatus' + id).src = '/images/catsearch_minus.gif';
            rowComment.style.display = displayvalue;
            rowComment.style.background = 0;
        }
        else {
            document.getElementById('imgstatus' + id).src = '/images/catsearch_plus.gif';
            rowComment.style.display = 'none';
        }
    }
};


function setVisible(obj, track, urn, name, city, date, size)
{
    obj = document.getElementById(obj);
    var myWidth, myHeight;

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else
        if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else
        if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientWidth;
        // stupid IE does not understand CSS
        document.getElementById('text').style.background = '#E0E0E0';
        document.getElementById('text').style.borderWidth='0 2 2 0';
        document.getElementById('text').style.cursor='pointer';
    }

    if (obj.style.visibility == 'visible') {
        obj.style.visibility = 'hidden';
        document.body.style.background = 'white';
    }
    else { // show window
        obj.style.visibility = 'visible';
        obj.style.left = String(myWidth * (browser.isIE ? 0.18 : 0.3)) + 'px';
        obj.style.top = String(myHeight * (browser.isIE ? 0.18 : 0.3)) + 'px';
        document.body.style.background = 'LightGrey';
        document.getElementById('name').firstChild.nodeValue = name;
        var file = urn.lastIndexOf('/') == -1 ? urn : urn.substr(urn.lastIndexOf('/')+1);
        document.getElementById('mp3lnk').firstChild.nodeValue = file;
        document.getElementById('ziplnk').firstChild.nodeValue = file.replace(/.mp3/i, '.zip');
        document.getElementById('mp3lnk').href = 'http://bvgm.org/mp3.php?disk_number=' + track + '&urn=' + Url.encode(urn);
        document.getElementById('ziplnk').href = 'http://bvgm.org/mp3.php?disk_number=' + track + '&urn=' + Url.encode(urn.replace(/.mp3/i, '.zip'));
        document.getElementById('mp3size').firstChild.nodeValue = Math.round(size/1024) + 'Kb';
        document.getElementById('zipsize').firstChild.nodeValue = Math.round(size/1024) + 'Kb';
        document.getElementById('date').firstChild.nodeValue = date;
        document.getElementById('city').firstChild.nodeValue = city;
    }
};


