(function($) {
    $.fn.extend({
        toyPanel: function() {
            //Create our overlay object
            var overlay = $("<div id='modal-overlay'></div>");
            //Create our modal window
            //var modalWindow = $("<div id='modal-window'></div>");
            //var loader = $("<div id='loader'></loader>");
            var modalWindow = $("#modal-window");
            var loader = $("<div id='loader'></loader>");
            //modalWindow.append(loader); //loader for image
            return this.each(function() {
                //Listen for clicks on objects passed to the plugin
                $(this).click(function(e) {

                    if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
                        $("body", "html").css({ height: "100%", width: "100%" });
                        $("html").css("overflow", "hidden");
                    }

                    //Append the overlay to the document body
                    $("body").append(overlay);
                    $("#closeModal").click(function() { modalHide(); });
                    $("#prevModalImg").click(function() { nextModalImg(-1); });
                    $("#nextModalImg").click(function() { nextModalImg(1); });
                    //Add a loader to our page
                    $("body").append("<div id='modal-load'></div>");

                    //Set the css and fade in our overlay
                    overlay.css("opacity", 0.8);
                    overlay.fadeIn(150);

                    //Prevent the anchor link from loading
                    e.preventDefault();

                    //Activate a listener
                    //$(document).keydown(handleEscape);
                    //$("body").append(modalWindow);
                    modalWindow.fadeIn(500);
                    showImage();
                    /*
                    //Load the image
                    var img = new Image();
                    //var img = $("#modaltempl div img[@id=modalImg]");
                    $(img).load(function() {
                    var imageWidth = img.width / 2;
                    var imageHeight = img.height / 2;
                    modalWindow.css({
                    "margin-left": -imageWidth,
                    "margin-top": -imageHeight
                    });
                    $("#modal-load").remove();
                    $("#modalImg").append(img);
                    $(this).addClass("modal-image");
                    $("body").append(modalWindow);
                    modalWindow.fadeIn(150);
                    })
                    .attr({ src: getCurImage() }); //.click(function() { modalHide(); });
                    */
                });
            });
            function nextModalImg(inc) {
                $("body").append("<div id='modal-load'></div>");
                if (modal_Index + inc < modal_Images.length && modal_Index + inc > -1)
                    modal_Index += inc;
                showImage();
            }
            function showImage() {
                RedrawButtons();
                $("#loader img").fadeOut("normal").remove();
                var img = new Image();
                $(img).load(function() {
                    $(this).hide();
                    //$("#loader").append(this).removeClass("loading");
                    $("#modal-load").remove();

                    $(this).fadeIn("slow");
                });
                $("#loader").append(img);
                $("#loader img").hide();
                if (modal_Images.length > 0) {
                    $(img).attr("src", modal_Images[modal_Index]);
                } else {
                    //no images
                    $(img).attr("src", "/images/spacer.gif");
                }
            }
            function RedrawButtons() {
                $("#nextModalImg").hide();
                $("#prevModalImg").hide();
                $("#curModalImg").hide();
                $("#editModalImg").empty();
                $("#isActModalImg").empty();

                if (modal_Index > 0) {
                    $("#prevModalImg").fadeIn("slow");
                }
                if (modal_Index < modal_Images.length - 1) {
                    $("#nextModalImg").fadeIn("slow");
                }
                img = new Image();
                $(img).attr("src", ppth + "udt/img/day.gif");
                $("#curModalImg").empty();
                $("#curModalImg").append(img);
                $("#curModalImg").append($(ImgsNumbers(0)));
                $("#curModalImg").fadeIn("slow");

                //ADMIN FEATURES
                if (modal_EditLinks.length > 0 && modal_EditLinks[modal_Index].length > 0) {
                    var editlink = $("<a class='CommandButton' href='" + modal_EditLinks[modal_Index] + "'></a>");
                    var img = new Image();
                    $(img).attr("src", "/images/edit.gif");
                    $(img).attr("border", "0");
                    if (modal_isActive[modal_Index]) {
                        $("#isActModalImg").append("<img src='/images/checked.gif' /><span class='Normal'> Active</span>");
                    } else {
                        $("#isActModalImg").append("<img src='/images/unchecked.gif' /> <span class='Normal'>Inactive</span>");
                    }
                    editlink.append(img);
                    editlink.append(" Edit");
                    $("#editModalImg").append(editlink);
                    $("#editModalImg").hide(); $("#editModalImg").fadeIn("slow");
                    $("#isActModalImg").hide(); $("#isActModalImg").fadeIn("slow");
                }
            }
            function ImgsNumbers(inc) {

                var res = '';
                if (modal_Days.length > 0) {
                    num = modal_Days[modal_Index + inc] + '';
                    for (i = 0; i < num.length; i++) {
                        res += '<img src="' + ppth + 'udt/img/numbers/' + num.substring(i, i + 1) + '.gif" />';
                    }
                } else {
                    res = '<span></span>';
                }
                return res;
            }
            //Our function for hiding the modalbox
            function modalHide() {
                //$(document).unbind("keydown", handleEscape)
                var remove = function() { $(this).hide(); };
                overlay.fadeOut(remove);
                modalWindow
					.fadeOut("slow");
                //.empty();
            }
        }
    });
})(jQuery);

function getCurImage() {
    return "img/tn1.jpg";
}