jQuery(function() {
    $("div.leftMenuLink:has(div)").find("a:first").append(" &raquo; ");
    $("div.leftSubmenuLink:has(div)").find("a:first").append(" &raquo; ");
    $("div.leftSubmenuLink2:has(div)").find("a:first").append(" &raquo; ");

    // Menu toggle for the header container links
    jQuery("#headerMenu .menuLink.containerLink").click(function() {
        var nextElement = jQuery(this).next();

        if (jQuery(nextElement).hasClass('menuContainer')) {
            jQuery(nextElement).slideToggle();
        } else {
            if (jQuery(nextElement).next().hasClass('menuContainer')) {
                jQuery(nextElement).next().slideToggle();
            }
        }

        return false;
    });

    // Menu toggle for the left container links
    jQuery("#leftMenu .menuLink.containerLink").find('a:first').click(function() {
        var container = jQuery(this).parent().find('.menuContainer:first');
        jQuery(container).slideToggle();

        return false;
    });

    jQuery('img', '#innerContent').click(function() {
        if (!jQuery(this).hasClass('skipImageDialog')) {
            showImage(this);
        }
    });
});

function showImage(element) {
    $("#imageDialog").html('Ladevorgang...');
    $("#imageDialog").dialog( {
        title : 'Bildansicht',
        modal : true
    });

    var imagePathSmall = jQuery(element).attr('src');
    var imagePathBig = imagePathSmall.replace(/small/i, "big");

    var dialogImage = new Image();

    dialogImage.onload = function() {
        $("#imageDialog").remove();

        $("body").prepend("<div id='imageDialog'></div>");
        $("#imageDialog").html(this);

        //Set the correct dialog width
        dialogWidth = this.width + 15 + 'px';
        if(this.width >= screen.width) {
            dialogWidth = '90%';
        }

        $("#imageDialog").dialog( {
            title : 'Bildansicht',
            modal : true,
            width : dialogWidth,
            close : function(event, ui) {
                $("#imageDialog").remove();
                $("body").prepend("<div id='imageDialog'></div>");
            }
        });
    };

    dialogImage.src = imagePathBig;
}
