
var UI = {

    InitEvents: function () {
        $("a.print").click(function () {
            UI.ShowPrintDialog(this.href);
            return false;
        });
        $("#top-links li.nodeType7714 > a").click(function (e) {
            e.preventDefault();
            UI.ShowSearchBox(this);
        });
        $("ul#top-links .search-box input.text").focus(function () {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
            }
        });
        $("ul#top-links .search-box input.text").blur(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("title"));
            }
        });
        $("ul#top-links .search-box a.cancel").click(function () {
            UI.HideSearchBox(this);
        });
        $("ul#top-links .search-box .btn").click(function (e) {
            e.preventDefault();
            UI.SubmitSearch();
        });
        $('ul#top-links .search-box input.text').keypress(function (e) {
            c = e.which ? e.which : e.keyCode;
            if (c == 13) {
                e.preventDefault();
                UI.SubmitSearch();
            }
        });

    },

    ShowPrintDialog: function (url) {
        var options = "width=800, height=500, scrollbars=1";
        var win = window.open(url, "printWin", options);
        win.print();
    },

    ShowSearchBox: function (sender) {
        $(sender).closest("li").addClass("opened");
        $("ul#top-links .search-box").show();
    },

    HideSearchBox: function (sender) {
        $(sender).closest("li").removeClass("opened");
        $("ul#top-links .search-box").hide();
    },

    SubmitSearch: function () {
        var query = $("ul#top-links .search-box input.text").val();
        location.href = "/top-menu/search?q=" + query;
    }
}

// ---------------------------------------------------

var Frontpage = {
    InitEvents: function () {
        $('.cycle-container').cycle({
            fx: 'fade',
            speed: 1000,
            timeout: 10000,
            random: 0
        });
        $('.box-collection').cycle({
            fx: 'scrollHorz',
            speed: 1000,
            random: 0,
            timeout: 10000
        });

        function timeoutFn(currElement, nextElement, opts, isForward) {
            var minVal = 5000;
            var maxVal = 10000;
            var num = Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal;
            return num;
        }

    }
}

    // ---------------------------------------------------
    // ---------------------------------------------------

var Brochures = {

    InitEvents: function () {
        $(".order.btn").click(function () {
            var brochureTitle = $(this).siblings("input.brochureTitle").val();
            var brochureNodeId = $(this).siblings("input.brochureNodeId").val();

            if (!$("#order-list li.brochure" + brochureNodeId).length) {
                var li = $("<li class='brochure" + brochureNodeId + "'><div class='title'>" + brochureTitle + "</div><span class='amount'><input type='text' value='1' width=1 maxlength=2 />stk.</span> <a href='#' class='remove-brochure' title='Fjern denne brochure fra listen'>[x]</a><br style='clear: both;' /></li>");
                $("#order-list").append(li);
                Brochures.CheckBrochureList();
            }
        });

        $(".order-button").click(function () {
            $(this).hide();
            $(".order-form").show();
            $("textarea").val("");
        });

        $(".remove-brochure").live("click", function () {
            $(this).parent("li").remove();
            Brochures.CheckBrochureList();
        });

        $("form").submit(function (e) {
            var brochures = "";
            $.each($("#order-list li").not(".dummy"), function (i, li) {
                brochures += $(".title", li).text() + ": " + $("input", li).val() + " stk." + "\n";
            });
            $("input#brochures").val(brochures);
            $("#order-list li").not(".dummy").remove();            
        });
    },

    // ---------------------------------------------------

    CheckBrochureList: function () {
        if ($("#order-list li").not(".dummy").length) {
            $(".order-button").show();
        } else {
            $(".order-button").hide();
        }
        $(".order-form").hide();
    }
}

    // ---------------------------------------------------
    // ---------------------------------------------------

    var FAQ = {

        InitEvents: function () {
            $("a.question").click(function () {
                $("#faq-list li").removeClass("open");
                $(this).closest("li").toggleClass("open");
            });
        }
    }

    // ---------------------------------------------------
    // ---------------------------------------------------

    var Drawings = {

        InitEvents: function () {
            $("a.image-link").fancybox({ titleShow: false });
        }

    }

    // ---------------------------------------------------
    // ---------------------------------------------------

    var Contact = {

        InitEvents: function () {
            $("a.add-product").click(function (e) {
                e.preventDefault();
                Contact.AddProduct(this);
            });
        },

        AddProduct: function (sender) {
            var nextDiv = $("div.more-products:hidden:first");
            nextDiv.slideDown();
            if ($("div.more-products:hidden").length == 0)
                $(sender).hide();
        }

    }

    // ---------------------------------------------------
    // ---------------------------------------------------


$("document").ready(function () {

//    DeviceDetection.Init();
    UI.InitEvents();
    Frontpage.InitEvents();
    Brochures.InitEvents();
    FAQ.InitEvents();
    Drawings.InitEvents();
    Contact.InitEvents();

});
