var SectionView = BaseView.extend({
    init: function(){
        this.current_section = null;
        this.section_id = null;
        this.submenu_id = null;
        
        //O plugin do Accordion dispara este evento ao abrir a seção.
        $('.accordion-container').bind('change', $.proxy(this.set_current_section, this));
        
        $('#second_menu').template('second-menu');
        
        if($('a.fair').length){
            $('a.fair').fancybox();
        }
    },
    set_current_section: function(e){
        this.current_section = $(e.currentTarget);
        this.set_nav();
    },
    set_nav: function(){
        /* Seta a navegação interna, se houver menu horizontal */
        $('div.menu.horizontal a', this.current_section).bind('click', $.proxy(this.show, this));
        $('div.menu.vertical h4 a', this.current_section).bind('click', $.proxy(this.show_submenu, this));
    },
    show: function(e){
        e.preventDefault();
        var index = $(e.currentTarget).parent().index();
        
        if($(e.currentTarget).parent().hasClass('selected')) { return false; }
        
        if($(e.currentTarget).attr('href') == '#map') { this.set_map(); }
        
        $('div.menu.horizontal ul li', this.current_section).removeClass('selected');
        $(e.currentTarget).parent().addClass('selected');
        
        this.section_id = index;
        
        this.hide();
        $('div.header > ul > li:eq('+index+'), div.content > ul > li:eq('+index+')', this.current_section)
            .fadeIn()
            .addClass('selected');
    },
    hide: function(){
        $('div.header > ul > li, div.content > ul > li', this.current_section)
            .hide()
            .removeClass('selected');
    },
    show_submenu: function(e){
        e.preventDefault();
        this.submenu_id = $(e.currentTarget).parent().parent().index();
        this.config_submenu();
        
        $('ol.first #menu-paraiba').hide();
        
        var w = null;
        /* Verifica se é menu Paraiba */
        if(this.submenu_id == 4){ 
            w = 370;
            $('ol.second').width(370);
            this.current_section.animate({width: 510}, 600, 'easeInOutExpo');
        } else {
            w = 300;
            $('ol.second').width(160);
            this.current_section.animate({width: 300}, 600, 'easeInOutExpo');
        }
    },
    config_submenu: function(){
        $('ol.second').empty();
        if(this.submenu_id == 4){
            $('ol.first #menu-paraiba').clone().appendTo('ol.second').show();
            return false; 
        }
        
        $('ol.third').hide();
        
        var list_menu = [];
        $('div.second ol.first > li:eq('+this.submenu_id+') dt').each(function(i){
            var el = $(this);
            list_menu.push({
                name: el.text(),
                disabled: el.hasClass('disabled'),
                img: (el.hasClass('img')) ? $('img:first-child', el).attr('src') : null,
                url: (el.attr('data-url')) ? el.attr('data-url') : '#'
            });
        });
        
        $.tmpl('second-menu', list_menu).appendTo('ol.second');
        $('ol.second h5 a:not(.disabled-true)').bind('click', $.proxy(this.show_submenu_content, this));
    },
    show_submenu_content: function(e){
        $('ol.third').empty();
        
        var index = $(e.currentTarget).parent().parent().index();
        $('ol.first > li:eq('+this.submenu_id+') dd:eq('+index+') ul li').clone().appendTo('ol.third');
        $('ol.third').show();
        this.current_section.animate({width: 510}, 600, 'easeInOutExpo');
    },
    set_map: function(){
        setTimeout(function(){
            if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("map_canvas"));
                map.setCenter(new GLatLng(-23.569081, -46.664611), 14);
                map.setUIToDefault();

                var marker = new GMarker(new GLatLng(-23.569081, -46.664611));
                map.addOverlay(marker);

                GEvent.addListener(marker, 'click', function (latlng) {
                    window.open('http://maps.google.com/maps?q=Brasil+a+Gosto,+Sao+Paulo+-+S%C3%A3o+Paulo,+Brazil&hl=en&ie=UTF8&ll=-23.569179,-46.663946&spn=0.008182,0.010053&sll=37.0625,-95.677068&sspn=57.118084,82.353516&z=17');
                });
            }
        }, 500);
    },
    dispose: function(){
        
    }
});
