/**
 * TikiCMS
 * Copyright (C) 2009, Tiki Web Inteligente Ltda.
 * @requires jQuery 1.3.2 or latter
 *
 * $Id: application.js 2 2010-07-05 20:00:00Z caio $
 */

// 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($) {

    // invoca o controlador e o método solicitados
    var controller = $('meta[name=controller]').attr('content');
    var method = $('meta[name=method]').attr('content');
    var camelizedController = $.map(controller.split('_'), function(val) { return val.substr(0,1).toUpperCase() + val.substr(1) } ).join('');

    Application.Controller[camelizedController] &&
    Application.Controller[camelizedController][method] &&
    Application.Controller[camelizedController][method].call();

    // Abre links com o rel external em novas janelas
    $("a[rel^='external']").click(function(){
        window.open($(this).attr('href'));
        return false;
    });
    
    
    // Select Custom
    $('div.selectCustom > p a').click(function() {

        var speed = 250;
        var easing = 'easeInOutCirc';
        var $combo = $(this).closest('div.selectCustom');
        var $ul = $combo.children('ul'); 
        var realComboId = $combo.attr('id').replace(/custom_combo_/, '');
        var $realCombo = $('#'+realComboId);

        $('div.selectCustom').removeClass('current');
        
        $realCombo.trigger('focus');
        
        $combo.toggleClass('current');
        
        // zera o z-index de outras combos abertas
        $('div.selectCustom').not($combo).css('z-Index', 100);

        // esconde outras combos abertas
        $('div.selectCustom ul').not($ul).slideUp(speed, easing);

        if ($ul.is(':visible')) {
            $ul.slideUp(speed, easing);
        } else {
            $combo.css('z-index', 1000);
            $ul.slideDown(speed, easing);
        }
    });

    if ($('div.selectCustom').length > 0) {

        // ao clicar fora de combos abertas esconde-as
        $(document).bind('mouseup.custom_combo', function(e) {
            if ($(e.target).closest('div.selectCustom').length == 1) return false;
            $('div.selectCustom').removeClass('current');
            $('div.selectCustom ul').slideUp(250, 'easeInOutCirc');
        });
    }

    $('div.selectCustom li a').live('click', function() {

        var $combo = $(this).closest('div.selectCustom');
        var $ul = $combo.children('ul:first');
        var realComboId = $combo.attr('id').replace(/custom_combo_/, '');
        var $realCombo = $('#'+realComboId);
        var label = $(this).text();
        var value = $(this).parent().children('.value').text();

        $combo.removeClass('current');
        $ul.find('li > a').removeClass('current');
        $(this).addClass('current');

        $combo.find('p:first a span').text(label);
        $ul.hide();

        $realCombo.val(value);
        $combo.trigger('change', [label, value]);
    });

    // inicializa o valor de selects já marcadas
    
    // Inputs radio
    $('.input_radio input').change(function(){
        $(this).closest('ul').find('label').removeClass('checked');
        $(this).next('label').addClass('checked');                        
    });
    // Inputs checkbox    
    $('.input_checkbox input').change(function(){
        $(this).next('label').toggleClass('checked');
    });
});

