﻿var ObjectInContext = new ObjectInContextClass();
var oioKeyHandle = function(event) { ObjectInContext.HandleKeyPress(event); }

function ObjectInContextUrl(objectKind, objectId, linkType)
{
    this.ObjectKind = objectKind;
    this.ObjectId = objectId;
    this.LinkType = linkType;

    this.ToUrl = function ToUrl()
    {
        return '../../../Sites/Common/private/ajax_objectincontext.aspx?objectkind=' + this.ObjectKind + '&objectid=' + this.ObjectId + '&linktype=' + this.LinkType;
    }

    this.ToJS = function ToJS()
    {
        return 'javascript:ObjectInContext.Show(new ObjectInContextUrl(' + this.ObjectKind + ',' + this.ObjectId + ',' + this.LinkType + '));'
    }
}

function PageInContextUrl(pageUrl)
{
    this.PageUrl = pageUrl;
    this.ToUrl = function ToUrl()
    {
        return this.PageUrl;
    }

    this.ToJS = function ToJS()
    {
        return 'javascript:ObjectInContext.Show(new PageInContextUrl(\"' + this.PageUrl + '\"));';
    }
}

function ObjectInContextClass()
{
    var divObjectInContext, divObjectInContextClose, divOverlay, divObjectInContextBack, divBody, divLoading, iframe;
    var f_top, f_left, f_height, f_width;
    var t_top, t_left, t_height, t_width;
    var maxHeight = 0;
    var maxWidth = 0;
    var timeOutX = 0.50;
    var timeoutY = 0.50;
    var backTimeOut = 0.35;
    var whiteBorder = 10;
    var bodyBackgroundColor = 'white';
    var urls = new Array();
    var backgroundOpacity = 0.55;

    this.Inited = false;
    this.Active = false;
    this.Showing = false;
    this.Visible = false;
    this.KeyListening = false;

    this.AjaxHtml = '';


    this.Init = function Init()
    {
        if (!this.Inited)
        {
            divObjectInContext = DOM.get('DivObjectInContext');
            divObjectInContextClose = DOM.get('DivObjectInContext_close');
            divOverlay = DOM.get('DivObjectInContext_Overlay');
            divObjectInContextBack = DOM.get('DivObjectInContext_Back');
            divBody = DOM.get('DivObjectInContext_Body');
            divLoading = DOM.get('DivObjectInContext_Loading');

            iframe = DOM.get('DivObjectInContext_Iframe');

            this.Inited = true;
        }
    }

    this.GetIframe = function GetIframe()
    {
        for (var i = 0; i < window.frames.length; i++)
        {
            try
            {
                if (window.frames[i].name == 'DivObjectInContext_Iframe')
                {
                    return window.frames[i];
                }
            } catch (exception)
            {
                // do nothing, it must have been an external IFRAME and we have a security exception
            }
        }
        return null;
    }

    this.GoBack = function GoBack()
    {
        if (self != top)
        {
            parent.ObjectInContext.GoBack();
            return;
        }
        if (urls.length > 1)
        {
            url = urls[urls.length - 2];

            urls = urls.slice(0, urls.length - 2);


            this.Show(url); // url.ObjectKind, url.ObjectId, url.LinkType);
        }
    }

    this.Show = function Show(urlObject)
    {
        if (arguments.length == 3)
        {
            urlObject = new ObjectInContextUrl(arguments[0], arguments[1], arguments[2]);
        }
        
        if (self != top)
        {
            parent.ObjectInContext.Show(urlObject); // objectKind, objectId, linkType);
        }
        else
        {
            this.Active = true;
            this.Showing = true; // (!this.Visible);

            this.Init();


            //*** Handling URLs
            if (!this.Visible)
            {
                urls = new Array();
            }

            urlObject = this.NewUrl(urlObject); // just to re-create urlObject in topmost frame (blame IE)
            urls.push(urlObject);

            //*** Set Iframe URL
            var iframe = this.GetIframe();
            if (iframe != null)
            {
                iframe.location = urlObject.ToUrl();
            }

            //*** Compute sizes
            t_height = (DOM.getViewportHeight() - 40);
            t_width = BODYWIDTH + (whiteBorder * 2);
            t_top = DOM.getDocumentScrollTop() + 20;
            t_left = Math.round((DOM.getViewportWidth() - t_width) / 2);

            if (isIE)
            {
                t_top = t_top - 2;
                t_left = t_left - 2;
            }
            if (!this.Visible)
            {
                f_width = 200;
                f_height = 100;
                f_top = Math.round(t_top + ((t_height - f_height) / 2));
                f_left = Math.round(t_left + ((t_width - f_width) / 2));
            }
            else
            {
                f_width = parseInt(DOM.getStyle(divObjectInContext, 'width'), 10);
                f_height = parseInt(DOM.getStyle(divObjectInContext, 'height'), 10);
                f_top = parseInt(DOM.getStyle(divObjectInContext, 'top'), 10);
                f_left = parseInt(DOM.getStyle(divObjectInContext, 'left'), 10);
            }

            //*** Start Animation
            if (!this.Visible)
            {
                //*** Start animation
                this.ShowBackground();
            }
            else
            {
                DOM.setStyle(divBody, 'visibility', 'hidden');

                this.ExpandX();
            }
        }
    }

    this.ResizeOverlay = function ResizeOverlay()
    {
        DOM.setStyle(divOverlay, 'top', '0px');
        DOM.setStyle(divOverlay, 'left', '0px');
        //DOM.setStyle(divOverlay, 'height', '100%');
        DOM.setStyle(divOverlay, 'width', '100%');

        DOM.setStyle(divOverlay, 'height', DOM.getDocumentHeight() + 'px');
        if (!isSafari3)
        {
            // Safari3 includes vertical scrollbar in SL.getDocumentWidth()!
            // Leave overlay width at 100% for now...
            DOM.setStyle(divOverlay, 'width', DOM.getDocumentWidth() + 'px');
        }
    }

    this.ShowBackground = function ShowBackground()
    {
        //*** background
        this.ResizeOverlay();

        DOM.setStyle(divOverlay, 'opacity', 0);
        DOM.setStyle(divOverlay, 'visibility', 'visible');

        var attributes = { opacity: { to: backgroundOpacity} };

        var anim = new ANIM(divOverlay, attributes, backTimeOut, YAHOO.util.Easing.easeOut);

        var f = function()
        {
            anim.onComplete.unsubscribe(f);
            ObjectInContext.ExpandX();
        };
        anim.onComplete.subscribe(f, anim, true);

        anim.animate();
    }

    this.ExpandX = function ExpandX()
    {
        //*** Start Showing  	    
        DOM.setStyle(divBody, 'top', whiteBorder + 'px');
        DOM.setStyle(divBody, 'left', whiteBorder + 'px');
        DOM.setStyle(divBody, 'height', (f_height - (whiteBorder * 2)) + 'px');
        DOM.setStyle(divBody, 'width', (f_width - (whiteBorder * 2)) + 'px');
        DOM.setStyle(divBody, 'background-color', bodyBackgroundColor);

        DOM.setStyle(divObjectInContext, 'top', f_top + 'px');
        DOM.setStyle(divObjectInContext, 'left', f_left + 'px');
        DOM.setStyle(divObjectInContext, 'height', f_height + 'px');
        DOM.setStyle(divObjectInContext, 'width', f_width + 'px');
        DOM.setStyle(divObjectInContext, 'background-color', bodyBackgroundColor);
        DOM.setStyle(divObjectInContext, 'visibility', 'visible');
        DOM.setStyle(divLoading, 'visibility', 'visible');


        //*** Expand X
        var attributes = { width: { to: t_width }, left: { to: t_left} };

        var anim = new ANIM(divObjectInContext, attributes, timeOutX, YAHOO.util.Easing.easeOut);

        var f = function()
        {
            anim.onComplete.unsubscribe(f);
            ObjectInContext.ExpandY(t_top, t_height);
        };
        anim.onComplete.subscribe(f, anim, true);

        anim.animate();
    }

    this.ExpandY = function ExpandY(t_top, t_height)
    {
        //*** Expand Y
        var attributes = { top: { to: t_top }, height: { to: t_height} };

        var anim = new ANIM(divObjectInContext, attributes, timeoutY, YAHOO.util.Easing.easeOut);

        var f = function()
        {
            anim.onComplete.unsubscribe(f);
            ObjectInContext.Showing = false;
            ObjectInContext.Visible = true;
        };
        anim.onComplete.subscribe(f, anim, true);

        anim.animate();
    }

    this.IframeLoaded = function IframeLoaded()
    {
        if (this.Active)
        {
            if (!this.Showing)
            {

                var containerHeight = 0;
                var bodyHeight = iframe.contentWindow.document.body.scrollHeight;

                if (bodyHeight > t_height)
                {
                    containerHeight = bodyHeight + (whiteBorder * 2) + 10;
                    bodyHeight = bodyHeight + 10;
                }
                else
                {
                    containerHeight = t_height;
                    bodyHeight = containerHeight - (whiteBorder * 2);
                }


                //*** Main Div Height
                DOM.setStyle(divObjectInContext, 'height', containerHeight + 'px');

                //*** Body Height
                DOM.setStyle(divBody, 'top', whiteBorder + 'px');
                DOM.setStyle(divBody, 'left', whiteBorder + 'px');
                DOM.setStyle(divBody, 'height', bodyHeight + 'px');
                DOM.setStyle(divBody, 'width', BODYWIDTH + 'px');
                DOM.setStyle(divBody, 'overflow-x', 'hidden');
                DOM.setStyle(divBody, 'overflow-y', 'hidden');
                DOM.setStyle(divBody, 'overflow', 'hidden');

                //*** Iframe
                DOM.setStyle(iframe, 'height', bodyHeight + 'px');
                DOM.setStyle(iframe, 'width', BODYWIDTH + 'px');
                DOM.setStyle(iframe, 'overflow-x', 'hidden');
                DOM.setStyle(iframe, 'overflow-y', 'hidden');
                DOM.setStyle(iframe, 'overflow', 'hidden');

                DOM.setStyle(divLoading, 'visibility', 'hidden');
                DOM.setStyle(divBody, 'visibility', 'visible');


                this.CheckBackButton();
                this.ListenKeys(true);
                this.ShowCloseButton();
                this.ResizeOverlay();
            }
            else
            {
                setTimeout('ObjectInContext.IframeLoaded()', 100);
            }
        }
    }

    this.ShowCloseButton = function ShowCloseButton()
    {
        //*** Close button
        DOM.setStyle(divObjectInContextClose, 'top', (parseInt(DOM.getStyle(divObjectInContext, 'top'), 10) - 12) + 'px');
        DOM.setStyle(divObjectInContextClose, 'left', (parseInt(DOM.getStyle(divObjectInContext, 'left'), 10) + parseInt(DOM.getStyle(divObjectInContext, 'width'), 10) - 14) + 'px');
        DOM.setStyle(divObjectInContextClose, 'visibility', 'visible');
    }

    this.CheckBackButton = function CheckBackButton()
    {
        //*** Back button 
        if (urls.length > 1)
        {
            divObjectInContextBack.innerHTML = '<a href=\"javascript:void(0);\" onclick=\"ObjectInContext.GoBack();\"><img src=\"../../../app_skins/webpopdown/back.gif\" border=\"0\" /></a>';
            DOM.setStyle(divObjectInContextBack, 'top', (parseInt(DOM.getStyle(divObjectInContext, 'top'), 10) - 12) + 'px');
            DOM.setStyle(divObjectInContextBack, 'left', (parseInt(DOM.getStyle(divObjectInContext, 'left'), 10) + parseInt(DOM.getStyle(divObjectInContext, 'width'), 10) - 80) + 'px');
            DOM.setStyle(divObjectInContextBack, 'visibility', 'visible');
        }
        else
        {
            DOM.setStyle(divObjectInContextBack, 'visibility', 'hidden');
        }
    }

    this.Hide = function Hide()
    {
        if (self != top)
        {
            parent.ObjectInContext.Hide();
            return;
        }

        if (this.Visible)
        {
            this.Init();
            this.ListenKeys(false);

            var iframe = this.GetIframe();
            if (iframe != null)
            {
                iframe.document.body.innerHTML = "";
            }

            DOM.setStyle(divObjectInContextClose, 'visibility', 'hidden');
            DOM.setStyle(divObjectInContext, 'visibility', 'hidden');
            DOM.setStyle(divObjectInContextBack, 'visibility', 'hidden');
            DOM.setStyle(divBody, 'visibility', 'hidden');
            DOM.setStyle(divLoading, 'visibility', 'hidden');

            var attributes = { opacity: { to: 0} };

            var anim = new ANIM(divOverlay, attributes, backTimeOut, YAHOO.util.Easing.easeOut);

            var f = function()
            {
                anim.onComplete.unsubscribe(f);
                DOM.setStyle(divOverlay, 'visibility', 'hidden');
                ObjectInContext.Active = false;
                ObjectInContext.Showing = false;
                ObjectInContext.Visible = false;
            };
            anim.onComplete.subscribe(f, anim, true);

            anim.animate();
        }
    }

    // start (true/false) = start or stop listerning
    this.ListenKeys = function ListenKeys(enable)
    {
        var iframe = this.GetIframe();

        if (enable)
        {
            if (!this.KeyListening)
            {
                if (document.addEventListener)
                {
                    document.addEventListener('keydown', oioKeyHandle, false);

                    if (iframe != null)
                    {
                        iframe.window.document.addEventListener('keydown', oioKeyHandle, false);
                    }
                }
                else if (document.attachEvent)
                {
                    document.attachEvent('onkeydown', oioKeyHandle);

                    if (iframe != null)
                    {
                        iframe.window.document.attachEvent('onkeydown', oioKeyHandle);
                    }
                }

                this.KeyListening = true;
            }
        }
        else
        {
            var iframe = this.GetIframe();

            if (this.KeyListening)
            {
                if (document.removeEventListener)
                {
                    document.removeEventListener('keydown', oioKeyHandle, false);

                    if (iframe != null)
                    {
                        iframe.window.document.removeEventListener('keydown', oioKeyHandle, false);
                    }
                }
                else if (document.detachEvent)
                {
                    document.detachEvent('onkeydown', oioKeyHandle);

                    if (iframe != null)
                    {
                        iframe.window.document.detachEvent('onkeydown', oioKeyHandle);
                    }
                }
            }

            this.KeyListening = false;
        }
    }

    this.HandleKeyPress = function HandleKeyPress(e)
    {
        //Get the Key code
        var key = e.which ? e.which : e.keyCode;

        if (key == 27)
        {
            //ESC > close
            this.PreventEvent(e);
            this.Hide();
        }
        else if (key == 8 && urls.length > 1)
        {
            //Back Delete > back
            this.PreventEvent(e);
            this.GoBack();
        }
    }

    this.PreventEvent = function PreventEvent(e)
    {
        //Prevents the event's default behavior.
        if (e.preventDefault)
        {
            e.preventDefault();
        }
        else
        {
            e.returnValue = false;
        }
    }

    // used just to provide the object from topmost frame (otherwise IE throws errors)
    this.NewUrl = function NewUrl(urlObject)
    {
        var result = new Object();

        if (urlObject.ObjectKind)
        {
            return new ObjectInContextUrl(urlObject.ObjectKind, urlObject.ObjectId, urlObject.LinkType);
        }
        else
        {
            return new PageInContextUrl(urlObject.PageUrl);
        }

        return result;
    }
}
