// Name:        SilverlightControl.debug.js
// Assembly:    System.Web.Extensions
// Version:     3.6.0.0
// FileVersion: 3.6.20830.0
//!-----------------------------------------------------------------------
//! Copyright (C) Microsoft Corporation. All rights reserved.
//!-----------------------------------------------------------------------
//! SilverlightControl.js
Type.registerNamespace('Sys.UI.Silverlight');

Sys.UI.Silverlight.Control = function Sys$UI$Silverlight$Control(domElement) {
    /// <summary locid="M:J#Sys.UI.Silverlight.Control.#ctor" />
    /// <param name="domElement" domElement="true"></param>
    var e = Function._validateParams(arguments, [
        {name: "domElement", domElement: true}
    ]);
    if (e) throw e;
    Sys.UI.Silverlight.Control.initializeBase(this, [domElement]);
    this._scaleMode = Sys.UI.Silverlight.ScaleMode.none;
}











    function Sys$UI$Silverlight$Control$add_pluginError(handler) {
    /// <summary locid="E:J#Sys.UI.Silverlight.Control.pluginError" />
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().addHandler("pluginError", handler);
    }
    function Sys$UI$Silverlight$Control$remove_pluginError(handler) {
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().removeHandler("pluginError", handler);
    }

    function Sys$UI$Silverlight$Control$add_pluginFullScreenChanged(handler) {
    /// <summary locid="E:J#Sys.UI.Silverlight.Control.pluginFullScreenChanged" />
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().addHandler("pluginFullScreenChanged", handler);
    }
    function Sys$UI$Silverlight$Control$remove_pluginFullScreenChanged(handler) {
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().removeHandler("pluginFullScreenChanged", handler);
    }

    function Sys$UI$Silverlight$Control$add_pluginLoaded(handler) {
    /// <summary locid="E:J#Sys.UI.Silverlight.Control.pluginLoaded" />
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().addHandler("pluginLoaded", handler);
    }
    function Sys$UI$Silverlight$Control$remove_pluginLoaded(handler) {
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().removeHandler("pluginLoaded", handler);
    }

    function Sys$UI$Silverlight$Control$add_pluginResized(handler) {
    /// <summary locid="E:J#Sys.UI.Silverlight.Control.pluginResized" />
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().addHandler("pluginResized", handler);
    }
    function Sys$UI$Silverlight$Control$remove_pluginResized(handler) {
    var e = Function._validateParams(arguments, [{name: "handler", type: Function}]);
    if (e) throw e;
        this.get_events().removeHandler("pluginResized", handler);
    }

    function Sys$UI$Silverlight$Control$get_scaleMode() {
        /// <value type="Sys.UI.Silverlight.ScaleMode" locid="P:J#Sys.UI.Silverlight.Control.scaleMode"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._scaleMode;
    }
    function Sys$UI$Silverlight$Control$set_scaleMode(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: Sys.UI.Silverlight.ScaleMode}]);
        if (e) throw e;
        if (value !== this.get_scaleMode()) {
            this._scaleMode = value;
            if (this._loaded) {
                this._ensureTransform();
            }
        }
    }

    function Sys$UI$Silverlight$Control$get_source() {
        /// <value type="String" locid="P:J#Sys.UI.Silverlight.Control.source"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._source || "";
    }
    function Sys$UI$Silverlight$Control$set_source(value) {
        var e = Function._validateParams(arguments, [{name: "value", type: String}]);
        if (e) throw e;
        if (this._lockSource) {
            throw Error.invalidOperation(Sys.UI.Silverlight.Res.cannotChangeSource);
        }
        this._source = value;
        if (value && this._setOnLoad) {
            this._lockSource = true;
            this.get_element().Source = value;
        }
    }

    function Sys$UI$Silverlight$Control$addEventListener(element, eventName, handler) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.addEventListener" />
        /// <param name="element"></param>
        /// <param name="eventName" type="String"></param>
        /// <param name="handler" type="Function"></param>
        /// <returns></returns>
        var e = Function._validateParams(arguments, [
            {name: "element"},
            {name: "eventName", type: String},
            {name: "handler", type: Function}
        ]);
        if (e) throw e;
        if (!this._boundEvents) {
            this._boundEvents = [];
        }

        var token = element.addEventListener(eventName, handler);
        this._boundEvents[this._boundEvents.length] = { element: element, eventName: eventName, token: token };
        return token;
    }

    function Sys$UI$Silverlight$Control$addEventListeners(element, events, handlerOwner) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.addEventListeners" />
        /// <param name="element"></param>
        /// <param name="events" type="Object"></param>
        /// <param name="handlerOwner" optional="true"></param>
        /// <returns></returns>
        var e = Function._validateParams(arguments, [
            {name: "element"},
            {name: "events", type: Object},
            {name: "handlerOwner", optional: true}
        ]);
        if (e) throw e;
        var tokens = { };
        for (var name in events) {
            var handler = events[name];
            if (typeof(handler) !== 'function') throw Error.invalidOperation(Sys.Res.cantAddNonFunctionhandler);
            if (handlerOwner) {
                handler = Function.createDelegate(handlerOwner, handler);
            }
            tokens[name] = this.addEventListener(element, name, handler);
        }
        return tokens;
    }

    function Sys$UI$Silverlight$Control$dispose() {
        if (this._loaded) {
            this.pluginDispose();
            this._loaded = false;
        }

        var host = this.get_element();
        if (host) {
            if (this._setOnLoad) {
                host.OnLoad = null;
            }
            if (this._setOnError) {
                host.OnError = null;
            }
            if (this._setOnFullScreenChange) {
                host.content.OnFullScreenChange = null;
            }
            if (this._setOnResize) {
                host.content.OnResize = null;
            }
        }

        if (this._boundEvents) {
            for (var i = 0, l = this._boundEvents.length; i < l; i++) {
                var e = this._boundEvents[i];
                e.element.removeEventListener(e.eventName, e.token);
            }
            this._boundEvents = null;
        }

        Sys.UI.Silverlight.Control.callBaseMethod(this, "dispose");
    }

    function Sys$UI$Silverlight$Control$_ensureTransform() {
        // ensures that the plugin has the correct ScaleTransform ScaleX/Y applied
        // based on the ActualWidth and ActualHeight of the plugin and the current ScaleMode.
        var host = this.get_element();
        
        var root = host.content.root;
        // compute the scale x/y that would be required to make the canvas fit the plugin
        var scalePlayer = Sys.UI.Silverlight.Control._computeScale(root);
        var x, y;
        switch (this.get_scaleMode()) {
            case Sys.UI.Silverlight.ScaleMode.zoom:
                // zoom maintains aspect ratio, so select the smaller of the two scales
                x = y = Math.min(scalePlayer.horizontal, scalePlayer.vertical);
                break;
            case Sys.UI.Silverlight.ScaleMode.stretch:
                // stretch just applies each scale x/y independently
                x = scalePlayer.horizontal;
                y = scalePlayer.vertical;            
                break;
            //case Sys.UI.Silverlight.ScaleMode.none:
            default:
                // none always applies no scale (1 times authored size)
                x = y = 1;
        }
        
        Sys.UI.Silverlight.Control._applyScale(root, x, y);
    }

    function Sys$UI$Silverlight$Control$initialize() {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.initialize" />
        if (arguments.length !== 0) throw Error.parameterCount();
        Sys.UI.Silverlight.Control.callBaseMethod(this, "initialize");

        var host = this.get_element();
        var tagName = host.tagName.toUpperCase();
        if ((tagName !== "OBJECT") && (tagName !== "EMBED")) {
            // host is the installation medallion
            this._needsInstallation = true;
        }
        else {
            if (!host.OnError) {
                host.OnError = Function.createDelegate(this, this._pluginError);
                this._setOnError = true;
            }
            
            if (host.Source && this.get_source()) {
                // note: could compare for equality but they are URLs and may be different but equal.
                // It's an error to specify a source when the object already has one, even if they are the same source.
                throw Error.invalidOperation(Sys.UI.Silverlight.Res.sourceAlreadySet);
            }
            if (host.IsLoaded) {
                // source is already loaded, jump straight to plugin loaded event
                this._lockSource = true;
                this._raisePluginLoaded();
            }
            else {
                // existing source is still loading or there is no source.
                // either way, we want to be notified when a document loads.
                host.OnLoad = Function.createDelegate(this, this._pluginLoaded);
                this._setOnLoad = true;
                
                if (!host.Source) {
                    // no source loading, assign the control's source.
                    var source = this.get_source();
                    if (source) {
                        this._lockSource = true;
                        host.Source = source;
                    }
                    // source not locked if none set and none loading.
                    // developer may set_source later, and the loaded event will fire
                }
                else {
                    // since a source is already loading you cannot change the source later
                    this._lockSource = true;
                }
            }
        }
    }

    function Sys$UI$Silverlight$Control$_onFullScreen() {
        // DevDiv Bugs 125915: may have been disposed by now. SL calls us even though we unhook.
        if (!this.get_element()) return;
        this.onPluginFullScreenChanged(Sys.EventArgs.Empty);
        this._raiseEvent("pluginFullScreenChanged");
    }

    function Sys$UI$Silverlight$Control$onPluginError(errorEventArgs) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.onPluginError" />
        /// <param name="errorEventArgs" type="Sys.UI.Silverlight.ErrorEventArgs"></param>
        var e = Function._validateParams(arguments, [
            {name: "errorEventArgs", type: Sys.UI.Silverlight.ErrorEventArgs}
        ]);
        if (e) throw e;
    }

    function Sys$UI$Silverlight$Control$onPluginFullScreenChanged(args) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.onPluginFullScreenChanged" />
        /// <param name="args" type="Sys.EventArgs"></param>
        var e = Function._validateParams(arguments, [
            {name: "args", type: Sys.EventArgs}
        ]);
        if (e) throw e;
    }

    function Sys$UI$Silverlight$Control$onPluginLoaded(args) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.onPluginLoaded" />
        /// <param name="args" type="Sys.EventArgs"></param>
        var e = Function._validateParams(arguments, [
            {name: "args", type: Sys.EventArgs}
        ]);
        if (e) throw e;
    }

    function Sys$UI$Silverlight$Control$onPluginResized(args) {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.onPluginResized" />
        /// <param name="args" type="Sys.EventArgs"></param>
        var e = Function._validateParams(arguments, [
            {name: "args", type: Sys.EventArgs}
        ]);
        if (e) throw e;
        if (this.get_scaleMode() !== Sys.UI.Silverlight.ScaleMode.none) {
            this._ensureTransform();
        }
    }

    function Sys$UI$Silverlight$Control$_onResize() {
        // DevDiv Bugs 125915: may have been disposed by now. SL calls us even though we unhook.
        if (!this.get_element()) return;
        this.onPluginResized(Sys.EventArgs.Empty);
        this._raiseEvent("pluginResized");
    }

    function Sys$UI$Silverlight$Control$pluginDispose() {
        /// <summary locid="M:J#Sys.UI.Silverlight.Control.pluginDispose" />
        if (arguments.length !== 0) throw Error.parameterCount();
    }

    function Sys$UI$Silverlight$Control$_pluginError(slSender, e) {
        // DevDiv Bugs 125915: may have been disposed by now. SL calls us even though we unhook.
        if (!this.get_element()) return;
        var args = new Sys.UI.Silverlight.ErrorEventArgs(e);
        this.onPluginError(args);
        
        if (!args.get_cancel()) {
            // In debug mode, when error goes unhandled, throw exception.
            // What fields you can access depends on the type of the error.
            // Accessing an inappropriate field will cause an error.
            if (!this._raiseEvent("pluginError", args)) {
                var errorType = e.errorType,
                    errorCode = e.errorCode,
                    errorMessage = e.errorMessage,
                    id = this.get_id(),
                    lineNumber = "", charPosition = "", source = "", methodName = "",
                    errorFormat = Sys.UI.Silverlight.Res.otherError;
                    
                // not all the elements are available for all error types (you get an exception if you try)
                if (errorType === "ParserError") {
                    errorFormat = Sys.UI.Silverlight.Res.parserError;
                    lineNumber = e.lineNumber;
                    charPosition = e.charPosition;
                    source = e.xamlFile;
                }
                else if (((errorType === "ImageError") || (errorType === "MediaError")) &&
                        (errorMessage === "AG_E_NOT_FOUND")) {
                        errorFormat = Sys.UI.Silverlight.Res.mediaError_NotFound;
                        errorMessage = slSender.Source;
                }
                else if (errorType === "RuntimeError") {
                    if (e.lineNumber) {
                        errorFormat = Sys.UI.Silverlight.Res.runtimeErrorWithPosition;
                        lineNumber = e.lineNumber;
                        charPosition = e.charPosition;
                        methodName = e.methodName;
                    }
                    else {
                        errorFormat = Sys.UI.Silverlight.Res.runtimeErrorWithoutPosition;
                        methodName = e.methodName;
                    }
                }
                throw Error.invalidOperation(String.format(errorFormat,
                        id,
                        errorType,
                        errorCode,
                        errorMessage,
                        lineNumber,
                        charPosition,
                        methodName,
                        source));
            }
        }
    }

    function Sys$UI$Silverlight$Control$_pluginLoaded() {
        // DevDiv Bugs 125915: may have been disposed by now. SL calls us even though we unhook.
        var element = this.get_element();
        if (element) {
            // DevDiv Bugs 157222: Accessing properties on 'content' fails when dom element is hidden
            // delay assigning FullScreenChange and Resize handlers until pluginLoaded. They can't fire
            // until after load anyway.
            
            // do not overwrite an existing handlers
            if (!element.content.OnFullScreenChange) {
                element.content.OnFullScreenChange = Function.createDelegate(this, this._onFullScreen);
                this._setOnFullScreenChange = true;
            }
            if (!element.content.OnResize) {
                element.content.OnResize = Function.createDelegate(this, this._onResize);
                this._setOnResize = true;
            }
        
            this._raisePluginLoaded();
        }
    }

    function Sys$UI$Silverlight$Control$_raiseEvent(name, args) {
        // in debug mode returns whether the event was handled.
        // pluginError uses this to know whether the error was handled and throws if not (in debug mode)
        var handler = this.get_events().getHandler(name);
        if (handler) {
            handler(this, args || Sys.EventArgs.Empty);
            return true;
        }
        return false;
    }

    function Sys$UI$Silverlight$Control$_raisePluginLoaded() {
        this._loaded = true;
        this.onPluginLoaded(Sys.EventArgs.Empty);
        this._raiseEvent("pluginLoaded");
    }
