﻿var idioma = 'es';
var oldLocation = '';
var web = 'iFrmHome.html', altura = 1084; /*428*/
var leerBookmark = true;
var seccionActual = 'Home';
var SL = true;

//*************************************************
//      FUNCIONES DE GESTION DE MENSAJES
//*************************************************
function receiveMessage(event) {
    var partes;
    if (event.data.indexOf('showBody') >= 0) {
        showBody(true);
    }
    else if (event.data == 'changeDisplay') {
        document.getElementById('iframe').style.visibility = 'visible';
    }
    else if (event.data == 'cerrarSesion') {
        cerrarSesion();
        scroll(true);
        datosRefresh('Home');
    }
    else if (event.data == 'ampliarSL') {
        ampliarSL();
    }
    else if (event.data == 'minimizarSL') {
        minimizarSL();
    }
    else if (event.data == 'noSilverlight') {
        noSilverlight();
    }
    else if (event.data == 'finSplash') {
        //Cuando acaba el Splash Screen activamos los botones
        //        var miSL2 = obtenerIframe('iframeHeader');
        //        if (miSL2) {
        //            var f = miSL2.getElementById('miSilverlight');
        //            if (f) { f.tabIndex = 0; f.focus(); }
        //        }
    }
    else if (event.data.indexOf('idioma') >= 0) {
        partes = event.data.split('-');
        cambiarIdioma(partes[1]);
        datosRefresh(seccionActual);
        actualizarIdiomaMenuYFooter();
    }
    else if (event.data == 'cambiarPestanya') cambiarPestanyaActual();
    else if (event.data.indexOf('datosRefresh') >= 0) {
        partes = event.data.split('-');
        datosRefresh(partes[1]);
    }
    else if (event.data.indexOf('scroll') >= 0) {
        partes = event.data.split('-');
        scroll(partes[1]);
        leerBookmark = partes[1];
    }
    else if (event.data.indexOf('setBookmark') >= 0) {
        partes = event.data.split('-');
        window.location.hash = partes[1];
    }
    else if (event.data.indexOf('setTitle') >= 0) {
        partes = event.data.split('+');
        document.title = partes[1];
    }
    else if (event.data.indexOf('actualizarBM') >= 0) {
        partes = event.data.split('-');
        actualizarBM(partes[1]);
    }
    else if (event.data == 'tinyBox') LanzarTinyBox();
    else if (event.data.indexOf('tinyBoxMsg') >= 0) {
        partes = event.data.split('-');
        LanzarTinyBoxMsg(partes[1]);
    }
    else if (event.data.indexOf('accederSL') >= 0) {
        partes = event.data.split('-');
        accederSL(partes[1]);
    }
    else if (event.data == 'recargarPagina') window.location="http://www.weplay.fm/";
}



//*************************************************
//      FUNCIONES DE LANZAMIENTO DE MENSAJES
//*************************************************
function showModal() {
    window.parent.postMessage('tinyBox', '*');
}

function lanzarAccederSL(seccion) {
    window.parent.postMessage('accederSL-' + seccion, '*');
}

function cambiarPestanya() {
    window.parent.postMessage('cambiarPestanya', '*');
}

//Funcion que cambia el Iframe del padre, partiendo desde el hijo
function showParentIframe(seccion) {
    window.parent.postMessage('datosRefresh-' + seccion, '*');
}

function cambiarIdiomaParent(idioma) {
    //document.getElementById('qrt').value = idioma;
    window.parent.postMessage('idioma-' + idioma, '*');
}


//*************************************************
//      FUNCIONES DE IFRAMES
//*************************************************

//Funcion que obtiene el iframe de cualquier hijo partiendo del padre (default.html)
function obtenerIframe(iframe) {
    var iFrame = document.getElementById(iframe);
    var doc;

    if (iFrame && iFrame.contentDocument) // FF
    {
        doc = iFrame.contentDocument;
    }
    else if (iFrame && iFrame.contentWindow) //IE, Chrome
    {
        doc = iFrame.contentWindow.document;
    }

    return doc;
}

function changeDisplay(){
	document.getElementById('iframe').style.visibility = 'visible';
}

function changeDisplayHeader() {
    document.getElementById('iframeHeader').style.visibility = 'visible';
}

