(function ($) {
    $().ready(function () {
        // Find the vertical content box and place it!
        var menubox = $("#menubox:visible");
        var contentbox = $("#contentbox_v");

        // Temporary fix.
        /* --------------------------------------- */
        var rightpanebox = $("#rightpanecontent");

        if (menubox.length && rightpanebox.length) {
            if (!contentbox.length)
                contentbox = rightpanebox;
            else
                rightpanebox.hide();
        }
        /* --------------------------------------- */

        // If the menu and vertical content
        // box is found, then we have to move it.
        if (menubox.length && contentbox.length) {
            // Insert it after the menu box.
            contentbox.insertAfter(menubox);

            // Show it.
            contentbox.show();
        }

        // Internet Explorer do not support round corners in css - therefore we need to create the corners with images.
        // Internet Explorer version 6 can not even handle the images correctly, so only add the corners to version 7 and above.
        if ($.browser.msie && $.browser.version > 6) {
            // Find any button and style it!
            $("input[type=button],input[type=submit]").each(function () {
                // Ulrik 2010. Skip all cases where the button is marked as a linkbutton
                // style (wiki)
                if ($(this).hasClass("buttonlink")) return;
                InsertCorners(this, "button_roundc_ie");
            });

            // Find input type text or password and with class roundc and style it.
            $("input[type=text][class*=roundc],input[type=password][class*=roundc]").each(function () { InsertCorners(this, "inputtext_roundc_ie"); });

            // Find hyperlinks with class containing link_button.
            $("a[class*=link_button]:not([class*=link_button_green]):visible").each(function () { InsertCorners(this, "link_button_ie"); });

            // Find hyperlinks with class containing link_button_green.
            $("a[class*=link_button_green]:visible").each(function () { InsertCorners(this, "link_button_green_ie"); });
        }

        // Preload any images in css.
        $.preloadCssImages();
    });

    // Create array of available margins.
    var arrMargins = new Array("margin-top", "margin-left", "margin-right", "margin-bottom");

    // Copy margins from one element to another.
    function CopyStyleMargin(eSource, eDesc) {
        // Loop each available margin.
        for (cM in arrMargins) {
            // Copy the margin value.
            eDesc.css(arrMargins[cM], eSource.css(arrMargins[cM]));

            // Reset the source margin value.
            eSource.css(arrMargins[cM], "0px");
        }
    }

    function InsertCorners(eElement, sElementClass) {
        // Get the current input and add class.
        var eCurrent = $(eElement);
        eCurrent.addClass(sElementClass);

        // Save the size before manipulation.
        var currentWidth = eCurrent.width();

        // Create a new parent span and add a style class.
        var spanContainer = $("<span>");
        spanContainer.addClass(sElementClass + "_box");

        // Insert the div before the element.
        spanContainer.insertBefore(eCurrent);

        // Move the element inside the parent span.
        eCurrent.prependTo(spanContainer);

        // Create a new span for the left image.
        var spanLeft = $("<span>");

        // Add the class.
        spanLeft.addClass(sElementClass + "_left");

        // Insert before the element.
        spanLeft.insertBefore(eCurrent);

        // Create a new span for the right image.
        var spanRight = $("<span>");

        // Add the class.
        spanRight.addClass(sElementClass + "_right");

        // Insert after the element.
        spanRight.insertAfter(eCurrent);

        // If the type is text or password, then we need to make
        // sure, that there are enough space for the corner images.
        if (eCurrent.is(":text") || eCurrent.is(":password")) {
            // Calculate the span width and the input height.
            var spanWidth = (spanLeft.width() + spanRight.width());
            var inputHeight = (spanLeft.height() - (eCurrent.innerHeight() - eCurrent.height()));

            // Remove any left and right padding and set the input height to match the span height.
            eCurrent.attr("style", "padding-left:0px!important;padding-right:0px!important;height:" + inputHeight + "px!important;");

            // Make space for the round corners.
            eCurrent.width(currentWidth - spanWidth);
        }

        // If the type is button, submit or hyperlink, then make the corners are click-able.
        if (eCurrent.is(":button") || eCurrent.is(":submit") || eCurrent.is("a[onclick]")) {
            spanLeft.click(function () { if (eCurrent.is(":enabled")) eCurrent.click(); });
            spanRight.click(function () { if (eCurrent.is(":enabled")) eCurrent.click(); });
        }

        // If the typs is hyperlink, then make sure
        // that the corners redirect to the href link.
        if (eCurrent.is("a[href]")) {
            var sLink = eCurrent.attr("href");
            spanLeft.click(function () { if (eCurrent.is(":enabled")) document.location.href = sLink; });
            spanRight.click(function () { if (eCurrent.is(":enabled")) document.location.href = sLink; });
        }

        // Copy the margin style.
        CopyStyleMargin(eCurrent, spanContainer);
    }

    // Function for preloading images.
    jQuery.PreloadImages = function () {
        for (var i = 0; i < arguments.length; i++) { jQuery("<img>").attr("src", arguments[i]); }
    }
})(jQuery);
