if (!window.RTVCPlayer)
    window.RTVCPlayer = { };

// Properties
RTVCPlayer.Stream = null;

RTVCPlayer.embedSilverlight = function(id, thumbnail, copyright) {
    if (id != null) {
        if (id.toString().indexOf("http") == 0) {
            this.id = 'ec';
            this.Stream = id.toString();
        }
        else {
            this.id = id;
            this.Stream = getStreaming(id);
        }
    }

    this.thumbnail = thumbnail;
    this.width = "100%";
    this.height = "100%";
    
    //var altHtml = pluginId == "sl1" ? null : "<!--not installed-->";
    return Silverlight.createObjectEx({
        source: "/ClientBin/RTVCPlayer.xap",
        parentElement: null,
        id: "SilverlightControl",
        properties: {
            width: this.width,
            height: this.height,
            background: "black",
            alt: "",
            version: "2.0.40115.0", //"3.0.40624.0",
            autoupgrade: true
        },
        events: {
            onError: onSLError,
            onLoad: onSLLoad
        },
        initParams: "m=" + this.Stream + ",autostart=false,thumbnail=" + this.thumbnail + ",copyright=" + copyright + ",autohide=false",
        context: null
    });
}

RTVCPlayer.Zapping = function(id, thumbnail) {
    RTVCPlayer.Zapping(id, thumbnail, null);
}

RTVCPlayer.Zapping = function(id, thumbnail, copyright) {
    try 
    {
        var silverlightControl = document.getElementById('SilverlightControl');
        if (silverlightControl) {
            this.Stream = getStreaming(id);
            silverlightControl.content.Page.Source(this.Stream, thumbnail, copyright);
        }
    }
    catch (msg) { }
}

RTVCPlayer.Play = function(id, thumbnail) {
    try {
        var silverlightControl = document.getElementById('SilverlightControl');
        if (silverlightControl) {
            silverlightControl.content.Page.Source(id, thumbnail, null);
        }
    }
    catch (msg) { }
}

RTVCPlayer.Stop = function(id) {
    var silverlightControl = document.getElementById('SilverlightControl');
    if (silverlightControl) {
        silverlightControl.content.Page.Stop();
    }
}

function getStreaming(id) {
    var _streaming = null;

    switch (id) {
        case "radio":
            _streaming = "mms://wm.gobiernodecanarias.org/radio";
            break;
        case "sat":
            _streaming = "mms://wm.gobiernodecanarias.org/tvcsat";
            break;
        case "tv":
            _streaming = "mms://wm.gobiernodecanarias.org/tvctdt00";
            break;
        default:
            _streaming = id;           
    }
    
    return _streaming;
}

function onSLLoad(plugIn, userContext, sender) {
    window.status += "Silverlight se cargo correctamente.";
}

function onSLError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    var errMsg = "Unhandled Error in Silverlight 2 Application \n";
    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }
         
    /*
    // Display error
    alert(errMsg);
    */     
    
    window.onerror = function() {
        return true;
    }     
}



