// wait until the DOM is ready
$(document).ready(function() {

    // check if the browser understands getElementById, if not don't do anything
    if (document.getElementById) {

        // pre load image function
        jQuery.preloadImages = function() {
            for (var i = 0; i < arguments.length; i++) {
                jQuery("<img>").attr("src", arguments[i]);
            }
        }

        // image gallery swap
        // get the filename of the page (this will be used for the image folder)
        var url = document.URL.substring(document.URL.lastIndexOf('/') + 1, document.URL.lastIndexOf('.'));

        // pre load the images
        $.preloadImages("/images/products/" + url + "/one.jpg", "/images/products/" + url + "/two.jpg");

        // utilise jQuery, swap images on click
        $("a#image1").click(function() {
            $("#gallery").attr("src", "/images/products/" + url + "/one.jpg");
            return false;
        });

        $("a#image2").click(function() {
            $("#gallery").attr("src", "/images/products/" + url + "/two.jpg");
            return false;
        });

        // make product descriptions clickable
        $("#tradLink").click(function() {
            window.location.href = $("#tradLink").parent().children()[0];
        });
        $("#classicLink").click(function() {
            window.location.href = $("#classicLink").parent().children()[0];
        });
        $("#latexLink").click(function() {
            window.location.href = $("#latexLink").parent().children()[0];
        });
        $("#memoryLink").click(function() {
            window.location.href = $("#memoryLink").parent().children()[0];
        });
        $("#orthoLink").click(function() {
            window.location.href = $("#orthoLink").parent().children()[0];
        });
        $("#latexOrthoLink").click(function() {
            window.location.href = $("#latexOrthoLink").parent().children()[0];
        });

        $("#sancLink").click(function() {
            window.location.href = $("#sancLink").parent().children()[0];
        });
        $("#sancMemoryLink").click(function() {
            window.location.href = $("#sancMemoryLink").parent().children()[0];
        });
        $("#sancLatexLink").click(function() {
            window.location.href = $("#sancLatexLink").parent().children()[0];
        });
        $("#sancDeluxeLink").click(function() {
            window.location.href = $("#sancDeluxeLink").parent().children()[0];
        });
        $("#sancMainLink").click(function() {
            window.location.href = $("#sancMainLink").parent().children()[0];
        });

        $("#bedsteadLink").click(function() {
            window.location.href = "/beds-mattresses/bedstead/";
        });
        $("#restLink").click(function() {
            window.location.href = "/beds-mattresses/restmaster-adjustable-bed.html";
        });


        $("#bedsteadClassicLink").click(function() {
            window.location.href = "/beds-mattresses/bedstead/bedstead-classic.html";
        });
        $("#bedsteadOrthoLink").click(function() {
            window.location.href = "/beds-mattresses/bedstead/bedstead-ortho.html";
        });
        $("#bedsteadLatexLink").click(function() {
            window.location.href = "/beds-mattresses/bedstead/bedstead-latex.html";
        });
        $("#bedsteadMemoryLink").click(function() {
            window.location.href = "/beds-mattresses/bedstead/bedstead-memory.html";
        });



        // float text
        // calculate height, then add some margin bottom and top
        var masterWrapperHeight = $("#masterWrapper").height();

        // show the float text
        $("#floatTextWrapper").css("display", "block");
        // set the absolute position of the height
        $("#floatTextWrapper").css("top", masterWrapperHeight);

        // show the rest of the copy on click (toggle)
        $("#readMoreFloat").click(function() {
            $("#moreFloatText").slideToggle("slow");
            return false;
        });

    }


});