/* Script desarrollado para Feels.tv por Fabbian Alvarez de disarrollador.com  */

$(function () {
    var titulo_web = document.title;
    var video_actual = -1;
    var total_videos = $('a.videobox').size();
    
    los_videos	= $('a.videobox');
			
    $('a.videobox, a.videobox2').bind('click', function (e) {
    	
        e.preventDefault();
        var $this = $(this);
		video_actual = los_videos.index(this);

        /* Buscamos el video de vimeo y lo transformamos en el player iframe */
        id_player = $this.attr('datos:vimeo').replace('http://www.vimeo.com/', '').replace('http://vimeo.com/', '');
    	window.location.hash = id_player;

		if($('#video_feels_'+id_player).length > 0) return false;
		
		$('#cargando_nombre').html($this.attr('datos:titulo'));
        $('.infobar, #loading').show();


        $player = $('<iframe src="http://player.vimeo.com/video/' + id_player + '?title=0&amp;byline=0&amp;portrait=0&amp;color=bf24b0&amp;autoplay=1&amp;nocache=' + noCache() + '" width="480" height="324" frameborder="0" id="video_feels_'+id_player+'"></iframe>'); /* cargamos el player iframe */

        $player.load(function () {

            $('.infobar, #loading').hide();
            $('#cerrar').show();
            $('body').css({
                'overflow': 'hidden'
            });
		 	if($('#el_video iframe').length > 1){
		 		$('#el_video iframe:first').hide(function () {
            	    $(this).remove();

            	    $('#el_video iframe').css({
            	        'width': '480px'
            	    }).show();
            	});
            }

            $('#contenido').css({
                'z-index': '0',
                'height': '0px'
            });
            $('#panel').css('height', '100%');
            var $player = $(this);
            $player.fadeIn(300);

            cargarDatos($this);

            if (video_actual == 0) $('#prev').hide();
            else $('#prev').show();

            if (video_actual == parseInt(total_videos - 1)) $('#next').hide();
            else $('#next').show();

        }).appendTo("#el_video");
    });


    $('#cerrar').live('click', function () {
        $this = $(this);
        $('.infobar, #cerrar').hide();
        $('#description').empty().hide();
        $('#contenido').css({
            'z-index': '10',
            'height': '100%'
        }).animate({
            'height': '100%'
        }, function () {
            var $theWrapper = $(this);
            $('#panel').css('height', '0px');
            $theWrapper.css('z-index', '0');
            $('#wrapper iframe').remove();
            $('#prev').hide();
            $('#next').hide();
            $('body').css({
                'overflow': 'auto'
            });
            document.title = titulo_web;
            window.location.hash = '';

        });
    });

    $('#next').bind('click', function () {
        $('a.videobox:eq(' + parseInt(video_actual + 1) + ')').click()
    });
    $('#prev').bind('click', function () {
        $('a.videobox:eq(' + parseInt(video_actual - 1) + ')').click();
    });


    function cargarDatos(obj) {
        var titulo = obj.attr('datos:titulo');

        if (titulo != '') $('#info_video h2').html(titulo).show();
        else $('#info_video h2').empty().hide();

        document.title = titulo + ' | ' + titulo_web;

        var descripcion = obj.attr('rel');

        if (descripcion) $('#info_video .descripcion').html(descripcion).show();
        else $('#info_video .descripcion').empty().hide();

        var cliente = obj.attr('datos:cliente');

        if (cliente) $('#info_video .cliente').hide().show().find('p').html(cliente);
        else $('#info_video .cliente').hide();

        var agencia = obj.attr('datos:agencia');

        if (agencia) $('#info_video .agencia').hide().show().find('p').html(agencia);
        else $('#info_video .agencia').hide();

        var descarga = obj.attr('datos:descarga');

        if (descarga) $('#info_video #descargar_video').show().attr('href',descarga);
        else $('#info_video #descargar_video').hide();

        id_player = obj.attr('href').replace('http://www.vimeo.com/', '');
        
        $('#info_video #twittear').attr('href','http://twitter.com/share?text=Viendo+el+video+%22'+titulo+'%22+en+Feels&url='+obj.attr('href'));

        $('#info_video #Facebook').attr('href','http://facebook.com/sharer.php?t=Viendo+%22'+titulo+'%22+en+Feels&u='+obj.attr('href'));

    }


    function noCache() {
        var fecha_actual = new Date;
        return fecha_actual.getTime();
    }

    var elHash = window.location.hash;
    
    elHash3 = location.hash.replace('#', '');
    if(elHash3.indexOf('board_') != -1){
			$("#" + elHash3).click();
	}
    else if (elHash.length > 1) {
        elHash = elHash.replace('#', '');
        $("#video_" + elHash).click();
    }

});

var hash = location.hash;

setInterval(function()
{
    if (location.hash != hash){
        elHash2 = location.hash.replace('#', '');


    	if(elHash2 != ''){
	        $("#video_" + elHash2).click();
	    }
	    else{
	    	$('#cerrar').click();
	    }
	        hash = location.hash;
    }
}, 100);
