// Ibercruises
jQuery.ltIE = function (i) {
    if ($.browser.msie && $.browser.version < i) {
        return true;
    }
    return false;
};

$(document).ready(function () {

    // classe js
    $('body').addClass('js');

    // menu
    if ($('ul#menu').length > 0) {
        var menu = $('ul#menu');

        $('li:has(ul)', menu).each(function () {
            $('ul', this).hover(function () {
                $(this).prev().toggleClass('active');
                $(this).parent().toggleClass('active');
            }, function () {
                $(this).prev().toggleClass('active');
                $(this).parent().toggleClass('active');
            });
        });

        // explicitar largura (IE6-7)
        if ($.ltIE(8)) {
            $('ul', menu).each(function () {
                $(this).width($(this).width());
            });
        }

        // suporte hover (IE6)
        if ($.ltIE(7)) {
            $('li:has(ul)', menu).each(function () {
                $(this).hover(function () {
                    $(this).toggleClass('hover');
                }, function () {
                    $(this).toggleClass('hover');
                });
            });
        }
    }

    // Slideshow - baseado http://bit.ly/blu1ho
    if ($('#slideshow img').length > 0) {
        var img = new Image();
        var imgsrc = $('#slideshow img').attr('src');

        img.src = imgsrc.replace(1, 2);
        $(img).bind('load', function () {
            var stack = [];

            // preload images into an array
            for (var i = 3; i < 7; i++) {
                img = new Image();
                img.src = imgsrc.replace(1, i);
                $(img).bind('load', function () {
                    stack.unshift(this);
                });
            }

            // add images to slideshow 
            function onBefore(curr, next, opts) {
                if (opts.addSlide) {
                    while (stack.length) {
                        opts.addSlide(stack.pop());
                    }
                }
            }

            // start slideshow
            $(this).appendTo('#slideshow');

            $('#slideshow').cycle({
                timeout: 3000,
                before: onBefore
            });

        });
    }

});