//var activado = true;
//var intval;

/* Gestión del slideshow de la página inicial ***************/
function slideSwitch() {
    var $active = $('#slideshow DIV.active'); //Identifica cual es el div activo
    if ( $active.length == 0 ) {
        $active = $('#slideshow DIV:last');
    }
	
    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
    : $('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );

    $active.addClass('last-active');

    $next.css({
        opacity: 0.3
    })
    .addClass('active')
    .animate({
        opacity: 1.0
    }, 250, function() {
        $active.removeClass('active last-active');
    });
}
/**********************************************************/

/* Funcion para hacer desplegable el menu superior ********/
function mainmenu(){

    $(" #nav ul ").css({
        display: "none"
    }); // Opera Fix
    $(" #nav li").hover(function(){
        $(this).find('ul:first').css({
            visibility: "visible",
            display: "none"
        }).show(200);
    },function(){
        $(this).find('ul:first').css({
            visibility: "hidden"
        });
    });
    $('.submenu ul li ul').hide();
	
    $('.submenu ul li a').click( function() {
        var checkElement = $(this).next();
        //var parent = this.parentNode.parentNode;
		
        if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            checkElement.slideUp('normal');
            return false;
        }
        if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            //$('#' + parent + ' ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
        }
    });
	
    $('.marco_noticias').hover(function(){
        clearInterval(intval);
        //activado = false;
        intval = setInterval( "slideSwitch()", 15000 );
        var elemento = "#S"+$(this).attr('id');
        var $active = $('#slideshow DIV.active'); //Identifica cual es el div activo
		
        var $next = $(elemento);

        // uncomment below to pull the divs randomly
        // var $sibs  = $active.siblings();
        // var rndNum = Math.floor(Math.random() * $sibs.length );
        // var $next  = $( $sibs[ rndNum ] );

        $active.addClass('last-active');

        $next.css({
            opacity: 0.3
        })
        .addClass('active')
        .animate({
            opacity: 1.0
        }, 250, function() {
            $active.removeClass('active last-active');
        //intval = setInterval( "slideSwitch()", 10000 );
        });
    });
	
    $(".villarreal_b_pp").hide();
    $(".villarreal_b_up").hide();
    $(".villarreal_b_c").hide();
    $(".villarreal_b_cl").hide();
	
    intval = setInterval( "slideSwitch()", 10000 );
    //intval2 = setInterval( "comprobarIntVal()", 2000);
	
	
    //$('.textofondo').hide();
    /*
	$('#slideshow a').mouseover(function(){
		$('.textofondo').show("slow");
    });
	$('#slideshow a').mouseout(function(){
		$('.textofondo').hide("slow");
    });
	*/

    $("#tab_contenidos").tabs({
        fx: {
            opacity: 'toggle'
        }
    });
$('#tab_contenidos').tabs({
    select: function(event, ui) {
    //alert(ui.tab);
    }
});
$('#tab_contenidos').bind('tabsselect',
    function(event, ui) {
    //ui.index();
    //alert(ui.index);
    });
$("#tab_contenidos_2").tabs({
    fx: {
        opacity: 'toggle'
    }
});

$(".pp1").addClass('ppselected');
$(".pp3").addClass('ppnotselected');
        
$(".up1").addClass('ppselected');
$(".up3").addClass('ppnotselected');
        
$(".c1").addClass('ppselected');
$(".c3").addClass('ppnotselected');
	
$(".cl1").addClass('ppselected');
$(".cl3").addClass('ppnotselected');
	
$(".pp1").click(function(){
    $(".villarreal_b_pp").hide("slow");
    $(".villarreal_pp").show("slow");
    $(".pp1").addClass('ppselected');
    $(".pp3").removeClass('ppselected');
    $(".pp1").css("background","url(../img/pp1.png) repeat-x");
    $(".pp3").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = false;
});
	
$(".pp3").click(function(){
    $(".villarreal_pp").hide("slow");
    $(".villarreal_b_pp").show("slow");
    $(".pp3").addClass('ppselected');
    $(".pp1").removeClass('ppselected');
    $(".pp3").css("background","url(../img/pp1.png) repeat-x");
    $(".pp1").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = true;
});
	
$(".up1").click(function(){
    $(".villarreal_b_up").hide("slow");
    $(".villarreal_up").show("slow");
    $(".up1").addClass('ppselected');
    $(".up3").removeClass('ppselected');
    $(".up1").css("background","url(../img/pp1.png) repeat-x");
    $(".up3").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = false;
});

$(".up3").click(function(){
    $(".villarreal_up").hide("slow");
    $(".villarreal_b_up").show("slow");
    $(".up3").addClass('ppselected');
    $(".up1").removeClass('ppselected');
    $(".up3").css("background","url(../img/pp1.png) repeat-x");
    $(".up1").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = true;
});
	
$(".c1").click(function(){
    $(".villarreal_b_c").hide("slow");
    $(".villarreal_c").show("slow");
    $(".c1").addClass('ppselected');
    $(".c3").removeClass('ppselected');
    $(".c1").css("background","url(../img/pp1.png) repeat-x");
    $(".c3").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = false;
});

$(".c3").click(function(){
    $(".villarreal_c").hide("slow");
    $(".villarreal_b_c").show("slow");
    $(".c3").addClass('ppselected');
    $(".c1").removeClass('ppselected');
    $(".c3").css("background","url(../img/pp1.png) repeat-x");
    $(".c1").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = true;
});
	
