/*
Copyright (c) 2008, VHI Inc. All rights reserved.
Portions Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.4.1
*/

if (typeof VHI == "undefined" || !VHI) {
	// The VHI global namespace object. It is already defined, don't overwrite.
	var VHI = {};
}

// This implements namespaces like Yahoo! YUI library.
VHI.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=VHI;

        // VHI is implied, so it is ignored if it is included
        for (j=(d[0] == "VHI") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

VHI.namespace("env");
VHI.namespace("util");
VHI.namespace("animation");

(function() {
    VHI.env.ua = function() {
        var o = {
            ie: 0,
            opera: 0,
            gecko: 0,
            webkit: 0,
            mobile: 0
        };

        var ua = navigator.userAgent, m;

        // Modern KHTML browsers should qualify as Safari X-Grade
        if ((/KHTML/).test(ua)) {
            o.webkit = 1;
        }

        // Modern WebKit browsers are at least X-Grade
        m = ua.match(/AppleWebKit\/([^\s]*)/);
        if (m && m[1]) {
            o.webkit = parseFloat(m[1]);

            // Mobile browser check
            if (/ Mobile\//.test(ua)) {
                o.mobile = "Apple"; // iPhone or iPod Touch
            } else {
                m = ua.match(/NokiaN[^\/]*/);
                if (m) {
                    o.mobile = m[0]; // Nokia N-series, ex: NokiaN95
                }
            }

        }

        if (!o.webkit) { // not webkit
            // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
            m = ua.match(/Opera[\s\/]([^\s]*)/);
            if (m && m[1]) {
                o.opera = parseFloat(m[1]);
                m = ua.match(/Opera Mini[^;]*/);
                if (m) {
                    o.mobile = m[0]; // ex: Opera Mini/2.0.4509/1316
                }
            } else { // not opera or webkit
                m = ua.match(/MSIE\s([^;]*)/);
                if (m && m[1]) {
                    o.ie = parseFloat(m[1]);
                } else { // not opera, webkit, or ie
                    m = ua.match(/Gecko\/([^\s]*)/);
                    if (m) {
                        o.gecko = 1; // Gecko detected, look for revision
                        m = ua.match(/rv:([^\s\)]*)/);
                        if (m && m[1]) {
                            o.gecko = parseFloat(m[1]);
                        }
                    }
                }
            }
        }

        return o;
    } ();

    // brower detection
    var isOpera = VHI.env.ua.opera,
        isSafari = VHI.env.ua.webkit,
        isGecko = VHI.env.ua.gecko,
        isIE = VHI.env.ua.ie;

    VHI.util.Dom = {
        get: function(el) {
            if (el && (el.tagName || el.item)) { // HTMLElement, or HTMLCollection
                return el;
            }

            if (typeof el == "string" || !el) { // HTMLElement or null
                return document.getElementById(el);
            }

            if (el.length !== undefined) { // array-like 
                var c = [];
                for (var i = 0, len = el.length; i < len; ++i) {
                    c[c.length] = VHI.util.get(el[i]);
                }

                return c;
            }

            return el; // some other object, just pass it back
        },

        // Returns the left scroll value of the document 
        getDocumentScrollLeft: function(doc) {
            doc = doc || document;
            return Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft);
        },

        // Returns the top scroll value of the document 
        getDocumentScrollTop: function(doc) {
            doc = doc || document;
            return Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
        },

        getViewportWidth: function() {
            var width = self.innerWidth;  // Safari
            var mode = document.compatMode;

            if (mode || isIE) { // IE, Gecko, Opera
                width = (mode == 'CSS1Compat') ?
                        document.documentElement.clientWidth : // Standards
                        document.body.clientWidth; // Quirks
            }
            return width;
        },

        getViewportHeight: function() {
            var height = self.innerHeight; // Safari, Opera
            var mode = document.compatMode;

            if ((mode || isIE) && !isOpera) { // IE, Gecko
                height = (mode == 'CSS1Compat') ?
                        document.documentElement.clientHeight : // Standards
                        document.body.clientHeight; // Quirks
            }

            return height;
        },

        getDocumentWidth: function() {
            var scrollWidth = (document.compatMode != 'CSS1Compat') ? document.body.scrollWidth : document.documentElement.scrollWidth;
            var w = Math.max(scrollWidth, VHI.util.Dom.getViewportWidth());
            return w;
        },

        getDocumentHeight: function() {
            var scrollHeight = (document.compatMode != 'CSS1Compat') ? document.body.scrollHeight : document.documentElement.scrollHeight;

            var h = Math.max(scrollHeight, VHI.util.Dom.getViewportHeight());
            return h;
        },

        setOpacity: function(el, val) {
            if (isIE) {
                if (typeof el.style.filter == 'string') { // in case not appended
                    el.style.filter = 'alpha(opacity=' + val * 100 + ')';

                    if (!el.currentStyle || !el.currentStyle.hasLayout) {
                        el.style.zoom = 1; // when no layout or cant tell
                    }
                }
            }
            else {
                el.style.opacity = val;
            }
        },

        center: function(el) {
            var nViewportOffset = Styles.VIEWPORT_OFFSET,
                elementWidth = el.offsetWidth,
                elementHeight = el.offsetHeight,
                viewPortWidth = Dom.getViewportWidth(),
                viewPortHeight = Dom.getViewportHeight(),
                x,
                y;
                
            if (elementWidth < viewPortWidth) {
                x = (viewPortWidth / 2) - (elementWidth / 2) + Dom.getDocumentScrollLeft();
            } else {
                x = nViewportOffset + Dom.getDocumentScrollLeft();
            }

            if (elementHeight < viewPortHeight) {
                y = (viewPortHeight / 2) - (elementHeight / 2) + Dom.getDocumentScrollTop();
            } else {
                y = nViewportOffset + Dom.getDocumentScrollTop();
            }

            //            this.cfg.setProperty("xy", [parseInt(x, 10), parseInt(y, 10)]);
            //            this.cfg.refireEvent("iframe");

            //el.style.position = 'absolute';
            el.style.left = x + 'px';
            el.style.top = y + 'px';
            //el.style.zIndex = 5;
        },

        maximize: function(el) {
            var nViewportOffset = 0, // Styles.VIEWPORT_OFFSET,
                elementWidth = el.offsetWidth,
                elementHeight = el.offsetHeight,
                viewPortWidth = Dom.getDocumentWidth(),
                viewPortHeight = Dom.getDocumentHeight(),
                x,
                y;

            x = 0; // nViewportOffset + Dom.getDocumentScrollLeft();
            y = 0; // nViewportOffset + Dom.getDocumentScrollTop();

            //el.style.position = 'absolute';
            el.style.left = x;
            el.style.top = y;
            el.style.width = viewPortWidth + "px";
            el.style.height = viewPortHeight + "px";
            //el.style.zIndex = 5;
        }

    };

    VHI.util.lang = {
        isArray: function(o) { 
            if (o) {
                return VHI.util.lang.isNumber(o.length) && VHI.util.lang.isFunction(o.splice);
            }
            return false;
        },

        isBoolean: function(o) {
            return typeof o === 'boolean';
        },
        
        isFunction: function(o) {
            return typeof o === 'function';
        },
            
        isNull: function(o) {
            return o === null;
        },
            
        isNumber: function(o) {
            return typeof o === 'number' && isFinite(o);
        },
          
        isObject: function(o) {
            return (o && (typeof o === 'object' || VHI.util.lang.isFunction(o))) || false;
        },
            
        isString: function(o) {
            return typeof o === 'string';
        },
            
        isUndefined: function(o) {
            return typeof o === 'undefined';
        }
    };
    
    var Styles = {};

    Styles.VIEWPORT_OFFSET = 3;

    var Dom = VHI.util.Dom;
    var $ = VHI.util.Dom.get;

    Styles.STYLE_BODY = "";

    // Viewer constructor
    VHI.animation.Viewer = function(el) {
        this.init(el);
    };

    VHI.animation.Viewer.prototype = {
        body: null,
        element: null,

        init: function(el) {
            this.element = el;
        },

        // NOTE: Call after render or width/height are not valid.
        centerOld: function() {
            console.log(this.element);
            console.log(this.element.offsetWidth);
            var x = VHI.util.Dom.getViewportWidth() - this.element.offsetWidth;
            if (x > 0) {
                this.element.style.left = x / 2;
            }
            var y = VHI.util.Dom.getViewportHeight() - this.element.offsetHeight;
            if (y > 0) {
                this.element.style.top = y / 2;
            }

            this.element.style.position = 'absolute';
            //			this.element.style.top = 50;
            //			this.element.style.left = 50;
            this.element.style.zIndex = 5;
        },

        center: function() {
            Dom.center(this.element);
        },

        createBody: function() {
            var el = document.createElement("div");
            return el;
        },

        setBody: function(bodyContent) {
            var oBody = this.body || (this.body = createBody());

            if (typeof bodyContent == "string") {
                oBody.innerHTML = bodyContent;
            } else {
                oBody.innerHTML = "";
                oBody.appendChild(bodyContent);
            }
        },

        render: function(appendToNode) {
            var self = this;

            function appendTo(parentNode) {
                if (typeof parentNode == "string") {
                    parentNode = document.getElementById(parentNode);
                }

                if (parentNode) {
                    parentNode.appendChild(self.element);
                }
            }

            if (appendToNode) {
                appendTo(appendToNode);
            }
        }
    };

})();
