﻿function slideGallery(containerID, idleIncrementTime, activeColor, backgroundColor) {
    this.containerID = containerID;
    this.idleIncrementTime = idleIncrementTime;
    this.sidebarSlideIncrement = 2;
    this.sidebarSlideInterval = 30;
    this.mainSlideIncrement = 60;
    this.mainSlideSlow = 5;
    this.mainSlideInterval = 20;
    this.activeColor = activeColor;
    this.backgroundColor = backgroundColor;
}

slideGallery.prototype.setSelectedItem = function(item) {
    jQuery("#" + this.containerID + ".galleryContainer .dp33 ul li").attr("class", "notselected").css("background-color", this.backgroundColor);
    jQuery(item).attr("class", "selected").css("background-color", this.activeColor);
};

slideGallery.prototype.idleRotateItem = function(item) {
    this.setSelectedItem(item);
    var list = jQuery("#" + this.containerID + ".galleryContainer .dp33 ul")[0];
    var currentPosition = list.offsetTop + item.offsetTop;
    if (currentPosition >= this.listHeight) {
        this.unsetSidebarItemButtons();
        jQuery(list).css("top", (item.offsetTop * -1) + "px");
    }
    else if (currentPosition < 0) {
        this.unsetSidebarItemButtons();
        jQuery(list).css("top", "0px");
    }
    this.slideToMainItem(null, item.id);
    var gallery = this;
    setTimeout(function() { gallery.setSidebarItemButtons(); }, 5);
};

slideGallery.prototype.startIdleTimeout = function() {
    var gallery = this;
    if (this.idleIncrementTime > 0) {
        this.idleTimeout = setTimeout(function() {
            var listItems = jQuery("#" + gallery.containerID + ".galleryContainer .dp33 ul li");
            for (var i = 0; i < listItems.length; i++) {
                if (jQuery(listItems[i]).attr("class") == "selected") {
                    var newSelectedIndex = 0;
                    if (i + 1 < listItems.length) {
                        newSelectedIndex = i + 1;
                    }
                    gallery.idleRotateItem(listItems[newSelectedIndex]);
                    break;
                }
            }
        }, this.idleIncrementTime);
    }
};

slideGallery.prototype.stopIdleTimeout = function() {
    clearTimeout(this.idleTimeout);
    delete (this.idleTimeout);
};

slideGallery.prototype.sidebarStop = function(startIdle) {
    clearInterval(this.sidebarInterval);
    if (startIdle) {
        this.startIdleTimeout();
    }
};

slideGallery.prototype.sidebarDownStart = function() {
    this.stopIdleTimeout();
    var gallery = this;
    var list = jQuery("#" + gallery.containerID + ".galleryContainer .dp33 ul")[0];
    this.sidebarInterval = setInterval(function() {
        var listBottom = list.offsetHeight + list.offsetTop;
        if (listBottom > gallery.listHeight) {
            jQuery(list).css("top", (list.offsetTop - gallery.sidebarSlideIncrement) + "px");
        }
        else {
            gallery.sidebarStop(false);
        }
    }, gallery.sidebarSlideInterval);
};

slideGallery.prototype.sidebarUpStart = function() {
    this.stopIdleTimeout();
    var gallery = this;
    var list = jQuery("#" + gallery.containerID + ".galleryContainer .dp33 ul")[0];
    this.sidebarInterval = setInterval(function() {
        var listTop = list.offsetTop;
        if (listTop < 0) {
            jQuery(list).css("top", (list.offsetTop + gallery.sidebarSlideIncrement) + "px");
        }
        else {
            gallery.sidebarStop(false);
        }
    }, gallery.sidebarSlideInterval);
};

slideGallery.prototype.stopMainSlide = function(startIdle) {
    clearInterval(this.mainInterval);
    if (startIdle) {
        this.startIdleTimeout();
    }
};


