//parseamos el xml recibido
function parse_XML_FOTOS(xml) {

    var cont = 0;
    var codigoHTMLFotos = '<ul class="ad-thumb-list" >';
    $(xml).find("elemento").each(function () {
        var descripcion = $(this).find("descripcion").text();
        var link = $(this).find("link").text();
        var titulo = $(this).find("titulo").text();
        codigoHTMLFotos += '<li><a href="' + link + '" name="' + cont + '"><img height="44px" src="' + link + '" title="' + titulo + '"/></a></li>';
        cont++;
    });
    codigoHTMLFotos += '</ul>';
    $('#lista-fotos').html(codigoHTMLFotos);
    addonclickActions('rajoyistas-mundo-fotos');


}

function cambiaHrefFotos() {

    $('.ad-thumb-list a').click(function (e) {
        SWFAddress.setValue('fotos/rajoyistas-mundo-fotos/' + $(this).attr('name'));
    });
}


//parseamos el xml recibido
function parse_XML_videos(xml, seccion) {
	
	$arrVideos = new Array();
	var cont = 0;
    var codigoCarrusel = "";
    $(xml).find("elemento").each(function () {
        var imagen = $(this).find("image").text();
        var video = $(this).find("link").text();
        var titulo = $(this).find("titulo").text();
        var idVideo = $(this).find("idVideo").text();
		$arrVideos.push(idVideo);
	    codigoCarrusel += ' <div class="carousel-feature"><a href="#" name="verVideo#!#' + idVideo + '"><img width="300px" height="226px" src="' + imagen + '" url="' + video + '" class="carousel-image" alt="Foto 1" /><span class="nomFoto">' + titulo + '</span><div class="carousel-caption"><p>PLAY <b>&gt;</b></p></div></a></div>';
        cont++;
    });


    $('#carousel').html(codigoCarrusel);

    $('#carousel').children().each(function (i) {

        //$("#img" + i).reflect();
        //$('#img1').reflect();
    });


    addonclickActions(seccion);
    processCarrusel();


}

function playRajoyistasMundo(idVideo) {

    play(idVideo);

    $("#boton-anterior").css('z-index', 0);
    $("#boton-siguiente").css('z-index', 0);
}

function addonclickActions(seccion) {

    //alert(seccion);
    if (seccion == 'rajoyistas-mundo') {
        $('.carousel-feature a').click(function (e) {

            //Get the A tag
            var idVideo = $(this).attr('name').split('#!#')[1];
            SWFAddress.setValue('video/' + seccion + '/' + idVideo);
            $("#boton-anterior").css('z-index', 0);
            $("#boton-siguiente").css('z-index', 0);
            //return false;
            e.preventDefault();
        });

    }

    if (seccion == 'rajoyistas-mundo' || seccion == 'rajoyistas-mundo-fotos')
    { 
        $('.window .closeSubeContenido').click(function (e) {
            //Cancel the link behavior
            //e.preventDefault();
           // $('.window').hide();
            $('#mask, .window').fadeOut(1000);            

        });

    }

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        //e.preventDefault();
        $('.window').hide();
        $('#mask, .window').fadeOut(1000);
        $('#playerVideo').html('');

    });

    //if mask is clicked
    $('#mask').click(function () {

        $('.window').hide();
        $('#mask, .window').fadeOut(1000);
        $('#playerVideo').html('');
    });

}



function processCarrusel() {
    $("#carousel").featureCarousel({
        pauseOnHover: true,
        largeFeatureWidth: "300px",
        largeFeatureHeight: "226px",
        smallFeatureWidth: "200px",
        smallFeatureHeight: "100px",
        movedToCenter: function ($feature) {
            var desc = $feature.find('.nomFoto').html();
            if (desc.length > 94)
                desc = cortarString(desc);
            $('.nombreFotoActual').html(desc);

        }

    });

}

function cortarString(str) {
    return str.substring(0, 91) + '...';
}

function play(idVideo) {
    muestraMascara('#video');

    //var params = { allowScriptAccess: "always" };
    //swfobject.embedSWF('http://www.youtube.com/v/' + idVideo +"?enablejsapi=1&playerapiid=ytplayer", 
    // "playerVideo", "460", "310", "8", null, null, params, atts);

    var iframeCode = '<iframe width="460" height="310" src="http://www.youtube.com/embed/' + idVideo + '" frameborder="0" allowfullscreen></iframe>';
    $.idVideoActivo = idVideo;
    //alert('idVideo');
    $('#playerVideo').html(iframeCode);
    //$('#iframevideo').attr('src','http://www.youtube.com/embed/' + idVideo);

}

function playVideoHome(idVideo) {

    play(idVideo+"?autoplay=1");

}

function muestraMascara(idDiv) {


    //transition effect		
    $('#mask').fadeIn(1000);
    //$('#mask').fadeTo("slow",1.0);	

    //Get the window height and width
    var winH = $('#mask').height(); //$(window).height();
    var winW = $('#mask').width();
    var wDiv = winH / 2 - $(idDiv).width() / 2 + 104;
    var hDiv = winH / 2 - $(idDiv).height() / 2 + 104
    //Set the popup window to center
    $(idDiv).css('top', winH / 2 - $(idDiv).height() / 2 + 104, 4);
    $(idDiv).css('left', winW / 2 - $(idDiv).width() / 2);
    $(idDiv).css('position', 'absolute');

    //transition effect
    $(idDiv).fadeIn(1000);
    $(idDiv).css('z-index', '1000000');

}

function stopVideo(player) {
    //player.stopVideo();


}

function showSubeContenido() {

    //transition effect		
    //$('#subeVideo').fadeIn(1000);
    //$('#mask').fadeTo("slow",1.0);
    muestraMascara('#subeContenido');	

}


