﻿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
    },

    visible: function(elem) {
        return (elem.style.display != 'none');
    },

    toggleDisplay: function(elem) {
        elem.style.display = this.visible(elem) ? 'none' : 'block';
    },

    move: function(target, senderObj, e, offsetX, offsetY, align, valign) {

        this.saveHtmlAttributes(e);

        if (senderObj.show != undefined) senderObj.show();

        switch (valign) {
            case "top":
                target.style.top = this.cursor.y - target.offsetHeight + offsetY + "px";
                break;
            case "middle":
                target.style.top = this.cursor.y - (target.offsetHeight / 2) + offsetY + "px";
                break;
            case "bottom":
                target.style.top = this.cursor.y + offsetY + "px";
                break;
            default:
                target.style.top = (this.htmlHeight - this.cursor.relativeY < (target.offsetHeight + offsetY)) ? (this.cursor.y - target.offsetHeight - offsetY) + "px" : this.cursor.y + offsetY + "px";
        }

        switch (align) {
            case "left":
                target.style.left = this.cursor.x - target.offsetWidth + offsetX + "px";
                break;
            case "center":
                target.style.left = this.cursor.x - (target.offsetWidth / 2) + offsetX + "px";
                break;
            case "right":
                target.style.left = this.cursor.x + offsetX + "px";
                break;
            default:
                target.style.left = (this.htmlWidth - this.cursor.relativeX < (target.offsetWidth + offsetX)) ? (this.cursor.x - target.offsetWidth - 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;
    },

    getChildNodes: function(elem) {
        //we only want actual tag-nodes. we dont wat tabulators, newlines and text
        var childNodes = new Array();
        var i = 0;
        for (j = 0; j < elem.childNodes.length; j++) {
            if (elem.childNodes[j].innerHTML != undefined) {
                childNodes[i] = elem.childNodes[j];
                i++;
            }
        }
        return childNodes;
    },

    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;
    } /*,
    
    windowEvents: {
        onloadList: [],
        onresizeList: [],

        register: function() {

        },

        initiated: function() {
            if (this.popup == undefined) this.register();
            return this.popup != undefined;
        },

        addEvent: function(event, func) {
            if (this.initiated()) {
                this[event][this[event].length] = func;
            }
        },

        onload: function() {
        
        }
    }*/
}
