function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://crestidg.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function SegNumToFileSize (segNum) {
    filesize = segNum * 1024;

    if (filesize >= 1073741824) {
	filesize = number_format(filesize / 1073741824, 2, '.', '') + ' Gb';
    } else {
	if (filesize >= 1048576) {
     	    filesize = number_format(filesize / 1048576, 2, '.', '') + ' Mb';
   	} else {
	    if (filesize > 1024) {
    		filesize = number_format(filesize / 1024, 0) + ' Kb';
  	    } else {
    		filesize = '< 1 Kb';
	    };
 	};
    };
    return filesize;
};

/**
 * @brief Convert binary data represented as non-escaped hex string to Uint8Array
 * @param str String like ba0cb43e4b9639c114a0487d5faa7c70452533963fc8beb37d1b67c09a48a21d
 *
 * Note that if string length is odd, null will be returned
 */
StringHashToUint8Array = function (str) {
    if (str.length % 2 != 0) {
        return null;
    }

    var buf = new Uint8Array (str.length / 2);

    for (var i = 0; i < str.length; i+=2) {
        value = parseInt (str.substring (i, i+2), 16);
        buf[i/2] = value;
    }

    return buf;
};

imgFullPath = function (imgName) {
    return "images/" + imgName + ".png";
}

fileExtension = function (fileName) {
    defaultExtension = "file";
    knownExtensions = ["ai", "aiff", "bib", "bz2", "c", "chm", "conf", "cpp", "css", "csv", "deb", "divx", "doc", "file", "gif", "gz", "hlp", "htm", "html", "iso", "jpeg", "jpg", "js", "mov", "mp3", "mpg", "odc", "odf", "odg", "odi", "odp", "ods", "odt", "ogg", "pdf", "pgp", "php", "pl", "png", "ppt", "pptx", "ps", "py", "ram", "rar", "rb", "rm", "rpm", "rtf", "sql", "swf", "sxc", "sxd", "sxi", "sxw", "tar", "tex", "tgz", "txt", "vcf", "wav", "wma", "wmv", "xls", "xml", "xpi", "xvid", "zip"];

    extStart = fileName.lastIndexOf('.');
    if (extStart < 0) {
	return imgFullPath (defaultExtension);
    }

    extension = fileName.substr (extStart+1);
    // return imgFullPath (extension);
    if ($.inArray(extension, knownExtensions) >= 0) {
    	return imgFullPath (extension);
    }
    else {
    	return imgFullPath (defaultExtension);
    }
};


openHistoryForItem = function (fileName) {
    url = new HistoryClosure (null).base_url ("fileHistory")
    url += "&item=" + encodeURIComponent (encodeURIComponent (fileName));
    document.location = url;
};

displayContent = function (newcontent, more, baseUrl) {

    $("#content").fadeOut ("fast", function () {
        $(this).replaceWith (newcontent);
        $("#content").fadeIn ("fast");
    });

    $("#content-nav").fadeOut ("fast", function () {
        $("#content-nav a").hide ();

        if (PARAMS.offset !== undefined || more !== undefined) {
            $("#content-nav").fadeIn ("fast");

            if (more !== undefined) {
                $("#get-more").show ();

                $("#get-more").unbind ('click').click (function () {
                    url = baseUrl;
                    url += "&offset="+more;

                    document.location = url;
                });
            }
            if (PARAMS.offset > 0) {
                $("#get-less").show ();

                $("#get-less").unbind ('click').click (function () {
                    url = baseUrl;
                    if (PARAMS.offset > 5) {
                        url += "&offset="+(PARAMS.offset - 5);
                    }

                    document.location = url;
                });
            }
        }
    });
};

function custom_alert (output_msg, title_msg)
{
    if (!title_msg)
        title_msg = 'Alert';

    if (!output_msg)
        output_msg = 'No Message to Display';

    $("<div></div>").html(output_msg).dialog({
        title: title_msg,
        resizable: false,
        modal: true,
        buttons: {
            "Ok": function()
            {
                $( this ).dialog( "close" );
            }
        }
    });
}