﻿var Slider =
{
    images: [],
    idMapping: new Hashtable(),

    currentStep: 1,
    lastSavedStep: 0,
    handleMouseDownCoordinates: null,
    handleMouseDownOffsetLeft: 0,
    handleMouseDown: false,
    divMouseDown: false,
    only2Steps: false,

    confirmChangeUrl: "/",

    token: null,
    serverRequest: new ServerRequest("GET", this.dummy, this.dummy),

    disableFilterConfirmed: false,
    disableFilterConfirmText: "Are you sure you want to see the picture?",

    addImage: function(toggleableEroticImage) {
        this.images.push(toggleableEroticImage);
        this.idMapping.set(toggleableEroticImage.id, toggleableEroticImage)
    },

    removeImage: function(id) {
        for (var i = 0; i < this.images.length; i++) {
            if (this.images[i].id == id) {
                this.images.splice(i, 1);
            }
        }

        this.idMapping.remove(id);
    },

    getImageByID: function(id) {
        return this.idMapping.get(id)
    },

    htmlElements:
	{
	    div: null,
	    handle: null
	},

    windowOnLoad: function() {
        S.Debugger.writeLine(this.currentStep);
        this.lastSavedStep = this.currentStep;

        // Get elements.		
        this.htmlElements.div = S.Get("Slider");
        this.htmlElements.handel = S.Get("SliderHandle");

        if (this.htmlElements.div == null || this.htmlElements.handel == null)
            return;

        S.Event.add(document, "mouseup", this.onDocumentMouseUp.bindAsEventListener(this));
        S.Event.add(this.htmlElements.handel, "mousedown", this.onHandleMouseDown.bindAsEventListener(this));
        S.Event.add(this.htmlElements.div, "mousedown", this.onDivMouseDown.bindAsEventListener(this));
        S.Event.add(document, "mousemove", this.onDocumentMouseMove.bindAsEventListener(this));
    },

    onDocumentMouseUp: function(e) {
        if (this.handleMouseDown || this.divMouseDown) {
            this.snapHandle();

            //if (this.lastSavedStep != this.currentStep)
            this.save();
        }

        this.handleMouseDown = false;
        this.divMouseDown = false;
    },

    save: function() {
        /*S.Debugger.writeLine(this.lastSavedStep + " - " + this.currentStep);
        S.Debugger.writeLine((this.lastSavedStep != this.currentStep && this.currentStep == 3));
        S.Debugger.writeLine((this.lastSavedStep == 3 && this.currentStep != 3));*/

        // Remove or add profiles.
        if ((this.lastSavedStep != this.currentStep && this.currentStep == 3) || (this.lastSavedStep == 3 && this.currentStep != 3)) {
            window.location.href = this.confirmChangeUrl.replace("##RequestedStep##", this.currentStep);
            return;
        }
        // there is no step 1 when erotic content is disabled
        if (this.only2Steps && this.currentStep == 1) {
            this.currentStep = 2;
        }
        this.lastSavedStep = this.currentStep;

        // Update configuration.
        this.serverRequest.send("/Global/Profiles/UpdateSliderStep.aspx", "Value=" + this.currentStep + "&Token=" + this.token);

        if (this.currentStep == 1)
            this.showEroticImages();
        else //if (this.currentStep == 2)
            this.hideEroticImages();
    },

    showEroticImages: function() {
        this.handleImages();
    },

    hideEroticImages: function() {
        this.handleImages();
    },

    handleImages: function() {
        this.images.each(function(toggleableEroticImage) {
            toggleableEroticImage.setSource(this.currentStep != 1);
        } .bind(this));
    },

    handleImageWithConfirm: function(e) {
        var ev = S.Event.wrap(e);

        if (this.currentStep == 2 && !this.disableFilterConfirmed) {
            if (confirm(this.disableFilterConfirmText)) {
                this.disableFilterConfirmed = true;
                this.currentStep = 1;
                this.htmlElements.handel.style.left = "6px";
                this.save();
            }

            ev.preventDefault();
            return false;
        }

        return true;
    },

    snapHandle: function() {
        var currentLeft = (S.Element.getPosition(this.htmlElements.handel).x - S.Element.getPosition(this.htmlElements.div).x - 6);

        if (this.only2Steps) {
            // there is no step 1 when erotic content is disabled
            // Step 2.
            if (currentLeft <= 48) {
                this.currentStep = 2;
                this.htmlElements.handel.style.left = "6px";
            }

            // Step 3.
            else {
                this.currentStep = 3;
                this.htmlElements.handel.style.left = "60px";
            }
        }
        else {
            // Step 1.
            if (currentLeft <= 21) {
                this.currentStep = 1;
                this.htmlElements.handel.style.left = "6px";
            }

            // Step 2.
            else if (currentLeft <= 48) {
                this.currentStep = 2;
                this.htmlElements.handel.style.left = "33px";
            }

            // Step 3.
            else {
                this.currentStep = 3;
                this.htmlElements.handel.style.left = "60px";
            }
        }
    },

    onDivMouseDown: function(e) {
        if (this.handleMouseDown)
            return;

        this.divMouseDown = true;
        var ev = S.Event.wrap(e);
        var divPositionLeft = S.Element.getPosition(this.htmlElements.div).x + 6;
        var offset = ev.mouseCoordinates.x - divPositionLeft;

        if (this.only2Steps) {
            // there is no step 1 when erotic content is disabled
            // Step 2.
            if (offset <= 48) {
                this.currentStep = 2;
                this.htmlElements.handel.style.left = "6px";
            }

            // Step 3.
            else {
                this.currentStep = 3;
                this.htmlElements.handel.style.left = "60px";
            }
        }
        else {
            // Step 1.
            if (offset <= 21) {
                this.currentStep = 1;
                this.htmlElements.handel.style.left = "6px";
            }

            // Step 2.
            else if (offset <= 48) {
                this.currentStep = 2;
                this.htmlElements.handel.style.left = "33px";
            }

            // Step 3.
            else {
                this.currentStep = 3;
                this.htmlElements.handel.style.left = "60px";
            }
        }
    },

    onHandleMouseDown: function(e) {
        var ev = S.Event.wrap(e);
        this.handleMouseDownCoordinates = ev.mouseCoordinates;
        this.handleMouseDownOffsetLeft = S.Element.getPosition(this.htmlElements.handel).x;
        this.handleMouseDown = true;
        ev.preventDefault();
    },

    onDocumentMouseMove: function(e) {
        if (!this.handleMouseDown)
            return;

        var ev = S.Event.wrap(e);

        var divPositionLeft = S.Element.getPosition(this.htmlElements.div).x + 6;
        var difference = ev.mouseCoordinates.x - this.handleMouseDownCoordinates.x;
        var newOffsetLeft = this.handleMouseDownOffsetLeft + difference;
        var newLeft = newOffsetLeft - divPositionLeft;

        if (newLeft < 0)
            this.htmlElements.handel.style.left = "0px"
        else if (newLeft > 66)
            this.htmlElements.handel.style.left = "66px"
        else
            this.htmlElements.handel.style.left = newLeft + "px";
    }
};

S.Event.add(window, "load", Slider.windowOnLoad.bind(Slider));


