var wp, hheight, cheight, factor_im, hplayer, wplayer, wp1, wgal;
var wwidth = 0;
var wheight = 0;

var min_directo = false;
var medidas_directo = [];
var vid = 0;
var inicio = false;

var instanceOne = new ImageFlow();
var player = null;

function carga(vid1, url, tipo) {
    if (! (cEl = document.getElementById('body'))) 
		cEl = document.getElementsByTagName('body')[0];
    wbrowsertype();
    init_medidas();
    $('dialog').hide();
    $('IF').show();
    vid = vid1;
    var cadenaIFlow = 'cat-93';
    if (document.location.toString().indexOf('vid')>0){
    var temp=document.location.toString().split('#');
    cadenaIFlow = 'video-'+temp[1].replace('vid','');
    }
    new Ajax.Updater('contador', 'include/php/sesion/contador.php', {
        method: 'post',
        parameters: 'resol=' + wwidth + 'x' + wheight + '&browser=' + navegador[net_browser] + '&version=' + navigator.appVersion
    });
    $('scroller').show();
    $('FlashWrap').hide();
    if (net_browser==50){
    	esIE6();
    }
    Effect.Fade('intro', {
        duration: 2.0,
        afterFinish: function () {
            IFlow(cadenaIFlow);
            setTimeout("suscribir();", 3000);
        }
    });
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function suscribir() {
    if (getCookie("mail5min")) {
        //alert('hay cookie');
    } else {
        $('alert_mail').morph('bottom:-10px;');
    }
}

function IFlow(cadena) {
    $('content_IF').show();
    $('dialog').show();
    var temp = new Ajax.Updater($('IF'), 'include/php/IF/IF.php?cadena=' + cadena+'&time='+Math.floor(new Date().getTime()/1000), {
        method: 'get',
        onComplete: initIF
    });
}

function initIF() {
    $('media').hide();
    $('IF').show();
    var imgs = $('IF').getElementsByTagName('img');
    if (imgs.length >1){
	instanceOne.init({
        ImageFlowID: 'myImageFlow',
        reflectionGET: '&height=20%',
        reflectionPNG: true,
        imageFocusM: 1.15,
        imageCursor: 'pointer',
        startID: $('IF_startID').value,
        onClick: function () {
            AccionIF(this.url, this.tipo, this.vid);
        }
    });
    }else{
    	ActualizaPanel(imgs[0]);
    	      var ratio = imgs[0].getAttribute('width')/imgs[0].getAttribute('height');
    	      var imgurl=imgs[0].getAttribute('longdesc');
    	      var imgtipo=imgs[0].getAttribute('tipo');
    	      var imgvid=imgs[0].getAttribute('vid');
    	      
    	$('IF').innerHTML = '<img id="IFimg1" src="'+imgs[0].getAttribute('src')+'" style="position:absolute;bottom:0;height:65%;cursor:pointer" onclick="AccionIF(\''+imgurl+'\', \''+imgtipo+'\', \''+imgvid+'\');"/>';
    	$('IFimg1').style.left = (0.5*($('IF').style.width.replace('px','')-0.65*$('IF').style.height.replace('px','')*ratio))+'px';
    }
    $('info_categoria').innerHTML='';
    return true;
}

function AccionIF(IFUrl, IFTipo, IFVid) {
    if (IFTipo == 'cat') {
        IFlow('cat-' + IFVid);
    } else {
        MuestraPlayer(IFUrl, IFTipo, IFVid);
    }
}

function MuestraPlayer(url1, tipo, vid) {
    new Ajax.Request('divs/tareas.php?cadena=incr_visto-' + vid);
    $('IF').hide();
    $('FlashWrap').show();
    $('media').show();
    switch (tipo) {
    case 'SFTV':
        SetPlayer(url1, 'video');
        break;
    case 'youtube':
        SetPlayer(url1, 'youtube');
        break;
    case 'sound':
        url1 = url1.replace('videos', 'audios');
        SetPlayer(url1, 'sound');
        break;
    default:
        SetPlayer(url1, 'video');
    }
}

function playerReady(thePlayer) {
    player = window.document[thePlayer.id];
    player.addModelListener("STATE", "stateMonitorPlayer");

}

function stateMonitorPlayer(obj) {
    if (obj.newstate == 'COMPLETED') {
//        $('FlashWrap').innerHTML = '<div style="text-align:center"><img src="rec/images/html/FinPelicula.png" style="height:100%" /></div>';
//        $('IF').show();
//        var a = setTimeout("cierraPlayer()", 3000);
		$('IF').show();
		cierraPlayer();
    }
};

function cierraPlayer() {
    Effect.Fade('FlashWrap', {
        duration: 2.0
    });
    var a = setTimeout("$('media').hide()", 3000);
}

function SetPlayer(archivo, tipo) {
    autostart = 'true';
    shuffle = 'false';
    playlistsize = '0';
    playlist = 'none';
    controlbar = 'bottom';
    var flashvars = 'file=' + archivo + '&type=' + tipo + '&autostart=' + autostart + '&shuffle=' + shuffle + '&playlistsize=' + playlistsize + '&playlist=' + playlist + '&controlbar=' + controlbar + '&skin=rec/flash/modieus.swf';
    createPlayer(flashvars);
}

function UnPlayer() {
    $('FlashWrap').innerHTML = '';
}

function createPlayer(flashvars) {
    $('FlashWrap').style.width = wplayer + 'px';
    $('FlashWrap').style.height = hplayer + 'px';
    $('FlashWrap').innerHTML = '<!--[if !IE]> --><embed id="player1" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" name="player1" src="rec/flash/player.swf" width="' + wplayer + '" height="' + hplayer + '" allowscriptaccess="always" allowfullscreen="true" allowtransparency="allowtransparency" flashvars="' + flashvars + '" /><!-- <![endif]--><!--[if IE]><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,22,0" id="player1" width="' + wplayer + '" height="' + hplayer + '"> <param name="movie" value="rec/flash/player.swf"/> <param name="bgcolor" value="#FFFFFF"/> <param name="quality" value="high"/> <param name="wmode" value="transparent"/> <param name="loop" value="false"/><param name="menu" value="false" /> <param name="FlashVars" value="' + flashvars + '"><param name="allowscriptaccess" value="always"/></object><!-- <![endif]--><img src="rec/images/html/closebox.png" style="position:absolute; bottom:-35px; left:5px; z-index:100000; cursor:pointer" width=30 onclick="UnPlayer(); $(\'media\').hide(); $(\'IF\').show(); unmute(); return true;" />';
}

function OcultaPlayer() {
    UnPlayer();
    return true;
}

function getElementsByClass(searchClass, domNode, tagName) {
    if (domNode == null) domNode = document;
    if (tagName == null) tagName = '*';
    var el = new Array();
    var tags = domNode.getElementsByTagName(tagName);
    var tcl = " " + searchClass + " ";
    for (i = 0, j = 0; i < tags.length; i++) {
        var test = " " + tags[i].className + " ";
        if (test.indexOf(tcl) != -1) el[j++] = tags[i];
    }
    return el;
}

function init_medidas() {
    if (typeof(window.innerWidth) == 'number') {
        //Non-IE
        wwidth = window.innerWidth;
        wheight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        wwidth = document.documentElement.clientWidth;
        wheight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        wwidth = document.body.clientWidth;
        wheight = document.body.clientHeight;
    }

    wp = Math.max(400, Math.min(wheight * 1.5, 0.785 * wwidth));
    wp1 = 0.90909 * wp;
    margin = 0.5 * (wwidth - wp);
    hheight = 0.135 * 0.90909 * wp + 0.09 * 0.386 * wp;
    cheight = wheight - hheight;
    factor_im = wwidth / 590;
    hplayer = cheight - 35;
    wplayer = wp1;

    var fs = variacion(800, 2000, 10, 14, wwidth);
    $('bd').style.fontSize = Math.round(fs) + 'px';

    $('menu').innerHTML = '<a href="http://www.youtube.com/user/sagradafamiliatv" target="_blank"><img src="http://www.sagradafamilia.tv/rec/images/html/icon_youtube40.gif" height="20" style="margin:4px;margin-top:1px; border:none"/></a>    <a href="http://twitter.com/SFWebTV" target="_blank"><img src="http://www.sagradafamilia.tv/rec/images/html/icon_twitter40.gif" height="20" style="margin:4px;margin-top:1px; border:none"/></a>    <a href="http://www.facebook.com/profile.php?id=1337643871&ref=nf" target="_blank"><img src="http://www.sagradafamilia.tv/rec/images/html/icon_facebook40.gif" height="20" style="margin:4px;margin-top:1px; border:none"/></a><a href="http://sagradafamiliatelevision.blogspot.com/" target="_blank"><img src="http://www.sagradafamilia.tv/rec/images/html/icon_blogspot40.gif" height="20" style="margin:4px;margin-top:1px; border:none"/></a>';

    $('logo').style.marginLeft = (margin + 1.1 * 0.04545 * wp) + 'px';
    $('logo').style.marginTop = (0.09 * 0.386 * wp) + 'px';
    $('logo').style.width = wp1 + 'px';
    $('logo').style.height = (0.135 * 0.90909 * wp) + 'px';
    $('logo').style.display = 'block';

    $('alert_mail').style.left = (0.5 * (wwidth - 700)) + 'px';

    $('web').style.width = wp1 + 'px';
    $('web').style.right = (wwidth - margin - 1.1 * 0.04545 * wp - wp1) + 'px';

    $('media').style.height = (cheight) + 'px';
    $('media').style.width = (wp1) + 'px';

    $('contenido').style.height = cheight + 'px';
    $('contenido').style.width = wp1 + 'px';

    $('footer').style.width = (wp1) + 'px';

        $('dialog').className = 'dialog_css5';
        $('dialog').innerHTML = '<div id="info_title"></div><div id="info_description"></div><div id="info_categoria"></div><div id="rated"></div><div id="share"></div>';


    hdialog1 = (wheight - (hheight + 0.9 * (0.85 * wp - 22) / 1.964) - 10);
    hdialog = Math.min(150, hdialog1);
    $('dialog').style.height = hdialog + 'px';
    
    hif1 = (0.85 * wp - 22) / 1.964;
    hif = hif1;
    mTif = -0.5 * hheight;

    $('IF').style.marginTop = mTif + 'px';
    $('IF').style.height = hif + 'px';
    $('IF').style.width = (wp1) + 'px';
    $('IF').hide();
    $('popup').hide();
    $('mascara_popup').hide();
}

function suscripcion5m() {
    new Ajax.Request('include/php/sesion/registro_mail_5min.php', {
        method: 'post',
        parameters: 'mail=' + document.formulario.mailfield.value,
        onComplete: function (transport) {
            var temp = transport.responseText.split('##');
            var respuesta = temp[1];
            if (respuesta == 'ok') {
            	setCookie("mail5min", document.formulario.mailfield.value, 200);
                $('alert_mail').morph('bottom:-50px;');
            } else {
                $('alert_mail_text').hide();
                $('alert_mail_error').innerHTML = '<b>ERROR: </b>' + respuesta;
                setInterval('alert_mail1();', 2000);
            }
        }
    });

}

function alert_mail1() {
    $("alert_mail_text").show();
    $("alert_mail_error").hide();
}

var ved1, ved2, vm;
function popup(textoHTML, width, height) {
    $('mascara_popup').show();
    $('popup').style.width = width + 'px';
    $('popup').style.height = height + 'px';
    $('popup').style.top = (0.5 * (wheight - height)) + 'px';
    $('popup').style.left = (0.5 * (wwidth - width)) + 'px';
    $('popup').innerHTML = textoHTML.replace('##','\'');
    $('popup').show();
}

function popdown() {
    $('popup').innerHTML = '';
    $('popup').hide();
    $('mascara_popup').hide();
}

function include(file_path) {
    if (file_path.indexOf(".js") != -1) {
        fileref = document.createElement('script');
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", file_path);
    } else if (file_path.indexOf(".css") != -1) {
        fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", file_path);
    }

    document.getElementsByTagName("head").item(0).appendChild(fileref);
}

function include_once(file_path) {
    var sc = document.getElementsByTagName("script");

    for (var x in sc)
    if (sc[x].src != null && sc[x].src.indexOf(file_path) != -1) return;

    include(file_path);
}

function variacion(x0, x1, y0, y1, x) {
    return (y0 + (y1 - y0) * (x - x0) / (x1 - x0));
}

var net_browser = 0;
var navegador = [];
var css5 = false; //¿soporta css5?

navegador[0] = 'Undetermined OS / Browser';
navegador[17] = 'Undetermined OS / Browser';
navegador[2] = 'MAC OS / Unknown Browser';
navegador[3] = 'MAC OS / Internet Explorer';
navegador[4] = 'MAC OS / Safari';
navegador[5] = 'MAC OS / Firefox';
navegador[6] = 'MAC OS / Netscape';
navegador[7] = 'MAC OS / Opera';
navegador[8] = 'MAC OS / Camino';
navegador[9] = 'MAC OS / Firebird';
navegador[26] = 'MAC OS / Google Chrome';
navegador[10] = 'WINDOWS / Unknown Browser';
navegador[11] = 'WINDOWS / Internet Explorer';
navegador[12] = 'WINDOWS / Firefox';
navegador[13] = 'WINDOWS / Netscape';
navegador[14] = 'WINDOWS / Opera';
navegador[15] = 'WINDOWS / Camino';
navegador[16] = 'WINDOWS / Firebird';
navegador[25] = 'WINDOWS / Google Chrome';
navegador[17] = 'OS DESCONOCIDO / Unknown Browser';
navegador[18] = 'OS DESCONOCIDO / Safari';
navegador[19] = 'OS DESCONOCIDO / Internet Explorer';
navegador[20] = 'OS DESCONOCIDO / Firefox';
navegador[21] = 'OS DESCONOCIDO / Netscape';
navegador[22] = 'OS DESCONOCIDO / Opera';
navegador[23] = 'OS DESCONOCIDO / Camino';
navegador[24] = 'OS DESCONOCIDO / Firebird';
navegador[27] = 'OS DESCONOCIDO / Google Chrome';
navegador[50] = 'Internet Explorer 6 o menor';

function wbrowsertype() {
    var btfound = 0;
    browser_detect = navigator.userAgent.toLowerCase();

    if ((browser_detect.indexOf("konqueror") + 1)) {
        btfound = 1;
        net_browser = 1;
    }

    if ((browser_detect.indexOf("mac_powerpc") + 1)) {
        btfound = 1;
        net_browser = 3;
    }
    
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
    	var ieversion=new Number(RegExp.$1);
    	if (ieversion<7){
	        btfound = 1;
	        net_browser = 50;
        }
    }

    if (btfound == 0) {
        // MAC OS
        if ((browser_detect.indexOf("macintosh") + 1)) {
            if ((browser_detect.indexOf("safari") + 1)) {
                css5 = true;
                btfound = 1;
                net_browser = 4;
            } else if ((browser_detect.indexOf("firefox") + 1)) {
                css5 = true;
                btfound = 1;
                net_browser = 5;
            } else if ((browser_detect.indexOf("netscape") + 1)) {
                btfound = 1;
                net_browser = 6;
            } else if ((browser_detect.indexOf("opera") + 1)) {
                btfound = 1;
                net_browser = 7;
            } else if ((browser_detect.indexOf("camino") + 1)) {
                btfound = 1;
                net_browser = 8;
            } else if ((browser_detect.indexOf("firebird") + 1)) {
                btfound = 1;
                net_browser = 9;
            } else if ((browser_detect.indexOf("chrome") + 1)) {
                css5 = true;
                btfound = 1;
                net_browser = 26;
            } else {
                btfound = 1;
                net_browser = 2;
            }
        }
        // Windows OS
        if ((browser_detect.indexOf("windows") + 1) && btfound == 0) {
            if ((browser_detect.indexOf("msie") + 1)) {
                btfound = 1;
                net_browser = 11;
            } else if ((browser_detect.indexOf("firefox") + 1)) {
                css5 = true;
                btfound = 1;
                net_browser = 12;
            } else if ((browser_detect.indexOf("netscape") + 1)) {
                btfound = 1;
                net_browser = 13;
            } else if ((browser_detect.indexOf("opera") + 1)) {
                btfound = 1;
                net_browser = 14;
            } else if ((browser_detect.indexOf("camino") + 1)) {
                btfound = 1;
                net_browser = 15;
            } else if ((browser_detect.indexOf("firebird") + 1)) {
                btfound = 1;
                net_browser = 16;
            } else if ((browser_detect.indexOf("chrome") + 1)) {
                css5 = true;
                btfound = 1;
                net_browser = 25;
            } else {
                btfound = 1;
                net_browser = 10;
            }
        }
        // Unknown OS
        if (btfound == 0) {
            if ((browser_detect.indexOf("safari") + 1)) {
                net_browser = 18;
            } else if ((browser_detect.indexOf("msie") + 1)) {
                net_browser = 19;
            } else if ((browser_detect.indexOf("firefox") + 1)) {
                css5 = true;
                net_browser = 20;
            } else if ((browser_detect.indexOf("netscape") + 1)) {
                net_browser = 21;
            } else if ((browser_detect.indexOf("opera") + 1)) {
                net_browser = 22;
            } else if ((browser_detect.indexOf("camino") + 1)) {
                net_browser = 23;
            } else if ((browser_detect.indexOf("firebird") + 1)) {
                net_browser = 24;
            } else if ((browser_detect.indexOf("chrome") + 1)) {
                css5 = true;
                net_browser = 27;
            } else {
                net_browser = 17;
            }
        }
    }
    /* In most cases, Google Chrome will behave the same as Firefox. If not you can remove these value overwrites. */
    //    if (net_browser == 25) {
    //        net_browser = 12;
    //    }
    //    else if (net_browser == 26) {
    //        net_browser = 5;
    //    }
    //    else if (net_browser == 27) {
    //        net_browser = 20;
    //    }
}
function esIE6(){
	$('alert_mail').hide();
	$('imagenes').hide();
	$('scroller').hide();
	$('alertaIE6').style.display='block';
}