var loaded = true;

if ($.address != undefined) {
    
    $.address.init(function(event) {
        //$('a:not([href="javascript:void(0)"], [href="#"], .external)').address();
        $('ul.lista-produtos a, a.lk-prev, a.lk-next, a.lk-fechar, a.item-media').address();
    }).change(function(event) {

        var $body = $('body');

        if ( ! loaded) {
            var value = $.address.state().replace(/^\/$/, '') + event.value;

            if ((event.value == '/' && $.address.state().search('chocolates') != -1) || event.value.search('chocolates') != -1) {
                $.address.title('Chocolates | LACTA® 100 anos');
                internal_track('chocolates');
            } else if (value.search('chocolates') != -1) {
                pieces = event.value.split('/');
                
                internal_track('chocolates/'+pieces[1]);

                if ( ! $('#detail-'+pieces[1])[0]) {
                    $('.products-list').hide();
                    $('.loader').fadeIn();
                    $.post(value, {}, function(data) {
                        $.address.title(data.title);
                        $('.products-list .product-loaded').css('display', 'none');
                        $('#detalhe-produto .products-list').append(data.content).css('display', 'block');
                        $('.loader').fadeOut(function(){
                            $('.products-list').show();
                        });
                        
                        if ($('.cat-todos').not('.active')) {
                            $('.filtered').show();
                            $('.no-filter').hide();
                        } else {
                            $('.no-filter').show();
                            $('.filtered').hide();
                        }
                        
                        FB.XFBML.parse();
                        twttr.widgets.load();
                        gapi.plusone.go();
                    });
                } else {
                    $('.products-list').show();
                    $('.products-list .product-loaded').hide();
                    $('#detail-'+pieces[1]).show();
                    $.address.title($('#detail-'+pieces[1]).find('h2.tt-produto').html()+' | Chocolates Lacta');
                    
                    if ($('.cat-todos').is('.active')) {
                        $('.no-filter').show();
                        $('.filtered').hide();
                    } else {
                        $('.filtered').show();
                        $('.no-filter').hide();
                    }
                    
                    FB.XFBML.parse();
                    twttr.widgets.load();
                    gapi.plusone.go();
                }
            } else if ((event.value == '/' && $.address.state().search('historia') != -1) || event.value.search('historia') != -1) {
                $.address.title('História | LACTA® 100 anos');
                internal_track('historia');
				//return $(this).attr('href').replace(/^#/, '');
            } else if (value.search('historia') != -1) {
                pieces = event.value.split('/');
                
                internal_track('historia/'+pieces[1]);
                
                $.post(value, {}, function(data){
                    $.address.title(data.title);
                    $('.modal-content').html(data.content);
                    
                    FB.XFBML.parse();
                    twttr.widgets.load();
                });
            }
        }

        loaded = false;
    });

}

$(window).load(function(){
    if ($('#view-item')[0]) {
        var html,
            top = parseInt($('#dec-'+$('#story-year').val()).offset().top),
            left = Math.floor(parseInt($('#modal').outerWidth())/2)
        
        $('#modal').css({
            'top' : top+15+'px',
            'marginLeft' : '-'+left+'px'
        });
        
        html = $('#view-item').html();
        
        $('body').append(html);
        $('#view-item').remove();
        $('html, body').animate({ scrollTop: top }, 500);
    }
});

function blockElement(element, color) {
    element.block({ 
        message: '<img src="'+base_url+'public/imgs/loader.gif" />', 
        css: {
            padding: 4,
            backgroundColor: 'none',
            border: 'none'
        },
        overlayCSS: {
            backgroundColor: color,
            opacity: 0.95
        }
    }); 
}

function unblockElement(element) {
    element.unblock();
}

function validate_fields($form, fields) {
    $form.find('.error').removeClass('error');
    
    var enabled = true;
    
    for (i=0; i<fields.length; i++) {        
        if ($form.find(fields[i]).val() == '') {
            $form.find(fields[i]).addClass('error');
            enabled = false;
        } else if ($form.find(fields[i]+':checked').val() == undefined && $form.find(fields[i]+':radio')[0]) {
            $form.find(fields[i]).parent().addClass('error');
            enabled = false;
        }
    }
    
    return enabled;
}

function internal_track(page) {
    _gaq.push(['_trackPageview', page]);
    _gaq.push(['lacta._trackPageview', page]);
}
