﻿Type.registerNamespace("Lush");

/// CREATES IMAGE BORDERS AND RENDERS THE ALT TAG UNDERNEATH
Lush.AttachmentIcons = function(contentElement) {
    this._contentElement = contentElement;
    $addHandler(window, "load", this.addInContent); 
}

Lush.AttachmentIcons.prototype = {
    addInContent: function(e) {
        //Add it to our list of controls
        var as = $get('pageContentDiv').getElementsByTagName('A');
        for (var i = 0; i < as.length; i++) {
            var a = as[i];
            var showsize = (a.attributes["showsize"] && a.attributes["showsize"].value == "true");
            var showicon = (a.attributes["showicon"] && a.attributes["showicon"].value == "true");

            if (!a.href || (!showsize && !showicon)) continue;

            net_studiolush_cms_common_OM_Files_Binary.GetBinaryProperties(a.href, window._AttachmentIcons.binaryPropertiesFound, window._AttachmentIcons.binaryPropertiesNotFound, a);
        }
    },
    binaryPropertiesFound: function(result, userContext, methodName) {
        var props = result;
        var a = userContext;
        if (props.IconUrl) {
            a.className = a.className + ' AttachmentIcon';
            a.style.backgroundImage = "url('" + props.IconUrl + "')";
        }
        if (props.Size || props.Extension) {
            if (props.Extension) props.Extension = props.Extension.toUpperCase().replace('.', '');
            var span = document.createElement('SPAN');
            span.className = 'AttachmentSize';
            if (props.Size && props.Extension)
                span.innerHTML = '&nbsp;(' + props.Extension + ', ' + props.Size + ')';
            else if (props.Size)
                span.innerHTML = '&nbsp;(' + props.Size + ')';
            else
                span.innerHTML = '&nbsp;(' + props.Extension + ')';

            Node_insertAfter(a, span);
        }
    },
    binaryPropertiesNotFound: function(result, userContext, methodName) {
        //Do nothing if an icon wasn't found, not critical
    },

    dispose: function() {
    }
}
Lush.AttachmentIcons.registerClass('Lush.AttachmentIcons', null, Sys.IDisposable);

window._AttachmentIcons = new Lush.AttachmentIcons(window);