//Funcion que cambia el iframe central que se muestra en pantalla
function datosRefresh(seccion) {
    if (seccion == 'HOME') seccion = "Home";
    else if (seccion == 'ARTISTS') seccion = "Artists";
    else if (seccion == 'PROFESSIONALS') seccion = "Professionals";
    else if (seccion == 'BUSINESSES') seccion = "Businesses";
    else if (seccion == 'FANS') seccion = "Fans";
    else if(seccion == undefined) seccion = "Home";

    seccionActual = seccion;

    cambiarPestanyaActual();

    tratarIframe(seccion);

    //if (seccion != 'Home' && seccion != 'Artistas' && seccion != 'Empresas' && seccion != 'Profesionales' && seccion != 'Fans') {
    if (seccion != 'Home') {
        seccion = seccion.toUpperCase();
        oldLocation = '#' + seccion;
        window.location.hash = seccion;
    }
    else {
        oldLocation = '';
        window.location.hash = '';
    }

    //alert("Antes de cargar "+web);
    document.getElementById('Combo').innerHTML = '<iframe src="html_' + idioma + '/' + web + '" id="iframe" width="100%" height="' + altura + '" scrolling="no" frameborder="1" >'+
                                                '<p>Panel central de weplay.fm<a href="html_' + idioma + '/' + web + '">Visualiza el panel central</a></p></iframe>';
    window.scrollTo(0, 0);
}

//Funcion que marca la pestaña actual del menu como seleccionada
function cambiarPestanyaActual() {
    var doc = obtenerIframe('iframeMenu');
    if (doc && doc.getElementById("Home") != null) {
        doc.getElementById("Home").setAttribute("class", "botonMenu");
        doc.getElementById("Artists").setAttribute("class", "botonMenu");
        doc.getElementById("Businesses").setAttribute("class", "botonMenu");
        doc.getElementById("Professionals").setAttribute("class", "botonMenu");
        doc.getElementById("Fans").setAttribute("class", "botonMenu");

        doc.getElementById(seccionActual).setAttribute("class", "botonMenuCurrent");
    }
}

function tratarIframe(nombreIframe) {
    switch (nombreIframe) {
        case 'Artists': nombreIframe = 'Artistas';
        case 'Artistas': altura = 1143; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Professionals': nombreIframe = 'Profesionales';
        case 'Profesionales': altura = 1139; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Businesses': nombreIframe = 'Empresas';
        case 'Empresas': altura = 950; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Fans': altura = 1139; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Contacto': altura = 800; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Footer': altura = 500; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Home': altura = 792; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Novedades': altura = 500; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'Press': altura = 600; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'ContactoProfesionales': altura = 1000; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'ContactoEmpresas': altura = 1000; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'ContactoArtistas': altura = 1000; web = 'iFrm' + nombreIframe + '.html'; break;
        case 'PostFormulario': altura = 500; web = 'iFrm' + nombreIframe + '.html'; break;
        default: altura = 792; nombreIframe = 'Home'; web = 'iFrm' + nombreIframe + '.html'; break;
    }
}


//*********************************************
//      FUNCIONES DE IDIOMAS
//*********************************************

function cambiarIdioma(lang) {
    idioma = lang;

    //	doc = obtenerIframe('iframeMenu');
    //	if(doc) doc.getElementById('qrt').value = idioma;

    doc = obtenerIframe('iframeHeader');
    if (doc) {
        switch (idioma) {
            case 'es':
                doc.getElementById('btDescubre').innerHTML = '<div class="boton">DESCUBRE<p>nueva música</p></div>';
                doc.getElementById('btEncuentra').innerHTML = '<div class="boton">PARTICIPA<p>en proyectos</p></div>';
                doc.getElementById('btAporta').innerHTML = '<div class="boton">FINANCIA<p>proyectos</p></div>';
                doc.getElementById('txtLogin').innerHTML = 'Inicia sesión';
                doc.getElementById('txtRegistro').innerHTML = 'Regístrate';
                break;

            case 'en':
                doc.getElementById('btDescubre').innerHTML = '<div class="boton">DISCOVER<p>new music</p></div>';
                doc.getElementById('btEncuentra').innerHTML = '<div class="boton">PARTICIPATE<p>in projects</p></div>';
                doc.getElementById('btAporta').innerHTML = '<div class="boton">FINANCE<p>projects</p></div>';
                doc.getElementById('txtLogin').innerHTML = 'Login';
                doc.getElementById('txtRegistro').innerHTML = 'Register';
                break;
        }

        doc.getElementById('miSilverlight').Content.LoginSilverlight.CambiarIdioma(lang);
    }
}

