//FX-JavaScript library

//--------------------------------
//    ANIMATION
//--------------------------------
function fx_move(object,
                 _x,
                 _y)
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return;
        };

        _x      = Math.floor(_x);
        _y      = Math.floor(_y);
        visible = fx_visible(object);

        with (object.style)
        {
                if ((parseInt(left) != _x) || (parseInt(top) != _y))
                {
                        if (visible) visibility = "hidden";
                        left = _x + "px";
                        top  = _y + "px";
                        if (visible) visibility = "visible";
                };
        };
};
//................................
function fx_change_image(object,
                         image_url)
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return;
        };

        object.src = image_url;
};
//................................
function fx_visible(object) //arguments: 1 => set
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return false;
        };

        if (fx_visible.arguments.length > 1)
        {
                object.style.visibility = fx_visible.arguments[1] ? "visible" : "hidden";
                return fx_visible.arguments[1];
        };

        return object.style.visibility == "visible";
};
//................................
function fx_left(object) //arguments: 1 => relative
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return 0;
        };
        
        if (fx_left.arguments.length > 1) return object.offsetLeft;

        var value  = object.offsetLeft;
        var parent = object.offsetParent;

        while (parent)
        {
                value  += parent.offsetLeft;
                parent  = parent.offsetParent;
        };

        return value;
};
//................................
function fx_top(object) //arguments: 1 => relative
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return 0;
        };

        if (fx_top.arguments.length > 1) return object.offsetTop;
        
        var value  = object.offsetTop;
        var parent = object.offsetParent;

        while (parent)
        {
                value  += parent.offsetTop;
                parent  = parent.offsetParent;
        };

        return value;
};
//................................
function fx_width(object)
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return 0;
        };

        return object.offsetWidth;
};
//................................
function fx_height(object)
{
        if (typeof object != "object")
        {
                object = document.getElementById(object);
                if (typeof object != "object") return 0;
        };

        return object.offsetHeight;
};
//................................
function fx_page_left()
{
        if (window.pageYOffset)
                return window.pageXOffset;

        if (document.documentElement && document.documentElement.scrollLeft)
                return document.documentElement.scrollLeft;

        if (document.body)
                return document.body.scrollLeft;

        return 0;
};
//................................
function fx_page_top()
{
        if (window.pageXOffset)
                return window.pageYOffset;

        if (document.documentElement && document.documentElement.scrollTop)
                return document.documentElement.scrollTop;

        if (document.body)
                return document.body.scrollTop;

        return 0;
};
//................................
var fx_window_width  = 0;
var fx_window_height = 0;

function fx_update_page_size()
{
        if (window.innerWidth)
        {
                fx_window_width  = window.innerWidth;
                fx_window_height = window.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientWidth)
        {
                fx_window_width  = document.documentElement.clientWidth;
                fx_window_height = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                fx_window_width  = document.body.clientWidth;
                fx_window_height = document.body.clientHeight;
        };
};
//--------------------------------
//    EVENTS
//--------------------------------
/*
 * register callback function
 *
 * example 1:
 * function my_callback_function(my_event) { ... };
 * fx_register_callback(my_callback_function);
 *
 * example 2:
 * function my_callback_function(my_event) { ... };
 * function my_class() { ... this.callback = my_callback_function; ... };
 * var my_instance = new my_class();
 * fx_register_callback(my_instance);
 *
 * possible values for my_event:
 * "window_resize", "window_load",
 * "mousedown",     "mousemove", "mouseup",
 * "keydown",       "keypress",  "keyup"
 */

var fx_callback = new Array();
var fx_event    = false;

function fx_register_callback(callback)
{
        fx_callback[fx_callback.length] = callback;
};
//................................
function fx_event_raise(event)
{
        fx_event = event;
        fx_event_distribute();
        fx_event_consume();
};
//................................
function fx_event_distribute()
{
        var callback;

        for (callback in fx_callback)
        {
                callback = fx_callback[callback];

                switch (typeof callback)
                {
                case "object":
                        callback.callback(fx_event);
                        break;

                case "function":
                        callback(fx_event);
                };

                if (fx_event == false) break;
        };
};
//................................
function fx_event_consume()
{
        fx_event = false;
};
//--------------------------------
//    VARIOUS
//--------------------------------
function fx_event_window_onresize()
{
        fx_update_page_size();
        fx_event_raise("window_resize");
};
//................................
function fx_event_window_onload(e)
{
        fx_update_page_size();
        fx_event_raise("window_load");
};
//................................
function fx_event_disabled() {return false};
//................................
//    MOUSE
//................................
var fx_mouse_keystate = false;
var fx_mouse_key      = 0;

function fx_event_onmousedown(e)
{
        fx_mouse_keystate = true;

        if (window.event)
        {
                fx_mouse_key = window.event.button;
                if (fx_mouse_key == 2)      fx_mouse_key = 3;
                else if (fx_mouse_key == 4) fx_mouse_key = 2;
        }
        else
        {
                fx_mouse_key = e.which;
        };

        fx_update_mouse_position(e);
        fx_event_raise("mousedown");
};
//................................
function fx_event_onmousemove(e)
{
        fx_update_mouse_position(e);
        fx_event_raise("mousemove");
};
//................................
function fx_event_onmouseup(e)
{
        fx_mouse_keystate = false;
        fx_update_mouse_position(e);
        fx_event_raise("mouseup");
};
//................................
var fx_mouse_x = 0;
var fx_mouse_y = 0;

function fx_update_mouse_position(e)
{
        if (window.event)
        {
                fx_mouse_x = fx_page_left() + window.event.clientX;
                fx_mouse_y = fx_page_top()  + window.event.clientY;
        }
        else
        {
                fx_mouse_x = e.pageX;
                fx_mouse_y = e.pageY;
        };
}
//................................
//    KEYBOARD
//................................
var fx_keyboard_keystate = false;

function fx_event_onkeydown(e)
{
        if (window.event) fx_keyboard_keystate = window.event.keyCode;
        else              fx_keyboard_keystate = e.which
        fx_event_raise("keydown");
};
//................................
function fx_event_onkeypress()
{
        fx_event_raise("keypress");
};
//................................
function fx_event_onkeyup()
{
        fx_keyboard_keystate = false;
        fx_event_raise("keyup");
};

window.onresize      = fx_event_window_onresize;
window.onload        = fx_event_window_onload;
document.onkeydown   = fx_event_onkeydown;
document.onkeypress  = fx_event_onkeypress;
document.onkeyup     = fx_event_onkeyup;
document.onmousedown = fx_event_onmousedown;
document.onmousemove = fx_event_onmousemove;
document.onmouseup   = fx_event_onmouseup;