$(".cl1").click(function(){
    $(".villarreal_b_cl").hide("slow");
    $(".villarreal_cl").show("slow");
    $(".cl1").addClass('ppselected');
    $(".cl3").removeClass('ppselected');
    $(".cl1").css("background","url(../img/pp1.png) repeat-x");
    $(".cl3").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = false;
});

$(".cl3").click(function(){
    $(".villarreal_cl").hide("slow");
    $(".villarreal_b_cl").show("slow");
    $(".cl3").addClass('ppselected');
    $(".cl1").removeClass('ppselected');
    $(".cl3").css("background","url(../img/pp1.png) repeat-x");
    $(".cl1").css("background","url(../img/pp3.png) repeat-x");
    villarrealB = true;
});
}


$(document).ready(function(){
    var identificadorvideo = 0;
    var villarrealB = false;
	
    mainmenu();
	
    if(!$('.active').is(':visible')){
        var desplegar = $('.active').parent();
        if(desplegar.is('ul')){
            var desplegar1 = desplegar.parent();
            if(desplegar1.is('li')){
                var desplegar2 = desplegar1.parent();
                desplegar2.slideDown('normal');
                desplegar.slideDown('normal');
            }else{
                desplegar.slideDown('normal');
            }
        }
    }
	
    $(".prevPage").click(function(){
        if(identificadorvideo != 0){
            identificadorvideo--;
        }else{
            identificadorvideo = 0;
        }
        $("#videos"+identificadorvideo).show('slow');
        $("#videos"+identificadorvideo+3).hide('slow');
    });
    $(".nextPage").click(function(){
        $("#videos"+identificadorvideo).hide('slow');
        $("#videos"+identificadorvideo+3).show('slow');
        if(identificadorvideo != 5){
            identificadorvideo++;
        }
    });
    //$(".player").hide();
    //$(".container").show();
    $("img[name*='imagenvideo']").hover(function(){
        //var video = $("#videos"+$(this).attr("id"));
        var elemento = $(".descripcionvideo"+$(this).attr("id"));
        var elemento2 = $(".contenidovideo"+$(this).attr("id"));
        var elemento3 = $(".source"+$(this).attr("id"));

        //$(".container").css("background-image","url("+$(this).attr("src")+")");
        $("#textovideo").html(elemento.val());
        $("#descripcion").html(elemento2.val());
        //$f().getControls().css({backgroundImage: 'url('+$(this).attr("src")+')'});
        //alert($f().getConfig(false).canvas.background);
        //$f().getPlugin(false).canvas.backgroundImage = 'url('+$(this).attr("src")+')';
        $f().getPlugin("canvas").css({
            //backgroundImage:'url('+$(this).attr("src")+')'
            backgroundImage:'url('+elemento3.val()+')'
        });
    //$f().css({backgroundImage: 'url('+$(this).attr("src")+')'});
    //$(".player").hide("slow");
    //$(".container").show("slow");
    //$(".player").hide("href",video.attr("href"));
    });
	
    $("img[name*='imagenvideo']").click(function(){
        var video = $("#videos"+$(this).attr("id"));
		
        //$(".player").show("slow");
        //$(".container").hide("slow");
        $(".player").attr("href",video.attr("href"));
    //alert($(".player").attr("href"));
    });
});

/**********************************************************/

// Función para la validación de correos
function validarCorreo(emailStr) {
    /* Patrón que deben seguir las direcciones de correo */
    var emailPat=/^(.+)@(.+)$/
    /* Carácteres especiales que no queremos en la dirección */
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    /* carácteres válidos en la dirección */
    var validChars="\[^\\s" + specialChars + "\]"
    /* Adoptamos medidas para las cuentas con comillas:
	Ej. "jiminy cricket"@disney.com es una dirección legal */
    var quotedUser="(\"[^\"]*\")"
    /* Dominios como dirección ip. Ej. joe@[123.124.233.4] .
	NOTA: los corchetes son obligatorios */
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    /* un átomo es una serie de carácteres no especiales. */
    var atom=validChars + '+'
    /* Con la definición anterior montamos las palabras. Un usuario será un átomo
	o un nombre entre comillas */
    var word="(" + atom + "|" + quotedUser + ")"
    // En definitiva, este es el patrón para los usuarios válidos
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
    /* Patrón de un dominio "simbólico" */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
    /* Comprobamos si la cuenta existe realmente */
    var matchArray=emailStr.match(emailPat)
    if (matchArray==null) {
        return false	// eti_errorcorreo
    }
	
    var user=matchArray[1]
    var domain=matchArray[2]
	
    if (user.match(userPat)==null) {
        return false	// eti_errorusuario
    }
	
    var IPArray=domain.match(ipDomainPat)
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return false	//	errorip
            }
        }
        return true
    }
	
    var domainArray=domain.match(domainPat)
    if (domainArray==null) {
        return false	//	errordominio
    }
	
    var atomPat=new RegExp(atom,"g")
    var domArr=domain.match(atomPat)
    var len=domArr.length
    if (domArr[domArr.length-1].length<2 ||
        domArr[domArr.length-1].length>3) {
        return false	// errordominiopais
    }
	
    if (len<2) {
        return false		// errorip
    }
	
    return true
}