function trim(str)
{
    return str.replace(/^\s*|\s*$/g,"");
}

function browseAttachments(url, field_id, type, subtype)
{
    url += '?field=' + field_id;
    if (type) {
        url += '&type=' + type;
        if (subtype) {
            url += '&subtype=' + subtype;
        }
    }

    window.open(url, 'att_browser', 'top=10,left=10,width=600,height=500,location=0,status=1,scrollbars=1,toolbar=0,resizable=1');
}

function sendBack(field_id, id, guid, filename, width, height)
{
    if (field_id == 'tinyMCE') {
        sendBackToTinyMCE(guid, filename, width, height);
    } else if (field_id.substr(0, 18) == 'gallery_container_') {
        sendBackToGalleryContainer(field_id.substr(18, field_id.length - 18), id, guid, filename, width, height);
    } else {
        var win = window.opener;
        var elm = win.document.getElementById(field_id);
        if (elm) {
            elm.value = id;
            with (win.document) {
                getElementById('txt_' + field_id).firstChild.nodeValue = filename;
                getElementById('reset_' + field_id).style.display = 'inline';

                var preview = getElementById('preview_' + field_id);
                if (!preview) {
                    var container = getElementById('preview_container_' + field_id);
                    var preview = createElement('img');

                    container.appendChild(preview);

                    preview.id = 'preview_' + field_id;
                    preview.alt = 'preview';
                }
                preview.src = generateAttachmentUrl(guid, filename, 200, 48, true);
            }
            win.focus()
            window.close();
        }
    }
}

function sendBackMany(field_id, fields)
{
    if (field_id.substr(0, 18) == 'gallery_container_') {
        var win = window.opener;
        var gallery_name = field_id.substr(18, field_id.length - 18);

        fields.each(
            function (item)
            {
                win.addImageToGallery(gallery_name, item.id, item.guid, item.filename, item.width, item.height);            
            }
        )

        win.focus();
        window.close();        
    } else {
        sendBack(field_id, fields[0].id, fields[0].guid, fields[0].filename, fields[0].width, fields[0].height);
    }
}

function sendBackToGalleryContainer(gallery_name, id, guid, filename, width, height)
{
    var win = window.opener;
    win.addImageToGallery(gallery_name, id, guid, filename, width, height);
    win.focus();
    window.close();        
}

function sendBackToTinyMCE(guid, filename, width, height)
{
    var url = 'attachment/' + guid + '/' + filename;
    var win = opener.tinyMCE.getWindowArg('window');

    if (win.document.getElementById('src')) {
        win.document.getElementById('src').value = url;
        win.showPreviewImage(url);                
    } else if (win.document.getElementById('file')) {
        win.document.getElementById('file').value = url;
        win.document.getElementById('width').value = width;
        win.document.getElementById('height').value = height;
    }

    win.focus();

    window.close();
}

function resetAttachmentsChoice(id)
{
    with (document) {
        getElementById(id).value = '';
        getElementById('txt_' + id).firstChild.nodeValue = '(файл не выбран)';
        getElementById('reset_' + id).style.display = 'none';

        var preview = getElementById('preview_' + id);
        if (preview) {
            preview.parentNode.removeChild(preview);
        }
    }
}

function generateAttachmentUrlFromObj(obj)
{
    return generateAttachmentUrl(obj.guid, obj.filename, obj.width, obj.height, true);
}

function generateAttachmentPreviewUrlFromObj(obj)
{
    return generateAttachmentUrl(obj.guid, obj.filename, 64, 64, true);
}

function generateAttachmentUrl(guid, filename, width, height, proportional)
{
    var url = '/attachment/' + guid;

    if (proportional == true)
        url += '/proportional';

    if (width || height) {
        url += '/';
        if (width)
            url += width;
        url += 'x';
        if (height)
            url += height;
    }

    url += '/' + filename;

    return url;
}
