﻿SlideBar = function() {

    this.lastMouseUp = undefined;

    this.sliderSetMouseMoved = false;
    this.sliderSetMouseUp = false;

    // register in the global collection	
    if (!window.CurrentSlide) window.CurrentSlide = null;
    window.CurrentSlide = this;

    // save config parameters in the slider object
    this.n_minValue = 0;
    this.n_maxValue = 0;
    this.n_value = 0;
    this.n_step = 1;

    this.b_watch = false;
    this.n_controlWidth = 300;
    this.n_controlHeight = 7;
    this.n_sliderWidth = 14;
    this.n_sliderHeight = 7;
    this.n_pathLeft = 0;
    this.n_pathTop = 0;
    this.n_pathLength = (300 - 14);
    this.n_zIndex = 1;

    this.Create = function() {
        this.n_pix2value = this.n_pathLength / (this.n_maxValue - this.n_minValue);

        var containerElement = document.getElementById('sliderparent');

        vml = document.createElement('div');
        vml.id = 'sl0base';
        vml.className = 'slider';
        vml.onclick = function() { return false; }
        vml.onmousedown = this.f_sliderControlDown;
        containerElement.appendChild(vml);

        vml.style.width = this.n_controlWidth + 'px';
        vml.style.height = this.n_controlHeight + 'px';
        vml.style.visibility = 'hidden';
        vml2 = document.createElement('div');
        vml2.className = 'sliderbutton';
        vml2.id = 'sl0slider';
        vml2.onclick = function() { return false; }
        vml2.onmousedown = this.f_sliderMouseDown

        vml.appendChild(vml2);

        vml2.style.width = this.n_sliderWidth + 'px';
        vml2.style.height = this.n_sliderHeight + 'px';
        vml2.style.border = '0';
        vml2.style.position = 'relative';
        vml2.style.left = this.n_pathLeft + 'px';
        vml2.style.top = this.n_pathTop + 'px';
        vml2.style.zIndex = this.n_zIndex;
        vml2.style.visibility = 'hidden';

        this.e_base = document.getElementById('sl0base');
        this.e_slider = document.getElementById('sl0slider');

        // safely hook document/window events
        if (!this.sliderSetMouseMoved) {
            window.f_savedMouseMove = document.onmousemove;
            document.onmousemove = this.f_sliderMouseMove;
            this.sliderSetMouseMoved = true;
        }
        if (!this.sliderSetMouseUp) {
            window.f_savedMouseUp = document.onmouseup;
            document.onmouseup = this.f_sliderMouseUp;
            this.sliderSetMouseUp = true;
        }

        // Initialwert übernehmen
        this.f_setValue(this.n_value, false, true /* non-user generated */);
    };
    this.Show = function() {
        this.e_base.style.visibility = 'visible';
        this.e_slider.style.visibility = 'visible';
    };
    this.f_scrollLeft = function() {
        return this.f_filterResults(
	        window.pageXOffset ? window.pageXOffset : 0,
	        document.documentElement ? document.documentElement.scrollLeft : 0,
	        document.body ? document.body.scrollLeft : 0
        );
    };
    this.f_scrollTop = function() {
        return this.f_filterResults(
	        window.pageYOffset ? window.pageYOffset : 0,
	        document.documentElement ? document.documentElement.scrollTop : 0,
	        document.body ? document.body.scrollTop : 0
        );
    };
    this.f_filterResults = function(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
            n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    };
    this.f_sliderMouseMove = function(evt) {
        if (!evt && window.event) evt = window.event;

        // check if in drag mode
        if (window.n_activeSliderId != null) {
            var o_slider = window.CurrentSlide;

            if (evt) {
                var MyMouseX = evt.clientX + window.CurrentSlide.f_scrollLeft();
                var MyMouseY = evt.clientY + window.CurrentSlide.f_scrollTop();
            }

            var n_pxOffset;
            var n_sliderLeft = MyMouseX - o_slider.n_sliderWidth / 2 - o_slider.f_getPos(1) - (o_slider.isIE() ? 3 : 0);
            // limit the slider movement
            if (n_sliderLeft < o_slider.n_pathLeft) n_sliderLeft = o_slider.n_pathLeft;
            var n_pxMax = o_slider.n_pathLeft + o_slider.n_pathLength;
            if (n_sliderLeft > n_pxMax) n_sliderLeft = n_pxMax;
            o_slider.e_slider.style.left = n_sliderLeft + 'px';
            n_pxOffset = n_sliderLeft - o_slider.n_pathLeft;

            var nvalue = Math.round((o_slider.n_minValue + ((n_sliderLeft - o_slider.n_pathLeft)) / o_slider.n_pix2value));

            if (window.CurrentSlideFlow) {
                var Nb_Mov = (nvalue - window.CurrentSlideFlow.ImgS);
                if (Nb_Mov != 0) window.CurrentSlideFlow.MoveTo(Nb_Mov);
            }

            if (o_slider.b_watch) o_slider.f_sliderMouseUp(evt, 1);

            return false;
        }

        /*if (window.f_savedMouseMove) return window.f_savedMouseMove(evt);*/
    };
    this.f_sliderMouseUp = function(e_event, b_watching) {
        if (window.n_activeSliderId != null) {
            var o_slider = window.CurrentSlide;
            o_slider.f_setValue(o_slider.n_minValue + ((parseInt(o_slider.e_slider.style.left) - o_slider.n_pathLeft)) / o_slider.n_pix2value);
            o_slider.lastMouseUp = new Date();

            if (b_watching) return;
            window.n_activeSliderId = null;
        }
        /*if (window.f_savedMouseUp) return window.f_savedMouseUp(e_event);*/
    };
    this.f_setValue = function(n_value, b_noInputCheck, b_nonUser, b_Cover) {
        // Nicht aktualisieren, wenn User gerade damit arbeitet
        if (b_nonUser && window.n_activeSliderId != null) return;
        if (n_value == null) n_value = this.n_value == null ? this.n_minValue : this.n_value;
        if (isNaN(n_value)) return false;
        // round to closest multiple if step is specified
        if (this.n_step) n_value = Math.round((n_value - this.n_minValue) / this.n_step) * this.n_step + this.n_minValue;
        // smooth out the result
        if (n_value % 1) n_value = Math.round(n_value * 1e5) / 1e5;

        if (n_value < this.n_minValue) n_value = this.n_minValue;
        if (n_value > this.n_maxValue) n_value = this.n_maxValue;

        this.n_value = n_value;

        // move the slider
        this.e_slider.style.left = (this.n_pathLeft + Math.round((n_value - this.n_minValue) * this.n_pix2value)) + 'px';

        if (!b_nonUser && this.onSetValue != undefined) this.onSetValue(this.n_value);

        // move the coverflow
        if (window.CurrentSlideFlow && !b_Cover) {
            var Nb_Mov = (this.n_value - window.CurrentSlideFlow.ImgS);
            if (Nb_Mov != 0) window.CurrentSlideFlow.MoveTo(Nb_Mov);
        }
    };
    this.f_getPos = function(b_base) {
        var n_pos = 0, s_coord = ('Left');
        var o_elem = o_elem2 = b_base ? this.e_base : this.e_slider;

        while (o_elem) {
            n_pos += o_elem["offset" + s_coord];
            o_elem = o_elem.offsetParent;
        }
        o_elem = o_elem2;

        var n_offset;
        while (o_elem.tagName != "BODY") {
            n_offset = o_elem["scroll" + s_coord];
            if (n_offset)
                n_pos -= o_elem["scroll" + s_coord];
            o_elem = o_elem.parentNode;
        }
        return n_pos;
    };
    this.f_sliderMouseDown = function() {
        window.n_activeSliderId = 0;
        return false;
    };
    this.isIE = function() {
        return (-1 != navigator.userAgent.indexOf("MSIE"));
    };
    this.f_sliderControlDown = function(evt) {
        if (!evt) evt = window.event;

        var o_slider = window.CurrentSlide;
        window.n_mouseX = evt.clientX + o_slider.f_scrollLeft();
        window.n_mouseY = evt.clientY + o_slider.f_scrollTop();

        /* horizontal slider */
        var n_sliderLeft = window.n_mouseX - o_slider.n_sliderWidth / 2 - o_slider.f_getPos(1) - (o_slider.isIE() ? 3 : 0);
        // limit the slider movement
        if (n_sliderLeft < o_slider.n_pathLeft) n_sliderLeft = o_slider.n_pathLeft;
        var n_pxMax = o_slider.n_pathLeft + o_slider.n_pathLength;
        if (n_sliderLeft > n_pxMax) n_sliderLeft = n_pxMax;
        o_slider.e_slider.style.left = n_sliderLeft + 'px';
        n_pxOffset = n_sliderLeft - o_slider.n_pathLeft;

        o_slider.f_setValue(o_slider.n_minValue + ((n_sliderLeft - o_slider.n_pathLeft)) / o_slider.n_pix2value);

        return o_slider.f_sliderMouseDown;
    };
};

