/**
 * TikiCMS
 * Copyright (C) 2009, Tiki Web Inteligente Ltda.
 * @requires jQuery 1.3.2 or latter
 *
 * $Id: application.js 328 2011-05-05 16:54:47Z 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]['init'] &&
    Application.Controller[camelizedController]['init'].call();

    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;
    });
    
    $(document).ready(function(){
        $("a[rel^='lightbox']").prettyPhoto({
            show_title: false,
            social_tools: '',
            markup: '<div class="pp_pic_holder"> \
						<div class="ppt">&nbsp;</div> \
						<div class="pp_content_container"> \
                            <div class="pp_content"> \
                                <div class="pp_loaderIcon"></div> \
                                <div class="pp_fade"> \
                                    <a href="#" class="pp_expand" title="Expand the image">Expand</a> \
                                    <div class="pp_hoverContainer"> \
                                        <a class="pp_next" href="#">next</a> \
                                        <a class="pp_previous" href="#">previous</a> \
                                    </div> \
                                    <div id="pp_full_res"></div> \
                                    <div class="pp_details"> \
                                        <p class="pp_description"></p> \
                                        <a class="pp_close" href="#">Close</a> \
                                    </div> \
                                </div> \
								</div> \
						</div> \
					</div> \
					<div class="pp_overlay"></div>',
            gallery_markup: ''
        });
    });

    
    /*
    $('#nav a').hover(function(){
        $(this).stop(false,false).animate({
            color: '#bd466d'
        }, 300);
    },function(){
        $(this).stop(false,false).animate({
            color: '#797979'
        }, 300);
    });
    */
    
    $('#nav p.inativo').live('click', function(){
        $('#nav li:not(.current) .ativo').removeClass('ativo').addClass('inativo').siblings('.sub').slideUp(300);
        
        $(this).removeClass('inativo').addClass('ativo').siblings('.sub').slideDown(300);
    });
    
    $('#nav li:not(.current) p.ativo').live('click', function(){
        $(this).removeClass('ativo').addClass('inativo').siblings('.sub').slideUp(300);
    });
    
    $('.bt_a').not('.current').hover(function(){
        $(this).children().stop(false,false).fadeTo(300, 1);
    }, function(){
        $(this).children().stop(false,false).fadeTo(300, 0);
    });
    
    $('.footer_nav > li').hover(function(){
        $(this).find('.sub').stop(true,true).fadeIn(500);
    },function(){
        $(this).find('.sub').stop(true,true).fadeOut(500);
    });
    /*
    $('.produtos_list .valor').each(function(){
        var $this = $(this),
            diferenca = 241 - $(this).outerWidth(true);
        
        $this.closest('a').hover(function(){
            $this.siblings('img').stop(false,false).fadeTo(500, '.75', 'easeOutQuint');
            $this.siblings('.tit').stop(false,false).animate({height: '45px'}, 500, 'easeOutQuint');
            $this.stop(false,false).animate({left: diferenca}, 500, 'easeOutQuint');
        },function(){
            $this.siblings('img').stop(false,false).fadeTo(500, 1, 'easeOutQuint');
            $this.siblings('.tit').stop(false,false).animate({height: '40px'}, 500, 'easeOutQuint');            
            $this.stop(false,false).animate({left: '100%'}, 500, 'easeOutQuint');
        });
    })
    */
    
    $('.produtos_list:not(.produtos_list_colecao, .campanha_list) a').hover(function(){
        $(this).find('img').stop(false,false).fadeTo(500, .75, 'easeOutQuint');
        $(this).find('.tit').stop(false,false).animate({height: '45px'}, 500, 'easeOutQuint');
        $(this).find('.valor').stop(false,false).animate({right: 0}, 500, 'easeOutQuint');
    },function(){
        $(this).find('img').stop(false,false).fadeTo(500, 1, 'easeOutQuint');
        $(this).find('.tit').stop(false,false).animate({height: '40px'}, 500, 'easeOutQuint');
        $(this).find('.valor').stop(false,false).animate({right: '-134px'}, 500, 'easeOutQuint');
    });
    
    
    // Select Custom
    $('div.select_custom > p').click(function() {
        var speed = 250;
        var easing = 'easeInOutCirc';
        var $combo = $(this).closest('div.select_custom');
        var $ul = $combo.children('ul'); 
        var realComboId = $combo.attr('id').replace(/custom_combo_/, '');
        var $realCombo = $('#'+realComboId);

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

        // esconde outras combos abertas
        $('div.select_custom 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.select_custom').length > 0) {

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

    $('div.select_custom li').live('click', function() {
        var $combo = $(this).closest('div.select_custom');
        var $ul = $combo.children('ul:first');
        var realComboId = $combo.attr('id').replace(/custom_combo_/, '');
        var $realCombo = $('#'+realComboId);
        var label = $(this).find('.text').text();
        var value = $(this).find('.value').text();

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

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

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

