﻿$.fn.extend({ xSlideShow: function (obj) {
    var currentslide = 1;
    var storyBoardIndex = 1;
    var currentStoryBoard = "";
    var previousWrapped = false;
    var that = $(this);
    var storyMax;

    $("#story_next").click(function () {
        if (storyBoardIndex < storyMax) {
            storyBoardIndex++;
            $("#slide-image").detach("load");
            $("#slide-image").load(function () { $(this).fadeIn(250); });
            $("#slide-image").fadeOut(220, function () { $(this).attr("src", "/Content/Images/" + currentStoryBoard + "_" + storyBoardIndex + ".png"); });
        }
    });
    $("#story_previous").click(function () {
        if (storyBoardIndex > 1) {
            storyBoardIndex--;
            $("#slide-image").detach("load");
            $("#slide-image").load(function () { $(this).fadeIn(250); });
            $("#slide-image").fadeOut(220, function () { $(this).attr("src", "/Content/Images/" + currentStoryBoard + "_" + storyBoardIndex + ".png"); });
        }
    });


    if (obj.filter != null || obj.filter != "") {
        document.title = "MertArt | Cartoon Illustration and Animation | Portfolio ( " + obj.filter + " )";
        var slides = [];
        for (var i = 0; i < obj.slides.length; i++) {
            if (obj.slides[i][0].toLowerCase().indexOf(obj.filter) >= 0) {
                slides.push(obj.slides[i]);
            }
        }
    } else {
        var slides = obj.slides;
    }
    for (var i = 0; i < slides.length; i++) {
        Preload_Image = new Image();
        Preload_Image.src = obj.imagePath + slides[i][0];
        Preload_Image2 = new Image();
        Preload_Image2.src = obj.imagePath + slides[i][1];
    }
    var width = obj.width;
    var height = obj.height;
    $(that).css({ marginLeft: "auto", marginRight: "auto", textAlign: "center", overflow: "hidden", width: width, height: height });
    $(that).append("<div style='width:100%;height:100%;overflow:hidden;position:relative;top:0px;' id='container' ><img id='selectorbox' src='/Content/Images/selector_box.gif' style='display:none;position:absolute;cursor:pointer;z-index:5000;' /><img id='selectorbox2' src='/Content/Images/selector_box.gif' style='display:none;position:absolute;cursor:pointer;z-index:5000;' /><img id='slide-image' style='position:absolute;' /></div>");
    $("#slide-image").css({ maxHeight: height, maxWidth: width });
    $("#info").load(function () { $(this).fadeIn(500); });
    var length = slides.length;
    function gotoSlide(index, reverse, back) {
        $("#slide-image").css("cursor", "");
        storyBoardIndex = 1;
        $("#slide-image").unbind("click");
        var source = slides[currentslide - 1][0];
        var info_source = slides[currentslide - 1][1];
        $("#story_next").hide();
        $("#story_previous").hide();
        if (source.indexOf("goto:") < 0 && source.indexOf("story:") < 0) {
            $("#obstructed").hide();
            $("#slide-image").unbind("hover");

            if (slides[currentslide - 1][6] != null) {
                var split = slides[currentslide - 1][6].split(',');
                $("#selectorbox").show();
                $("#selectorbox").css("width", split[0]);
                $("#selectorbox").css("height", split[1]);
                $("#selectorbox").css("top", split[2]);
                $("#selectorbox").css("left", split[3]);
                $("#selectorbox").unbind("click");
                $("#selectorbox").click(function () {
                    window.open(split[4]);
                });
            } else {
                $("#selectorbox").hide();
            }
            if (slides[currentslide - 1][7] != null) {
                var split = slides[currentslide - 1][7].split(',');
                $("#selectorbox2").show();
                $("#selectorbox2").css("width", split[0]);
                $("#selectorbox2").css("height", split[1]);
                $("#selectorbox2").css("top", split[2]);
                $("#selectorbox2").css("left", split[3]);
                $("#selectorbox2").unbind("click");
                $("#selectorbox2").click(function () {
                    window.open(split[4]);
                });
            }
            else {
                $("#selectorbox2").hide();
            }

            if (slides[currentslide - 1][2] != null) {
                $("#slide-image").hover(function () { $("#slide-image").attr("src", obj.imagePath + slides[currentslide - 1][2]); }, function () { $("#slide-image").attr("src", obj.imagePath + slides[currentslide - 1][0]); });
            }

            $("#slide-image").unbind("load").load(function () {
                if (slides[currentslide - 1][4] != null) {
                    $("#slide-image").css({ top: slides[currentslide - 1][4] });
                } else {
                    $("#slide-image").css({ top: "0px" });
                }
                if (slides[currentslide - 1][5] != null) {
                    $(this).animate({ left: ((parseInt(width) - $(this).width()) / 2) + parseInt(slides[currentslide - 1][5]) + "px" }, 300, "swing", function () { $(obj.description).attr("src", obj.imagePath + info_source); });
                } else {
                    $(this).animate({ left: ((parseInt(width) - $(this).width()) / 2) + "px" }, 300, "swing", function () { $(obj.description).attr("src", obj.imagePath + info_source); });
                }
            });
            $(obj.description).fadeOut(500);

            if (!reverse) {
                $("#slide-image").animate({ left: width }, 400, "swing", function () { $(this).css("left", "-" + width).attr("src", obj.imagePath + source); });
            } else {
                $("#slide-image").animate({ left: "-" + width }, 400, "swing", function () { $(this).css("left", width).attr("src", obj.imagePath + source); });
            }


        } else if (source.indexOf("story:") > -1) {
            $("#obstructed").hide();
            $("#story_next").show(300);
            $("#story_previous").show(300);
            $(obj.description).attr("src", obj.imagePath + info_source);
            $(obj.description).fadeIn(0);
            $("#slide-image").unbind("load").load(function () {
                if (slides[currentslide - 1][4] != null) {
                    $("#slide-image").css({ top: slides[currentslide - 1][4] });
                } else {
                    $("#slide-image").css({ top: "0px" });
                }
                if (slides[currentslide - 1][5] != null) {
                    $(this).animate({ left: ((parseInt(width) - $(this).width()) / 2) + parseInt(slides[currentslide - 1][5]) + "px" }, 300, "swing", function () { $(obj.description).attr("src", obj.imagePath + info_source); });
                } else {
                    $(this).animate({ left: ((parseInt(width) - $(this).width()) / 2) + "px" }, 300, "swing", function () { $(obj.description).attr("src", obj.imagePath + info_source); });
                }
            });
            var spl = source.split(':')[1];
            currentStoryBoard = spl;
            if (currentStoryBoard == "story1") {
                $("#obstructed").show();
            }
            storyMax = source.split(':')[2];
            var source = "/Content/Images/" + spl + "_1.png";

            $("#slide-image").attr("src", source);

        } else {
            var href = source.split(':');
            if (!back) {
                location.href = href[1];
            } else {
                location.href = href[1] + "?fromstory=fromstory";
            }
        }

        if (slides[currentslide - 1][3] != null && slides[currentslide - 1][3] != "") {
            $("#slide-image").css("cursor", "pointer");
            $("#slide-image").click(function () {
                window.open(slides[currentslide - 1][3]);
            });
        }
    }


    $(obj.next).click(function (e) {
        if (currentslide < length) {
            currentslide++;
            gotoSlide(currentslide, true, false);
            e.stopPropagation();

        } else {
            currentslide = 1;
            gotoSlide(currentslide, true, false);
            e.stopPropagation();
        }
    });
    $(obj.previous).click(function (e) {
        if (currentslide > 1) {
            currentslide--;
            gotoSlide(currentslide, false, true);
            e.stopPropagation();
        }
    });
    if (obj.startIndex == null || obj.startIndex == undefined || obj.startIndex == "") {
        gotoSlide(1, false);
    } else {
        currentslide = obj.startIndex;
        gotoSlide(obj.startIndex, false);
    }
}




});
