﻿//$(function () {
//    $(".slideshow-campaign").each(function () {
//        slideshow($(this));
//    });
//});

var slideshow = function (slideshow, autoSlide, scrollTime) {
    var pub = {};
    var priv = {};
    pub.slideshow = slideshow;
    priv.scrolltime = scrollTime;
    priv.slideSpeed = 300;
    priv.isRunning = autoSlide;
    priv.activeItem = null;
    priv.activeThumb = null;
    priv.totalVisibleThumbs = 4;
    priv.thumbsIsRunning = priv.isRunning;
    priv.thumbHolder = pub.slideshow.find(".miniscreen .images .holder");
    priv.noneActiveThumbOpacity = 0.3;

    priv.isHovering = false;
    pub.slideshow.find(".bigscreen").mouseover(function () {
        priv.isHovering = true;
    }).mouseout(function () {
        priv.isHovering = false;
    });


    priv.showNextItem = function () {
        if (priv.isHovering === true) {
            return;
        }

        if (priv.isRunning == false) return;
        priv.activeItem = priv.activeItem.next();
        if (priv.activeItem.length == 0) priv.activeItem = priv.getFirstItem();

        priv.showItem(priv.activeItem);
    };
    priv.showItem = function (item) {
        priv.activeItem = item;
        priv.MoveThumbsIfNessasarry(function () {
            priv.fadeOutAllThumbsAndFadeIn(priv.getCorrespondingThumb(priv.activeItem));

            var itemIndex = priv.activeItem.index();
            var itemWidth = priv.activeItem.outerWidth(true);
            priv.activeItem.parent().animate({
                marginLeft: "-" + itemWidth * itemIndex + "px"
            }, priv.slideSpeed);
        });
    };
    priv.getFirstItem = function () {
        return pub.slideshow.find(".bigscreen li:first");
    };
    priv.getCorrespondingThumb = function (item) {
        return pub.slideshow.find(".miniscreen a[customId='" + priv.getId(item) + "']");
    };
    priv.getCorrespondingItem = function (thumb) {
        return pub.slideshow.find(".bigscreen li[customId='" + priv.getId(thumb) + "']");
    };
    priv.getId = function (item) {
        return item.attr("customId");
    };
    priv.fadeOutAllThumbsAndFadeIn = function (thumb) {
        pub.slideshow.find(".miniscreen .images a").not(priv.getCorrespondingThumb(priv.activeItem)).fadeTo(priv.slideSpeed, priv.noneActiveThumbOpacity)
        thumb.fadeTo(priv.slideSpeed, 1)
    };
    priv.MoveThumbsIfNessasarry = function (funcToRunWhenFinished) {
        var runFunc = true;
        if (priv.thumbsIsRunning) {
            if (priv.totalVisibleThumbs < priv.thumbHolder.find("a").length) {
                runFunc = false;
                priv.activeThumb = priv.getCorrespondingThumb(priv.activeItem);
                priv.focusOnThumb(priv.activeThumb, funcToRunWhenFinished);
            }
        }

        if (runFunc) funcToRunWhenFinished();
    };
    priv.focusOnThumb = function (thumb, funcToRunWhenFinished) {
        priv.activeThumb = thumb;
        if (priv.totalVisibleThumbs < priv.thumbHolder.find("a").length) {
            var widthPerThumb = thumb.outerWidth(true);

            if (thumb.index() == 0) {
                runFunc = false;
                priv.thumbHolder.animate({ marginLeft: "0px" }, priv.slideSpeed);
            }
            else if (priv.totalVisibleThumbs * widthPerThumb <= widthPerThumb * thumb.index()) {
                runFunc = false;
                priv.thumbHolder.animate({ marginLeft: "-=" + widthPerThumb + "px" }, priv.slideSpeed);
            }
        }

        funcToRunWhenFinished();
    };
    priv.moveThumbs = function (left) {
        priv.thumbsIsRunning = false;
        var thumbs = priv.thumbHolder.find("a");
        var widthPerThumb = thumbs.first().outerWidth(true);
        var maxMargin = (((thumbs.length * widthPerThumb) - priv.thumbHolder.parent().width()) * -1);

        if (left) {

            if (priv.activeThumbMargin() < maxMargin) return;
            priv.thumbHolder.animate({ marginLeft: "-=" + widthPerThumb + "px" }, priv.slideSpeed, function () {
                if (priv.activeThumbMargin() < maxMargin) {
                    priv.thumbHolder.animate({ marginLeft: maxMargin + "px" }, priv.slideSpeed);
                }
            });
        }
        else {
            if (priv.activeThumbMargin() > 0) return;
            priv.thumbHolder.animate({ marginLeft: "+=" + widthPerThumb + "px" }, priv.slideSpeed, function () {
                if (priv.activeThumbMargin() > 0) {
                    priv.thumbHolder.animate({ marginLeft: "0px" }, priv.slideSpeed / 2);
                }
            });
        }

    };
    priv.activeThumbMargin = function () {
        return priv.thumbHolder.css("margin-left").replace("px", "");
    };
    priv.thumbClick = function (thumb) {
        priv.isRunning = false;
        priv.showItem(priv.getCorrespondingItem(thumb));
    };

    priv.init = function () {
        priv.activeItem = priv.getFirstItem();
        priv.fadeOutAllThumbsAndFadeIn(priv.getCorrespondingThumb(priv.activeItem));

        $(".miniscreen .move a").live("click", function (e) {
            priv.moveThumbs($(this).parent(".left").length == 0);
            e.preventDefault();
        });
        $(".miniscreen .images a").live("click", function (e) {
            priv.thumbClick($(this));
            e.preventDefault();
        });
        setInterval(function () { priv.showNextItem(); }, priv.scrolltime);
    } ();


    return pub;
};




