$(function () {

    // меняем тип инпута для нативной поисковой строчки в Safari
    // и показываем пригласительное сообщение
    $("#search input.safari-search").each(function () {
        try {
            var ele = $(this),
                placeholder = ele.metadata().placeholder || "";
            if (navigator.userAgent.indexOf("WebKit/") > -1) {
                this.type = "search";
                this.setAttribute("results", 10);
                this.setAttribute("placeholder", placeholder);
                ele.siblings(".submit").addClass("hidden");
            } else {
                ele.bind("focus", function () {
                    if (ele.val() === placeholder) {
                        ele.removeClass("empty").val("");
                    }
                }).bind("blur", function () {
                    if (ele.val() === "") {
                        ele.addClass("empty").val(placeholder);
                    }
                }).trigger("focus").trigger("blur");
            }
        } catch (ex) {}
    });



    $("div.showroom").each(function () {
        var showroom = $(this);

        showroom.bind("slideShow", function (e, name) {
            showroom.find(".showroom-default")
                    .addClass("show")
                    .stop().animate({opacity: 0});
            showroom.find("." + name).stop().animate({right: 0});
        }).bind("slideHide", function (e, name) {
            if (showroom.find("li.show").length < 2) {
                showroom.find(".showroom-default").stop().animate({opacity: 1});
            }
            showroom.find("." + name)
                    .removeClass("show")
                    .stop().animate({right: -400});
        });

        function getItemName(cls) {
            var name = cls.match(/cat-\d/);
            return name && name.length ? name[0] : "";
        }

        $("#category li").hover(function () {
            $(this).addClass("hover");
            showroom.trigger("slideShow", getItemName(this.className))
        }, function () {
            $(this).removeClass("hover");
            showroom.trigger("slideHide", getItemName(this.className));
        });

    });

});
$(function () {

    // меняем тип инпута для нативной поисковой строчки в Safari
    // и показываем пригласительное сообщение
    $("#search input.safari-search").each(function () {
        try {
            var ele = $(this),
                placeholder = ele.metadata().placeholder || "";
            if (navigator.userAgent.indexOf("WebKit/") > -1) {
                this.type = "search";
                this.setAttribute("results", 10);
                this.setAttribute("placeholder", placeholder);
                ele.siblings(".submit").addClass("hidden");
            } else {
                ele.bind("focus", function () {
                    if (ele.val() === placeholder) {
                        ele.removeClass("empty").val("");
                    }
                }).bind("blur", function () {
                    if (ele.val() === "") {
                        ele.addClass("empty").val(placeholder);
                    }
                }).trigger("focus").trigger("blur");
            }
        } catch (ex) {}
    });



    $("div.showroom").each(function () {
        var showroom = $(this);

        showroom.bind("slideShow", function (e, name) {
            showroom.find(".showroom-default")
                    .addClass("show")
                    .stop().animate({opacity: 0});
            showroom.find("." + name).stop().animate({right: 0});
        }).bind("slideHide", function (e, name) {
            if (showroom.find("li.show").length < 2) {
                showroom.find(".showroom-default").stop().animate({opacity: 1});
            }
            showroom.find("." + name)
                    .removeClass("show")
                    .stop().animate({right: -400});
        });

        function getItemName(cls) {
            var name = cls.match(/cat-\d/);
            return name && name.length ? name[0] : "";
        }

        $("#category li").hover(function () {
            $(this).addClass("hover");
            showroom.trigger("slideShow", getItemName(this.className))
        }, function () {
            $(this).removeClass("hover");
            showroom.trigger("slideHide", getItemName(this.className));
        });

    });



    $("#dashboard div.block-board").each(function () {
        
        var nav = $("em.board-nav", this),
            items = $("li", this),
            scroll = $("ul", this).css("left", 0);
            
        nav.bind("mousedown", function () {
            var dir, pos, pWidth, width = 0;

            // block multiple triggers while animating
            if (scroll.filter(":animated").length > 0) {
                return;
            }

            dir = $(this).hasClass("nav-next") ? 1 : $(this).hasClass("nav-prev") ? -1 : 0;

            pWidth = scroll.parent().width();
            items.each(function () {
                width = width + $(this).width();
            });

            pos = dir - parseInt(scroll.css("left"), 10) / 5;

            if (pos + 2 > items.length) {
                pos = items.length - 2;
            }
            if (pos < 0) {
                pos = 0;
            }

            scroll.animate({"left": (-5 * pos) + "%"});

            /** show/hide buttons **/
            nav.eq(0)[pos > 0 ? "removeClass" : "addClass"]("hidden");
            nav.eq(1)[pos + 3 < items.length ? "removeClass" : "addClass"]("hidden");

        }).eq(0).triggerHandler("mousedown");
    });


});

