ddaccordion.init({
    headerclass: "level_0", //Shared CSS class name of headers group
    contentclass: "level_1", //Shared CSS class name of contents group
    revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
    mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
    collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
    onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
    animatedefault: false, //Should contents open by default be animated into view?
    persiststate: false, //persist state of opened contents within browser session?
    toggleclass: ["", "active"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
    togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
    animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
    oninit: function (expandedindices) { //custom code to run when headers have initalized
        //do nothing
    },
    onopenclose: function (header, index, state, isuseractivated) { //custom code to run whenever a header is opened or closed
        //do nothing
    }
})

$(function () {
    $('#pesquisar').focus(function () { $(this).val("") });
    $('.level_0_no_sub').bind('mouseenter', function () {
        ddaccordion.collapseall('level_0');
    });
	
	var is_focused = 0;
    var cur_index;
    $('.level_0').focusin(function () {
        var cur_index;
        var comp_level = $(this).text();
        if (!is_focused) {
            $('.level_0').each(function (i) {
                if (comp_level == $(this).text()) { cur_index = i; }
            });
            is_focused = 1;
            ddaccordion.expandone('level_0', cur_index);
        } else {
            is_focused = 0;
            ddaccordion.collapseall('level_0');
            $('.level_0').each(function (i) {
                if (comp_level == $(this).text()) { cur_index = i; }
            });
            is_focused = 1;
            ddaccordion.expandone('level_0', cur_index);
        }
    });
    $('.level_0_no_sub').focusin(function () { ddaccordion.collapseall('level_0'); is_focused = 0; })

	
    /* BOTOES IMAGENS HOVER */
    /*$('.img_hover').hover(function(){
    $(this).attr('src','images/icons/'+$(this).attr('alt')+"_hover.png");
    },
    function(){
    $(this).attr('src','images/icons/'+$(this).attr('alt')+".png");

    });*/

    $(function () {
        $(".img_hover")
        .mouseover(function () {
            var src = $(this).attr("src").match(/[^\.]+/) + "_hover.png";
            $(this).attr("src", ".." + src);
        })
        .mouseout(function () {
            var src = $(this).attr("src").replace("_hover", "");
            $(this).attr("src", src);
        });
    });

    $(function () {
        $(".img_hover2")
        .mouseover(function () {
            var src = $(this).attr("src").match(/[^\.]+/) + "_hover.png";
            $(this).attr("src", src);
        })
        .mouseout(function () {
            var src = $(this).attr("src").replace("_hover", "");
            $(this).attr("src", src);
        });
    });

    /* END BOTOES IMAGENS HOVER */

    /* TABELAS LINHAS CLASSES */

    $('.prod_info_tb tr:even td').addClass('col_impar');
    $('.prod_info_tb tr:odd td').addClass('col_par');

    /* END TABELAS LINHAS CLASSES */

    /* AGENDA BOTOES */
    $('a[href="#proximo"]').click(function () {
        if ($(".agenda_menu ul li.active").next().length) {
            $(".agenda_menu ul li.active").removeClass('active').next().addClass("active");
            matchtheday();
        }
    });
    $('.agenda_menu ul li').click(function () {
        $('.agenda_menu ul li').each(function () {
            $(this).removeClass('active');
        });
        $(this).addClass('active');
        matchtheday();
    });

    /* END AGENDA BOTOES */

    /* HOME SLIDESHOW */

    $('#home_sld_esq').click(function () {
        $('.home_dest_img').fadeOut(500, function () {
            $(this).attr('src', 'images/img_dest_loja_1.jpg');
        }).fadeIn(500);
        $('.slide_desc').fadeOut(500, function () {
            $(this).html('<span class="txt_bold">Título teste</span><br>teste');
        }).fadeIn(500);
    });

    $('#home_sld_drt').click(function () {
        $('.home_dest_img').fadeOut(500, function () {
            $(this).attr('src', 'images/img_1.jpg');
        }).fadeIn(500);
        $('.slide_desc').fadeOut(500, function () {
            $(this).html('<span class="txt_bold">Fundação sorteia "pacote" pedagógico:</span><br>Os docentes inscritos na sessão de «Apresentação aos ...');
        }).fadeIn(500);
    });

    /* END HOME SLIDESHOW */

    /* NEW SLIDESHOW */

    var totWidth = 0;
    var ar_pos = Array;
    var position_x = 100;
    var sl_active;
    var marg_right = 0;
    var tam_img = [];

    $('#slides .slide').length;
    $('#slides .slide').each(function (i) {
        ar_pos[i] = $(this).width();
        totWidth += ar_pos[i];
        var w_img = $(this).children('a').children('img').attr("width");
        var h_img = $(this).children('a').children('img').attr("height");


        if (!$(this).hasClass('sl_activo')) {
            if (w_img > 260 * 0.8) {
                h_img = h_img * ((260 * 0.8) / w_img);
                w_img = 260 * 0.8;
                if (h_img > 180 * 0.8) {
                    w_img = w_img * ((180 * 0.8) / h_img);
                    h_img = 180 * 0.8;
                }
            } else if (h_img > 180 * 0.8) {
                w_img = w_img * ((180 * 0.8) / h_img);
                h_img = 180 * 0.8;
                if (w_img > 260 * 0.8) {
                    h_img = h_img * ((260 * 0.8) / w_img);
                    w_img = 260 * 0.8;
                }
            }
        } else {
            if (w_img > 260) {
                h_img = h_img * (260 / w_img);
                w_img = 260;
                if (h_img > 180) {
                    w_img = w_img * (180 / h_img);
                    h_img = 180;
                }
            } else if (h_img > 180) {
                w_img = w_img * (180 / h_img);
                h_img = 180;
                if (w_img > 260) {
                    h_img = h_img * (260 / w_img);
                    w_img = 260;
                }
            }
            sl_active = i;
        }
        $(this).children('a').children('img').width(w_img);
        $(this).children('a').children('img').height(h_img);
        $(this).css('top', (218 - h_img) / 2);
        tam_img[i] = [w_img, h_img];
    });

    $('#slides .slide').each(function (i) {
        var mg_left = 0;
        if (i < sl_active) {
            for (var m = i; m < sl_active; m++) {
                var img_tam = tam_img[m];
                mg_left = mg_left - (img_tam[0] + 20);
            }
            mg_left += 250;
        } else if (i == sl_active) {
            mg_left = 250;
        } else {
            for (var n = sl_active; n < i; n++) {
                var img_tam = tam_img[n];
                mg_left = mg_left + (img_tam[0] + 20);
            }
            mg_left += 250;
        }
        $(this).css('left', mg_left)
    });

    $('#slides').width(totWidth);

    $('#sld_drt').bind('click', proximo);

    function anterior() {
        if (sl_active == 0) return;
        $('#sld_drt').unbind('click');
        $('#sld_esq').unbind('click');
        $('#slides .slide').each(function (i) {
            if (i == sl_active - 1) {
                var dif_h = (218 - $(this).height() * 1.25) / 2;
                mostraProx(sl_active - 1);
                $(this).children('a').children('img').animate({
                    width: $(this).width() * 1.25,
                    height: $(this).height() * 1.25
                }, 1200)
                $(this).animate({
                    left: 250,
                    top: dif_h
                }, 1200, function () {
                    $(this).addClass('sl_activo');
                    sl_active = i;
                })
            } else if (i == sl_active) {
                var dif_h = (218 - $(this).height() * 0.8) / 2;
                $(this).children('a').children('img').animate({
                    width: $(this).width() * 0.8,
                    height: $(this).height() * 0.8
                }, 1200)
                $(this).animate({
                    left: 250 + $(this).prev().children('a').children('img').width() * 1.25 + 20,
                    top: dif_h
                }, 1200, function () {
                    $(this).removeClass('sl_activo');
                    $('#sld_drt').bind('click', proximo);
                    $('#sld_esq').bind('click', anterior);
                })
            } else if (i > sl_active) {
                var img_tam = tam_img[sl_active - 1];
                $(this).animate({
                    left: '+=' + (img_tam[0])
                }, 1200)
            } else {
                var img_tam = tam_img[sl_active - 1];
                $(this).animate({
                    left: '+=' + (20 + img_tam[0])
                }, 1200)
            }
        });
    }

    $('#sld_esq').bind('click', anterior);

    function proximo() {
        if (sl_active == $('#slides .slide').length - 1) return;
        $('#sld_esq').unbind('click');
        $('#sld_drt').unbind('click');
        $('#slides .slide').each(function (i) {
            if (i == sl_active + 1) {
                mostraProx(sl_active + 1);
                var dif_h = (218 - $(this).height() * 1.25) / 2;
                $(this).children('a').children('img').animate({
                    width: $(this).width() * 1.25,
                    height: $(this).height() * 1.25
                }, 1200)
                $(this).animate({
                    left: 250,
                    top: dif_h
                }, 1200, function () {
                    $(this).addClass('sl_activo');
                    sl_active = i;
                })
            } else if (i == sl_active) {
                var dif_h = (218 - $(this).height() * 0.8) / 2;
                var wiiidd = $(this).width() * 0.8;
                $(this).children('a').children('img').animate({
                    width: $(this).width() * 0.8,
                    height: $(this).height() * 0.8
                }, 1200)
                $(this).animate({
                    left: 250 - wiiidd - 20,
                    top: dif_h
                }, 1200, function () {
                    $(this).removeClass('sl_activo');
                    $('#sld_drt').bind('click', proximo);
                    $('#sld_esq').bind('click', anterior);
                })
            } else if (i > sl_active) {
                var img_tam = tam_img[sl_active];
                $(this).animate({
                    left: '-=' + (img_tam[0] * 0.8)
                }, 1200)
            } else {
                var img_tam = tam_img[sl_active];
                $(this).animate({
                    left: '-=' + (img_tam[0] + 20)
                }, 1200)
            }
        })
    }
    /* ALTERADO */
    function mostraProx(prox) {
        $('.item_dest_desc.item_active').fadeOut(600, function () {
            $(this).removeClass("item_active");
            if (sl_active > prox) {
                $(this).prev().addClass("item_active");
                $(this).prev().css("display", "block");
            } else {
                $(this).next().addClass("item_active");
                $(this).next().css("display", "block");
            }
        });
    }

    /* END SLIDESHOW LOJA */

    /* PASSWORD MATCH */

    $('a[href="#alterar"]').click(function () {
        validate_form();
        new_pass_val();
    });

    $('#new_pass_2').blur(function () {
        new_pass_val();
    });

    function validate_form() {
        $('.form_error_list').remove();
        var form_val = $('<div>', {
            'class': 'form_error_list'
        });
        var form_error_html = "";
        var bool_error = false;
        $('form input').each(function () {
            var this_name = $(this).attr('name');
            if ($(this).val() == "") {
                bool_error = true;
                if ($('label[for="' + this_name + '"]').text() != "") {
                    var txt = "Por favor preencha o campo " + $('label[for="' + this_name + '"]').text() + "<br>";
                    form_error_html += txt;
                }
                $(this).parent().addClass('input_error');
                $('label[for="' + this_name + '"]').addClass('label_error');
            } else {
                $(this).parent().removeClass('input_error');
                $('label[for="' + this_name + '"]').removeClass('label_error');
            }
        });
        if (bool_error) {
            form_val.html(form_error_html).insertAfter('form .tit_img');
        } else {
            form_val.addClass('valido').html("Foram feitas as alterações").insertAfter('form .tit_img');
        }
    }

    function new_pass_val() {
        $('.erro_abs').remove();
        if ($('#new_pass_1').val() != $('#new_pass_2').val()) {
            var erro_abs = $('<div>', {
                'class': 'erro_abs',
                html: "<div>As Passwords não coincidem.<br>Por Favor tente novamente.</div>"
            });
            var dist = $('#new_pass_2').position();
            erro_abs.css('top', dist.top);
            erro_abs.css('left', dist.left + $('#new_pass_2').width() + 20);
            erro_abs.appendTo('form');
            $('#new_pass_1').parent().addClass('input_error');
            $('#new_pass_2').parent().addClass('input_error');
            $('label[for="new_pass_1"]').addClass('label_error');
            $('label[for="new_pass_2"]').addClass('label_error');
        }
    }

    /* IMAGENS PRODUTO */

    $('.img_xxs').click(function () {
        var id = $(this).children('img').attr('id');
        var img_xxs_n = id.charAt(id.length - 1);
        var imagem_l = $('.detalhe_prod_img_gr_xl')[img_xxs_n];
        if (!$(imagem_l).hasClass('ativa')) {
            $('.detalhe_prod_img_gr_xl.ativa').fadeOut(1000, function () {
                $(this).removeClass('ativa')
            });
            $(imagem_l).fadeIn(1000, function () {
                $(this).addClass('ativa')
            });
        }
    });

    $('.detalhe_prod_img_gr_xl').click(function () {
        $('.overlay_img').fadeIn(1000);
    })

    $('.close_img').click(function () {
        $('.overlay_img').fadeOut(1000);
    })

    $('.overlay_prox').click(function () {
        var n_imgs = $('.overlay_images').children().length;
        var n_cur = $('.overlay_images .ativa').index();
        $('.overlay_img .inner').fadeOut(1000, function () {
            var img_ativa = $('.overlay_images .ativa');
            $(img_ativa).removeClass('ativa');
            if (n_cur + 1 < n_imgs) {
                $(img_ativa).next().addClass('ativa');
            } else {
                $('.img_dest:first-child').addClass('ativa');
            }
        }).fadeIn(1000);
    })

    $('.overlay_prev').click(function () {
        var n_imgs = $('.overlay_images').children().length;
        var n_cur = $('.overlay_images .ativa').index();
        $('.overlay_img .inner').fadeOut(1000, function () {
            var img_ativa = $('.overlay_images .ativa');
            $(img_ativa).removeClass('ativa');
            if (n_cur > 0) {
                $(img_ativa).prev().addClass('ativa');
            } else {
                $('.img_dest:last-child').addClass('ativa');
            }
        }).fadeIn(1000);
    });

    /* MENU HORIZONTAL */

    $('.menu li').hover(function () {
        $(this).children('.level_0').addClass('active');
        $(this).children('.level_1').show(200);
    }, function () {
        $(this).children('.level_0').removeClass('active');
        $(this).children('.level_1').hide(200);
    })


    $('.menu .level_2').parent('li').hover(function () {
        $(this).children('.level_2').show(200);
    }, function () {
        $(this).children('.level_2').hide(200);
    })

    /* HOME PAGE 3 COLUNAS SLIDESHHOW */
    var tam_img_hp = 213;
    var img_mar_r = 15;
    var mg_left = 0;
    var num_slides = $('.dest_3cols_slide .dest_slide').length;
    var slide_width = num_slides * (tam_img_hp + img_mar_r);
    $('.dest_3cols_slide .dest_3cols').css('width', slide_width);

    $('.dest_3cols_slide .slide_left').hide();

    $('.dest_3cols_slide .slide_right').click(function () {
        $('.dest_3cols_slide .slide_left').show();
        if (slide_width + mg_left - (3 * (tam_img_hp + img_mar_r)) > 0) {
            $('.dest_3cols_slide .dest_3cols').animate({
                marginLeft: '-=' + (tam_img_hp + img_mar_r)
            }, 1000)
        }
        mg_left -= (tam_img_hp + img_mar_r);
        if (slide_width + mg_left - (3 * (tam_img_hp + img_mar_r)) <= 0) {
            $(this).hide()
        }
        clearTimeout(slide_timer);
        slide_timer = setTimeout(slide_auto, 5000);
    })

    $('.dest_3cols_slide .slide_left').click(function () {
        $('.dest_3cols_slide .slide_right').show();
        if (mg_left < 0) {
            $('.dest_3cols_slide .dest_3cols').animate({
                marginLeft: '+=' + (tam_img_hp + img_mar_r)
            }, 1000)
        }
        mg_left += (tam_img_hp + img_mar_r);
        if (mg_left == 0) {
            $(this).hide()
        }
        clearTimeout(slide_timer);
        slide_timer = setTimeout(slide_auto, 5000);
    })

    function slide_auto() {
        if (slide_width + mg_left - (3 * (tam_img_hp + img_mar_r)) <= 0) {
            $('.dest_3cols_slide .dest_3cols').animate({
                marginLeft: 0
            }, 1000);
            mg_left = 0;
            $('.dest_3cols_slide .slide_right').show();
        } else if (slide_width + mg_left - (3 * (tam_img_hp + img_mar_r)) > 0) {
            $('.dest_3cols_slide .dest_3cols').animate({
                marginLeft: '-=' + (tam_img_hp + img_mar_r)
            }, 1000);
            mg_left -= (tam_img_hp + img_mar_r);
            $('.dest_3cols_slide .slide_left').show();
        }
        if (slide_width + mg_left - (3 * (tam_img_hp + img_mar_r)) <= 0) {
            $('.dest_3cols_slide .slide_right').hide();
        }
        if (mg_left == 0) {
            $('.dest_3cols_slide .slide_left').hide();
        }
        clearTimeout(slide_timer);
        slide_timer = setTimeout(slide_auto, 5000);
    }

    var slide_timer = setTimeout(slide_auto, 5000);

    /* END HOME PAGE 3 COLUNAS SLIDESHHOW */
});


/* AGENDA DESCRICAO */

function matchtheday() {
    var day_active = $(".agenda_menu ul li.active a").attr('href').substr(1);
    var new_agenda = [];
    switch (day_active) {
        case "13":
            new_agenda[0] = ["14:30", '<span class="txt_bold">Apresentação aos Professores dos</span><br>Programas Educativos para o ano 2011/2012', "apresentacao_programas_educativos.html"];
            new_agenda[1] = ["19:00", '<span class="txt_bold">Concerto Aberto</span><br>com André Carvalho 5teto', "concerto_alberto.html"];
            break;
        case "21":
            new_agenda[0] = ["15:00", "Dia 21 às 15:00", "link.html"];
            new_agenda[1] = ["17:00", "Dia 21 às 17:00", "link.html"];
            new_agenda[2] = ["20:00", "Dia 21 às 20:00", "link.html"];
            break;
        case "24":
            new_agenda[0] = ["15:00", "Dia 24 às 15:00", "link.html"];
            break;
        case "29":
            new_agenda[0] = ["15:00", "Dia 29 às 15:00", "link.html"];
            new_agenda[1] = ["20:00", "Dia 29 às 20:00", "link.html"];
            break;
        case "9":
            new_agenda[0] = ["15:00", "Dia 9 às 15:00", "link.html"];
            new_agenda[1] = ["17:00", "Dia 9 às 17:00", "link.html"];
            new_agenda[2] = ["20:00", "Dia 9 às 20:00", "link.html"];
            new_agenda[3] = ["20:00", "Dia 9 às 20:00", "link.html"];
            break;
    }
    var tabela = $('<table>');
    for (i = 0; i < new_agenda.length; i++) {
        var linha = $('<tr>');
        $('<td>', {
            "class": "tb_col_1",
            text: new_agenda[i][0]
        }).appendTo(linha);
        $('<td>').html("<a href='" + new_agenda[i][2] + "'>" + new_agenda[i][1] + "</a>").appendTo(linha);
        linha.appendTo(tabela);
    }
    $(".agenda table").replaceWith(tabela);
}



/* END AGENDA DESCRICAO */
