
var offSetX = 80;
var offSetY = 50;

var theX;
var theY;

var ServerName = 'http://'+location.hostname;

// -----------------------------------------------------------------------------
// Abre la ventana para enviar un artículo por mail
function initUtilesPlayer() {
	var body = document.getElementsByTagName("body")[0];
	body.onmousedown = setPositionFromMouse;
};

addLoadEvent(initUtilesPlayer, '');

// -----------------------------------------------------------------------------
// Abre la ventana para enviar un artículo por mail
function enviarPlayer(tipo, prontus, ts) {
    if(ts==null || ts=='' || ts==undefined || ts=='undefined' || ts==NaN || ts=='NaN') {
        return
    };
    var w = '395';
    var h = '445';
    var dirts = ts.substr(0,8);
    var url = '/'+prontus+'/site/artic/'+dirts+'/pags/'+ts+'.html';
    var loc = '/'+prontus+'/stat/enviar/formulario.html?_url=' + escape(url) + "&_tipo=" + tipo;
    var envia = window.open(loc,'enviar', 'toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,location=0,directories=0,width='+w+',height='+h);
    if(envia) {
        envia.focus();
    } else {
        alert("Debes habilitar las Ventanas Emergentes para poder enviar por correo");
    }

};

// -----------------------------------------------------------------------------
// Abre la ventana para enviar un artículo por mail
function linkPlayer(tipo, prontus, ts) {

    if(ts==null || ts=='' || ts==undefined || ts=='undefined' || ts==NaN || ts=='NaN') {
        return
    };
    var miDiv = document.getElementById('myDivLink'+tipo+ts);
    if(miDiv) {
        eliminar_confirmacion(tipo, ts, 'Link');
        // Or Do Something
    };

    miDiv = creaDivPlayers(tipo, ts, 'Link');
    if(miDiv) {
        var posY = theY-offSetY;
        var posX = theX-offSetX;
        if((posX+500)>1024) {
            posX = 1024 - 500 - 50;
        } else if(posX < 0) {
            posX = 50;
        };
    } else {
        return;
    };

    var linkURL;
    if(tipo=='audio') {
        linkURL = ServerName + '/'+prontus+'/site/edic/base/port/audios.html?_ts='+ts;
    } else if(tipo=='video') {
        linkURL = ServerName + '/'+prontus+'/site/edic/base/port/videos.html?_ts='+ts;
    } else {
        alert('Tipo de Media, no es válido');
        return;
    };
    var txtArea = document.getElementById('inputLink'+tipo+ts);
    if(txtArea) {
        txtArea.innerHTML = linkURL;
        txtArea.style.width = '490px';
        txtArea.style.height = '60px';
    } else {
        return;
    };
    miDiv.style.width = '500px';
    miDiv.style.height = '115px';
    miDiv.style.display = 'inline';

    miDiv.style.position = 'absolute';
    miDiv.style.top = posY + 'px';
    miDiv.style.left = posX + 'px';

    txtArea.select();

};

// -----------------------------------------------------------------------------
// Abre la ventana para enviar un artículo por mail
function embedPlayer(tipo, prontus, ts, wflv, hflv) {

    if(ts==null || ts=='' || ts==undefined || ts=='undefined' || ts==NaN || ts=='NaN') {
        return
    };

    var miDiv = document.getElementById('myDivEmbed'+tipo+ts);
    if(miDiv) {
        eliminar_confirmacion(tipo, ts, 'Embed');
        // Or Do Something
    };

    miDiv = creaDivPlayers(tipo, ts, 'Embed');
    if(miDiv) {
        var posY = theY-offSetY*2;
        var posX = theX-offSetX;
        if((posX+400)>1024) {
            posX = 1024 - 400 - 50;
        } else if(posX < 0) {
            posX = 50;
        };
    } else {
        return;
    };

    var movieUrl;
    var xmlUrl;
    var dirts = ts.substr(0, 8);
    var flashvars;
    var ancho;
    var alto;
    var params1 = '';
    var params2 = ' <param name="base" value="'+ServerName+'">';

    if(tipo=='audio') {
        movieUrl = ServerName+'/prontus_nots/flash/_PlayerAudio.swf';
        xmlUrl = ServerName+'/'+prontus+'/site/artic/'+dirts+'/xmlpub/'+ts+'.xml';
        flashvars = "xml=" + xmlUrl + "&start=stop&vol=90&noutils=true";
        ancho = 350;
        alto = 50;
        idObj = 'idFlashAudio'+ts;

    } else if(tipo=='video') {
        movieUrl = ServerName+'/prontus_nots/flash/_PlayerVideo.swf';
        xmlUrl = ServerName+'/'+prontus+'/site/artic/'+dirts+'/xmlpub/'+ts+'.xml';
        flashvars = "xml=" + xmlUrl + "&start=stop&vol=90&noutils=true";
        ancho = 300;
        alto = 350;
        idObj = 'idFlashVideo'+ts;

    } else {
        alert('Tipo de Media, no es válido');
        return;
    };

    if(wflv!=null && wflv!='' && wflv!=undefined) ancho = wflv;
    if(hflv!=null && hflv!='' && hflv!=undefined) alto = hflv;

    var strSwf = getStringObject(movieUrl, idObj, ancho, alto, flashvars, params1, params2);
    var txtArea = document.getElementById('inputEmbed'+tipo+ts);
    if(txtArea) {
        strSwf = convertTextareaEntities(strSwf);
        txtArea.innerHTML = strSwf;
        txtArea.style.width = '440px';
        txtArea.style.height = '250px';

    } else {
        return;
    };

    miDiv.style.width = '450px';
    miDiv.style.height = '320px';
    miDiv.style.display = 'inline';

    miDiv.style.position = 'absolute';
    miDiv.style.top = posY + 'px';
    miDiv.style.left = posX + 'px';

    txtArea.select();
};