slideGallery.prototype.slideToMainItem = function(e, thumbID) {
    this.stopIdleTimeout();
    if (!thumbID) {
        this.setSelectedItem(e.currentTarget);
        thumbID = e.currentTarget.id;
    }
    var gallery = this;
    var mainID = "mainScrollItem" + thumbID.substring(5);
    gallery.stopMainSlide();
    var item = jQuery("#" + gallery.containerID + ".galleryContainer .dp66 #" + mainID)[0];
    var mainScrollContainer = jQuery("#" + gallery.containerID + ".galleryContainer .dp66 .mainScrollContainer")[0];
    gallery.mainInterval = setInterval(function() {
        var currentPosition = mainScrollContainer.offsetLeft + item.offsetLeft;
        if (currentPosition > 0) {
            if (currentPosition > gallery.mainSlideIncrement) {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft - gallery.mainSlideIncrement) + "px");
            }
            else if (currentPosition > gallery.mainSlideSlow) {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft - gallery.mainSlideSlow) + "px");
            }
            else {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft - 1) + "px");
            }
        }
        else if (currentPosition < 0) {
            if (currentPosition < gallery.mainSlideIncrement * -1) {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft + gallery.mainSlideIncrement) + "px");
            }
            else if (currentPosition < gallery.mainSlideSlow * -1) {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft + gallery.mainSlideSlow) + "px");
            }
            else {
                jQuery(mainScrollContainer).css("left", (mainScrollContainer.offsetLeft + 1) + "px");
            }
        }
        else {
            gallery.stopMainSlide(true);
        }
    }, gallery.mainSlideInterval);
};


slideGallery.prototype.setSidebarScrollButtons = function() {
    var previousButton = jQuery("#" + this.containerID + ".galleryContainer .dp33 .previous")[0];
    var nextButton = jQuery("#" + this.containerID + ".galleryContainer .dp33 .next")[0];
    var gallery = this;
    jQuery(previousButton).hover(function(e) { gallery.sidebarUpStart(); }, function(e) { gallery.sidebarStop(true); });
    jQuery(nextButton).hover(function(e) { gallery.sidebarDownStart(); }, function(e) { gallery.sidebarStop(true); });
};

slideGallery.prototype.setSidebarItemButtons = function() {
    var sidebarItems = jQuery("#" + this.containerID + ".galleryContainer .dp33 .sidebarScrollContainer li");
    var gallery = this;
    for (var i = 0; i < sidebarItems.length; i++) {
        var sidebarItem = sidebarItems[i];
        jQuery(sidebarItem).hover(function(e) { gallery.slideToMainItem(e); }, null);
    }
};

slideGallery.prototype.unsetSidebarItemButtons = function() {
    var sidebarItems = jQuery("#" + this.containerID + ".galleryContainer .dp33 .sidebarScrollContainer li");
    for (var i = 0; i < sidebarItems.length; i++) {
        var sidebarItem = sidebarItems[i];
        jQuery(sidebarItem).unbind("mouseenter mouseleave");
    }
};

slideGallery.prototype.setMainScrollWidth = function() {
    var itemCount = jQuery("#" + this.containerID + ".galleryContainer .dp66 ul li .mainScrollItem").length;
    jQuery("#" + this.containerID + " .mainScrollContainer").css("width", ((itemCount * this.mainItemWidth) + 2) + "px");
    jQuery("#" + this.containerID + " .mainScrollContainer li").css("width", this.mainItemWidth + "px");
};


slideGallery.prototype.init = function() {
    this.sidebarTop = jQuery("#" + this.containerID + ".galleryContainer .dp33 .previous")[0].offsetHeight;
    this.sidebarBottom = jQuery("#" + this.containerID + ".galleryContainer .dp33 .next")[0].offsetTop;
    this.listHeight = jQuery("#" + this.containerID + ".galleryContainer .dp33 .sidebarScrollContainer")[0].offsetHeight;
    this.mainItemWidth = jQuery("#" + this.containerID + ".galleryContainer .dp66")[0].offsetWidth;
    this.setMainScrollWidth();
    this.setSidebarScrollButtons();
    this.setSidebarItemButtons();
    this.startIdleTimeout();
};