﻿var levelChangers = [];

levelChangers.DestroyAll = function()
{
    for(var l in levelChangers)
    {
        if(levelChangers[l] instanceof levelChanger)
        {
            levelChangers[l].Destroy(l);
        }
    }
}

levelChangers.ReleaseCapture = function()
{
    for(var l in levelChangers)
    {
        if(levelChangers[l] instanceof levelChanger)
        {
            var ch = levelChangers[l];
            if(ch.isMouseCaptured)
            {
                ch.levelObject.ReleaseMouseCapture();
                ch.isMouseCaptured = false;
                if(ch.endFunction instanceof Function)
                {
                    ch.endFunction(null);
                }
            }
        }
    }
}

var levelChanger = function(levelObject, changingFunction, changingEndFunction, flagSetFunction)
{
    this.levelObject = levelObject;
    this.changingFunction = changingFunction;
    this.tokens = {mlbu: null, mlbd: null, mm: null};
    this.endFunction = changingEndFunction;
    this.flagSet = flagSetFunction;
    this.isMouseCaptured = false;
    
    this.tokens.mlbd = this.levelObject.addEventListener("MouseLeftButtonDown", this.MouseButtonDown);
    this.tokens.mlbu = this.levelObject.addEventListener("MouseLeftButtonUp", this.MouseButtonUp);
    this.tokens.mm = this.levelObject.addEventListener("MouseMove", this.MouseMove);
}

levelChanger.prototype = 
{
    Init: function(name)
    {
        return levelChangers[name];
    },
    
    MouseButtonDown: function(sender, eventArgs)
    {
        that = levelChanger.prototype.Init(sender.Name);
        if(that.flagSet instanceof Function)
        {
            that.flagSet(true);
        }
        
        var offset = eventArgs.getPosition(sender).x;
        
        that.changingFunction(offset / sender.Width);
        that.isMouseCaptured = true;
        sender.CaptureMouse();        
    },
    
    MouseButtonUp: function(sender, eventArgs)
    {
        that = levelChanger.prototype.Init(sender.Name);
        
        that.isMouseCaptured = false;
        sender.ReleaseMouseCapture();
        if(that.endFunction instanceof Function)
        {
            var position = eventArgs.getPosition(sender).x;
            var min = 0;
            var max = sender.Width;
            if(position < min) position = min;
            if(position > max) position = max;
            that.endFunction(position / max);
        }
        if(that.flagSet instanceof Function)
        {
            that.flagSet(false);
        }
    },
    
    MouseMove: function(sender, eventArgs)
    {
        that = levelChanger.prototype.Init(sender.Name);
        
        if(that.isMouseCaptured)
        {
            var position = eventArgs.getPosition(sender).x
                        
            var min = 0;
            var max = sender.Width;
            if(position < min) position = min;
            if(position > max) position = max;
            
            that.changingFunction(position / sender.Width);
        }
    },
    
    Destroy: function(name)
    {
        that = levelChangers[name];
        that.levelObject.removeEventListener("MouseLeftButtonDown", that.tokens.mlbd);
        that.levelObject.removeEventListener("MouseLeftButtonUp", that.tokens.mlbu);
        that.levelObject.removeEventListener("MouseMove", that.tokens.mm);
        delete levelChangers[name];
    }
}
