﻿//==================================================
// slide object
//==================================================
function Slide(divId,leftDivId,rightDivId){
    this.Id = divId;
    this.left = leftDivId;
    this.right = rightDivId;
}

//==================================================
// slideshow object
//==================================================

function SlideShow(container, numBaseSlide, numSlideCount, numSlidesShowing, animationSeconds){
    return SlideShow(container,numBaseSlide, numSlideCount,numSlidesShowing,animationSeconds, "#ffffff");
}

function SlideShow(container, numBaseSlide, numSlideCount, numSlidesShowing, animationSeconds, bgColor){

    this.container = container;
    this.slideWidth = function(){
        return Math.round(parseInt(document.getElementById(this.container).style.width.slice(0, document.getElementById(this.container).style.width.indexOf('px'))) / this.numSlidesShowing);
     }
    this.numBaseSlide = numBaseSlide;
    this.numSlideCount = numSlideCount;
    this.numSlidesShowing = numSlidesShowing;
    this.timerLength = 5;
    this.AnimationLength = animationSeconds * 1000;
    this.featureSlide;
    this.startTime;
    this.timer;
    this.enabled;
    this.slides = new Array();
    this.bgColor = function(){
        if (bgColor == ""){
            return "#ffffff";
        } else {
            return bgColor;
        }
    }
    
    this.add_slide = function(slide){
        // Add a slide to the slideshow.
        // For example:
        // SLIDES1.add_slide(new Slide('divId','leftDivId','rightDivId'))
      
        var i = this.slides.length;
        this.slides[i] = slide;
    }
    
    this.update_slide = function(slide){
        // Update a slide's contents.
        // For example:
        // SLIDES1.update_slide(new Slide('divId','leftDivId','rightDivId'))
        var checkSlide;
        for (var i = 0; i < this.slides.length; i++){
            checkSlide = this.slides[i];
            if (checkSlide.Id == slide.Id){
                this.slides[i] = slide;
            }
        }
    }
    
    this.get_slide = function(slideId){
        var checkSlide;
        for (var i = 0; i < this.slides.length; i++){
            checkSlide = this.slides[i];
            if (checkSlide.Id == slideId){
                return this.slides[i];
            }
        }
    }
    
    this.ensure_slide_placement = function(direction){
        var ensureSlide = this.featureSlide;
        if (direction == 'left'){
            ensureSlide = this.get_slide(ensureSlide.right);
            document.getElementById(ensureSlide.Id).style.right = "-" + this.slideWidth() + "px";//(parseInt(document.getElementById(this.container).style.width.slice(0, document.getElementById(this.container).style.width.indexOf('px'))) 
        } else {
            for (var i = 0; i <= this.numSlidesShowing; i++){
                ensureSlide = this.get_slide(ensureSlide.left);
            }
            document.getElementById(ensureSlide.Id).style.right = (this.slideWidth() * this.numSlidesShowing) + 'px';
        }
    }
    
    this.move = function(direction){
        if (this.enabled == null){
            this.enabled = true;
        }
        if (this.enabled){
            this.enabled = false;
            if (this.featureSlide == null){
                this.featureSlide = this.slides[0];
            }
            this.ensure_slide_placement(direction);
            var farLeftSlide = this.featureSlide;
            if (direction == 'left'){
                for( var i=0; i< this.numSlidesShowing -1; i++){
                    farLeftSlide = this.get_slide(farLeftSlide.left);
                }                
            } else {
                for(var i=0; i<this.numSlidesShowing; i++){
                    farLeftSlide = this.get_slide(farLeftSlide.left);
                }    
            }
            this.startTime = (new Date()).getTime();
            
            var oInstance=this;
            this.timer = setInterval(function(){oInstance.move_slides(farLeftSlide.Id,direction)},this.timerLength);
        } else {
            return;
        }
    }

    this.move_slides = function(leftSlideId, direction) {
        var elapsed = (new Date()).getTime() - this.startTime;
        var movingSlides = new Array();
        var getSlide = this.get_slide(leftSlideId);
        // get the divs that will be moving
        for (var i = 0; i <= this.numSlidesShowing; i++) {
            movingSlides[i] = document.getElementById(getSlide.Id);
            getSlide = this.get_slide(getSlide.right);
        }

        // move the divs
        var startPosition = parseInt(document.getElementById(this.container).style.width.slice(0, document.getElementById(this.container).style.width.indexOf('px')));
        if (elapsed > this.AnimationLength) {
            // complete the movements
            for (div in movingSlides) {
                if (div.toString().search(/^[0-9]+$/) == 0) {
                    if (direction == 'right') {
                        movingSlides[div].style.right = (startPosition - this.slideWidth()) + "px";
                    } else {
                        movingSlides[div].style.right = startPosition + "px";
                    }
                    changeOpac(100, movingSlides[div].id, this.bgColor);
                    startPosition = startPosition - this.slideWidth();
                }
            }
            // reset featureSlide
            if (direction == 'right') {
                this.featureSlide = this.get_slide(this.featureSlide.left);
            } else {
                this.featureSlide = this.get_slide(this.featureSlide.right);
            }
            // clear and reset variables
            this.timer = window.clearInterval(this.timer);
            this.startTime = 0;
            this.enabled = true;
        } else {
            var d = Math.round(elapsed / this.AnimationLength * this.slideWidth());
            for (div in movingSlides) {
                if (div.toString().search(/^[0-9]+$/) == 0) {
                    if (direction == 'right') {
                        movingSlides[div].style.right = (startPosition - d) + 'px';
                        if (div == 0) {
                            changeOpac(((d / this.slideWidth()) * 100), movingSlides[div].id, this.bgColor);
                        }
                        if (div == this.numSlidesShowing) {
                            changeOpac(((100 - (d / this.slideWidth()) * 100)), movingSlides[div].id, this.bgColor);
                        }
                    } else {
                        movingSlides[div].style.right = (startPosition - this.slideWidth() + d) + 'px';
                        if (div == 0) {
                            changeOpac(((100 - (d / this.slideWidth()) * 100)), movingSlides[div].id, this.bgColor);
                        }
                        if (div == this.numSlidesShowing) {
                            changeOpac(((d / this.slideWidth()) * 100), movingSlides[div].id, this.bgColor);
                        }
                    }
                    startPosition = startPosition - this.slideWidth();
                }
            }
        }
        return;
    }
}

// reference for opacity http://brainerror.net/scripts/javascript/blendtrans/

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    changeOpac(opacity, id, "#ffffff");
} 

//change the opacity for different browsers 
function changeOpac(opacity, id, bgColor) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")";
	object.backgroundColor =  bgColor;
} 