Sys.UI.Silverlight.Control.prototype = {
    _source: null,
    _needsInstallation: false,
    _loaded: false,
    _boundEvents: null,
    _setOnLoad: false,
    _setOnFullScreenChange: false,
    _setOnResize: false,
    _setOnError: false,
    _lockSource: false,
    add_pluginError: Sys$UI$Silverlight$Control$add_pluginError,
    remove_pluginError: Sys$UI$Silverlight$Control$remove_pluginError,
    add_pluginFullScreenChanged: Sys$UI$Silverlight$Control$add_pluginFullScreenChanged,
    remove_pluginFullScreenChanged: Sys$UI$Silverlight$Control$remove_pluginFullScreenChanged,
    add_pluginLoaded: Sys$UI$Silverlight$Control$add_pluginLoaded,
    remove_pluginLoaded: Sys$UI$Silverlight$Control$remove_pluginLoaded,
    add_pluginResized: Sys$UI$Silverlight$Control$add_pluginResized,
    remove_pluginResized: Sys$UI$Silverlight$Control$remove_pluginResized,
    get_scaleMode: Sys$UI$Silverlight$Control$get_scaleMode,
    set_scaleMode: Sys$UI$Silverlight$Control$set_scaleMode,
    get_source: Sys$UI$Silverlight$Control$get_source,
    set_source: Sys$UI$Silverlight$Control$set_source,
    addEventListener: Sys$UI$Silverlight$Control$addEventListener,
    addEventListeners: Sys$UI$Silverlight$Control$addEventListeners,
    dispose: Sys$UI$Silverlight$Control$dispose,
    _ensureTransform: Sys$UI$Silverlight$Control$_ensureTransform,
    initialize: Sys$UI$Silverlight$Control$initialize,
    _onFullScreen: Sys$UI$Silverlight$Control$_onFullScreen,
    onPluginError: Sys$UI$Silverlight$Control$onPluginError,
    onPluginFullScreenChanged: Sys$UI$Silverlight$Control$onPluginFullScreenChanged,
    onPluginLoaded: Sys$UI$Silverlight$Control$onPluginLoaded,
    onPluginResized: Sys$UI$Silverlight$Control$onPluginResized,
    _onResize: Sys$UI$Silverlight$Control$_onResize,
    pluginDispose: Sys$UI$Silverlight$Control$pluginDispose,
    _pluginError: Sys$UI$Silverlight$Control$_pluginError,
    _pluginLoaded: Sys$UI$Silverlight$Control$_pluginLoaded,
    _raiseEvent: Sys$UI$Silverlight$Control$_raiseEvent,
    _raisePluginLoaded: Sys$UI$Silverlight$Control$_raisePluginLoaded    
}

