﻿function imageSlide(psSlideId, psAreaId, paImgSlides) {

    if (paImgSlides.length == 0) return;
    this.SlideId = psSlideId;
    this.SlideArea = document.getElementById(psAreaId);
    this.SlideAnchor = $("a.teaserlink", this.SlideArea);
    this.SlideImages = new Array();

    this.Slides = paImgSlides;
    this.SlideIdx = 0;
    this.SlideInterval = null;
    this.SlideIntervalDuration = 6000; // 6 sekunden
    this.SlideDirection = '+';

    this.buildControls = function() {
        this.ControlArea = this.SlideArea.appendChild(document.createElement('div'));
        this.ControlArea.style.position = 'absolute';
        this.ControlArea.style.bottom = '10px';
        this.ControlArea.style.right = '10px';
        this.ControlArea.style.display = 'block';
        this.ControlArea.style.border = '0px solid red';
        sImgPath = 'images';
        sInnerHTML = '<a href="#" onclick="' + this.SlideId + '.slideBackward();return false;" onfocus="this.blur();"><img src="' + sImgPath + '/slide_left.png" /></a>';
        sInnerHTML += '<a href="#" onclick="' + this.SlideId + '.slideStop();return false;" onfocus="this.blur();"><img src="' + sImgPath + '/slide_mid.png" /></a>';
        sInnerHTML += '<a href="#" onclick="' + this.SlideId + '.slideForward();return false;" onfocus="this.blur();"><img src="' + sImgPath + '/slide_right.png" /></a>';
        this.ControlArea.innerHTML = sInnerHTML;
    }

    this.slideInit = function() {
        for (var i = 0; i < this.Slides.length; i++) {
            var img = new Image();
            img.src = this.Slides[i].file;
            this.SlideImages.push(img);
        }
        $("input[type=hidden]", this.SlideArea).val(this.Slides[this.SlideIdx].id);
        this.SlideAnchor.html(this.SlideImages[this.SlideIdx]);
        this.slideForward();
    }

    this.slideForward = function() {
        this.slideStop();
        this.SlideDirection = '+';
        this.SlideInterval = setInterval(this.SlideId + '.doSlideStep()', this.SlideIntervalDuration);
    }

    this.slideBackward = function() {
        this.slideStop();
        this.SlideDirection = '-';
        this.SlideInterval = setInterval(this.SlideId + '.doSlideStep()', this.SlideIntervalDuration);
    }

    this.slideStop = function() {
        clearInterval(this.SlideInterval);
        this.SlideInterval = null;
    }

    this.doSlideStep = function() {
        if (this.SlideDirection == '+') {
            this.SlideIdx = this.SlideIdx + 1;

            if (this.SlideIdx > this.Slides.length - 1) {
                this.SlideIdx = 0;
            }
        }

        if (this.SlideDirection == '-') {
            this.SlideIdx = this.SlideIdx - 1;

            if (this.SlideIdx < 0) {
                this.SlideIdx = this.Slides.length - 1;
            }
        }
        
        $("input[type=hidden]", this.SlideArea).val(this.Slides[this.SlideIdx].id);
        this.SlideAnchor.html(this.SlideImages[this.SlideIdx]);
    }

    this.destroy = function() {
        this.slideStop();
        imageSlideWebsite = null;
    }

    this.buildControls();
    this.slideInit();
}

function doImageSlideWebsite(psAreaId, paImgSlides) {
    if (typeof (imageSlideWebsite) != 'undefined') {
        imageSlideWebsite.destroy();
    }

    imageSlideWebsite = new imageSlide('imageSlideWebsite', psAreaId, paImgSlides);
}
