﻿var DynamicMedia = {};

function $(i) { return document.getElementById(i) }

DynamicMedia.slideshow = function(name, divId) {
    this.slidespeed = 10;
    this.name = name;
    this.divId = divId;
    this.SegmentSelected = false;
    this.currentSlideId = 1;
    this.SegmentTextColor = '#5b5b5b';
    this.SegmentSelectedTextColor = '#ffffff';
    this.SegmentSelectedBackground = 'url(images/SegmentHeaderSelected.jpg) #ffffff repeat-x left top';
    this.SegmentBackground = 'url(images/SegmentHeader.jpg) #ffffff repeat-x left top';
};

DynamicMedia.slideshow.prototype = {
    init: function(divId, segId, length, segmentLength, prevImageButton, nextImageButton) {
        this.length = parseInt(length);
        this.divId = divId;
        this.segId = segId;
        this.segmentLength = parseInt(segmentLength);
        if (prevImageButton && prevImageButton) {
            var pButton = $(prevImageButton);
            var nButton = $(nextImageButton);
            if (pButton) {
                pButton.onclick = new Function(this.name + '.mv(-1,1,0)');
            }
            if (nButton) {
                nButton.onclick = new Function(this.name + '.mv(1,1,0)')
            }
        }
        for (var i = 1; i <= this.segmentLength; i = i + 1) {
            var segmentSelector = $(this.segId + i);
            if (segmentSelector) {
                segmentSelector.onclick = new Function(this.name + '.mv(1,1,' + (this.length - this.segmentLength + i) + ')')
            }
        }
        this.auto ? this.is(0, 0) : this.is(0, 1)
    },
    is: function(s, c) {
        this.mv(s, c);
        if (!c) {
            this.at = setInterval(new Function(this.name + '.mv(1,0,0)'), this.slidespeed * 1000)
        }

    },
    mv: function(d, c, s) {

        if (c) {
            clearTimeout(this.at)
        }

        var si = s;

        if (!s) {
            si = this.currentSlideId + d;

            if (this.SegmentSelected) {
                if (d > 0) {
                    if (si > this.length) {
                        si = 1 + (this.length - this.segmentLength);
                    }
                }
                else {
                    if (si <= (this.length - this.segmentLength)) {
                        si = this.length;
                    }
                }

            }
            else {
                if (d > 0) {
                    if (si > this.length) {
                        si = 1;
                    }
                }
                else {
                    if (si < 1) {
                        si = this.length;
                    }
                }
            }
        }
        else {
            this.SegmentSelected = true;
            si = s;
        }

        if (si != this.currentSlideId) {
            if (si > (this.length - this.segmentLength)) {
                var segSelceted = $(this.segId + (si - (this.length - this.segmentLength)));
                if (segSelceted) {
                    segSelceted.style.background = this.SegmentSelectedBackground;
                    segSelceted.style.color = this.SegmentSelectedTextColor; ;
                }
            }

            if (this.currentSlideId > (this.length - this.segmentLength)) {
                var prevSegSelceted = $(this.segId + (this.currentSlideId - (this.length - this.segmentLength)));
                if (prevSegSelceted) {
                    prevSegSelceted.style.background = this.SegmentBackground;
                    prevSegSelceted.style.color = this.SegmentTextColor; ;
                }
            }

            var cuurentslide = $(this.divId + si)

            var previousslide = $(this.divId + this.currentSlideId);
            if (cuurentslide) {
                if (previousslide) {
                    previousslide.style.display = "none";
                }
                cuurentslide.style.display = "block";
            }
            this.currentSlideId = si;
        }
    }

}