Sys.UI.Silverlight.Control._computeScale = function Sys$UI$Silverlight$Control$_computeScale(element) {
    // Calculates the scale of a Silverlight element in relation to the Silverlight host.
    // used by scaleMode !== none
    var width = element.width;
    var height = element.height;
    var host = element.getHost();
    return { horizontal: width ? (host.content.ActualWidth / width) : 0,
             vertical: height ? (host.content.ActualHeight / height) : 0 };
}

Sys.UI.Silverlight.Control._applyScale = function Sys$UI$Silverlight$Control$_applyScale(e, x, y) {
    var transform = e.RenderTransform;
    if (!transform) {
        e.RenderTransform = transform =
            e.getHost().content.createFromXaml('<ScaleTransform ScaleX="1" ScaleY="1"/>');
    }
    else if (transform.toString() !== "ScaleTransform") {
        // if root has a transform
        throw Error.invalidOperation(Sys.UI.Silverlight.Res.scaleModeRequiresScaleTransform);
    }
    transform.ScaleX = x;
    transform.ScaleY = y;
}
Sys.UI.Silverlight.Control.registerClass("Sys.UI.Silverlight.Control", Sys.UI.Control);
Sys.UI.Silverlight.ErrorEventArgs = function Sys$UI$Silverlight$ErrorEventArgs(error) {
    /// <summary locid="M:J#Sys.UI.Silverlight.ErrorEventArgs.#ctor" />
    /// <param name="error"></param>
    var e = Function._validateParams(arguments, [
        {name: "error"}
    ]);
    if (e) throw e;
    this._error = error;
    Sys.UI.Silverlight.ErrorEventArgs.initializeBase(this);
}

    function Sys$UI$Silverlight$ErrorEventArgs$get_error() {
        /// <value locid="P:J#Sys.UI.Silverlight.ErrorEventArgs.error"></value>
        if (arguments.length !== 0) throw Error.parameterCount();
        return this._error;
    }