function actualizarIdiomaMenuYFooter() {
    document.getElementById('menuPrincipal').innerHTML = '<iframe src="html_' + idioma + '/iFrmMenuPrincipal.html" id="iframeMenu" width="100%" height="40" scrolling="no" frameborder="1" >' +
                                                        '<p>Menu principal de weplay.fm<a href=html_' + idioma + '/iFrmMenuPrincipal.html">Visualiza el menu principal</a></p></iframe>';

    document.getElementById('footerInferior').innerHTML = '<iframe src="html_' + idioma + '/iFrmFooter.html" style="visibility: visible;" width="100%" height="335" scrolling="no" frameborder="0">' +
                                                        '<p>Footer de weplay.fm<a href="html_' + idioma + '/iFrmFooter.html">Visualiza el footer</a></p></iframe>';
}


//*********************************************
//      FUNCIONES DE BOOKMARKS
//*********************************************

//Funcion que actualiza un BM a pelo
function actualizarBM(bmActual) {
    leerBookmark = false;
    oldLocation = "#" + bmActual;
    leerBookmark = true;
}

//método que navega entre los bookmarks de la Landing Page
function comprobarDeeplinks() {
    oldLocation = window.location.hash;
    setInterval(function () {
        if (leerBookmark == true) {
            if (window.location.hash != oldLocation) {
                //Caso de moverme entre bookmarks
                if (window.location.hash != '' && window.location.hash != '#' && IsSLBookmark(window.location.hash) == false) {
                    datosRefresh(window.location.hash.substring(1));
                }
                else {   //Caso de estar en un bookmark y retroceder al principio: "http://server/
                    if (window.location.hash == '' && window.location.hash != '#' && oldLocation != '' && IsSLBookmark(window.location.hash) == false) {
                        datosRefresh('Home');
                    }
                }

                oldLocation = window.location.hash;
            }
        } // fin if(LeerBookmark)

    }, 500);
}

//Funcion que comprueba que no se trate de un bookmark de Silverlight
function IsSLBookmark(bm) {
    var bmLimpio = bm.substring(1);
    var patron = /^(LISTAS|MARKETPLACE|CROWDFUNDING|NOTIFICACIONES|INBOX|GESTION|SUGERENCIAS)$/;

    if (bmLimpio != undefined && bmLimpio != '' && bmLimpio.match(patron)) {
        return true;
    }
    else return false;
}


//********************************************
//      FUNCIONES DE SILVERLIGHT
//********************************************
function comprobarSilverlight() {
    showBody(false);

    document.getElementById("miBody").style.overflow = 'auto';
    //document.body.style.overflow = 'auto';

    if (Silverlight.isInstalled("5.0") == true) {
        document.getElementById("silverlightControlHost").innerHTML = '<iframe src="accesoConSilverlight.html" id="iframeHeader" style="visibility:hidden;" onload="changeDisplayHeader()" width="100%" height="100%" scrolling="no" frameborder="0" ></iframe>';
        document.getElementById("silverlightControlHost").style.zIndex = '1';
    } 
    else noSilverlight();
    
    cargarUserVoice();

    comprobarDeeplinks();
}

function noSilverlight() {
    SL = false;

    document.body.style.display = 'inherit';

    doc = obtenerIframe('iframeMenu');
    if (doc) idioma = doc.getElementById('qrt').value;

    document.getElementById("silverlightControlHost").innerHTML = '<iframe src="html_' + idioma + '/headerInstaleSL.html" id="iframeHeader" style="visibility:hidden;" onload="changeDisplayHeader()" width="100%" height="70px" scrolling="no" frameborder="0" ></iframe>';

    //document.getElementById("silverlightControlHost").innerHTML = '<iframe src="accesoConSilverlight.html" id="iframeHeader" style="visibility:hidden;" onload="changeDisplayHeader()" width="100%" height="70px" scrolling="no" frameborder="0" ></iframe>';

    datosRefresh('Home');
}

//Método que accede a SL desde default.html
function accederSL(seccion) {
    if (Silverlight.isInstalled("5.0") == true) {
        //Conseguimos el doc de accesoSilverlight
        var doc = obtenerIframe('iframeHeader');
        if (doc) {
            var control = doc.getElementById('miSilverlight');
            if (control != undefined && control.Content != undefined && control.Content.LoginSilverlight != undefined) {
                ampliarSL();

                window.parent.postMessage('setBookmark-' + seccion, '*');
                //actualizarBM(accion);

                control.Content.LoginSilverlight.slClick(seccion);
            }
        } // fin if doc
    } //fin if SL == 5
    else LanzarTinyBox();
}

