(function($) {

    $(document).ready(
        function() {

            $("div.order-file input[type='file']").each(
                function() {
                    var curInput      = $(this);
                    var curBlockInput = curInput.parent();
                    var curBlock      = curBlockInput.parent();
                    var curFileName   = curBlock.find("strong");

                    curBlock.bind("mouseover",
                        function(e) {
                            curBlockInput.css("left", (e.pageX - curBlock.offset().left - Math.round(curBlockInput.width() / 2)) + "px");
                        }
                    );

                    curBlockInput.bind("mousemove",
                        function(e) {
                            if ((e.pageX >= curBlock.offset().left) && (e.pageX <= curBlock.offset().left + curBlock.width())) {
                                curBlockInput.css("left", (e.pageX - curBlock.offset().left - Math.round(curBlockInput.width() / 2)) + "px");
                            } else {
                                curBlockInput.css("left", "-10000px");
                            }
                        }
                    );
                    curBlockInput.bind("mouseout",
                        function(e) {
                            curBlockInput.css("left", "-10000px");
                        }
                    );

                    curInput.bind("change",
                        function() {
                            newFileName = curInput.val();
                            reWin = /.*\\(.*)/;
                            var newFileName = newFileName.replace(reWin, "$1");
                            reUnix = /.*\/(.*)/;
                            newFileName = newFileName.replace(reUnix, "$1");
                            curFileName.html(newFileName);
                        }
                    );
                }
            );

            $("div.detail-big").data("curIndex", 0);

            $("a.detail-prev").click(
                function() {
                    var curBlock = $("div.detail-big");
                    var curPreview = $("div.detail-preview");
                    var curIndex = Number(curBlock.data("curIndex"));
                    curIndex--;
                    if (curIndex < 0) {
                        curIndex = curPreview.find("a").length - 1;
                    }
                    curBlock.data("curIndex", curIndex);
                    curBlock.find("img").attr("src", curPreview.find("a").eq(curIndex).attr("href"));

                    return false;
                }
            );

            $("a.detail-next").click(
                function() {
                    var curBlock = $("div.detail-big");
                    var curPreview = $("div.detail-preview");
                    var curIndex = Number(curBlock.data("curIndex"));
                    curIndex++;
                    if (curIndex == curPreview.find("a").length) {
                        curIndex = 0;
                    }
                    curBlock.data("curIndex", curIndex);
                    curBlock.find("img").attr("src", curPreview.find("a").eq(curIndex).attr("href"));

                    return false;
                }
            );

            $("div.detail-preview a").click(
                function() {
                    $("div.detail-big img").attr("src", $(this).attr("href"));

                    var curIndex = $("div.detail-preview a").index($(this));
                    $("div.detail-big").data("curIndex", curIndex);

                    return false;
                }
            );

        }
    );

})(jQuery);
