function mktxt(text)
{
  ntext = '';
  if (text.length <= 80)
    return text;
  text = text.substr(0, 80).split(' ');
  for (i=0; i<(text.length-1); i++)
  {
    ntext += text[i]
    if (i <= (text.length-2) )
      ntext += ' ';
  }
  return ntext + '...';
}

$(function () {
   
    var URL_VIDEO = '/appss/' + VIDEO_APP_ID + '/getVideo'
    var URL_ULTIMOS = '/appss/' + VIDEO_APP_ID + '/getVideos'
    var NUMERO_PAGINAS_MOSTRAR = 10;

    var p = Pagination({

        layout_page_total: NUMERO_PAGINAS_MOSTRAR,
        layout_page_items_total: VIDEOS_POR_PAGINA,
        
        list_container_add: function (item, index) {
	        $('<a></a>').attr('href', 'javascript: void(0)').attr('style', ( parseInt(index) % 2 == 0  ? 'clear:left':''))
                        .append($('<b class="innerborder left"><img height="76" width="101" src="' + item.thumb + '"/></b>'))
                        .append($('<span class="h4"></span>').text(item.titulo))
                        .append($('<span></span>').text( mktxt(item.descricao) ))
                        .appendTo(this.list_container)
                        .click(function () {
                            window.location.hash = 'video_item'
                            window.location.hash = 'video_' + item.sea_id
                            $("#video_subtitulo").text(item.titulo);
                            $("#video_descricao").text(item.descricao);
                            $("#video_player").attr('video_id', item.sea_id);
                            $("#video_player").html(unescape(item.embed).replace('autoLoad=false&autoPlay=true', 'autoLoad=false\n&', 'g'));
                        })
                        
        },
        
        page_container_add: function (page_number) {
                var self = this;
                var $a = $('<a href="javascript: void(0)">' + page_number + '</a>').click(function () { 
                    self.page_current = page_number;
                    self._show_pages();
                    self.page_click(self, page_number) 
                })
                $(self.control_next).before($a);
                
         },
        
        set_items1: function (items) {
            var self = this;
            var last_data = undefined;
            this.list_container_empty();
            $.each(items, function (index, item) {
                var publicado_em = item.publicado_em.slice(0, -6);
                if (publicado_em != last_data) {
                    $('<span class="date"></span>').text(kingkong(publicado_em)).appendTo(self.list_container);
                }
                last_data = publicado_em;
                self.list_container_add(item, index);
            })
        },
        
        control_first_enable: function () {
            $(this.control_first).removeClass('inactive')
        },
        control_first_disable: function () {
            $(this.control_first).addClass('inactive')
        },
        control_previous_enable: function () {
            $(this.control_previous).removeClass('inactive')
        },
        control_previous_disable: function () {
            $(this.control_previous).addClass('inactive')
        },
        control_next_enable: function () {
            $(this.control_next).removeClass('inactive')
        },
        control_next_disable: function () {
            $(this.control_next).addClass('inactive')
        },
        control_last_enable: function () {
            $(this.control_last).removeClass('inactive')
        },
        control_last_disable: function () {
            $(this.control_last).addClass('inactive')
        },
        
        page_container_empty: function (page_number) {
            
            while ($(this.control_previous).next().get(0) != $(this.control_next).get(0)) 
                $(this.control_previous).next().remove();
        },
        
        page_click: function (self, page_number) {
            var offset = (page_number-1) * VIDEOS_POR_PAGINA;
            loading(self.list_container)
            window.location.hash = 'videos_items'
            $.get(self.URL, {
                    'tags': 'environ',
                    'limit': VIDEOS_POR_PAGINA,
                    'offset': offset
                },
                function (data) {
                    if (data.items.length == 0) {
                        $(self.list_container).html('<center>Busca não retornou resultados.</center>')
                    }
                    else {
                        self.set_items(data.items)
                        self.set_items_total(data.qtde)
                        $(self.control_previous).nextAll().eq(page_number-1).addClass('current');
                    }
                    window.location.hash = 'videos_items'
                },
                'json')
        }
    
    })
    p.URL = URL_ULTIMOS
    
    if (window.location.hash.indexOf('#video_') == 0) {
        var video_id = window.location.hash.split('#video_')[1]
        $.get(URL_VIDEO, {
                'id': video_id 
              },
              function (data) {
                if (!data.sea_id)
                {
                        $("#video_subtitulo").text('Vídeo não disponível');
                        $("#video_descricao").text('');
                        $("#video_player").html('');

                } else {
                        window.location.hash = 'video_item'
                        window.location.hash = 'video_' + data.sea_id;
                        $("#video_subtitulo").text(data.titulo);
                        $("#video_descricao").text(data.descricao);
                        $("#video_player").attr('video_id', data.sea_id);
                        $("#video_player").html( unescape(data.embed).replace('autoLoad=false&autoPlay=true', 'autoLoad=false\n&', 'g') );
                }
              },
              'json')
    }
    
    function loading(to) {
        $(to).empty().append('<center><img src="imgs/loading.gif"/></center>')
    }
    
    p.set_items_total(VIDEOS_TOTAL)

    $(p.control_previous).next().addClass('current');

    $("#video_enviar").click(function () {
        $("#url").remove()
        $('<input type="hidden" id="url"/>').val(window.location.href).appendTo(document.body)
        var winl = (screen.width)/2 - 230;
        var wint = (screen.height)/2 -178;
        window.open(URL_ENVIAR, 'wenviar', 'status=no, toolbar=no, menubar=no, personalbar=no, resizable=no, scrollbars=no, width=560, height=376, top='+wint+', left='+winl);
    })
    $("#video_imprimir").click(function () { print() })
    
    
    $('#gotobutton').click(function () {
        var v = $.trim($('#gotopage').val())
        if (v != '') p.page_click(p, v);
        
    })
	 
	$('#paginator_items > a').click(function () {
        var o = $(this)
	    window.location.hash = 'video_item'
        window.location.hash = 'video_' + o.attr('video_id')
        $("#video_subtitulo").text(o.attr('titulo'));
        $("#video_descricao").text(o.attr('descricao'));
        $("#video_player").html( unescape(o.attr('embed')).replace('autoLoad=false&autoPlay=true', 'autoLoad=false\n&', 'g') );
        $("#video_player").attr('video_id', o.attr('video_id'));
    })
})