﻿Type.registerNamespace ("Nestle.Crunch");

Nestle.Crunch.ContentPopup = function (element) {
    this._closeLink = null;
    this._displayTriggers = [];
    this._hideTriggers = [];
    this._showCloseButton = null;
    
    Nestle.Crunch.ContentPopup.initializeBase (this, [element]);
}

Nestle.Crunch.ContentPopup.prototype = {

    // ----------------------------------------
    get_showCloseButton : function () { return this._showCloseButton; },
    set_showCloseButton : function (value) { this._showCloseButton = value; },
    
    // ----------------------------------------
    get_displayTriggers : function () { return this._displayTriggers; },
    set_displayTriggers : function (value) {
        var ids = eval('(' + value + ')');
        
        for (var name in ids)
            this.registerDisplayTrigger (ids[name]);
    },
    
    // ----------------------------------------
    get_hideTriggers : function () { return this._hideTriggers; },
    set_hideTriggers : function (value) {
        var ids = eval('(' + value + ')');
        
        for (var name in ids)
            this.registerHideTrigger (ids[name]);
    },
    
    // ----------------------------------------
    registerDisplayTrigger : function (control) {
        if (!control) throw Error.argumentNull ('control');

        var el;
        if (typeof(control) === 'string')
        {
            el = $get(control);
            if (!el) throw Error.argument ('control', 'Unable to find control with ID "' + control + '".');
        }
        else
        {
            el = control;
        }
            
        var displayDelegate = Function.createDelegate (this, this._displayPopup);
        $addHandler (el, 'click', displayDelegate);
        
        Array.add (this._displayTriggers, el);
    },

    // ----------------------------------------
    registerHideTrigger : function (control) {
        if (!control) throw Error.argumentNull ('control');

        var el;
        if (typeof(control) === 'string')
        {
            el = $get(control);
            if (!el) throw Error.argument ('control', 'Unable to find control with ID "' + control + '".');
        }
        else
        {
            el = control;
        }
            
        var hideDelegate = Function.createDelegate (this, this._hidePopup);
        $addHandler (el, 'click', hideDelegate);
        
        Array.add (this._hideTriggers, el);
    },
    
    // ----------------------------------------
    displayPopup : function () {
        this._displayPopup ();
    },
    
    // ----------------------------------------
    _displayPopup : function (e) {
        var el = this.get_element();
        $(el).show();
        
        var scrollX = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
        var scrollY = window.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop;
        
        var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
        var windowWidth  = window.innerWidth  || document.documentElement.clientWidth  || document.body.clientWidth;
        
        var x = Math.round (windowWidth/2 - el.offsetWidth/2) + scrollX;
        var y = Math.round (windowHeight/2 - el.offsetHeight/2) + scrollY;

        document.body.style.overflow = 'hidden';
        var screenFade = $get('__screenFade__');

        $(screenFade)
            .height (windowHeight + scrollY)
            .width (windowWidth + scrollX)
            .show ();

        $(el)
            .css('top', y)
            .css('left', x);
    },
    
    // ----------------------------------------
    _hidePopup : function (e) {
        var screenFade = $get('__screenFade__');
        
        $(this.get_element()).hide();
        $(screenFade).hide();
        
        document.body.style.overflow = 'auto';
    },
    
    // ----------------------------------------
    hidePopup : function () {
        this._hidePopup();
    },

    // ----------------------------------------
    initialize: function () {
        Nestle.Crunch.ContentPopup.callBaseMethod (this, "initialize");
        
        var el = this.get_element();
        
        // Create the "screen fade" element if it's not already on the page
        if (!$get('__screenFade__'))
        {
            $('body').appendDom([{
                tagName : 'div',
                id : '__screenFade__',
                className : 'popup_overlay'
            }]);
        }
        
        // Wire the close event
        if (this._showCloseButton)
        {
            $(el).appendDom([{
                tagName : 'a',
                className : 'closeLink',
                href : 'javascript:void(0);',
                innerHTML : 'X&nbsp;Close',
                id : el.id + '_close'
            }])
            .css ("paddingTop", "25px");
            
            this._closeLink = $get(el.id + '_close');
            this.registerHideTrigger (this._closeLink);
        }
    },
    
    // ----------------------------------------
    dispose : function () {
        if (this._closeLink)
            $clearHandlers (this._closeLink);

        this._disposeTriggers (this._displayTriggers);
        this._disposeTriggers (this._hideTriggers);
        
        this._closeLink = null;
        this._displayTriggers = null;
        this._hideTriggers = null;
        
        Nestle.Crunch.ContentPopup.callBaseMethod (this, "dispose");
    },
    
    // ----------------------------------------
    _disposeTriggers : function (triggers) {
        for (var name in triggers)
        {
            var trigger = triggers[name];
            $clearHandlers (trigger);
        }
    }
}

Nestle.Crunch.ContentPopup.registerClass ('Nestle.Crunch.ContentPopup', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();