Sys.UI.Silverlight.ErrorEventArgs.prototype = {
    get_error: Sys$UI$Silverlight$ErrorEventArgs$get_error
}
Sys.UI.Silverlight.ErrorEventArgs.registerClass("Sys.UI.Silverlight.ErrorEventArgs", Sys.CancelEventArgs);
Sys.UI.Silverlight.ScaleMode = function Sys$UI$Silverlight$ScaleMode() {
    /// <summary locid="M:J#Sys.UI.Silverlight.ScaleMode.#ctor" />
    /// <field name="none" type="Number" integer="true" static="true" locid="F:J#Sys.UI.Silverlight.ScaleMode.none"></field>
    /// <field name="zoom" type="Number" integer="true" static="true" locid="F:J#Sys.UI.Silverlight.ScaleMode.zoom"></field>
    /// <field name="stretch" type="Number" integer="true" static="true" locid="F:J#Sys.UI.Silverlight.ScaleMode.stretch"></field>
    if (arguments.length !== 0) throw Error.parameterCount();
    throw Error.notImplemented();
}




Sys.UI.Silverlight.ScaleMode.prototype = {
    none: 0,
    zoom: 1,
    stretch: 2
}
Sys.UI.Silverlight.ScaleMode.registerEnum('Sys.UI.Silverlight.ScaleMode');


Type.registerNamespace('Sys.UI.Silverlight');
Sys.UI.Silverlight.Res={
"cannotChangeSource":"The source cannot be changed after initialization.",
"runtimeErrorWithoutPosition":"Runtime error {2} in control \u0027{0}\u0027, method {6}: {3}",
"scaleModeRequiresScaleTransform":"When ScaleMode is set to zoom or stretch, only a ScaleTransform is permitted on the root Canvas.",
"mediaError_NotFound":"Media \"{3}\" in control \"{0}\" could not be found.",
"runtimeErrorWithPosition":"Runtime error {2} in control \u0027{0}\u0027, method {6} (line {4}, col {5}): {3}",
"sourceAlreadySet":"The source cannot be set because the Silverlight host already has a source.",
"otherError":"{1} error #{2} in control \u0027{0}\u0027: {3}",
"parserError":"Invalid XAML for control \u0027{0}\u0027. [{7}] (line {4}, col {5}): {3}"
};

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();