$(document).ready(function () {
    $("#topo ul#menu li a.main").click(function (event) {
        $(".sub").slideUp(150);

        if ($(this).parent().find(".sub").is(":visible")) {
            $(this).parent().find(".sub").slideUp(150);
        } else {
            $(this).parent().find(".sub").slideDown(200);
        }

        return false;
    });

    $("div#novidades ul").cycle({
        prev: ":parent div.navegacao a.anterior",
        next: ":parent div.navegacao a.proxima",
        pause: true,
        //nowrap: true,
        speed: 650,
        timeout: 4000,
        before: function (curr, next, o, forward) {
            var index = o.nextSlide;

            $(":parent div.navegacao a.anterior").removeClass("disabled");
            $(":parent div.navegacao a.proxima").removeClass("disabled");

            //if (index == 0 || (curr == next)) $(":parent div.navegacao a.anterior").addClass("disabled");
            //if (index == o.slideCount - 1) $(":parent div.navegacao a.proxima").addClass("disabled");

            if (curr != next) {
                $("div *", next).hide();

                var titulo = $("div strong", next);
                var texto = $("div p", next);
                var botao = $("div a", next);

                botao.fadeIn(50, "easeInCirc", function () {
                    movimentarMargens(titulo, "marginTop", -50, 420, function () {
                        movimentarMargens(texto, "marginBottom", 20, 520, null);
                    });
                });
            }
        }
    });

    $("div#destaque ul").cycle({
        pager: ":parent :parent ul.navegacao",
        pause: true,
        activePagerClass: "selected",
        speed: 500,
        timeout: 7000,
        pagerAnchorBuilder: function (i, slide) {
            return '<li' + (i == 0 ? ' class="first"' : '') + '><a href="#item-' + i + '">' + $("img", slide).attr("alt") + '</a></li>';
        },
        updateActivePagerLink: function (pager, index) {
            $("li", pager).each(function () {
                $("a", this).removeClass("selected");
            });
            $("li:eq(" + index + ")", pager).find("a").addClass("selected");
        },
        before: function (curr, next, o, forward) {
            $("span, strong, p, a, img", next).hide();

            $(document).oneTime(300, function () {
                $("img", next).fadeIn(800, "easeOutSine");

                $(document).oneTime(100, function () {
                    movimentarMargens($("span", next), "marginTop", -5, 230);
                    $(document).oneTime(190, function () {
                        movimentarMargens($("strong", next), "marginLeft", -80, 360, function () {
                            $(document).oneTime(10, function () {
                                movimentarMargens($("p", next), "marginBottom", 120, 500);

                                $(document).oneTime(180, function () {
                                    animarBotao(0, next);
                                });
                            });
                        });
                    });
                });
            });
        }
    });
});

function animarBotao(i, slide) {
    var botao = $("a:eq(" + i + ")", slide);
    if (botao.length > 0) {
        var margem = botao.css("marginLeft").toString().replace("px", "");
        movimentarMargens(botao, "marginLeft", parseInt(margem) - 50, 300, function () {
            animarBotao(i + 1, slide);
        });
    } else {
        return;
    }
}

function movimentarMargens(elemento, margem, distancia, velocidade, callback) {
    elemento.each(function () {
        var valorFinal = $(this).css(margem);

        if (jQuery.support.opacity) {
            $(this).css(margem, distancia + "px").css("opacity", 0);
            var parametros = jQuery.parseJSON('{ "' + margem + '": "' + valorFinal + '", "opacity": "1" }');
        } else {
            $(this).css(margem, distancia + "px");
            var parametros = jQuery.parseJSON('{ "' + margem + '": "' + valorFinal + '" }');
        }

        $(this).show().animate(parametros, velocidade, "easeOutSine", callback);
    });
}

function navigate(target, combo, restore) {
    if (combo.options[combo.selectedIndex].value != "#")
        eval(target + ".location = '" + combo.options[combo.selectedIndex].value + "'");
    if (restore) combo.selectedIndex = 0;
}

function abrirJanela(path, name, w, h) {
    var left = (screen.width) ? (screen.width - w) / 2 : 0;
    var top = (screen.height) ? (screen.height - h) / 2 : 0;
    var settings = "height=" + h + ",width=" + w + ",top=" + top + ",left=" + left + ",scrollbars=" + scroll + ",resizable";
    var win = window.open(path, name, settings);
}
