/**
 * TikiCMS
 * Copyright (C) 2009, Tiki Web Inteligente Ltda.
 * @requires jQuery 1.3.2 or latter
 *
 * $Id: application.js 64 2010-03-17 20:17:09Z jair $
 */

// define o namespace da aplicação
Application = {
    Controller: {}
};

/**
 * Retorna uma URL completa dado um caminho relativo.
 *
 * É importante que esta função seja definida antes
 * da definição das biliotecas "thickbox" e "sIFR",
 * pois alterei o código-fonte delas para que caminhos
 * relativos sejam convertidos em caminhos absolutos utilizando
 * esta função.
 *
 * @param  string url Um pedaço de URL (caminho relativo dentro do servidor)
 * @return string     Uma URL completa
 */
Application.build_url = function(url) {

    if (!Application.BASE_URL || !Application.BASE_URL.match(/^http/)) {
        Application.BASE_URL = $('meta[name=base_url]').attr('content');
    }

    return Application.BASE_URL + url; 
}

jQuery(document).ready(function($) {

    //IDÉIAS
    $('.ideiasChamada a').click(function(){
        $('.ideias').fadeIn(500);
        $(".ideiasContent").jCarouselLite({
            visible: 1,
            start: 0,
            speed: 700,
            btnGo: [".ideiasNav .1", ".ideiasNav .2", ".ideiasNav .3", ".ideiasNav .4"]
        });
    });

    $('.ideiasHeader input').click(function(){
        $('.ideias').fadeOut(500);
    });


    $("#clientes ul li").each(function(i){
        if(i % 2 == 0){
            $(this).addClass("top");
        }
    });



    //TARGET="_BLANK"
    $('a[rel*=_blank]').click(function() {
        window.open(this.href);
        return false;
    });

    /*POSICIONA BOX DE NOTÍCIAS NO BOTTOM DA DIV
    var hContent = $('#content').height();
    var hNews = $('#noticias').height();
    $('#noticias').css({'margin-top':(hContent-81)-hNews});*/

    // carrega o feed de notícias
    $.getFeed({
        url: Application.build_url('blog/feed'),
        success: function(feed) {
            if (feed.items && feed.items[0]) {

                var titulo = feed.items[0].title;
                var url = feed.items[0].link;
                var data = new Date(feed.items[0].updated);
                var corpo = feed.items[0].description.substring(0, 200)+' [...]';

                var dataFormatada = data.getDate() + '/' + (data.getMonth() + 1) + '/' + data.getFullYear();

                $('h3.noticias').after(
                    '<div id="noticia">'+
                    '    <span class="data">'+dataFormatada+'</span>'+
                    '    <p><a href="'+url+'" target="_blank">'+corpo+'</a></p>'+
                    '</div>'
                );
            }
        }
    });

    // invoca o controlador e o método solicitados
    var controller = $('meta[name=camelized_controller]').attr('content');
    var method = $('meta[name=method]').attr('content');

    Application.Controller[controller] &&
    Application.Controller[controller][method] &&
    Application.Controller[controller][method].call();
});