//---------------------------------------------------------------------
//
function creaDivPlayers(tipo, ts, util) {

    if(util=='Link') {
        var texto = 'Copie esta dirección (Ctrl+C). A través de ella tendrá acceso directo a este contenido.';
        var titulo = "Ver noticia en su browser";

    } else if(util=='Embed') {
        var texto = 'Copie este código (Ctrl+C) e insértelo en su página web para publicar este contenido en ella.';
        var titulo = "Publicar en su sitio";

    } else {
        return null;
    };


    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', 'myDiv'+util+tipo+ts);
    newdiv.className = 'myDiv'+util+'Player';

    var closelink = document.createElement('a');
    closelink.setAttribute('href', 'javascript:eliminar_confirmacion("'+tipo+'", "'+ts+'", "'+util+'");');
    var closeimg = document.createElement('img');
    closeimg.setAttribute('src', '/prontus_nots/imag/auxi/cerrar_pop.gif');
    closeimg.setAttribute('border', '0');
    closeimg.className = util+'Player_imgclose';
    closelink.appendChild(closeimg);

    var tbl = document.createElement("table");
    tbl.width = '100%';
    tbl.cellSpacing = 5;
    tbl.cellPadding = 5;

    tbdy = document.createElement("tbody");

    var row1 = document.createElement("tr");
    var cell1 = document.createElement("td");
    cell1.className = util+'Player_text_tit';
    // cell11.setAttribute('width', '80%');
//    var cell12 = document.createElement("td");
//    cell12.setAttribute('align', 'right');
    cell1.appendChild(closelink);
    cell1.innerHTML = cell1.innerHTML + titulo;

    row1.appendChild(cell1);
    // row1.appendChild(cell12);

    var row2 = document.createElement("tr");
    var cell2 = document.createElement("td");
    // cell2.setAttribute('colspan', '2');
    cell2.className = util+'Player_text_body';
    cell2.innerHTML = texto;
    row2.appendChild(cell2);

    var row3 = document.createElement("tr");
    var cell3 = document.createElement("td");
    // cell3.setAttribute('colspan', '2');
    // cell3.setAttribute('style', 'text-align:center;');
    cell3.setAttribute('class', util+'_text_body');
    var txtarea = document.createElement("textarea");
    txtarea.rows = 4;
    txtarea.cols = 80;
    txtarea.className = util+'Player_textarea';
    txtarea.setAttribute('id', 'input'+util+tipo+ts);
    txtarea.readOnly = true;
    cell3.appendChild(txtarea);
    row3.appendChild(cell3);

    tbdy.appendChild(row1);
    tbdy.appendChild(row2);
    tbdy.appendChild(row3);
    tbl.appendChild(tbdy);
    newdiv.appendChild(tbl);

    var body = document.getElementsByTagName("body")[0];
    if(body) {
        body.appendChild(newdiv);
    }
    return newdiv;
};

//---------------------------------------------------------------------
function eliminar_confirmacion(tipo, ts, util) {

    try {
        var myDiv = document.getElementById("myDiv"+util+tipo+ts);
        var body = document.getElementsByTagName("body")[0];
        body.removeChild(myDiv);

    } catch(e) {

    }
};

//---------------------------------------------------------------------
function setPositionFromMouse(e) {

	if (!e) e = window.event;
	if (e.pageX || e.pageY) 	{
		theX = e.pageX;
		theY = e.pageY;
	} else if (e.clientX || e.clientY) 	{
		theX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		theY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	} else {
	    theX = 100;
	    theY = 100;
    };
};

function convertTextareaEntities(strSwf) {

    strSwf = strSwf.replace(/</g, "&lt;");
    strSwf = strSwf.replace(/>/g, "&gt;");
    return strSwf;
};