﻿var undefined;

var wun = (wun == undefined) ? {} : wun;

wun.common = {
    htmlWidth: undefined,
    htmlHeight: undefined,
    cursor: {
        x: 0,
        y: 0,
        relativeX: 0,
        relativeY: 0
    },

    scrolled: {
        x: 0,
        y: 0
    },

    move: function(target, e, offsetX, offsetY, align, valign) {

        this.saveHtmlAttributes(e);

        switch (valign) {
            case 'top':
                target.css('top', this.cursor.y - target.height() + offsetY + 'px');
                break;
            case 'middle':
                target.css('top', this.cursor.y - (target.height() / 2) + offsetY + 'px');
                break;
            case 'bottom':
                target.css('top', this.cursor.y + offsetY + 'px');
                break;
            default:
                target.css('top', (this.htmlHeight - this.cursor.relativeY < (target.height() + offsetY)) ? (this.cursor.y - target.height() - offsetY) + 'px' : this.cursor.y + offsetY + 'px');
        }

        switch (align) {
            case 'left':
                target.css('left', this.cursor.x - target.width() + offsetX + 'px');
                break;
            case 'center':
                target.css('left', this.cursor.x - (target.width() / 2) + offsetX + 'px');
                break;
            case 'right':
                target.css('left', this.cursor.x + offsetX + 'px');
                break;
            default:
                target.css('left', (this.htmlWidth - this.cursor.relativeX < (target.width() + offsetX)) ? (this.cursor.x - target.width() - offsetX) + 'px' : this.cursor.x + offsetX + 'px');
        }

        /* stay inside window
        target.style.left = (htmlWidth - cursor.relativeX < (this.outerDiv.offsetWidth + 20)) ? (cursor.x - this.outerDiv.offsetWidth)  + "px" : this.outerDiv.style.left = cursor.x + 20 + "px"; 
        target.style.top = (htmlHeight - cursor.relativeY < this.outerDiv.offsetHeight) ? (cursor.y - this.outerDiv.offsetHeight)  + "px" : cursor.y + "px";
        */
    },

    getHtmlNode: function() {
        this.getBodyNode().parentNode.style.height = '100%';
        return this.getBodyNode().parentNode;
    },

    getBodyNode: function() {
        document.body.style.height = 'auto';
        return document.body;
    },

    getScrolled: function() {
        // chrome : this.getBodyNode().scrollTop/scrollLeft
        // others : this.getHtmlNode().scrollTop/scrollLeft
        this.scrolled.x = (this.getBodyNode().scrollLeft != 0) ? this.getBodyNode().scrollLeft : this.getHtmlNode().scrollLeft;
        this.scrolled.y = (this.getBodyNode().scrollTop != 0) ? this.getBodyNode().scrollTop : this.getHtmlNode().scrollTop;
        return this.scrolled;
    },

    saveHtmlAttributes: function(e) {
        e = e || window.event;

        if (e.pageX || e.pageY) {
            //firefox, chrome, opera
            this.cursor.x = e.pageX;
            this.cursor.y = e.pageY;

            this.htmlWidth = window.innerWidth;
            this.htmlHeight = window.innerHeight;
        }
        else {
            //ie
            this.cursor.x = e.clientX + (document.documentElement.scrollLeft || this.getBodyNode().scrollLeft) - document.documentElement.clientLeft;
            this.cursor.y = e.clientY + (document.documentElement.scrollTop || this.getBodyNode().scrollTop) - document.documentElement.clientTop;

            this.htmlWidth = document.documentElement.clientWidth;
            this.htmlHeight = document.documentElement.clientHeight;
        }

        // chrome : this.getBodyNode().scrollTop/scrollLeft
        // others : this.getHtmlNode().scrollTop/scrollLeft
        this.scrolled.x = (this.getBodyNode().scrollLeft != 0) ? this.getBodyNode().scrollLeft : this.getHtmlNode().scrollLeft;
        this.scrolled.y = (this.getBodyNode().scrollTop != 0) ? this.getBodyNode().scrollTop : this.getHtmlNode().scrollTop;

        this.cursor.relativeX = this.cursor.x - this.scrolled.x;
        this.cursor.relativeY = this.cursor.y - this.scrolled.y;
    }
}
