// Auto-resize all form labels to equal widths, so their text fields are
// left-aligned.
function cstone_utils_resize_form_labels() {
    var max_width = 0;
    $("label[for]").each(function() {
        if ($(this).width() > max_width) { max_width = $(this).width(); }
    });
    $("label[for]").width(max_width);
}

// Size window to fit content
function cstone_utils_size_window_to_fit(content, addnWidth, addnHeight) {
/*    if (typeof console != 'undefined') {
        console.log('body: ' + $('body').offset().left + ',' + $('body').offset().top);
        console.log('body: ' + $('body').width() + ',' + $('body').height());
        console.log('popup: ' + $('#popup').offset().left + ',' + $('#popup').offset().top);
        console.log('popup: ' + $('#popup').width() + ',' + $('#popup').height());
        console.log('content: ' + content.offset().left + ',' + content.offset().top);
        console.log('content: ' + content.width() + ',' + content.height());
        console.log('window: ' + window.innerWidth + ',' + window.innerHeight);
        console.log('doc client: ' + document.documentElement.clientWidth + ',' + document.documentElement.clientHeight);
        console.log('body client: ' + document.body.clientWidth + ',' + document.body.clientHeight);
    }*/

    var newW = content.offset().left + content.width() + addnWidth;
    var newH = content.offset().top + content.height() + addnHeight;

    if (newH > screen.availHeight - 100) {
        newH = screen.availHeight - 100;
    }

    var currentW = window.innerWidth ? window.innerWidth : document.body.clientWidth;
    var currentH = window.innerHeight ? window.innerHeight : document.body.clientHeight;
    window.moveTo((screen.availWidth - newW)/2, (screen.availHeight - newH) / 2);
    window.resizeBy(newW - currentW, newH - currentH);
}

// Pop up window
function cstone_utils_popup(path, name, width, height) {
    if (width == null) width = 450;
    if (height == null) height = 400;

    var url = cstone_utils_base_path + 'popup/' + path;
    window[name] =
        open(url, name,
             "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes");

    if (window[name] && !window[name].closed)
	window[name].focus();

    return false;
}
