
// keep info about hoverable things
var info_for = {
    wine1: {
        show: ['desc1', 'pict_w1', 'bot1']
    },
    wine2: {
        show: ['desc2', 'pict_w2', 'bot2']
    },
    wine3: {
        show: ['desc3', 'pict_w3', 'bot3']
    },
    
    head1: {
        show: ['text1', 'pict1']
    },
    head2: {
        show: ['text2', 'pict2']
    },
    head3: {
        show: ['text3', 'pict3'],
        hide: ['pict0', 'pict2']
    }
};

var image_for = {
    '#b1': '100Huegel_Silvaner_vorne.jpg', 
    '#b2': '100Huegel_WeisserBurgunder_vorne.jpg',
    '#b3': '100Huegel_Riesling_vorne.jpg'
};


$(function() {
    // mark all hoverable things as closed and register a click handler
    $.each(info_for, function(name, info) {
        $('#' + name)
            .data('is_open', false)
            .click(function(event) {
                //event.preventDefault();

                var is_open = !$(this).data('is_open');
                $(this).data('is_open', is_open);

                update_blocks();
            });
    });
    
    //
    // bottle hover logic
    //
    $('#wine1, #wine2, #wine3').hover(
        function() { 
            if (!$(this).data('is_open')) {
                $(this.id.replace(/wine/,'#bot')).fadeIn();
            }
        },
        function() { 
            if (!$(this).data('is_open')) {
                $(this.id.replace(/wine/,'#bot')).fadeOut();
            }
        }
    );
    
    //
    // bottle zoom logic
    //
    $('a.zoom').click(function() {
        var image = image_for[ $(this).attr('href') ];
        
        $('#dialog').dialog({
            autoOpen: true,
            modal: true,
            width: 260,
            height: 720
            // buttons: {
            //     "Schließen": function() { 
            //         $(this).dialog("close"); 
            //     },
            // }
        }).html('<img src="images/' + image + '" />');
    });

    //
    // close buttons
    //
    $('a.close').click(function(event) {
        event.preventDefault();
        var element = $(this).attr('href');
        $(element).data('is_open', false);
        
        update_blocks();
    });

    //
    // update visible things
    //
    function update_blocks() {
        var show = [];
        var hide = [];
        $.each(info_for, function(name, info) {
            var is_open = $('#' + name).data('is_open');
            for (s in info.show || []) {
                if ($.inArray(info.show[s], show) == -1) {
                    (is_open ? show : hide).push(info.show[s]);
                }
            }
            for (h in info.hide || []) {
                if ($.inArray(info.hide[h], hide) == -1) {
                    (is_open ? hide : show).push(info.hide[h]);
                }
            }
        });
        
        // remove to-hide things from show list
        show = $.grep(show, function(x) { return $.inArray(x, hide) == -1 });
        
        // show and hide things...
        $.each(show, function(index, name) {
            $('#' + name)
                .not(':visible')
                .fadeIn();
        });
        $.each(hide, function(index, name) {
            $('#' + name + ':visible')
                .fadeOut();
        });
    }
});

