window.AgInstall = {
    INSTALL_REQUIRED: "install",
    UPGRADE_REQUIRED: "upgrade",
    RESTART_REQUIRED: "restart",
    INSTALLING: "installing",
    NOT_SUPPORTED: "notSupported",
    NARROW:"Narrow",
    SMALL:"Small",
    WIDE: "Wide",
    ES: "ES",
    
    IsIE6: typeof window.XMLHttpRequest == "undefined" && window.external,
    IsIE: /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent),

    DefaultConfig:
    {
        title: "Silverlight Project",
        picture: "images/wide/light.jpg",
        buttonLabel: "UPDATE AND START &gt;"
    },

    Template: function(t, i) {
        return "<div class=\"slInstall\"><div class=\"slInstallPopup" + t + "\"><div class=\"slPopupContent" + t + "\"><div class=\"slScreenshot" + t + "\"><img src=\"" + i.config.picture + "\" /></div><div class=\"slTextContent" + t + "\"><div class=\"slHeadline" + t + "\">" + i.config.title + "</div><div class=\"slDescription\"></div><img class=\"slDivider\" src=\"" + (t == AgInstall.WIDE ? "images/wide/divider.png" : "images/narrow/divider.png") + "\" /><a class=\"slButton" + t + "\" href=\"javascript:Silverlight.getSilverlight('{1}');\">" + i.config.buttonLabel + "</a></div></div><div class=\"slPopupTop" + t + "\"></div><div class=\"slPopupLoop" + t + "\"></div><div class=\"slPopupBot" + t + "\"></div></div></div>";
 },

    // Iterate objects and copy contents where it is missing in the original
    Fill: function(f, t) {
        for (var k in f) {
            if (!t[k] || (typeof t[k] != typeof f[k]))
                t[k] = f[k];
            else if (typeof f[k] == "object")
                AgInstall.Fill(f[k], t[k]);
        }
    },

    Installer: function(params) {

        var NEED_INSTALL = 'install';
        this.config = params.installerUiConfig;
    
        params.properties.alt = NEED_INSTALL;
        var version = params.properties.version;

        var element = document.getElementById(params.parentElement);
        if (!element) {
            alert("Silverlight could not find a DOM element with id '" + params.parentElement + "'.");
            return;
        }

      //  var parametros = params.properties.parametros;
        // Perform the injection ourselves, so make sure Silverlight knows not to do it
        params.parentElement = null;
        var obj = Silverlight.createObjectEx(params);

        if (obj == NEED_INSTALL) {
            // Installation is required.
            var t = AgInstall.WIDE;

            var w = element.offsetWidth;
            var h = element.offsetHeight;
            if (w <= 350 || h <= 475)
                t = AgInstall.SMALL;
            else if ((w <= 600 && w >= 350) && (h <= 700 && h >= 475))
                t = AgInstall.NARROW;
            this.type = this.config.type ? this.config.type : t;
            t = AgInstall.WIDE;
            this.type=AgInstall.WIDE;

            // Ensure that the styles are attached.
            if (!Silverlight.attachedInstallDependencies) {
                document.write('<link rel="stylesheet" type="text/css" href="', 'css/slInstall.css', '"/>');
                // If we are in IE6, attach a stylesheet that allows for png transparency
                if (AgInstall.IsIE6)
                    document.write('<link rel="stylesheet" type="text/css" href="', 'css/ie6.css', '"/>');
                Silverlight.attachedInstallDependencies = true;
            }
            if (!Silverlight.attachedTemplateDependencies || !Silverlight.attachedTemplateDependencies[this.type]) {
                document.write('<link rel="stylesheet" type="text/css" href="', 'css/slInstall' + this.type + '.css', '"/>');
            }
            if (!Silverlight.installControls)
                Silverlight.installControls = [];
            Silverlight.installControls.push(this);

            element.innerHTML = AgInstall.Template(this.type, this).replace(/\{1\}/g, version);

            this.root = element;
            element.style.textAlign = "center";
            var divs = this.root.getElementsByTagName("*");
            for (var i = 0; i < divs.length; i++) {
                this[divs[i].className] = divs[i];
            }
            this.state = AgInstall.UPGRADE_REQUIRED;

            // Update the installer to a predefined state
            this.update = function(state) {
                // If no state was passed, just redraw with the current state
                if (!state)
                    state = this.state;
                if (this.slDivider && (this.type == AgInstall.WIDE || this.type == AgInstall.NARROW))
                    this.slDivider.style.display = 'block';


                this['slButton' + this.type].style.display = 'block';
                this.state = state;

                //alert(state + " " + this.type.toLowerCase());
                var desc = this.config.description[state][this.type.toLowerCase()];
                switch (state) {
                    case AgInstall.INSTALLING:
                        if (this.slDivider)
                            this.slDivider.style.display = 'none';
                        this['slButton' + this.type].style.display = 'none';
                        break;
                    case AgInstall.RESTART_REQUIRED:
                        if (this.slDivider)
                            this.slDivider.style.display = 'none';
                        this['slButton' + this.type].style.display = 'none';
                        break;
                    case AgInstall.NOT_SUPPORTED:
                        if (this.slDivider)
                            this.slDivider.style.display = 'none';
                        this['slButton' + this.type].style.display = 'none';
                        break;
                }
                this.slDescription.innerHTML = desc;
                // The layout updating is slightly different for the small install experience
                if (this.type == AgInstall.SMALL) {
                    var contentSmallHeight = this.slInstallSmall.offsetHeight;
                    var calcContentSmallHeight = contentSmallHeight - 59;
                    this.slInstallSmall.style.height = Math.max(calcContentSmallHeight, this.root.offsetHeight - 2) + "px";
                }
                else if (this.type == AgInstall.ENCODER) {
                    this.root.style.textAlign = "center";
                    w = this.config.pictureWidth;
                    h = this.config.pictureHeight;
                    var small = this.config.pictureWidth < 300 || this.config.pictureHeight < 250;
                    if (small || (state != AgInstall.INSTALL_REQUIRED && state != AgInstall.UPGRADE_REQUIRED))
                        this.slInstallEncoderPlayBtn.style.display = 'none';
                    else
                        this.slInstallEncoderPlayBtn.style.display = 'block';

                    //var w = Math.max(300, this.config.pictureWidth);
                    //var h = Math.max(250, this.config.pictureHeight);
                    this.slInstallEncoder.style["background"] = "#000000 url(" + this.config.picture + ") no-repeat scroll center center";
                    this.slInstallWrapper.style.width = this.slInstallEncoder.style.width = (w - (AgInstall.IsIE ? 0 : 1)) + "px";
                    this.slInstallWrapper.style.height = this.slInstallEncoder.style.height = (h - (AgInstall.IsIE ? 0 : 1)) + "px";
                    this.slInstallEncoderOverlay.style.width = this.slInstallEncoder.style.width = w + "px";
                    this.slInstallEncoderOverlay.style.height = this.slInstallEncoder.style.height = h + "px";
                    var calcContentHeight = this.slInstallEncoder.offsetHeight - (small ? 0 : 102) - this['slInstallEncoderTitle'].offsetHeight - 55 - 18;
                    this['slInstallEncoderContent'].style.height = calcContentHeight + "px";
                }
                else {
                    var t = this['slPopupTop' + this.type].offsetHeight;
                    var b = this['slPopupBot' + this.type].offsetHeight;
                    var h = Math.max(t + b, this['slPopupContent' + this.type].offsetHeight + 20);
                    this['slPopupLoop' + this.type].style.height = Math.max(0, h - t - b) + "px";
                    this['slInstallPopup' + this.type].style.height = h + "px";
                    this['slInstallPopup' + this.type].style.marginTop = -Math.round(this['slInstallPopup' + this.type].offsetHeight * 0.5) + "px"; //Math.round((this.root.offsetHeight / 2) - (this['slInstallPopup' + this.type].offsetHeight / 2)) + "px";
                }
            };
        }
        // No installation required, inject the control
        else
            element.innerHTML = obj;
    }
}
