﻿Type.registerNamespace ("Nestle.Crunch");

Nestle.Crunch.FlashPopup = function (element) {
    this._closeLink = null;
    this._locator = null;
    this._displayTriggers = [];
    
    this._showCloseButton = null;
    this._flashHeight = null;
    this._flashWidth = null;
    this._flashUrl = null;
    this._flashBase = null;

    Nestle.Crunch.FlashPopup.initializeBase (this, [element]);
}

Nestle.Crunch.FlashPopup.prototype = {

    // ----------------------------------------
    get_showCloseButton : function () { return this._showCloseButton; },
    set_showCloseButton : function (value) { this._showCloseButton = value; },

    // ----------------------------------------
    get_flashHeight : function () { return this._flashHeight; },
    set_flashHeight : function (value) { this._flashHeight = value; },
    
    // ----------------------------------------
    get_flashWidth : function () { return this._flashWidth; },
    set_flashWidth : function (value) { this._flashWidth = value; },

    // ----------------------------------------
    get_flashUrl : function () { return this._flashUrl; },
    set_flashUrl : function (value) { this._flashUrl = value; },

    // ----------------------------------------
    get_flashBase : function () { return this._flashBase; },
    set_flashBase : function (value) { this._flashBase = value; },
    
    // ----------------------------------------
    get_displayTriggers : function () { return this._displayTriggers; },
    set_displayTriggers : function (value) {
        var ids = eval('(' + value + ')');
        
        for (var name in ids)
            this.registerDisplayTrigger (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);
    },
    
    // ----------------------------------------
    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 () {
        var screenFade = $get('__screenFade__');
        
        $(this.get_element()).hide();
        $(screenFade).hide();
        
        document.body.style.overflow = 'auto';
    },

    // ----------------------------------------
    initialize: function () {
        Nestle.Crunch.FlashPopup.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');
            
            var closeDelegate = Function.createDelegate (this, this._hidePopup);
            $addHandler (this._closeLink, "click", closeDelegate);        
        }

        $(el).appendDom([{
            tagName : 'div',
            id : el.id + '_locator'
        }]);
        
        this._locator = $get(el.id + '_locator');

        var fo = {
            movie : this._flashUrl,
            width : this._flashWidth + 'px',
            height : this._flashHeight + 'px',
            majorversion : "8",
			build : "0",
			flashvars : "autoStart=true"
        };
        
        if (this._flashBase)
            fo.base = this._flashBase;
        
        UFO.create (fo, this._locator.id);
    },
    
    // ----------------------------------------
    dispose : function () {
        if (this._closeLink)
            $clearHandlers (this._closeLink);
        
        var triggers = this._displayTriggers;
        for (var name in triggers)
        {
            var trigger = triggers[name];
            $clearHandlers (trigger);
        }
        
        this._closeLink = null;
        this._locator = null;
        this._displayTriggers = null;
        
        Nestle.Crunch.FlashPopup.callBaseMethod (this, "dispose");
    }    
}

Nestle.Crunch.FlashPopup.registerClass ('Nestle.Crunch.FlashPopup', Sys.UI.Control);
Sys.Application.notifyScriptLoaded();