﻿// Función para cargar dinámicamente objetos silverlight en un div de Html.
function createSL(source, DivHostId, silverlightControlId, Token) {
    Silverlight.createObjectEx({
        source: source,
        parentElement: document.getElementById(DivHostId),
        id: silverlightControlId,
        properties: {
            width: "100%",
            height: GetHeightScreen(),
            background: "#FF121727",
            alt: "<!--silverlight not installed-->",
            version: "4.0.50826.0",
            splashscreensource: "Content/Styles/SplashScreen.xaml",
            windowless:"true"
        },
        events: {
            //onError: onSLError,
            //onLoad: onSLLoad,
            onSourceDownloadProgressChanged: 'onSourceDownloadProgressChanged',
            onSourceDownloadComplete: 'onSourceDownloadComplete'
        },
        initParams: "Guid=" + Token
    });
}

function changeWidth(DivIdHtml, DivIdSl, WidthHtml) {
    $("#" + DivIdHtml).width(WidthHtml);
    var tam = GetWidthScreen() - WidthHtml;
    $("#" + DivIdSl).width(tam);

    if (WidthHtml == 0) {
        $("#HostHTML").hide();
    }
    else {
        $("#HostHTML").show();
    }
}

function GetWidthHtmlPanel() {
    var w = $("#HostHTML").width();
    return w;
}

function SetHeight(height) {
    var theHeight = GetHeightScreen();
    // Adaptamos la altura del timeline a la pantalla del usuario.
    $("#HostSL").height(theHeight);
}

function SetWidth(width) {
    $("#HostSL").width(width);
}

function SetUserName(name) {

   /* var idiomaNavegador = new String;    

    if (navigator.userLanguage) 
    {
        idiomaNavegador = navigator.userLanguage;
        alert("1.-" + idiomaNavegador);
    }
    else if (navigator.language)
    {
        idiomaNavegador = navigator.language;
        alert("2.-" + idiomaNavegador);
    }
    else if (navigator.userLanguage)
    {
        idiomaNavegador = navigator.browserLanguage;
        alert("4.-" + idiomaNavegador);
    }
    else 
    {    
        idiomaNavegador = "en";
        alert("3.-" + idiomaNavegador);
     }
   
    var culture = idiomaNavegador.substring(0, 2);
   // var culture = "es";
    var texto = new String;
    switch (culture) 
    {
        case "es":
            texto = "¡Hola " + name + "!";
            alert("1.-" + idiomaNavegador + "-" + "¡Hola " + name + "!");
            break;
        case "en":
            texto = "Hello " + name + "!";
            alert("2.-" + idiomaNavegador + "-" + "Hello " + name + "!");
            break;
        default:
            texto = "Hello " + name + "!";
            alert("default.-" + idiomaNavegador + "-" + "Hello " + name + "!");
    }*/

    var texto = name + "!";

    $("#UserName").text(texto);

    document.LoginDiv =
         eval('document.getElementById("ctl00_LoginStatus1")');
    document.RegistroDiv =
         eval('document.getElementById("Registro")');
    if (name == "Espectador") {

        removeElement("Login", "ctl00_LoginStatus1");

//        document.LoginDiv.style.visibility = "hidden";
//        document.RegistroDiv.style.visibility = "visible";

    }
    else {

        removeElement("Header", "Registro");
//        document.LoginDiv.style.visibility = "visible";
//        document.RegistroDiv.style.visibility = "hidden";

    }
}
function removeElement(parentDiv, childDiv) {
    if (childDiv == parentDiv) {
        //alert("The parent div cannot be removed.");
    }
    else if (document.getElementById(childDiv)) {
        var child = document.getElementById(childDiv);
        var parent = document.getElementById(parentDiv);
        parent.removeChild(child);
    }
    else {
        //alert("Child div has already been removed or does not exist.");
        return false;
    }
}

// Función para validar los textbox de login de la portada
function ValidateTextBox(source, args) {
    var cntrl_id = $(source).attr("controltovalidate");
    var cntrl = $("#" + cntrl_id);
    var is_valid = $(cntrl).val() != "";
    is_valid ? $(cntrl).removeClass("error") : $(cntrl).addClass("error");

    args.IsValid = is_valid;
}

function GetWidthScreen() {
    var theWidth;

    if (window.innerWidth) {
        theWidth = window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        theWidth = document.documentElement.clientWidth;
    }
    else if (document.body) {
        theWidth = document.body.clientWidth;
    }

    var myNavigator = DetectaBrowser();
    if (myNavigator == 'Ms') {
        theWidth = theWidth;
    }
    else if (myNavigator == 'Opera') {
        theWidth = theWidth;
    }
    else if (myNavigator == 'Mozilla') {
        theWidth = theWidth - 3;
    }
    else if (myNavigator == 'Chrome') {
        theWidth = theWidth;
    }

    return theWidth; //  - 2; // Restamos 2 para que no desborde por el tamaño de los bordes del div que contiene HTML
}

function GetHeightScreen() {
    var theHeight;

    if (window.innerHeight) {
        theHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        theHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        theHeight = document.body.clientHeight;
    }

    var myNavigator = DetectaBrowser();
    if (myNavigator == 'Ms') {
        theHeight = theHeight - 40;
    }
    else if (myNavigator == 'Opera') {
        theHeight = theHeight - 40;
    }
    else if (myNavigator == 'Mozilla') {
        theHeight = theHeight - 30;
    }
    else if (myNavigator == 'Chrome') {
        theHeight = theHeight - 10;
    }
    else{
        theHeight = theHeight - 40;
    }



    return theHeight; //  - 40; // Restamos 58 para que no desborde por el tamaño de los elementos HTML (cabecera y footer)
}

function GetSilverlightTimelineInstance() {
    return document.getElementById('SilverlightTimeline');
}

function GetCurrentUserGuid() {
    PageMethods.GetCurrentUserGuid(OnSucceeded);
}

function OnSucceeded(result, userContext, methodName) {
    var plugin = GetSilverlightTimelineInstance();
    plugin.Content.TimelineTrayClass.SetCurrentUser(result);
}

function DebugMessage(message) {
    //alert(message);
}

function DetectaBrowser(){
var tipoBrowser;
if (navigator.userAgent.indexOf('MSIE') == 0) {
    tipoBrowser = 'Ms'; 
}
if (navigator.userAgent.indexOf('Opera') == 0) {
   tipoBrowser = 'Opera'; 
}
if (navigator.appName == 'Netscape') {
    tipoBrowser = 'Chrome';
}
if (navigator.userAgent.indexOf('Mozilla') == 0) {
     tipoBrowser = 'Mozilla';
}
return tipoBrowser
}