//Método que muestra SL delante del default.html
function ampliarSL() {
    scroll(false);
    showBody(true);

    document.getElementById('silverlightControlHost').style.height = '100%';
    document.getElementById('silverlightControlHost').style.width = '100%';
    document.getElementById('header').style.height = '100%';
    document.getElementById('header1').style.height = '100%';
    document.getElementById('iframeMenu').style.visibility = 'hidden';
    if(document.getElementById('uvTab'))
        document.getElementById('uvTab').style.visibility = 'hidden';

    var accesoSL = obtenerIframe('iframeHeader');
    if (accesoSL) {
        try {
            accesoSL.getElementById('form1').style.height = '100%';
            accesoSL.getElementById('container').style.height = '0%';
        }
        catch (ex) {
            //alert(ex);
        }
    }
}

//Método que minimiza SL, limitandolo a la barra superior
function minimizarSL() {
    document.getElementById('silverlightControlHost').style.height = '70px';
    document.getElementById('header').style.height = '70px';
    document.getElementById('header1').style.height = '70px';
    document.getElementById('iframeMenu').style.visibility = 'visible';
    document.getElementById('uvTab').style.visibility = 'visible';
}

//Método que minimiza SL y muestra la landing page entera
function cerrarSesion() {
    minimizarSL();
    scroll(true);

    var accesoSL = obtenerIframe('iframeHeader');
    if (accesoSL) {
        accesoSL.getElementById('form1').style.height = '0%';
        accesoSL.getElementById('container').style.height = '100%';
    }
}


//****************************************************
//      FUNCIONES DE VISIBILIDAD + SCROLL
//****************************************************
function scroll(visibilidad) {
    //alert("Overflow hidden = " + visibilidad);
    if (visibilidad == true) {
        document.getElementById('miBody').style.overflow = 'visible';
    }
    else document.getElementById('miBody').style.overflow = 'hidden';
}

function showBody(visibilidad) {
    if (visibilidad == true) document.body.style.display = 'inherit';
    else document.body.style.display = 'none';
}


//*********************************************
//      FUNCION DE VENTANA MODAL
//*********************************************
function LanzarTinyBox() {
	
	/* tiny for downLoad downLoad Silverlight */
     TINY.box.show({url:'html_es/iFrmInstaleSL.html', width:350, height:400,
     boxid: 'frameless', animate: true, topsplit: 2, close: true
    });
    
    
    /* tiny for downLoad Mac WePlay App*/
    /* TINY.box.show({url:'html_es/iFrmMacApp.html', width:350, height:420,
        boxid: 'frameless', animate: true, topsplit: 2, close: true
    });
    */

}

function cosasCerrarTinyBox() {
    //Esto quita la imagen
    var j = document.createElement('div'); j.className = 'tbox';
    TINY.box.alpha(j, -1, 0, 3); document.onkeypress = null;

    TINY.box.hide();
}


function LanzarTinyBoxMsg(msg) {
    TINY.box.show({ html: '<div style="background:black; border-radius:5px;"><div style="height:70px; margin-left:15px; "><img alt="" src="images/logoWePlayArial.png"/></div><div style="margin-top:10px; border-top:1px rgba(255,255,255,0.4) solid; padding:15px; color:#f4f4f4; ">' + msg + '</div></div>', 
    width: 200, height: 120, boxid: 'frameless', animate: true, topsplit: 2, close: true
    });
}


//********************************************
//      FUNCION USERVOICE
//********************************************

function cargarUserVoice() {
    var uvOptions = {};
    (function () {
        var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true;
        uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/30SAnzLghAtDg0CPGYa0hg.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
    })();
}

//********************************************
//      FUNCION VALIDACION
//********************************************

function validarFormEmpresa() 
{
    var nombreEmpresa = document.getElementById("entry_0").value;
    var webEmpresa = document.getElementById("entry_2").value;
    var nombreContacto = document.getElementById("entry_4").value;
    var mailContacto = document.getElementById("entry_6").value;
    var telefono = document.getElementById("entry_10").value;
    var msg = document.getElementById("entry_12").value;

    if (!isEmpty(nombreEmpresa) && !isEmpty(webEmpresa) && !isEmpty(nombreContacto) &&
        (!isEmpty(mailContacto) || !isEmpty(telefono) || !isEmpty(msg)))
        return true;
    else return false;
}

function isEmpty(valor)
{
    if(valor != undefined && valor != null && valor.length > 0) return false;
    else return true;
}
