var HOST = "http://www.uai.com.br";

$(function () {

    var URL_ULTIMOS = HOST + '/apps/' + APP_ID + '/getGraficosPublicado'
    var URL_INFO = HOST + '/apps/' + APP_ID + '/getGrafico'
    var NUMERO_PAGINAS_MOSTRAR = 18;

    var p = Pagination({

        layout_page_total: NUMERO_PAGINAS_MOSTRAR,
        layout_page_items_total: POR_PAGINA,
        
        list_container_add: function (item) {
            if (item.content_type.search('image') >= 0)
                content_type = 'image';
            else if (item.content_type.search('swf') >= 0)
                content_type = 'swf';
            else
                content_type = 'flv';

            $('<a></a>').attr('rel', 'box').attr('href', (content_type == 'image' ? item.arquivo:item.arquivo+'#inflash')).attr('content_type', content_type)
                        .append($('<b class="innerborder left"><img width="101" height="76" src="' + item.imagem_destaque + '"/></b>'))
                        .append($('<span class="h4"></span>').text(item.titulo))
                        .append($('<span></span>').text(item.descricao))
                        .appendTo(this.list_container)
                        .fancybox({'hideOnContentClick':true,
                                   'imageScale':false,
                                   'centerOnScroll':false,
                                   'overlayShow':true,
                                   'frameWidth':720,
                                   'frameHeight':480});
        },
        
        page_container_add: function (page_number) {
                var self = this;
                var item = $('<a href="javascript: void(0)">' + page_number + '</a>').click(function () { 
                    self.page_current = page_number;
                    self._show_pages();
                    self.page_click(self, page_number) 
                });
                if (self.page_current == page_number)
                  item.addClass("current");
                $(self.page_container).append(item);
         },
        
        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_click: function (self, page_number) {
            var offset = (page_number-1) * POR_PAGINA;
            loading(self.list_container)
            window.location.hash = 'videos_items'
            self.page_current = page_number;
            $.get(self.URL, {
                    'limit': POR_PAGINA,
                    'offset': offset,
                    'id_categoria': ID_CATEGORIA
                },
                function (data) {
                    if (data.itens.length == 0) {
                        $(self.list_container).html('<center>Busca não retornou resultados.</center>')
                    }
                    else {
                        self.set_items(data.itens)
                        self.set_items_total(data.qtde)
                        $(self.control_previous).nextAll().eq(page_number-1).addClass('current');
                    }
                    window.location.hash = 'videos_items';
                    self._show_pages();
                },
                'json');
        }
    
    })
    p.URL = URL_ULTIMOS;

    if (window.location.href.indexOf('i=') >= 0) {
        var pattern = /i=([0-9]*)/;
        var info_id = pattern.exec(window.location.href)[1];
        $.get(URL_INFO, {
                'id_infografico': info_id 
              },
              function (data) {
                if (!data.id_infografico)
                {
                        alert('Vídeo não disponível');
                } else {
                  var ct = (data.content_type.search('image') >= 0 ? 'image': (data.content_type.search('swf') >= 0 ? 'swf':'flv') );
                  $('<a href="'+ data.arquivo + (data.content_type.search('image') < 0 ? '#inflash' : '') +'" content_type="'+ ct +'"><img src="imgs/ie6.gif" /></a>')
                    .appendTo($(document.body))
                    .fancybox({'hideOnContentClick':true,
                        'imageScale':false,
                        'centerOnScroll':false,
                        'overlayShow':true,
                        'frameWidth':720,
                        'frameHeight':480
                    }).click(); 
                }
              },
              'json')
    }
    
    function loading(to) {
        $(to).empty().append('<center><img src="imgs/loading.gif"/></center>')
    }
    
    p.set_items_total(TOTAL)

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

    $('#gotobutton').click(function () {
        var v = $.trim($('#gotopage').val())
        if (v != '') p.page_click(p, v);
        
    })    
})
